gpt4 book ai didi

java - Tic Tac Toe - 9 步后平局并获胜 Bug

转载 作者:行者123 更新时间:2023-11-29 19:30:05 25 4
gpt4 key购买 nike

我写了一个井字游戏。但是,它的本地多人游戏屏幕(我也有一个 AI 匹配屏幕 - 已完成)有 1 个问题。

在玩完 9 盒之前,它很容易检测到获胜。如果玩了 9 个盒子,即使有一个盒子赢了,它也会宣布平局。

下面是我的代码。请帮忙。

static int activePlayer = 0; //Handles the player state
static boolean gameIsActive = true; //Checks whether the game is active or not

static int gameMoves = 0; //Handles the number of game moves -- Useful in draw games (9 Moves)

static int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2}; //Handles the GameState -- 2 means Unplayed

ImageView homeImageView; //Green Home Button
GridLayout boardGrid; //Tic-Tac-Toe Game Board
TextView resultTextView; //Dummy Result Text
Button resetButton; //Dummy Reset Button

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_multiplayer);


//Initialization
homeImageView = (ImageView) findViewById(R.id.iv_green_home);
boardGrid = (GridLayout) findViewById(R.id.boardGrid);
resultTextView = (TextView) findViewById(R.id.resultTextView);
resetButton = (Button) findViewById(R.id.resetButton);

//Handles fading in animation of the board pieces + GAME LOGIC!
public void fadeIn(View view) {

gameMoves++; //Increments the number of moves with each click on board piece

//Getting the view into an boardPiece(ImageView) (ofcourse its an ImageView, thats why)
ImageView boardPiece = (ImageView) view;

//Game Logic

int tappedCounter = Integer.parseInt(boardPiece.getTag().toString()); //Tapped Location
int[][] winningPositions = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6},
{1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}}; //Winning Positions Location

if (gameState[tappedCounter] == 2 && gameIsActive) {

gameState[tappedCounter] = activePlayer;

//Checking SharedPreferences Themes
int themeChosen = Utils.loadPreferences(getBaseContext(), "theme", 0);

//Changing Board Pieces based on Theme Selected
if (themeChosen == 0) {
if (activePlayer == 0) {
boardPiece.setImageResource(R.drawable.nought);
activePlayer = 1;
} else if (activePlayer == 1) {
boardPiece.setImageResource(R.drawable.cross);
activePlayer = 0;
}
boardPiece.setTranslationY(-1000f); //Sets the view off-screen
boardPiece.animate().translationYBy(1000f).setDuration(1000); //Gets it back on screen
} else if (themeChosen == 1) {
if (activePlayer == 0) {
boardPiece.setImageResource(R.drawable.heart);
activePlayer = 1;
} else if (activePlayer == 1) {
boardPiece.setImageResource(R.drawable.chocolate);
activePlayer = 0;
}
boardPiece.setTranslationY(-1000f); //Sets the view off-screen
boardPiece.animate().translationYBy(1000f).setDuration(1000); //Gets it back on screen
} else if (themeChosen == 2) {
if (activePlayer == 0) {
boardPiece.setImageResource(R.drawable.tom);
activePlayer = 1;
} else if (activePlayer == 1) {
boardPiece.setImageResource(R.drawable.jerry);
activePlayer = 0;
}
boardPiece.setTranslationY(-1000f); //Sets the view off-screen
boardPiece.animate().translationYBy(1000f).setDuration(1000); //Gets it back on screen
} else if (themeChosen == 3) {
if (activePlayer == 0) {
boardPiece.setImageResource(R.drawable.wizard);
activePlayer = 1;
} else if (activePlayer == 1) {
boardPiece.setImageResource(R.drawable.hog);
activePlayer = 0;
}
boardPiece.setTranslationY(-1000f); //Sets the view off-screen
boardPiece.animate().translationYBy(1000f).setDuration(1000); //Gets it back on screen
} else if (themeChosen == 4) {
if (activePlayer == 0) {
boardPiece.setImageResource(R.drawable.bat);
activePlayer = 1;
} else if (activePlayer == 1) {
boardPiece.setImageResource(R.drawable.ball);
activePlayer = 0;
}
boardPiece.setTranslationY(-1000f); //Sets the view off-screen
boardPiece.animate().translationYBy(1000f).setDuration(1000); //Gets it back on screen
} else if (themeChosen == 5) {
if (activePlayer == 0) {
boardPiece.setImageResource(R.drawable.tiger);
activePlayer = 1;
} else if (activePlayer == 1) {
boardPiece.setImageResource(R.drawable.mammoth);
activePlayer = 0;
}
boardPiece.setTranslationY(-1000f); //Sets the view off-screen
boardPiece.animate().translationYBy(1000f).setDuration(1000); //Gets it back on screen
}
}

