我正在研究udemy。我正在编写XOX游戏的代码。我想我们所有人都知道XOX游戏。我的代码正常工作,但是我不明白该代码的作用?
for (int [] winningPosition : winningPositions){
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] && gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 2)}
我没有得到的代码在上面。请帮助我理解这一点。
{ package com.example.anild.xox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// 0 = yellow, 1 = red
int activePlayer = 0;
boolean gameIsActive = true;
//2 means unplayed
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 && gameIsActive) {
//burada tappedCounterdaki sayıyı çekiyor yani tag'i daha sonra gamstate[] dizisine atıyor. Ve zaten hepsi 2 if koşulu sağlanıyor
//Bir asağısında bu kural değişiyor. Bu alınan tag gametate'te artık 0 olarak saklanıyor :)
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(300);
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 winnerMessage = (TextView) findViewById(R.id.winnerMessage);
winnerMessage.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 winnerMessage = (TextView) findViewById(R.id.winnerMessage);
winnerMessage.setText("it's 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);
// 0 = yellow, 1 = red
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);
}
}
}
答案 0 :(得分:1)
根据我的评论下方提出的问题进行回答:
从根本上讲,for循环的格式为
for('one object' : 'group of objects')
现在,在通常情况下,您将按照以下方式使用某些东西:
for(String oneString : arrayOfStrings)
根据您的问题,似乎您不知道可以通过将值放在大括号中来静态指定值数组。例如:
String[] arrayOfStrings = {"zero", "one", "two"};
产生的结果与以下内容完全相同:
String[] arrayOfStrings = new String[3];
arrayOfStrings[0] = "zero";
arrayOfStrings[1] = "one";
arrayOfStrings[2] = "two";
您可以使用for(String string : arrayOfStrings)
...
回到原始代码-它们在括号内嵌入了括号,这就是您声明多维数组的方式。因此,以下内容:
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}};
产生与以下相同的数组:
int[][] winningPositions = new int[8][3];
winningPositions[0][0] = 0;
winningPositions[0][1] = 1;
winningPositions[0][2] = 2;
...
winningPositions[7][0] = 2;
winningPositions[7][1] = 4;
winningPositions[7][2] = 6;
到目前为止和我在一起吗?
完整的“ winningPositions”变量是类型为array的对象,其内容本身是数组序列。好吧,所以现在当您执行循环时,您不了解的是:
for (int [] winningPosition : winningPositions)
“ winningPosition”数组中的每个项目本身都是一个数组-正如我刚才所说的-因此,“一个对象”解析为内部数组,该数组具有3个元素,而外部数组具有8个元素。
有帮助吗?
附带说明:这样的代码要么针对新程序员,要么由不懂OO编程的人编写。有必要了解它是如何工作的,它不一定要被模仿。或者至少在您知道我对此有误的时候,您就会知道我为什么错了。
答案 1 :(得分:0)
for (int [] winningPosition : winningPositions)
遍历名为winningPositions
的数组。每次迭代都有一个名为winningPosition
的变量,它是一个长度为3的整数数组。
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 2)
检查所有位置是否都由同一玩家拥有,最后一部分gameState[winningPosition[0]] != 2
检查该行是否为空。
编辑:
@ChrisParker在评论中很好地解释了这一行。 for (int [] winningPosition : winningPositions)
答案 2 :(得分:0)
for循环正在遍历数组并检查if语句中的每个条件,如果所有条件都为true,则代码将进入IF循环内部,否则将跳过并继续。
欢呼