Ich möchte einfach Tic Tac Toe machen.
Jetzt habe ich diesen Code.
Hier klicke ich einfach auf einen Knopf und es wird X, und wenn ich 3 X in einer Reihe habe, kommt ein Toast, der "Game over" anzeigt.
Ich habe Os noch nicht eingestellt.Tic Tac Toe Spiel App funktioniert nicht
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();
}
}
}
Compiler findet keinen Bug, aber wenn ich die App in der Emulator App öffne funktioniert nicht.
Hier ist meine 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)
.
Compile verwenden Zeitprüfungen unterscheiden sich von Laufzeitausnahmen - Sie haben Ausnahmen aktiviert und deaktiviert. Sie sollten den relevanten Stack-Trace veröffentlichen. –
Wie geht das? Kannst du mir den Code schicken? Ich bin neu in Android Studio. –
Was ist der Fehler, den Sie erhalten? Erhalten Sie einen Fehler? Ihre App schließt sofort? Beschreiben Sie Ihr Problem – firegloves