//Handles Winning - Losing
for (int[] winningPosition : winningPositions) {

//Checking SharedPreferences Themes
int themeChosen = Utils.loadPreferences(getBaseContext(), "theme", 0);

if (gameState[winningPosition[0]] == gameState[winningPosition[1]]
&& gameState[winningPosition[1]] == gameState[winningPosition[2]]
&& gameState[winningPosition[0]] != 2) {

//Someone has won!
gameIsActive = false; //Disable Game Playablity

if (themeChosen == 0) {
if (gameState[winningPosition[0]] == 0) {
//Noughts Won!
Log.i("winner", "normal1");
resultTextView.setText("Winner: Nought");
} else {
//Cross Won!
Log.i("winner", "normal2");
resultTextView.setText("Winner: Cross");
}
} else if (themeChosen == 1) {
if (gameState[winningPosition[0]] == 0) {
//Hearts Won!
Log.i("winner", "love1");
resultTextView.setText("Winner: Heart");
} else {
//Chocolates Won!
Log.i("winner", "love2");
resultTextView.setText("Winner: Chocolate");
}
} else if (themeChosen == 2) {
if (gameState[winningPosition[0]] == 0) {
//Tom Won!
Log.i("winner", "tomandjerry1");
resultTextView.setText("Winner: Tom");
} else {
//Jerry Won!
Log.i("winner", "tomandjerry2");
resultTextView.setText("Winner: Jerry");
}
} else if (themeChosen == 3) {
if (gameState[winningPosition[0]] == 0) {
//Wizards Won!
Log.i("winner", "clashofclans1");
resultTextView.setText("Winner: Wizard");
} else {
//Hogs Won!
Log.i("winner", "clashofclans2");
resultTextView.setText("Winner: Hog");
}
} else if (themeChosen == 4) {
if (gameState[winningPosition[0]] == 0) {
//Bat Won!
Log.i("winner", "cricket1");
resultTextView.setText("Winner: Bat");
} else {
//Ball Won!
Log.i("winner", "cricket2");
resultTextView.setText("Winner: Ball");
}
} else if (themeChosen == 5) {
if (gameState[winningPosition[0]] == 0) {
//Tiger Won!
Log.i("winner", "iceage1");
resultTextView.setText("Winner: Tiger");
} else {
//Mammoth Won!
Log.i("winner", "iceage2");
resultTextView.setText("Winner: Mammmoth");
}
}
} else if (gameState[winningPosition[0]] != 2
&& gameState[winningPosition[0]] != gameState[winningPosition[1]]
&& gameState[winningPosition[1]] != gameState[winningPosition[2]]
&& gameMoves >= 9) {

gameIsActive = false; //Disable Game Playablity

//Its a draw!
if (themeChosen == 0) {
Log.i("winner", "normal");
resultTextView.setText("Winner: Draw 1");
} else if(themeChosen == 1) {
Log.i("winner", "love");
resultTextView.setText("Winner: Draw 2");
} else if(themeChosen == 2) {
Log.i("winner", "tomandjerry");
resultTextView.setText("Winner: Draw 3");
} else if(themeChosen == 3) {
Log.i("winner", "clashofclans");
resultTextView.setText("Winner: Draw 4");
} else if(themeChosen == 4) {
Log.i("winner", "cricket");
resultTextView.setText("Winner: Draw 5");
} else if(themeChosen == 5) {
Log.i("winner", "iceage");
resultTextView.setText("Winner: Draw 6");
}
}
}

}

//Resetting game - Method
public void resetGame() {

//Resets the Result Text
resultTextView.setText("");

//Resets the Game
activePlayer = 0; //Clears the player state
gameIsActive = true; //Reactivates the game - Sets it to activated state

gameMoves = 0; //Resets the number of game moves done to 0

//Clears the Game State
gameState[0] = 2;
gameState[1] = 2;
gameState[2] = 2;
gameState[3] = 2;
gameState[4] = 2;
gameState[5] = 2;
gameState[6] = 2;
gameState[7] = 2;
gameState[8] = 2;

//Clears the ImageViews
for(int i = 0; i < boardGrid.getChildCount(); i++){
ImageView iv = (ImageView) boardGrid.getChildAt(i);
iv.setImageResource(0);
}
}

最佳答案

所以问题是,当它在循环不同的可能获胜位置时,如果它遇到一个可能获胜的位置不是连续 3 个,那么一旦进行了 9 步,它就会将其报告为平局。您需要做的是创建一个 int winner 并将其默认为 2。您的 for 现在应该如下所示

int winner = 2;
for (int[] winningPosition : winningPositions) {

//Checking SharedPreferences Themes
int themeChosen = Utils.loadPreferences(getBaseContext(), "theme", 0);

if (gameState[winningPosition[0]] == gameState[winningPosition[1]]
&& gameState[winningPosition[1]] == gameState[winningPosition[2]]
&& gameState[winningPosition[0]] != 2) {

//Someone has won!
gameIsActive = false; //Disable Game Playablity

winner = gameState[winningPosition[0]];
break;
} else if(gameMoves >= 9) {
gameIsActive = false;
}
}

然后在 for 循环之外你应该有

if(winner != 2) {
CODE FOR HANDLING WIN HERE
} else {
CODE FOR HANDLING DRAW HERE
}

关于java - Tic Tac Toe - 9 步后平局并获胜 Bug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40223656/

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