我想制作简单的Tic Tac Toe
现在我有了这段代码
在这里,我只需点击一个按钮,它就变成了X,当我在一行中有3个X时,会出现一个Toast显示" Game over"。
我还没有设置Os。
public class MainActivity extends AppCompatActivity {
ImageButton[] imageButton = new ImageButton[8];
boolean[] isIt = new boolean[8];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton [0] = (ImageButton) findViewById(R.id.imageButton7) ;
imageButton [1] = (ImageButton) findViewById(R.id.imageButton2) ;
imageButton [2] = (ImageButton) findViewById(R.id.imageButton3) ;
imageButton [3] = (ImageButton) findViewById(R.id.imageButton4) ;
imageButton [4] = (ImageButton) findViewById(R.id.imageButton5) ;
imageButton [5] = (ImageButton) findViewById(R.id.imageButton6) ;
imageButton [6] = (ImageButton) findViewById(R.id.imageButton10) ;
imageButton [7] = (ImageButton) findViewById(R.id.imageButton8) ;
imageButton [8] = (ImageButton) findViewById(R.id.imageButton9) ;
imageButton[0].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gunwo(0);
checkIt();
}
});
imageButton[1].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gunwo(1);
checkIt();
}
});
imageButton[2].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gunwo(2);
checkIt();
}
});
imageButton[3].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gunwo(3);
checkIt();
}
});
imageButton[4].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gunwo(4);
checkIt();
}
});
imageButton[5].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gunwo(5);
checkIt();
}
});
imageButton[6].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gunwo(6);
checkIt();
}
});
imageButton[7].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gunwo(7);
checkIt();
}
});
imageButton[8].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gunwo(8);
checkIt();
}
});
}
public void gunwo(int y)
{
if(!isIt[y])
{
isIt[y] = true;
imageButton[y].setImageResource(R.drawable.trasfasd);
}
}
public void checkIt()
{
if (isIt[0] && isIt[1]&&isIt[2]){
Toast.makeText(getApplicationContext(), "Game Over", Toast.LENGTH_LONG).show();
}
else if(isIt[3]&&isIt[4]&&isIt[5])
{
Toast.makeText(getApplicationContext(), "Game Over", Toast.LENGTH_LONG).show();
}
else if(isIt[6]&&isIt[7]&&isIt[8])
{
Toast.makeText(getApplicationContext(), "Game Over", Toast.LENGTH_LONG).show();
}
else if(isIt[0]&&isIt[3]&&isIt[6])
{
Toast.makeText(getApplicationContext(), "Game Over", Toast.LENGTH_LONG).show();
}
else if(isIt[1]&&isIt[4]&&isIt[7])
{
Toast.makeText(getApplicationContext(), "Game Over", Toast.LENGTH_LONG).show();
}
else if(isIt[2]&&isIt[5]&&isIt[8])
{
Toast.makeText(getApplicationContext(), "Game Over", Toast.LENGTH_LONG).show();
}
else if(isIt[6]&&isIt[4]&&isIt[2])
{
Toast.makeText(getApplicationContext(), "Game Over", Toast.LENGTH_LONG).show();
}
else if(isIt[0]&&isIt[4]&&isIt[8])
{
Toast.makeText(getApplicationContext(), "Game Over", Toast.LENGTH_LONG).show();
}
}
}
编译器没有发现任何错误,但是当我在模拟器应用程序中打开应用程序时无法正常工作。
这是我的LogCat:
09-24 14:56:27.899 2781-2781/com.holland.tictactoe E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.holland.tictactoe, PID: 2781
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.holland.tictactoe/com.holland.tictactoe.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8
at com.holland.tictactoe.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:1)
一旦你学会阅读logcat,你就会看到
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8
at com.holland.tictactoe.MainActivity.onCreate(MainActivity.java:28)
它告诉你所需的所有信息,真的
由于数组为零索引imageButton [8]
是数组的第九元素。
你只用8
初始化ImageButton[] imageButton = new ImageButton[8];
boolean[] isIt = new boolean[8];
很难创建一个包含8个件的3x3板
将这些更改为[9]
,你真的应该在这里使用循环
for (int i = 0; i < imageButton.length; i++) {
final int position = i;
imageButton[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gunwo(position);
checkIt();
}
});
}