public class TicTacToe {
public static void main(String [] args){
char x[][]=new char[3][3];
int turn=0;
char computer='*';
char human='*';
//TicTacToe TTC = new TicTacToe();
Move(x,HumanLetter(),ComputerLetter(computer,human),turn,WhosTurn(turn));
System.out.println(turn);
}
public static char HumanLetter(){
System.out.println("What would you like to be X or O?");
char human=Console.readChar();
if (human=='o' || human=='O'){
return human='O';
}
else if (human=='x' || human=='X'){
return human='X';
}
System.out.println("Entry Invalid");
return 0;
}
public static char ComputerLetter(char computer, char human){
if (human=='X'){
return computer='O';
}
else if(human=='O') {
return computer='X';
}
return 0;
}
public static boolean WhosTurn(int turn){
boolean HumanTurn;
if (turn%2==0){
HumanTurn=true;
}
else {
HumanTurn=false;
}
//turn++;
return HumanTurn;
}
public static void Move(char [][]x, char human, char computer,int turn, boolean HumanTurn)
{
if (HumanTurn==true){
System.out.println();
System.out.println("Where do you want move");
System.out.println("Choose from Box Spaces 1 - 9");
int space=Console.readInt();
InsertValue(space,x,human);
NewPrint(x);
turn++;
WhosTurn(turn);
CheckIfWon(x,human,computer);
}
else if(turn == 9){
Draw();
}
else if(HumanTurn==false){
CompMove(x,human,computer,turn);
//create the AI FUnction here
}
Move(x,human,computer,turn,HumanTurn);
}
public static void CompMove(char x[][], char human, char computer,int turn){
if(x[1][1] != human){
x[1][1]=computer;
}
else if(x[0][0] != human){
x[0][0]=computer;
}
else if(x[0][2] != human){
x[0][2]=computer;
}
else if(x[2][0] != human){
x[2][0]=computer;
}
else if(x[2][2] != human){
x[2][2]=computer;
}
else if(x[0][1] != human){
x[0][1]=computer;
}
else if(x[1][0] != human){
x[1][0]=computer;
}
else if(x[1][2] != human){
x[1][2]=computer;
}
else if(x[2][1] != human){
x[2][1]=computer;
}
turn++;
CheckIfWon(x,human,computer);
Move(x,HumanLetter(),computer,turn,WhosTurn(turn));
}
public static char InsertValue(int space,char x[][], char t){
if (space==1){
return x[0][0]=t;
}
else if (space==2){
return x[0][1]=t;
}
else if (space==3){
return x[0][2]=t;
}
else if (space==4){
return x[1][0]=t;
}
else if (space==5){
return x[1][1]=t;
}
else if (space==6){
return x[1][2]=t;
}
else if (space==7){
return x[2][0]=t;
}
else if (space==8){
return x[2][1]=t;
}
else if(space==9){
return x[2][2]=t;
}
else {
System.out.println("Invalid Entry");
return 0;
}
}
public static void CheckIfWon(char x[][], char computer, char human){
if (x[0][0]==computer && x[0][1]==computer && x[0][2]==computer
|| x[1][0]==computer && x[1][1]==computer && x[1][2]==computer
|| x[2][0]==computer && x[2][1]==computer && x[2][2]==computer
|| x[0][0]==computer && x[1][0]==computer && x[2][0]==computer
|| x[0][1]==computer && x[1][1]==computer && x[2][1]==computer
|| x[0][2]==computer && x[1][2]==computer && x[2][2]==computer
|| x[0][0]==computer && x[1][1]==computer && x[2][2]==computer
|| x[2][0]==computer && x[1][1]==computer && x[0][2]==computer)
{
System.out.println("Computer has won the game");
}
else if (x[0][0]==human && x[0][1]==human && x[0][2]==human
|| x[1][0]==human && x[1][1]==human && x[1][2]==human
|| x[2][0]==human && x[2][1]==human && x[2][2]==human
|| x[0][0]==human && x[1][0]==human && x[2][0]==human
|| x[0][1]==human && x[1][1]==human && x[2][1]==human
|| x[0][2]==human && x[1][2]==human && x[2][2]==human
|| x[0][0]==human && x[1][1]==human && x[2][2]==human
|| x[2][0]==human && x[1][1]==human && x[0][2]==human){
System.out.println("You have won the game");
}
}
public static void NewPrint(char [][]x){
System.out.println();
System.out.print(" " + x[0][0] + " " + x[0][1] + " " + x[0][2]);
System.out.println();
System.out.print(" " + x[1][0] + " " + x[1][1] + " " + x[1][2]);
System.out.println();
System.out.print(" " + x[2][0] + " " + x[2][1] + " " + x[2][2]);
}
public static void Draw()
{
System.out.println("The Game Has Ended in a Draw");
}
}
2007-01-23
07:43:59
·
1 answers
·
asked by
Anonymous