我正在为学校写一个西蒙说的应用程序。
我有一个后台线程,该线程允许Simon运行其序列,然后在完成Simon的代码执行后,它将更改布尔条件以允许simonClickListener()起作用。
目前,当我单击一个按钮并调用simonClickListener时,编译器会注册与循环迭代次数相同的按钮点击次数,但是会立即进行,仅允许我单击一个按钮,而其余的按钮输入则应该由该按钮提供。用户将充满与单击第一个按钮相同的输入。
因此,只有在玩家正确选择了第一个按钮之后,这才成为问题。
然后,Simon选择2个按钮,例如蓝色然后是绿色。
如果单击蓝色,则编译器将其注册为单击两次蓝色的命令。
我怎样才能解决这个问题?突破我的循环以接受另一个用户输入并保持玩家继续前进直到达到guessAllowed的最佳方法是什么?
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.");
}
最佳答案
在第二轮猜测中Allowed应该为2,所以循环
for (int clickCounter = 0; clickCounter < guessesAllowed; clickCounter++) {
...
单击两次。
我认为您不需要任何循环:
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;
}
}
}