我已经在Android上制作了第一个井字游戏,但是我发现了一个错误,并且不知道如何解决。
当玩家1以某些组合(例如水平底行或垂直第一行)的最后一轮获胜时,它弹出-这是平局-当有获胜者时。
在这里,我定义了变量。
public class MainActivity extends AppCompatActivity {
// 0 yellow ; 1 red;
int activePlayer = 0;
boolean gameIsActive = true;
// 2 unplayed;
int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
int[][] winningPositions = {
{0,1,2}, {3,4,5}, {6,7,8}, //hor
{0,3,6}, {1,4,7}, {2,5,8}, // ver
{0,4,8}, {2,4,6}
}; // dia
这是我背后的逻辑。我知道需要在评论后的某处进行修复:检查获胜者!因为它不会掉入低谷,但会进入其他区域并突然弹出-这是平局。
JAVA要求的完整代码:
public class MainActivity extends AppCompatActivity {
// 0 yellow ; 1 red;
int activePlayer = 0;
boolean gameIsActive = true;
// 2 unplayed;
int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
int[][] winningPositions = {
{0,1,2}, {3,4,5}, {6,7,8}, //hor
{0,3,6}, {1,4,7}, {2,5,8}, // ver
{0,4,8}, {2,4,6}
}; // dia
public void dropIn (View view) {
ImageView counter = (ImageView) view;
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter] == 2 && gameIsActive) {
gameState[tappedCounter] = activePlayer;
counter.setTranslationY(-1000f);
if (activePlayer == 0) {
counter.setImageResource(R.drawable.yellow);
activePlayer = 1;
} else {
counter.setImageResource(R.drawable.red);
activePlayer = 0;
}
counter.animate()
.translationYBy(1000f)
.rotation(360)
.setDuration(500);
//checking for the winner
for (int[] winningPosition : winningPositions) {
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] !=2 ) {
//Someone has won
gameIsActive = false;
String winner = "Red";
if (gameState[winningPosition[0]] == 0){
winner = "Yellow";
}
TextView winnerMsg = (TextView) findViewById(R.id.winnerMsg);
winnerMsg.setText(winner + " has won!");
LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
} else {
boolean gameIsOver = true;
for (int counterState : gameState ) {
if (counterState == 2) {
gameIsOver = false;
}
}
if (gameIsOver) {
TextView winnerMsg = (TextView) findViewById(R.id.winnerMsg);
winnerMsg.setText("It is a DRAW!");
LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
}
}
}
}
}
public void playAgain(View view) {
gameIsActive = true;
LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
layout.setVisibility(View.INVISIBLE);
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);
}
和XML:
<GridLayout
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:columnCount="3"
android:rowCount="3"
android:background="@drawable/board"
android:id="@+id/gridLayout">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView"
android:layout_row="0"
android:layout_column="0"
android:layout_margin="10dp"
android:onClick="dropIn"
android:tag="0"/>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView2"
android:layout_row="0"
android:layout_column="1"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:onClick="dropIn"
android:tag="1"/>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView3"
android:layout_row="0"
android:layout_column="2"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:onClick="dropIn"
android:tag="2"/>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView4"
android:layout_row="1"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="3"/>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView5"
android:layout_row="1"
android:layout_column="1"
android:layout_marginLeft="15dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="4"/>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView6"
android:layout_row="1"
android:layout_column="2"
android:layout_marginLeft="15dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="5"/>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView7"
android:layout_row="2"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="6"/>>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView8"
android:layout_row="2"
android:layout_column="1"
android:layout_marginLeft="15dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="7"/>
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/imageView9"
android:layout_row="2"
android:layout_column="2"
android:layout_marginLeft="15dp"
android:layout_marginTop="30dp"
android:onClick="dropIn"
android:tag="8"/>
</GridLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="#deed17"
android:padding="30dp"
android:id="@+id/playAgainLayout"
android:baselineAligned="false"
android:visibility="invisible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/winnerMsg"
android:layout_gravity="center_horizontal"
android:textSize="30sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Again!"
android:id="@+id/playBtn"
android:layout_gravity="center_horizontal"
android:onClick="playAgain" />
</LinearLayout>
最佳答案
在for循环中,您正在检查所有可能的获胜条件,当找到条件时,它将继续处理其余的获胜条件。如果您只是在代码的末尾放了一点
for (int[] winningPosition : winningPositions)
{
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] !=2 )
{
//Someone has won
gameIsActive = false;
String winner = "Red";
if (gameState[winningPosition[0]] == 0)
{
winner = "Yellow";
}
TextView winnerMsg = (TextView) findViewById(R.id.winnerMsg);
winnerMsg.setText(winner + " has won!");
LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
break;
}
那可能会做到。