- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了一个井字游戏。但是,它的本地多人游戏屏幕(我也有一个 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/
我正在开发一个 CMD 批次。我想在里面做一些数学运算。这个公式:(x+1)100:y 所以批量处理,x = %x%, and y = %y% .我知道如何设置变量。现在,如何批量计算这个? (WIN
我正在使用 Electron 制作桌面应用程序,我制作了自己的最小化最大化和关闭按钮,所有这些按钮都工作正常,但是当您单击最大化并且它已经最大化时,它并没有取消最大化。这是我的代码: const $
目前我在这里面临着危机。 问题是,当我尝试使用 Windows 窗体的默认 WebBrowser 控件打开 G-Mail 时,它说浏览器不支持较新版本的 HTML 即 XHTML。 那么,有人能建议我
我遇到了一个让我发疯的问题。我在一个网站上工作,该网站在与主导航相同的 div 中有一个框。它一直显示到右边(栏的宽度为 100%),我需要它显示在带有主导航的 div 中(宽度 1020px) 我正
我是一名优秀的程序员,十分优秀!