I want to follow up on my previous post by looking at the code for the onClick method. However, a quick look tells me that it is too complex for a tyro to make any sense out of. So, I'm going to start with an earlier proof of concept:
[1] public void onClick(View v){
[2] int dispNumber;
[3] TextView text = (TextView)findViewById(R.id.LineOne);
[4] EditText formText = (EditText)this.findViewById(R.id.entry);
[5] TextView lastText = (TextView)findViewById(R.id.EndOfGame);
[6] String DoDahTxt = formText.getText().toString();
[7] dispNumber = Integer.parseInt(DoDahTxt);
[8] dispNumber = dispNumber + 1;
[9] DoDahTxt = Integer.toString(dispNumber);
[10] text.setText(DoDahTxt);
[11] lastText.setText("You Loose!"); }
[2] int dispNumber;
[3] TextView text = (TextView)findViewById(R.id.LineOne);
[4] EditText formText = (EditText)this.findViewById(R.id.entry);
[5] TextView lastText = (TextView)findViewById(R.id.EndOfGame);
[6] String DoDahTxt = formText.getText().toString();
[7] dispNumber = Integer.parseInt(DoDahTxt);
[8] dispNumber = dispNumber + 1;
[9] DoDahTxt = Integer.toString(dispNumber);
[10] text.setText(DoDahTxt);
[11] lastText.setText("You Loose!"); }
This went (replaced) the existing onClick method. It did everything that was needed in the real onClick method: it displayed something, took input from the text box, did something with that input, and displayed some kind of a result. So let's go over this line by line:
[1] First line of the block. Note that this is a Public method, and that it does not return anything.
[2] The final result will need to keep track of the current working numbers: the number behind what the Rat is displaying (either RAT, CAT, RATCAT, or the number itself if it is not divisible by 3 or 5). So, we define a local integer variable.
[3] The first line of our layout (in horizontal mode) consists two views: id/TopLine, which we plan to use as a static text field 'The RAT Says', and the view id/LineOne, which we will use as a dynamic field, and which starts out with the value '1'. Our code needs to get a handle on this dynamic field. And we can't just change it's value with simple assignment statements. Line [3] gives us a variable (which I like to think of as a handle) named 'text'
[3] TextView text = (TextView)findViewById(R.id.LineOne);
Which gives the variable 'text' the id of the text part of the LineOne view. We will need this later on. For now, I was getting all the handles at one time.
[4] EditText formText = (EditText)this.findViewById(R.id.entry);
[4] Remember that the data entry text box is part of an EditText view named 'entry'. And line [4] get us a handle on this data entry box. Note that the views are named @+id/viewname in the XML, but R.id.viewname in the code. Then you won't spend as much time as I did worrying about this.
[5] At this time I was planning on using a view to contain any system comments, such as end of game. This line gave me a handle on that field.
[6] Something different:
[6] String DoDahTxt = formText.getText().toString();
So, we take whatever was input in the input box, and whose handle is formText (and remember, we only execute this block of code if the button was pushed, so there should be something in the box. And we get it with the getText method, and then use to String to turn whatever it is into a string.
[7] What this silly block does is take whatever number the user enters (without checking that it really was a number), add one to it, display the result, and then display 'You Loose'. Since we plan to add one to whatever, we must make it an integer
[8] As planned, we add one to it.
[9] And turn it into a string, so we can stick it back onto our form.
[10] text.setText(DoDahTxt);
This is (duh!) the setText method, available to all strings. Since 'text' is a string, it has the setText method (note to the one or two really experienced Java programmers reading this post: inexperienced Java programmers really need to think like this once in a while). And this is how whatever is inside the parenthesis gets stuck into 'text'. No assignment operator needed.
[11] Then we put something out on the last line, just to prove that we can.
O.K., but what this thingee is really supposed to do is the following:
Variables:
currNumber: The number under consideration, from the point of view of whoever is making a move. At the start of the game, the currNumber is 1. When the player is ready to make his first move, the currNumber becomes 2. Then the RAT gets to play, and the currNumber is 3.
maxNumber: The currNumber at which the game is over.
dispString: What is supposed to be entered or displayed on that turn. dispString is related to currNumber as follows:
If currNumber is divisible by 15, dispString = 'RATCAT'
Otherwise, if currNumber is divisible by 5, dispString = 'CAT'
Otherwise, if currNumber is divisible by , dispString = 'RAT'
Otherwist, dispString = toString(currNumber).
PreCondition: currNumber = 1. Empty input box for the player.
The RAT has displayed '1'.
Loop Invariant (condition at end of player's turn): currNumber has been incremented by 2. The RAT has displayed the dispString for the currNumber. The player has entered the dispString for the currNumber - 1, and that string was accepted. After entering the correct number, the input box has been blanked out.
PostCondition: either currNumber has exceeded maxNumber, or the player made a mistake. Display an appropriate message, and restart the game.
Rather than display this message myself, I decided to use the Toast method to display the results of the game.
And, with all of this help, the reader should be able to make sense of the code:
public void onClick(View v){
String inputNumber = " ";
String matchString = " ";
RatCatApp app = (RatCatApp)getApplication();
TextView text = (TextView)findViewById(R.id.LineOne);
EditText formText = (EditText)this.findViewById(R.id.entry);
if (app.currNumber < (app.maxNumber))
{
app.currNumber++;
matchString = getMatchTxt(app.currNumber);
inputNumber = formText.getText().toString();
if (!matchString.equals(inputNumber)){
Toast.makeText(this, "You Loose!", Toast.LENGTH_SHORT).show();
restartMeUp();
} else {
app.currNumber++;
formText.setText("");
text.setText(getMatchTxt(app.currNumber));
}
} /* All turns until the last one go thru here */
if (app.currNumber == app.maxNumber) {
if (matchString.equals(inputNumber)) {
Toast.makeText(this, "You Beat Me!", Toast.LENGTH_SHORT).show();
restartMeUp(); /* Correct last turn, let's end here */
}
else {
Toast.makeText(this, "You Loose!", Toast.LENGTH_SHORT).show();
restartMeUp();
} /* Goofed on the last turn */
}
if (app.currNumber >(app.maxNumber))
{
Toast.makeText(this, "You Beat Me!", Toast.LENGTH_SHORT).show();
restartMeUp();
} /* should only get here if an odd number for maxNumber is set */
}
public static String getMatchTxt(int P1){
int mod = P1%15;
if (mod == 0){ return "RATCAT";}
if (mod%5 == 0) {return "CAT";}
if (mod%3 == 0) {return "RAT";}
return Integer.toString(P1);
}
public void restartMeUp() {
Toast.makeText(this, "Starting Over...", Toast.LENGTH_SHORT).show();
RatCatApp app = (RatCatApp)getApplication();
app.currNumber = 1;
EditText formTxt = (EditText)this.findViewById(R.id.entry);
TextView txt = (TextView)findViewById(R.id.LineOne);
txt.setText("1");
formTxt.setText("");
}
}
String inputNumber = " ";
String matchString = " ";
RatCatApp app = (RatCatApp)getApplication();
TextView text = (TextView)findViewById(R.id.LineOne);
EditText formText = (EditText)this.findViewById(R.id.entry);
if (app.currNumber < (app.maxNumber))
{
app.currNumber++;
matchString = getMatchTxt(app.currNumber);
inputNumber = formText.getText().toString();
if (!matchString.equals(inputNumber)){
Toast.makeText(this, "You Loose!", Toast.LENGTH_SHORT).show();
restartMeUp();
} else {
app.currNumber++;
formText.setText("");
text.setText(getMatchTxt(app.currNumber));
}
} /* All turns until the last one go thru here */
if (app.currNumber == app.maxNumber) {
if (matchString.equals(inputNumber)) {
Toast.makeText(this, "You Beat Me!", Toast.LENGTH_SHORT).show();
restartMeUp(); /* Correct last turn, let's end here */
}
else {
Toast.makeText(this, "You Loose!", Toast.LENGTH_SHORT).show();
restartMeUp();
} /* Goofed on the last turn */
}
if (app.currNumber >(app.maxNumber))
{
Toast.makeText(this, "You Beat Me!", Toast.LENGTH_SHORT).show();
restartMeUp();
} /* should only get here if an odd number for maxNumber is set */
}
public static String getMatchTxt(int P1){
int mod = P1%15;
if (mod == 0){ return "RATCAT";}
if (mod%5 == 0) {return "CAT";}
if (mod%3 == 0) {return "RAT";}
return Integer.toString(P1);
}
public void restartMeUp() {
Toast.makeText(this, "Starting Over...", Toast.LENGTH_SHORT).show();
RatCatApp app = (RatCatApp)getApplication();
app.currNumber = 1;
EditText formTxt = (EditText)this.findViewById(R.id.entry);
TextView txt = (TextView)findViewById(R.id.LineOne);
txt.setText("1");
formTxt.setText("");
}
}