Reputation: 1067
I am writing a Simon Says app for school.
I have a background thread which allows Simon to run it's sequence, and then when Simon's code is finished executing, it changes boolean conditions to allow my simonClickListener() to function.
Currently, when I click a button and simonClickListener is called, the compiler registers as many button clicks as there are loop iterations, but does this instantly, only allowing me to click one button and the remaining button inputs that are supposed to be supplied from the user are filled with the same input as the first button clicked.
So this is only becomes a problem after the player has correctly chosen the first button.
Then Simon chooses 2 buttons, example blue then green.
If I click blue, the compiler registers it as me clicking blue twice.
How can I fix this? What would be the best way to break out of my loop to accept another user input and still keep the players turn going until guessesAllowed is reached?
public class GameScreen extends AppCompatActivity implements View.OnClickListener {
private Simon simon;
private int guessesAllowed;
private boolean gameRunning;
private boolean simonsTurnBool;
private boolean playerTurnBool;
private int[] playerArray;
private int score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_screen);
Button blue_button = (Button) findViewById(R.id.blue_button);
Button yellow_button = (Button) findViewById(R.id.yellow_button);
Button green_button = (Button) findViewById(R.id.green_button);
Button red_button = (Button) findViewById(R.id.red_button);
blue_button.setOnClickListener(new simonClickListener());
yellow_button.setOnClickListener(new simonClickListener());
green_button.setOnClickListener(new simonClickListener());
red_button.setOnClickListener(new simonClickListener());
//Set global variables to default settings.
simon = new Simon();
guessesAllowed = 1;
gameRunning = false;
simonsTurnBool = true;
playerTurnBool = false;
playerArray = new int[100];
score = 0;
//Fills simon.simonCode array with random numbers
simon.setupSimon();
}
//simonClickListener used for button presses during players turn
class simonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
ImageView simonIMG = (ImageView) findViewById(R.id.simon);
TextView currentScoreTV = (TextView) findViewById(R.id.current_score_tv);
while (playerTurnBool == true && simonsTurnBool == false) {
Log.i("DEBUG", "playerTurnBool = true, running simonClickListener loop.");
for (int clickCounter = 0; clickCounter < guessesAllowed; clickCounter++) {
switch (v.getId()) {
case R.id.blue_button:
Log.i("DEBUG", "Blue button clicked.");
playerArray[clickCounter] = 1;
simonIMG.setImageResource(R.drawable.blue_lit);
playSound(a_blue_sound_id);
break;
case R.id.yellow_button:
Log.i("DEBUG", "Yellow button clicked.");
playerArray[clickCounter] = 2;
simonIMG.setImageResource(R.drawable.yellow_lit);
playSound(b_yellow_sound_id);
break;
case R.id.green_button:
Log.i("DEBUG", "Green button clicked.");
playerArray[clickCounter] = 3;
simonIMG.setImageResource(R.drawable.green_lit);
playSound(c_green_sound_id);
break;
case R.id.red_button:
Log.i("DEBUG", "Red button clicked.");
playerArray[clickCounter] = 4;
simonIMG.setImageResource(R.drawable.red_lit);
break;
}
if (playerArray[clickCounter] != simon.getSimonCode()[clickCounter]) {
Log.i("DEBUG", "Player lost");
gameRunning = false;
Toast.makeText(GameScreen.this, "You Lose!", Toast.LENGTH_SHORT);
}else if(playerArray[clickCounter] == simon.getSimonCode()[clickCounter]){
score+=1;
currentScoreTV.setText(""+score); // no score first time around and button click is registered twice second time around
Log.i("DEBUG", "Score increased. score = " + score);
}
}
playerTurnBool = false;
simonsTurnBool = true;
Log.i("DEBUG", "Player turn complete.");
}
if (gameRunning && simonsTurnBool == true) {
guessesAllowed++;
Log.i("DEBUG", "Increasing guessesAllowed and starting simonTurn().");
simonTurn();
}
}
}
SimonTask simonTask;
private void simonTurn(){
Log.i("DEBUG", "simonTurn() start.");
playerTurnBool = false;
simonsTurnBool = true;
simonTask = new SimonTask();
simonTask.execute(guessesAllowed);
simonsTurnBool = false;
playerTurnBool = true;
Log.i("DEBUG", "simonTurn() complete.");
}
Upvotes: 0
Views: 75
Reputation: 4914
In the second round guessesAllowed should be 2, so the loop
for (int clickCounter = 0; clickCounter < guessesAllowed; clickCounter++) {
...
registers the click twice.
I don't think you need any loop:
private clickCounter=0;
...
//simonClickListener used for button presses during players turn
class simonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
ImageView simonIMG = (ImageView) findViewById(R.id.simon);
TextView currentScoreTV = (TextView) findViewById(R.id.current_score_tv);
Log.i("DEBUG", "playerTurnBool = true, running simonClickListener loop.");
switch (v.getId()) {
case R.id.blue_button:
Log.i("DEBUG", "Blue button clicked.");
playerArray[clickCounter] = 1;
simonIMG.setImageResource(R.drawable.blue_lit);
playSound(a_blue_sound_id);
break;
case R.id.yellow_button:
Log.i("DEBUG", "Yellow button clicked.");
playerArray[clickCounter] = 2;
simonIMG.setImageResource(R.drawable.yellow_lit);
playSound(b_yellow_sound_id);
break;
case R.id.green_button:
Log.i("DEBUG", "Green button clicked.");
playerArray[clickCounter] = 3;
simonIMG.setImageResource(R.drawable.green_lit);
playSound(c_green_sound_id);
break;
case R.id.red_button:
Log.i("DEBUG", "Red button clicked.");
playerArray[clickCounter] = 4;
simonIMG.setImageResource(R.drawable.red_lit);
break;
}
if (playerArray[clickCounter] != simon.getSimonCode()[clickCounter]) {
Log.i("DEBUG", "Player lost");
gameRunning = false;
Toast.makeText(GameScreen.this, "You Lose!", Toast.LENGTH_SHORT);
}else if(playerArray[clickCounter] == simon.getSimonCode()[clickCounter]){
score+=1;
currentScoreTV.setText(""+score); // no score first time around and button click is registered twice second time around
Log.i("DEBUG", "Score increased. score = " + score);
}
clickCounter++:
if (clickCounter >= guessesAllowed {
playerTurnBool = false;
simonsTurnBool = true;
Log.i("DEBUG", "Player turn complete.");
}
if (gameRunning && simonsTurnBool == true) {
guessesAllowed++;
Log.i("DEBUG", "Increasing guessesAllowed and starting simonTurn().");
simonTurn();
clickCounter = 0;
}
}
}
Upvotes: 1