Hi,
gestern Abend habe ich einfach mal entschlossen Tic Tac Toe zu programmieren, aber bei der KI (Künstliche Intelligenz), hat es gescheitert.
Vorerst: Ich bin damit noch nicht fertig. Ich will erst einmal verhindern, dass der Spieler gewinnt bzw. drei in der richtigen Reigenfolge schafft.
Kompiliert das bitte mal. Es kreuzt einfach nicht an den richtigen Stellen an.
Zuvor war der Code bei mir so:
Da war das Problem, dass an manchen Stellen der Computer zwei Stellen gleichzeitig angekreutzt hat.
Wie kann ich das besser hinkriegen?!
Liebe Grüße
Reality
gestern Abend habe ich einfach mal entschlossen Tic Tac Toe zu programmieren, aber bei der KI (Künstliche Intelligenz), hat es gescheitert.
Vorerst: Ich bin damit noch nicht fertig. Ich will erst einmal verhindern, dass der Spieler gewinnt bzw. drei in der richtigen Reigenfolge schafft.
Code:
import java.io.*;
class TicTacToe {
public static void main(String[] args) {
int eingabe= -1;
char matrix[][]={
{' ', ' ', ' ','#', ' ', ' ', '#', ' ', ' ',' '},
{' ', '1', ' ','#', ' ', '2', '#', ' ', '3', ' '},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{' ', '4', ' ', '#', ' ', '5', '#', ' ', '6', ' '},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{' ', '7', ' ', '#', ' ', '8', '#', ' ', '9', ' '},
{' ', ' ', ' ','#', ' ', ' ', '#', ' ', ' ', ' '}, };
do{
try{
BufferedReader daten= new BufferedReader(new InputStreamReader(System.in));
for(int j=0; j<matrix.length; j++){
for (int i = 0; i < 10; i++) {
System.out.print(String.valueOf(matrix[j][i]));
}
System.out.println();
}
System.out.println("Welches Feld soll angekreuzt werden? (0 eingeben, um abzubrechen)");
eingabe = Integer.parseInt(daten.readLine());
if(eingabe == 0)
System.out.println("Beendet.");
else
switch(eingabe)
{
case 1:
matrix[1][1]='X';
break;
case 2:
matrix[1][5]='X';
break;
case 3:
matrix[1][8]='X';
break;
case 4:
matrix[3][1]='X';
break;
case 5:
matrix[3][5]='X';
break;
case 6:
matrix[3][8]='X';
break;
case 7:
matrix[5][1]='X';
break;
case 8:
matrix[5][5]='X';
break;
case 9:
matrix[5][8]='X';
break;
default:
System.out.println("Feld nicht vorhanden!");
}
// 1, 2
if(matrix[1][1]=='X' && matrix[1][5]=='X' && matrix[1][8]!='X' || matrix[1][8]!='O')
matrix[1][8]='O';
//1, 4
else if(matrix[1][1]=='X' && matrix[3][1]=='X' && matrix[5][1]!='X' || matrix[5][1]!='O')
matrix[5][1]='O';
//4, 7
else if(matrix[3][1]=='X' && matrix[5][1]=='X' && matrix[1][1]!='X' || matrix[1][1]!='O')
matrix[1][1]='O';
//2, 5
else if(matrix[1][5]=='X' && matrix[3][5]=='X' && matrix[5][5]!='X' || matrix[5][5]!='O')
matrix[5][5]='O';
//5, 8
else if(matrix[3][5]=='X' && matrix[5][5]=='X' && matrix[1][5]!='X' || matrix[1][5]!='O')
matrix[1][5]='O';
//3, 6
else if(matrix[1][8]=='X' && matrix[3][8]=='X' && matrix[5][8]!='X' || matrix[5][8]!='O')
matrix[5][8]='O';
//6, 9
else if(matrix[3][8]=='X' && matrix[5][8]=='X' && matrix[1][8]!='X' || matrix[1][8]!='O')
matrix[1][8]='O';
//4, 5
else if(matrix[3][1]=='X' && matrix[3][5]=='X' && matrix[3][8]!='X' || matrix[3][8]!='O')
matrix[3][8]='O';
//5, 6
else if(matrix[3][5]=='X' && matrix[3][8]=='X' && matrix[3][1]!='X' || matrix[3][1]!='O')
matrix[3][1]='O';
//1, 5
else if(matrix[1][1]=='X' && matrix[3][5]=='X' && matrix[5][8]!='X' || matrix[5][8]!='O')
matrix[5][8]='O';
//5, 9
else if(matrix[3][5]=='X' && matrix[5][8]=='X' && matrix[1][1]!='X' || matrix[1][1]!='O')
matrix[1][1]='O';
} catch(IOException f){
System.out.println("Fehler bei der Eingabe! Bitte nur Zahlen eingeben!");
}
}while(eingabe!=0);
}
}
Kompiliert das bitte mal. Es kreuzt einfach nicht an den richtigen Stellen an.
Zuvor war der Code bei mir so:
Code:
// 1, 2
if(matrix[1][1]=='X' && matrix[1][5]=='X')
matrix[1][8]='O';
//1, 4
if(matrix[1][1]=='X' && matrix[3][1]=='X')
matrix[5][1]='O';
//4, 7
if(matrix[3][1]=='X' && matrix[5][1]=='X')
matrix[1][1]='O';
//2, 5
if(matrix[1][5]=='X' && matrix[3][5]=='X')
matrix[5][5]='O';
//5, 8
if(matrix[3][5]=='X' && matrix[5][5]=='X')
matrix[1][5]='O';
//3, 6
if(matrix[1][8]=='X' && matrix[3][8]=='X')
matrix[5][8]='O';
//6, 9
if(matrix[3][8]=='X' && matrix[5][8]=='X')
matrix[1][8]='O';
//4, 5
if(matrix[3][1]=='X' && matrix[3][5]=='X')
matrix[3][8]='O';
//5, 6
if(matrix[3][5]=='X' && matrix[3][8]=='X')
matrix[3][1]='O';
//1, 5
if(matrix[1][1]=='X' && matrix[3][5]=='X')
matrix[5][8]='O';
//5, 9
if(matrix[3][5]=='X' && matrix[5][8]=='X')
matrix[1][1]='O';
Da war das Problem, dass an manchen Stellen der Computer zwei Stellen gleichzeitig angekreutzt hat.
Wie kann ich das besser hinkriegen?!
Liebe Grüße
Reality