gpt4 book ai didi

java - helper 处理循环

转载 作者:行者123 更新时间:2023-12-01 10:14:33 25 4
gpt4 key购买 nike

我正在为学校编写 Simon Says 应用程序。

我有一个后台线程,它允许 Simon 运行它的序列,然后当 Simon 的代码完成执行时,它会更改 boolean 条件以允许我的 simonClickListener() 运行。

目前,当我单击一个按钮并调用 simonClickListener 时,编译器会注册与循环迭代次数一样多的按钮单击次数,但会立即执行此操作,只允许我单击一个按钮,而其余的按钮输入应该是用户提供的输入内容与单击的第一个按钮相同。

因此,只有在玩家正确选择第一个按钮后,这才会成为问题。

然后 Simon 选择 2 个按钮,例如蓝色然后绿色。

如果我单击蓝色,编译器会将其注册为我单击蓝色两次。

我该如何解决这个问题?打破我的循环以接受另一个用户输入并仍然让玩家继续前进直到达到guessesAllowed 的最佳方法是什么?

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.");
}

最佳答案

第二轮中guessesAllowed应该是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;
}
}
}

关于java - helper 处理循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35975937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com