1. // parse input
2. // retrieve state
if (state exists) {
// process state (could print in the process)
} else {
// initialize state
}
3. // store state
4. // show state
5. // prepare for new input
For this reason your mock up should use an infinite loop. Take a look at the example we developed in Java.
import java.io.*; // I/O package needed
class State {
String message; // describes the state of the game
int acc; // the actual state of the game
String turn; // part of the state used internally
State() {
this.message = "Welcome to the game, enter a number.";
this.acc = 0;
this.turn = "user"; // user is always the first to move
}
void process(int arg) {
if (arg >= 1 && arg <= 10) { // this comes from the user
this.change(arg); // see below
this.showAccumulator(); // print part of state
this.showTurn(); // print part of state
if (this.acc == 0) { } // game has ended!...
else {
int comp = (int)(Math.random() * 10 + 1);
System.out.println("Computer is adding: " + comp); // other printing
this.change(comp);
}
} else {
System.out.println("Sorry, " + arg + " not a valid input value.");
}
}
void change(int arg) { // arg is the input, state is this
this.acc += arg; // check for legal moves elsewhere (in process, for example)
if (this.acc >= 100) {
this.message = "The " + this.turn + " has won.\nNew game, enter number.";
this.acc = 0;
this.turn = "user"; // new game
} else {
this.message = "Game is in progress...";
if (turn.equals("user")) { turn = "computer"; }
else { turn = "user"; }
}
}
void printMessage() { System.out.println(this.message); }
void showAccumulator() { System.out.println("Current accumulator: " + this.acc); }
void showTurn() { System.out.println("The " + this.turn + " moves."); }
void printState() {
this.printMessage();
this.showAccumulator();
this.showTurn();
}
}
class Program {
public static void main(String[] args) {
ConsoleReader c = new ConsoleReader(System.in);
State a = null;
String arg = ""; // input
int num_arg; // converted input
while (true) {
try {
num_arg = Integer.parseInt(arg);
} catch (Exception e) {
num_arg = 0;
}
// get state: a is it, perhaps you can't retrieve it,
// because you're at the beginning so in that case init.
if (a == null) {
a = new State(); // initialize
} else {
a.process(num_arg); // process state (could print in the process...)
// don't process the newly initialized state (there's nothing to it)
}
a.printState();
// store state: a is it
System.out.print("------------------------------------\n" +
"Enter a number: "); // prepare the new input
arg = c.readLine();
}
} // main
}
class ConsoleReader {
ConsoleReader(InputStream inStream) { // constructor
reader = new BufferedReader(
new InputStreamReader(
inStream));
}
String readLine() { // instance method
String inputLine = "";
try {
inputLine = reader.readLine();
} catch (IOException e) {
System.out.println(e);
System.exit(1);
}
return inputLine;
}
int readInt() { // instance method
String inputString = readLine();
int n = Integer.parseInt(inputString);
return n;
}
double readDouble() { // instance method
String inputString = readLine();
double x = Double.parseDouble(inputString);
return x;
}
private BufferedReader reader; // instance method
}