Android Studio 的 Tic-Tac-Toe 抽奖条件
Tic-Tac-Toe Draw condition for Android Studio
我正在制作井字游戏应用程序。我已经完成了 "win" 语句,但我卡在了 draw 语句上。
以下是我的代码。
public class MainActivity extends AppCompatActivity {
int activePlayer = 0; // 0 for red
int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2}; // 2 means unplayed.
int[][] winningLocations = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8},
{2, 4, 6}};
boolean gameOver = false;
public void gameLogic(View view) {
ImageView tappedView = (ImageView) view;
int tappedLocation = Integer.parseInt(view.getTag().toString());
if (gameState[tappedLocation] == 2 && !gameOver) {
gameState[tappedLocation] = activePlayer;
tappedView.setTranslationY(-3000f);
if (activePlayer == 0) {
tappedView.setImageResource(R.drawable.red);
activePlayer = 1;
} else if (activePlayer == 1) {
tappedView.setImageResource(R.drawable.yellow);
activePlayer = 0;
}
tappedView.animate().translationYBy(3000f).setDuration(500);
String msg = "";
for (int[] winningPosition : winningLocations) {
if (gameState[winningPosition[0]] == gameState[winningPosition[1]]
&& gameState[winningPosition[1]] == gameState[winningPosition[2]]
&& gameState[winningPosition[0]] != 2) {
if (activePlayer == 0)
msg = "Yellow is Winner!";
if (activePlayer == 1)
msg = "Red is Winner!";
LinearLayout winnerLayout = (LinearLayout) findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(View.VISIBLE);
TextView winnerMsg = (TextView) findViewById(R.id.textView);
winnerMsg.setText(msg);
gameOver = true;
}
}
}
}
public void playAgain(View view){
LinearLayout winnerLayout = (LinearLayout)findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(View.INVISIBLE);
gameOver = false;
activePlayer = 0;
for(int i = 0; i < gameState.length; i++)
gameState[i] = 2;
GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);
for(int i = 0; i < gridLayout.getChildCount(); i++)
((ImageView)gridLayout.getChildAt(i)).setImageResource(0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
LinearLayout winnerLayout = (LinearLayout) findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(View.INVISIBLE);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
假设 tic-tac-toe 中的平局是全部 9 个方格,没有找到 3 行,我会在检查获胜条件的 for
循环之后放置这样的内容:
boolean emptySquare = false;
for (int squareState : gameState) {
if (squareState == 2) {
emptySquare = true;
break;
}
}
if (!emptySquare && !gameOver) {
// Game is a draw
gameOver = true;
// Set draw message here...
}
我正在制作井字游戏应用程序。我已经完成了 "win" 语句,但我卡在了 draw 语句上。
以下是我的代码。
public class MainActivity extends AppCompatActivity {
int activePlayer = 0; // 0 for red
int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2}; // 2 means unplayed.
int[][] winningLocations = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8},
{2, 4, 6}};
boolean gameOver = false;
public void gameLogic(View view) {
ImageView tappedView = (ImageView) view;
int tappedLocation = Integer.parseInt(view.getTag().toString());
if (gameState[tappedLocation] == 2 && !gameOver) {
gameState[tappedLocation] = activePlayer;
tappedView.setTranslationY(-3000f);
if (activePlayer == 0) {
tappedView.setImageResource(R.drawable.red);
activePlayer = 1;
} else if (activePlayer == 1) {
tappedView.setImageResource(R.drawable.yellow);
activePlayer = 0;
}
tappedView.animate().translationYBy(3000f).setDuration(500);
String msg = "";
for (int[] winningPosition : winningLocations) {
if (gameState[winningPosition[0]] == gameState[winningPosition[1]]
&& gameState[winningPosition[1]] == gameState[winningPosition[2]]
&& gameState[winningPosition[0]] != 2) {
if (activePlayer == 0)
msg = "Yellow is Winner!";
if (activePlayer == 1)
msg = "Red is Winner!";
LinearLayout winnerLayout = (LinearLayout) findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(View.VISIBLE);
TextView winnerMsg = (TextView) findViewById(R.id.textView);
winnerMsg.setText(msg);
gameOver = true;
}
}
}
}
public void playAgain(View view){
LinearLayout winnerLayout = (LinearLayout)findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(View.INVISIBLE);
gameOver = false;
activePlayer = 0;
for(int i = 0; i < gameState.length; i++)
gameState[i] = 2;
GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);
for(int i = 0; i < gridLayout.getChildCount(); i++)
((ImageView)gridLayout.getChildAt(i)).setImageResource(0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
LinearLayout winnerLayout = (LinearLayout) findViewById(R.id.winnerLayout);
winnerLayout.setVisibility(View.INVISIBLE);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
假设 tic-tac-toe 中的平局是全部 9 个方格,没有找到 3 行,我会在检查获胜条件的 for
循环之后放置这样的内容:
boolean emptySquare = false;
for (int squareState : gameState) {
if (squareState == 2) {
emptySquare = true;
break;
}
}
if (!emptySquare && !gameOver) {
// Game is a draw
gameOver = true;
// Set draw message here...
}