2017-09-24 100 views
-1

我想制作一个简单的井字游戏。
现在我有这个代码。
在这里,我只需点击一个按钮,它就会变成X,而当我在一行中有3个X时,会有一个Toast显示“游戏结束”。
我还没有设置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)  

+1

编译时间检查与运行时异常不同 - 您已检查并取消选中异常。你应该发布相关的堆栈跟踪。 –

+0

如何做到这一点?你可以给我发送代码吗?我是Android Studio中的新成员。 –

+0

您收到的错误是什么?你收到一个错误?你的应用立即关闭?描述您的问题 – firegloves

回答

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个

改变那些创建一个3×3板[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(); 
     } 
    }); 
} 
+0

它现在有效。非常非常感谢你。我怎么能写8而不是9我是白痴 –

+0

欢迎。随意使用帖子旁边的复选标记接受此答案 –