TicTacToe
An old game that i used to play with friends
What it does
2 players can play with X and O and having fun one by one
How we built it
With simple methods in java
Accomplishments that we're proud of
simple and fun
code Tic-Tac-Toe game
```package mlh; import java.util.Scanner;
public class TicTacToe { private int cpt; private char pos[]=new char[10]; private char player;
public static void main(String args[])
{
String response;
TicTacToe Toe=new TicTacToe();
do{
Toe.newBoard();
Toe.play();
System.out.println ("\nPlay again (Enter yes/no)? ");
Scanner in =new Scanner(System.in);
response=in.nextLine();
}while (response.equals("yes"));
}
public void newBoard()
{
char posdef[] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9'};
int i;
cpt = 0;
player = 'X';
for (i=1; i<10; i++) pos[i]=posdef[i];
currentBoard();
}
public String currentBoard()
{
System.out.println( "\n" );
System.out.println( "\t\t" + pos [1] + " | " +pos [2]+ " | " +pos [3]);
System.out.println( " \t\t ___|____|___ " );
System.out.println( "\t\t" +pos [4]+ " | " +pos [5]+ " | " +pos [6]);
System.out.println( " \t\t ___|____|___ " );
System.out.println( "\t\t" +pos [7]+ " | " +pos [8]+ " | " +pos [9]);
System.out.println( " \t\t | | " );
return "currentBoard";
}
public void play()
{
int spot;
char blank = ' ';
System.out.println( "Player " + getPlayer() +" will be 'X'" );
do {
currentBoard();
System.out.print( "\nPlayer " + getPlayer() +" choose a position : " );
boolean posTaken = true;
while (posTaken) {
Scanner in =new Scanner (System.in);
spot=in.nextInt();
posTaken = checkPos(spot);
if(posTaken==false)
pos[spot]=getPlayer();
}
System.out.println( "Nice move." );
currentBoard();
nextPlayer();
}while ( checkWinner() == blank );
}
public char checkWinner()
{
char Winner = ' ';
// Check if X wins
if (pos[1] == 'X' && pos[2] == 'X' && pos[3] == 'X') Winner = 'X';
if (pos[4] == 'X' && pos[5] == 'X' && pos[6] == 'X') Winner = 'X';
if (pos[7] == 'X' && pos[8] == 'X' && pos[9] == 'X') Winner = 'X';
if (pos[1] == 'X' && pos[4] == 'X' && pos[7] == 'X') Winner = 'X';
if (pos[2] == 'X' && pos[5] == 'X' && pos[8] == 'X') Winner = 'X';
if (pos[3] == 'X' && pos[6] == 'X' && pos[9] == 'X') Winner = 'X';
if (pos[1] == 'X' && pos[5] == 'X' && pos[9] == 'X') Winner = 'X';
if (pos[3] == 'X' && pos[5] == 'X' && pos[7] == 'X') Winner = 'X';
if (Winner == 'X' )
{System.out.println("X wins the game." );
return Winner;
}
// Check if O wins
if (pos[1] == 'O' && pos[2] == 'O' && pos[3] == 'O') Winner = 'O';
if (pos[4] == 'O' && pos[5] == 'O' && pos[6] == 'O') Winner = 'O';
if (pos[7] == 'O' && pos[8] == 'O' && pos[9] == 'O') Winner = 'O';
if (pos[1] == 'O' && pos[4] == 'O' && pos[7] == 'O') Winner = 'O';
if (pos[2] == 'O' && pos[5] == 'O' && pos[8] == 'O') Winner = 'O';
if (pos[3] == 'O' && pos[6] == 'O' && pos[9] == 'O') Winner = 'O';
if (pos[1] == 'O' && pos[5] == 'O' && pos[9] == 'O') Winner = 'O';
if (pos[3] == 'O' && pos[5] == 'O' && pos[7] == 'O') Winner = 'O';
if (Winner == 'O' )
{
System.out.println( "O wins the game." );
return Winner; }
for(int i=1;i<10;i++)
{
if(pos[i]=='X' || pos[i]=='O')
{
if(i==9)
{
char Draw='D';
System.out.println(" Game is stalemate ");
return Draw;
}
continue;
}
else
break;
}
return Winner;
}
public boolean checkPos(int spot)
{
if (pos[spot] == 'X' || pos[spot] == 'O')
{
System.out.print("That position is already taken, please choose another : ");
return true;
}
else {
return false;
}
}
public void nextPlayer()
{
if (player == 'X')
player = 'O';
else player = 'X';
}
public char getPlayer()
{
return player;
}
}```