英文:
game crash after inserting a loop for reset game
问题
public class MainActivity extends AppCompatActivity {
// 黄色 = 0,红色 = 1
// 2 = 未落子
int activePlayer = 0;
int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
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}
};
public void dropIn(View view) {
ImageView counter = (ImageView) view;
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter] == 2) {
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).setDuration(300);
for (int[] winningPosition : winningPositions) {
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 2) {
System.out.println(gameState[winningPosition[0]]);
String winner = "Red";
if (gameState[winningPosition[0]] == 0) {
winner = "Yellow";
}
// 有人获胜
TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
winnerMessage.setText(winner + " 获胜!");
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
}
}
}
}
public void playAgain(View view) {
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);
}
}
英文:
I'm trying to create a connect style game for an assignment. Everything went well until I added the code for the playAgain layout which is supposed to reset the game to the start, but instead it crashes. I keep getting an error for the gridlayout for some reason.
public class MainActivity extends AppCompatActivity {
//yellow=0 ,red=1
//2=not played
int activePlayer=0;
int[]gameState={2,2,2,2,2,2,2,2,2,};
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}};
public void dropIn (View view){
ImageView counter=(ImageView) view;
int tappedCounter=Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter]==2) {
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).setDuration(300);
for (int[] winningPositions : winningPositions){
if (gameState[winningPositions[0]]==gameState[winningPositions[1]]
&& gameState[winningPositions[1]]==gameState[winningPositions[2]] &&
gameState[winningPositions[0]]!=2){
System.out.println(gameState[winningPositions[0]]);
String winner ="Red";
if (gameState[winningPositions[0]]==0){
winner="Yellow";
}
//someone has won
TextView winnerMessage=(TextView) findViewById(R.id.winnerMessage);
winnerMessage.setText(winner + " has WON!");
LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
}
}
}
}
public void playAgain(View view) {
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);
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论