Java Tic Tac Toe-Draw,9步后赢得Bug
我已经写了一篇tic-tac-toe。然而,它的本地多人游戏屏幕(我也有一个AI比赛屏幕-完成)有一个问题 它很容易检测到胜利,直到打了9个箱子。如果打9个箱子,即使有一个赢了,也会宣布平局 下面是我的代码。请帮忙Java Tic Tac Toe-Draw,9步后赢得Bug,java,android,Java,Android,我已经写了一篇tic-tac-toe。然而,它的本地多人游戏屏幕(我也有一个AI比赛屏幕-完成)有一个问题 它很容易检测到胜利,直到打了9个箱子。如果打9个箱子,即使有一个赢了,也会宣布平局 下面是我的代码。请帮忙 static int activePlayer = 0; //Handles the player state static boolean gameIsActive = true; //Checks whether the game is active or not static
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);
}
}
static int-activePlayer=0//处理玩家状态
静态布尔gameIsActive=true//检查游戏是否处于活动状态
静态整数gameMoves=0//处理游戏移动的数量——在平局游戏中很有用(9个移动)
静态int[]游戏状态={2,2,2,2,2,2,2,2}//处理游戏状态——2表示未播放
ImageView homeImageView//绿色主页按钮
网格布局板网格//井字游戏板
TextView结果TextView//虚拟结果文本
按钮复位按钮//虚拟复位按钮
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u local\u多人游戏);
//初始化
homeImageView=(ImageView)findViewById(R.id.iv_green_home);
boardGrid=(GridLayout)findViewById(R.id.boardGrid);
resultTextView=(TextView)findViewById(R.id.resultTextView);
resetButton=(按钮)findViewById(R.id.resetButton);
//处理板件动画中的淡入淡出+游戏逻辑!
公共void fadeIn(视图){
gameMoves++;//每次点击棋盘时增加移动次数
//将视图放入一块木板(ImageView)(当然是ImageView,这就是原因)
ImageView板件=(ImageView)视图;
//博弈逻辑
int tappedCounter=Integer.parseInt(boardPiece.getTag().toString());//点击位置
int[][]赢位={{0,1,2},{3,4,5},{6,7,8},{0,3,6},
{1,4,7},{2,5,8},{0,4,8},{2,4,6};//获胜位置
如果(游戏状态[tappedCounter]==2&&gameIsActive){
游戏状态[tappedCounter]=活动玩家;
//检查共享引用主题
int themeChosen=Utils.loadPreferences(getBaseContext(),“theme”,0);
//根据所选主题更换板件
如果(themeChosen==0){
如果(activePlayer==0){
boardPiece.setImageResource(R.drawable.nought);
activePlayer=1;
}else if(activePlayer==1){
木板。setImageResource(R.可绘制。十字);
activePlayer=0;
}
boardPiece.setTranslationY(-1000f);//设置屏幕外的视图
boardPiece.animate().translationYBy(1000f).setDuration(1000);//将其重新显示在屏幕上
}else if(themeChosen==1){
如果(activePlayer==0){
boardPiece.setImageResource(R.drawable.heart);
activePlayer=1;
}else if(activePlayer==1){
boardPiece.setImageResource(R.drawable.chocolate);
activePlayer=0;
}
boardPiece.setTranslationY(-1000f);//设置屏幕外的视图
boardPiece.animate().translationYBy(1000f).setDuration(1000);//将其重新显示在屏幕上
}否则如果(themeChosen==2){
如果(activePlayer==0){
boardPiece.setImageResource(R.drawable.tom);
activePlayer=1;
}else if(activePlayer==1){
boardPiece.setImageResource(R.drawable.jerry);
activePlayer=0;
}
boardPiece.setTranslationY(-1000f);//设置屏幕外的视图
boardPiece.animate().translationYBy(1000f).setDuration(1000);//将其重新显示在屏幕上
}否则如果(themeChosen==3){
如果(activePlayer==0){
setImageResource(R.drawable.wizard);
activePlayer=1;
}else if(activePlayer==1){
boardPiece.setImageResource(R.drawable.hog);
activePlayer=0;
}
boardPiece.setTranslationY(-1000f);//设置屏幕外的视图
boardPiece.animate().translationYBy(1000f).setDuration(1000);//将其重新显示在屏幕上
}否则如果(themeChosen==4){
如果(activePlayer==0){
boardPiece.setImageResource(R.drawable.bat);
activePlayer=1;
}else if(activePlayer==1){
boardPiece.setImageResource(R.drawable.ball);
activePlayer=0;
}
boardPiece.setTranslationY(-1000f);//设置屏幕外的视图
boardPiece.animate().translationYBy(1000f).setDuration(1000);//将其重新显示在屏幕上
}否则如果(themeChosen==5){
如果(activePlayer==0){
boardPiece.setImageResource(R.drawable.tiger);
activePlayer=1;
}else if(activePlayer==1){
boardPiece.setImageResource(R.drawable.mammoth);
activePlayer=0;
}
boardPiece.setTranslationY(-1000f);//设置屏幕外的视图
boardPiece.animate().translationYBy(1000f).setDuration(1000);//将其重新显示在屏幕上
}
}
//处理输赢
对于(int[]winningPosition:winningPositions){
//检查共享引用主题
int themeChosen=Utils.loadPreferences(getBaseContext(),“theme”,0);
如果(游戏状态[winningPosition[0]]==游戏状态[winningPosition[1]]
&&游戏状态[赢牌位置[1]]==游戏状态[赢牌位置[2]]
&&游戏状态[赢牌位置[0]!=2){
//有人赢了!
gameIsActive=false;//禁用游戏可玩性
如果(themeChosen==0){
如果(游戏状态[winningPosition[0]]==0){
//零赢了!
Log.i(“获胜者”、“正常人1”);
resultTextView.setText(“获胜者:零”);
}否则{
//克罗斯温!
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;
}
}
if(winner != 2) {
CODE FOR HANDLING WIN HERE
} else {
CODE FOR HANDLING DRAW HERE
}