/**TIC TAC TOE**/
//-------------------------------------------------------------------------------------------------------------------------
public class TicTacToe2
{
//Deklarierung der globalen Variablen//
static char feld1,feld2,feld3,feld4,feld5,feld6,feld7,feld8,feld9;
static int bfeld1,bfeld2,bfeld3,bfeld4,bfeld5,bfeld6,bfeld7,bfeld8,bfeld9;
static int eingabe;
static double zahl;
static int besetzt;
static int korrekt;
static double eingabeComputerberechnung;
static int spielZustand; //0 spiel läuft
//1 pc gewinnt
//2 spieler gewinnt
//3 unentschieden
public static Konsole kons;
/*Funktion damit Spieler am Zug ist */
//---------------------------------------------------------------------------------------------------------------------------
public static void spielerZieht()
{
Konsole kons = new Konsole();
do
{
kons.gotoXY(19,0);
kons.print("YOUR TURN !!! CHOOSE YOUR FIELD!!! (1-9): ");
eingabe = kons.readInt();
kons.cls();
if((eingabe==1)&&(bfeld1==0))
{
feld1 = 'X';
bfeld1 = 1;
korrekt = 1;
}
else
{
if((eingabe==2)&&(bfeld2==0))
{
feld2 = 'X';
bfeld2 = 1;
korrekt = 1;
}
else
{
if((eingabe ==3)&&(bfeld3==0))
{
feld3 = 'X';
bfeld3 =1;
korrekt = 1;
}
else
{
if((eingabe==4)&&(bfeld4==0))
{
feld4 = 'X';
bfeld4 =1;
korrekt=1;
}
else
{
if((eingabe==5)&&(bfeld5==0))
{
feld5='X';
bfeld5=1;
korrekt=1;
}
else
{
if((eingabe==6)&&(bfeld6==0))
{
feld6='X';
bfeld6=1;
korrekt=1;
}
else
{
if((eingabe==7)&&(bfeld7==0))
{
feld7='X';
bfeld7=1;
korrekt=1;
}
else
{
if((eingabe==8)&&(bfeld8==0))
{
feld8='X';
bfeld8=1;
korrekt=1;
}
else
{
if((eingabe==9)&&(bfeld9==0))
{
feld9='X';
bfeld9=1;
korrekt=1;
}
else
{
//fals das Feld bereits besetzt ist wird der Vorgang wiederholt//
System.out.print("\n\nVersuchen sie es lieber noch einmal!!\n\n");
korrekt=0;
}
}
}
}
}
}
}
}
}
}while(korrekt==0);//end of do/while//
}//end of Funktion "spielerZieht"//
/**Funktion damit überpfrüft wird ob schon jemand gewonnen hat oder nicht*/
//---------------------------------------------------------------------------------------------------------------------------------
public static void ueberpruefen()
{
//Hat der Spieler gewonnen?//
if ((feld1 == 'X')&&(feld2 == 'X')&&(feld3 == 'X'))
{//start of if
spielZustand = 1;
}//end of if
else
{
if ((feld4 == 'X')&&(feld5 == 'X')&&(feld6 == 'X'))
{//start of if
spielZustand = 1;
}//end of if
else
{
if ((feld7 == 'X')&&(feld8 == 'X')&&(feld9 == 'X'))
{//start of if
spielZustand = 1;
}//end of if
else
{
if ((feld1 == 'X')&&(feld5 == 'X')&&(feld9 == 'X'))
{//start of if
spielZustand = 1;
}//end of if
else
{
if ((feld1 == 'X')&&(feld4 == 'X')&&(feld7 == 'X'))
{//start of if
spielZustand = 1;
}//end of if
else
{
if ((feld2 == 'X')&&(feld5 == 'X')&&(feld8 == 'X'))
{//start of if
spielZustand = 1;
}//end of if
else
{
if ((feld3 == 'X')&&(feld6 == 'X')&&(feld9 == 'X'))
{//start of if
spielZustand = 1;
}//end of if
else
{
if ((feld3 == 'X')&&(feld5 == 'X')&&(feld7 == 'X'))
{//start of if
spielZustand = 1;
}//end of if
/*hat der computer gewonnen?*/ else
{
if ((feld1 == 'O')&&(feld2 == 'O')&&(feld3 == 'O'))
{//start of if
spielZustand = 2;
}//end of if
else
{
if ((feld4 == 'O')&&(feld5 == 'O')&&(feld6 == 'O'))
{//start of if
spielZustand = 2;
}//end of if
else
{
if ((feld7 == 'O')&&(feld8 == 'O')&&(feld9 == 'O'))
{//start of if
spielZustand = 2;
}//end of if
else
{
if ((feld1 == 'O')&&(feld5 == 'O')&&(feld9 == 'O'))
{//start of if
spielZustand = 2;
}//end of if
else
{
if ((feld1 == 'O')&&(feld4 == 'O')&&(feld7 == 'O'))
{//start of if
spielZustand = 2;
}//end of if
else
{
if ((feld2 == 'O')&&(feld5 == 'O')&&(feld8 == 'O'))
{//start of if
spielZustand = 2;
}//end of if
else
{
if ((feld3 == 'O')&&(feld6 == 'O')&&(feld9 == 'O'))
{//start of if
spielZustand = 2;
}//end of if
else
{
if ((feld3 == 'O')&&(feld5 == 'O')&&(feld7 == 'O'))
{//start of if
spielZustand = 2;
}//end of if
else
{
if ((besetzt == 9)&&(spielZustand == 0))
{
spielZustand = 3;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}//end of Funktion "ueberpruefen"//
/*Funktion wodurch ermittelt wird wer gewonnen hat!*/
//--------------------------------------------------------------------------------------------------------------------------------
public static void sieger()
{
Konsole kons = new Konsole();
if (spielZustand == 1)
{
System.out.println ("YOU WIN!!!!\n\n\n\n\n");
kons.sleep(2500);
}
else if (spielZustand == 2)
{
System.out.println ("YOU LOSE !!!\n\n\n\n\n");
kons.sleep(2500);
}
else if (spielZustand == 3)
{
System.out.println ("Keiner hat den Kampf gewonnen!!!!\n\n\n\n\n");
kons.sleep(2500);
}
}//end of Funktion "Sieger"//
/*Funktion in der der Computer am zug ist */
//---------------------------------------------------------------------------------------------------------------------------------
public static void computerZieht()
{
do
{
// zahl = Math.random()*9;
//Eine Zufallszahl wird ermittelt welche bestimmt wo der Computer sein "O" setzt//
eingabeComputerberechnung = (double) Math.random ()*9;
zahl = (int) eingabeComputerberechnung;
if((zahl==0)&&(bfeld1==0))
{
feld1 = 'O';
bfeld1 = 1;
korrekt = 1;
}
else
{
if((zahl==1)&&(bfeld2==0))
{
feld2 = 'O';
bfeld2 = 1;
korrekt = 1;
}
else
{
if((zahl ==2)&&(bfeld3==0))
{
feld3 = 'O';
bfeld3 =1;
korrekt =1;
}
else
{
if((zahl==3)&&(bfeld4==0))
{
feld4 = 'O';
bfeld4 =1;
korrekt=1;
}
else
{
if((zahl==4)&&(bfeld5==0))
{
feld5='O';
bfeld5=1;
korrekt=1;
}
else
{
if((zahl==5)&&(bfeld6==0))
{
feld6='O';
bfeld6=1;
korrekt=1;
}
else
{
if((zahl==6)&&(bfeld7==0))
{
feld7='O';
bfeld7=1;
korrekt=1;
}
else
{
if((zahl==7)&&(bfeld8==0))
{
feld8='O';
bfeld8=1;
korrekt=1;
}
else
{
if((zahl==8)&&(bfeld9==0))
{
feld9='O';
bfeld9=1;
korrekt=1;
}
else
{
korrekt=0;
}
}
}
}
}
}
}
}
}
}while(korrekt==0);
}
//Funktion mit der das Spielfeld ausgegeben wird !!!//
//---------------------------------------------------------------------------------------------------------------------------------
public static void spielfeldAusgeben ()
{
System.out.print("Tic-Tac-Toe\n\n");
System.out.print("\n\t1 2 3\n");
System.out.print("1.\t"+feld1+"|"+feld2+"|"+feld3+"\n");
System.out.print("\t-+-+-\n");
System.out.print("2.\t"+feld4+"|"+feld5+"|"+feld6+"\n");
System.out.print("\t-+-+-\n");
System.out.print("3.\t"+feld7+"|"+feld8+"|"+feld9+"\n\n\n\n\n");
}//end of Funktion "spielfedAusgeben"//
//MAIN//
//---------------------------------------------------------------------------------------------------------------------------------
public static void main (String args[])
{
//Vorspann bevor das Spiel beginnt !!//
Konsole kons = new Konsole();
kons.gotoXY(25,2);
System.out.print("Welcome to TIC TAC TOE");
kons.sleep(1500);
kons.cls();
kons.gotoXY(30,2);
System.out.print("Are You Ready???");
kons.sleep(2500);
kons.cls();
kons.gotoXY(38,5);
System.out.print("3");
kons.sleep(300);
kons.cls();
kons.gotoXY(38,5);
System.out.print("2");
kons.sleep(300);
kons.cls();
kons.gotoXY(38,5);
System.out.print("1");
kons.sleep(300);
kons.cls();
kons.gotoXY(35,5);
System.out.print("FIGHT!!!");
kons.sleep(1300);
kons.cls();
//Das Spiel beginnt"!!/
do
{
spielerZieht();
spielfeldAusgeben();
ueberpruefen();
kons.sleep(600);
sieger();
kons.cls();
computerZieht();
spielfeldAusgeben();
ueberpruefen();
kons.sleep(600);
sieger();
ueberpruefen();
}while(spielZustand==0);//end of do/while//
}//end of main
}//end of class