Hallo!
Ich bin wieder bei einer Aufgabe hängen geblieben^^
Habe ich keine Ahnung wie ich Java klar machen soll, dass wenn alle felder besetzt sind, er ein unentschieden ausgeben soll. Ich könnte natürlich
if(x[0][0][0]*x[1][0][0]... != 0){
system.out.println("Unentschieden")
}
machen, doch finde ich diese Variante nicht schön. Ich habe auch versucht diese Regel durch 6 Schleifen zu erfüllen, doch das ist auch fehlerhaft.
P.s. Wenn ihr sonstige Fehler, Tipps wie man etwas übersichtlicher gestalten kann oder Tipps wie man etwas einfacher gestalten kann habt, dann her damit!
Ich bin wieder bei einer Aufgabe hängen geblieben^^
Habe ich keine Ahnung wie ich Java klar machen soll, dass wenn alle felder besetzt sind, er ein unentschieden ausgeben soll. Ich könnte natürlich
if(x[0][0][0]*x[1][0][0]... != 0){
system.out.println("Unentschieden")
}
machen, doch finde ich diese Variante nicht schön. Ich habe auch versucht diese Regel durch 6 Schleifen zu erfüllen, doch das ist auch fehlerhaft.
P.s. Wenn ihr sonstige Fehler, Tipps wie man etwas übersichtlicher gestalten kann oder Tipps wie man etwas einfacher gestalten kann habt, dann her damit!
Java:
/**
*
* Beschreibung
*
* @version 1.0 vom 11.05.2011
* @author
*/
public class TicTacToe_3D {
public static void main(String[] args) {
int repeat = 0, drei = 0, fünf = 0, switch1 = 0, x = 0, b = 0, z = 0, spielerwahl = 1;
int[][][]TTT= new int[3][3][3];
System.out.println("Spieler 1 hat die Zahl 3");
System.out.println("Spieler 2 hat die Zahl 5");
System.out.println();
for(int l=0; l < TTT.length; l++){
for(int n=0; n < TTT.length; n++){
for(int i=0; i < TTT.length; i++){
System.out.print(TTT[i][n][l]+" ");
}
System.out.print(" ");
}
System.out.println(" ");
}
System.out.println();
while(repeat < 27){
if(spielerwahl == 1){
System.out.println("Spieler 1 ist an der Reihe");
switch1 = 1;
spielerwahl = 2;
}
else{
System.out.println("Spieler 2 ist an der Reihe");
switch1 = 2;
spielerwahl = 1;
}
switch (switch1){
case 1:
System.out.println("Geben Sie die Position wo Sie ihr Zeichen hinmachen wollen an.");
System.out.print("X-Achse: "); x = Input.readInt();
System.out.print("Dimension: ");b = Input.readInt();
System.out.print("Y-Achse: "); z = Input.readInt();
System.out.println();
if( TTT[x][b][z] == 0){
TTT[x][b][z] = 3;
}
else do{
System.out.println("Das Feld ist schon belegt, probieren Sie es noch einmal!");
System.out.print("X-Achse: "); x = Input.readInt();
System.out.print("Dimension: ");b = Input.readInt();
System.out.print("Y-Achse: "); z = Input.readInt();
System.out.println();
}while( TTT[x][b][z] != 0);
TTT[x][b][z] = 3;
break;
case 2:
System.out.println("Geben Sie die Position wo Sie ihr Zeichen hinmachen wollen an.");
System.out.print("X-Achse: "); x = Input.readInt();
System.out.print("Dimension: ");b = Input.readInt();
System.out.print("Y-Achse: "); z = Input.readInt();
System.out.println();
if( TTT[x][b][z] == 0){
TTT[x][b][z] = 5;
}
else do{
System.out.println("Das Feld ist schon belegt, probieren Sie es noch einmal!");
System.out.print("X-Achse: "); x = Input.readInt();
System.out.print("Dimension: ");b = Input.readInt();
System.out.print("Y-Achse: "); z = Input.readInt();
System.out.println();
}while( TTT[x][b][z] != 0);
TTT[x][b][z] = 5;
break;
}
for(int l=0; l < TTT.length; l++){
for(int n=0; n < TTT.length; n++){
for(int i=0; i < TTT.length; i++){
System.out.print(TTT[i][n][l]+" ");
}
System.out.print(" ");
}
System.out.println(" ");
}
System.out.println();
repeat++;
//---------------------------------------------REGEL Diagonal 2D-------------------------------------
for(int i = 0; i < TTT.length; i++){
for(int l = 0; l == 0; l++){
if((TTT[l][i][l])+(TTT[l+1][i][l+1])+(TTT[l+2][i][l+2]) == 9){
System.out.println("Spieler 1 hat mit einer 2D Diagonalen gewonnen!");
repeat = 27;
}
if((TTT[l][i][l])+(TTT[l+1][i][l+1])+(TTT[l+2][i][l+2]) == 15){
System.out.println("Spieler 2 hat mit einer 2D Diagonalen gewonnen!");
repeat = 27;
}
}
}
for(int i = 0; i < TTT.length; i++){
for(int l = 0; l == 0; l++){
if((TTT[l+2][i][l])+(TTT[l+1][i][l+1])+(TTT[l][i][l+2]) == 9){
System.out.println("Spieler 1 hat mit einer 2D Diagonalen gewonnen!");
repeat = 27;
}
if((TTT[l+2][i][l])+(TTT[l+1][i][l+1])+(TTT[l][i][l+2]) == 15){
System.out.println("Spieler 2 hat mit einer 2D Diagonalen gewonnen!");
repeat = 27;
}
}
}
//---------------------------------------------REGEL Diagonal 3D-------------------------------------
for(int i = 0; i == 0; i++){
if((TTT[i][i][i])+(TTT[i+1][i+1][i+1])+(TTT[i+2][i+2][i+2]) == 9){
System.out.println("Spieler 1 hat mit einer 3D Diagonalen gewonnen!");
repeat = 27;
}
if((TTT[i][i][i])+(TTT[i+1][i+1][i+1])+(TTT[i+2][i+2][i+2]) == 15){
System.out.println("Spieler 2 hat mit einer 3D Diagonalen gewonnen!");
repeat = 27;
}
}
for(int i = 0; i == 0; i++){
if((TTT[i+2][i+2][i])+(TTT[i+1][i+1][i+1])+(TTT[i][i][i+2]) == 9){
System.out.println("Spieler 1 hat mit einer 3D Diagonalen gewonnen!");
repeat = 27;
}
if((TTT[i+2][i+2][i])+(TTT[i+1][i+1][i+1])+(TTT[i][i][i+2]) == 15){
System.out.println("Spieler 2 hat mit einer 3D Diagonalen gewonnen!");
repeat = 27;
}
}
//---------------------------------------------REGEL Horizontal 2D-------------------------------------
for(int i = 0; i < TTT.length; i++){
for(int l=0; l < TTT.length; l++){
for(int u=0; u == 0; u++){
if((TTT[u][i][l])+(TTT[u+1][i][l])+(TTT[u+2][i][l]) == 9){
System.out.println("Spieler 1 hat mit einer 2D Horizontalen gewonnen!");
repeat = 27;
}
if((TTT[u][i][l])+(TTT[u+1][i][l])+(TTT[u+2][i][l]) == 15){
System.out.println("Spieler 2 hat mit einer 2D Horizontalen gewonnen!");
repeat = 27;
}
}
}
}
//---------------------------------------------REGEL Horizontal 3D-------------------------------------
for(int i = 0; i == 0; i++){
for(int l=0; l < TTT.length; l++){
for(int u=0; u < TTT.length; u++){
if((TTT[u][i][l])+(TTT[u][i+1][l])+(TTT[u][i+2][l]) == 9){
System.out.println("Spieler 1 hat mit einer 3D Horizontalen gewonnen!");
repeat = 27;
}
if((TTT[u][i][l])+(TTT[u][i+1][l])+(TTT[u][i+2][l]) == 15){
System.out.println("Spieler 2 hat mit einer 3D Horizontalen gewonnen!");
repeat = 27;
}
}
}
}
//---------------------------------------------REGEL Vertikal 2D-------------------------------------
for(int i = 0; i < TTT.length; i++){
for(int l=0; l < TTT.length; l++){
for(int u=0; u == 0; u++){
if((TTT[l][i][u])+(TTT[l][i][u+1])+(TTT[l][i][u+2]) == 9){
System.out.println("Spieler 1 hat mit einer 2D Vertikalen gewonnen!");
repeat = 27;
}
if((TTT[l][i][u])+(TTT[l][i][u+1])+(TTT[l][i][u+2]) == 15){
System.out.println("Spieler 2 hat mit einer 2D Vertikalen gewonnen!");
repeat = 27;
}
}
}
}
//---------------------------------------------REGEL Vertikal 3D-------------------------------------
for(int i = 0; i == 0; i++){
for(int l=0; l < TTT.length; l++){
for(int u=0; u == 0; u++){
if((TTT[l][i][u])+(TTT[l][i+1][u+1])+(TTT[l][i+2][u+2]) == 9){
System.out.println("Spieler 1 hat mit einer 3D Vertikalen gewonnen!");
repeat = 27;
}
if((TTT[u][i][l])+(TTT[u][i+1][l])+(TTT[u][i+2][l]) == 15){
System.out.println("Spieler 2 hat mit einer 3D Vertikalen gewonnen!");
repeat = 27;
}
}
}
}
//---------------------------------------------REGEL Unentschieden-------------------------------------
for(int i = 0; i < TTT.length; i++){
for(int l=0; l < TTT.length; l++){
for(int u=0; u == 0; u++){
if((TTT[u][i][l])*(TTT[u+1][i][l])*(TTT[u+2][i][l]) != 0){
for(int j=0; j < TTT.length; j++){
for(int n=0; n < TTT.length; n++){
for(int m=0; m == 0; m++){
if((TTT[n][j][m])*(TTT[n][j][m+1])*(TTT[n][j][m+2]) != 0){
System.out.println("Unentschieden!");
}
}
}
}
}
}
}
}
}
}
}