Hallo,
ich bin grad dabei hier ein kleines TicTacToe Spiel zu schreiben, jedoch habe ich bei den Gewinnoptionen(ich denke jeder kennt diese bei TicTacToe) einen Fehler. Und zwar, wenn ich als Spieler x gewinne wird alles beendet(Zeile 23), bei Spieler o jedoch werde ich im Anschluss noch um einen Zug gefragt und erst danach beendet es ich weiß aber nicht wieso.
Meine weitere Frage ist, wie ich hierbei noch eine Unentschieden Abfrage erstelle, also wenn alle Felder belegt sind, aber kein xxx oder ooo vorhanden ist. Das habe ich bereits mit der Variable counter(auch gehighlighted im code), als Zugermittler versucht, jedoch ohne Erfolg.
Wäre nett, wenn mir jemand schnell helfen könnte .
Grüße
[CODE lang="java" title="TicTacToe" highlight="13, 40, 62, 69, 23-34"]import java.math.*;
import java.util.*;
public class TicTakToe {
public static char board[] = new char[9]; // creating public char array board with 9 positions
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = 0; // declaring integer count
int counter = 0;
int pos; // initiating integer pos
int player1; // initiating integer player1
boolean finish = false; // declaring boolean finish
init(); // output init method
do // do as long as board got at least one ' ' spot and finish is false
{
if (board[0] == 'x' && board[1] == 'x' && board[2] == 'x' || board[0] == 'x' && board[3] == 'x' && board[6] == 'x' || board[0] == 'x' && board[4] == 'x' && board[8] == 'x' || board[3] == 'x' && board[4] == 'x' && board[5] == 'x' || board[6] == 'x' && board[7] == 'x' && board[8] == 'x' || board[1] == 'x' && board[4] == 'x' && board[7] == 'x' || board[2] == 'x' && board[5] == 'x' && board[8] == 'x' || board[6] == 'x' && board[4] == 'x' && board[2] == 'x')
{ // winning options for 'x'
System.out.println("Spieler x hat gewonnen!"); // output in console
finish = true; // set finish true
break; // end
}else if (board[0] == 'o' && board[1] == 'o' && board[2] == 'o' || board[0] == 'o' && board[3] == 'o' && board[6] == 'o' || board[0] == 'o' && board[4] == 'o' && board[8] == 'o' || board[3] == 'o' && board[4] == 'o' && board[5] == 'o' || board[6] == 'o' && board[7] == 'o' && board[8] == 'o' || board[1] == 'o' && board[4] == 'o' && board[7] == 'o' || board[2] == 'o' && board[5] == 'o' && board[8] == 'o' || board[6] == 'o' && board[4] == 'o' && board[2] == 'o')
{ // winning options for 'o'
System.out.println("Spieler o hat gewonnen!"); // output in console
finish = true; // set finish true
break; // end
}
do
{ // creating random number in between 0 - 8
pos = (int)(Math.random()*10) % 9;
count++;
counter++;
}while (board[pos] != ' ' && count < 1000); //
System.out.println("Spieler o wählt Position " + pos + "!"); // output in console
board[pos] = 'o'; // setting random number(computers turn) as 'o'
print(); // output print method (board)
System.out.println("An welcher Stelle wollen Sie 'x' setzen (0 - 8)?"); // output turn question for player1
player1 = sc.nextInt(); // sets player1 as keyboard input
if (board[player1] != ' ') // new input if Position is already used
{
System.out.println("Ungültige Eingabe! Entweder ihre gewählte Position ist bereits besetzt, oder ihre Eingabe ist falsch."); // output in console
System.out.println("Wählen Sie erneut eine Position 0 - 8!"); //output in console
player1 = sc.nextInt(); // sets player1 as keyboard input
}
if (board[player1] == ' ' ) // set player1 turn as 'x' if it is empty
{
board[player1] = 'x';
System.out.println("Sie haben Position " + player1 + " gewählt!");
counter++;
}
print(); // output print method
}while(board[0] == ' ' || board[1] == ' ' || board[2] == ' ' || board[3] == ' ' || board[4] == ' ' || board[5] == ' ' || board[6] == ' ' || board[7] == ' ' || board[8] == ' ' && finish == false && counter < 9);
}
public static void print() // output of playing board as method
{
System.out.println("[" + board[0] + "][" + board[1] + "][" + board[2] + "]");
System.out.println("[" + board[3] + "][" + board[4] + "][" + board[5] + "]");
System.out.println("[" + board[6] + "][" + board[7] + "][" + board[8] + "]");
}
public static void init() // initiating and declaring board values as own method
{
board[0] = ' ';
board[1] = ' ';
board[2] = ' ';
board[3] = ' ';
board[4] = ' ';
board[5] = ' ';
board[6] = ' ';
board[7] = ' ';
board[8] = ' ';
}
}[/CODE]
ich bin grad dabei hier ein kleines TicTacToe Spiel zu schreiben, jedoch habe ich bei den Gewinnoptionen(ich denke jeder kennt diese bei TicTacToe) einen Fehler. Und zwar, wenn ich als Spieler x gewinne wird alles beendet(Zeile 23), bei Spieler o jedoch werde ich im Anschluss noch um einen Zug gefragt und erst danach beendet es ich weiß aber nicht wieso.
Meine weitere Frage ist, wie ich hierbei noch eine Unentschieden Abfrage erstelle, also wenn alle Felder belegt sind, aber kein xxx oder ooo vorhanden ist. Das habe ich bereits mit der Variable counter(auch gehighlighted im code), als Zugermittler versucht, jedoch ohne Erfolg.
Wäre nett, wenn mir jemand schnell helfen könnte .
Grüße
[CODE lang="java" title="TicTacToe" highlight="13, 40, 62, 69, 23-34"]import java.math.*;
import java.util.*;
public class TicTakToe {
public static char board[] = new char[9]; // creating public char array board with 9 positions
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = 0; // declaring integer count
int counter = 0;
int pos; // initiating integer pos
int player1; // initiating integer player1
boolean finish = false; // declaring boolean finish
init(); // output init method
do // do as long as board got at least one ' ' spot and finish is false
{
if (board[0] == 'x' && board[1] == 'x' && board[2] == 'x' || board[0] == 'x' && board[3] == 'x' && board[6] == 'x' || board[0] == 'x' && board[4] == 'x' && board[8] == 'x' || board[3] == 'x' && board[4] == 'x' && board[5] == 'x' || board[6] == 'x' && board[7] == 'x' && board[8] == 'x' || board[1] == 'x' && board[4] == 'x' && board[7] == 'x' || board[2] == 'x' && board[5] == 'x' && board[8] == 'x' || board[6] == 'x' && board[4] == 'x' && board[2] == 'x')
{ // winning options for 'x'
System.out.println("Spieler x hat gewonnen!"); // output in console
finish = true; // set finish true
break; // end
}else if (board[0] == 'o' && board[1] == 'o' && board[2] == 'o' || board[0] == 'o' && board[3] == 'o' && board[6] == 'o' || board[0] == 'o' && board[4] == 'o' && board[8] == 'o' || board[3] == 'o' && board[4] == 'o' && board[5] == 'o' || board[6] == 'o' && board[7] == 'o' && board[8] == 'o' || board[1] == 'o' && board[4] == 'o' && board[7] == 'o' || board[2] == 'o' && board[5] == 'o' && board[8] == 'o' || board[6] == 'o' && board[4] == 'o' && board[2] == 'o')
{ // winning options for 'o'
System.out.println("Spieler o hat gewonnen!"); // output in console
finish = true; // set finish true
break; // end
}
do
{ // creating random number in between 0 - 8
pos = (int)(Math.random()*10) % 9;
count++;
counter++;
}while (board[pos] != ' ' && count < 1000); //
System.out.println("Spieler o wählt Position " + pos + "!"); // output in console
board[pos] = 'o'; // setting random number(computers turn) as 'o'
print(); // output print method (board)
System.out.println("An welcher Stelle wollen Sie 'x' setzen (0 - 8)?"); // output turn question for player1
player1 = sc.nextInt(); // sets player1 as keyboard input
if (board[player1] != ' ') // new input if Position is already used
{
System.out.println("Ungültige Eingabe! Entweder ihre gewählte Position ist bereits besetzt, oder ihre Eingabe ist falsch."); // output in console
System.out.println("Wählen Sie erneut eine Position 0 - 8!"); //output in console
player1 = sc.nextInt(); // sets player1 as keyboard input
}
if (board[player1] == ' ' ) // set player1 turn as 'x' if it is empty
{
board[player1] = 'x';
System.out.println("Sie haben Position " + player1 + " gewählt!");
counter++;
}
print(); // output print method
}while(board[0] == ' ' || board[1] == ' ' || board[2] == ' ' || board[3] == ' ' || board[4] == ' ' || board[5] == ' ' || board[6] == ' ' || board[7] == ' ' || board[8] == ' ' && finish == false && counter < 9);
}
public static void print() // output of playing board as method
{
System.out.println("[" + board[0] + "][" + board[1] + "][" + board[2] + "]");
System.out.println("[" + board[3] + "][" + board[4] + "][" + board[5] + "]");
System.out.println("[" + board[6] + "][" + board[7] + "][" + board[8] + "]");
}
public static void init() // initiating and declaring board values as own method
{
board[0] = ' ';
board[1] = ' ';
board[2] = ' ';
board[3] = ' ';
board[4] = ' ';
board[5] = ' ';
board[6] = ' ';
board[7] = ' ';
board[8] = ' ';
}
}[/CODE]
Zuletzt bearbeitet: