Tic tac toe
Chronologie : Tic tac toe. Recherche parmi 300 000+ dissertationsPar retrouta7 • 18 Décembre 2019 • Chronologie • 1 592 Mots (7 Pages) • 508 Vues
import java.util.Scanner;
public class tictactoc
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int line,column;
char user = 'X';
char[][]board=new char[3][3];
boolean gameend=false;
int winner=0;
int move = 1;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
board[i][j]='-';
}
displayboard(board);
while(!gameend && move<=9)
{
System.out.println("move: "+ move + " ; user "+ user +": enter the line number then the column number.");
boolean occupied = false;
while(!occupied)
{
line = in.nextInt()-1;
column = in.nextInt()-1;
if (line<1||line>3||column<1||column>3)
{
System.out.println("please give numbers of line and column between 1 and 3");
}
else if (board[line][column]=='-')
{ move++;
board[line][column] = user;
occupied = true;
}
else
{
System.out.println("this place is occupied, enter a different line and column");
}
}
displayboard(board);
if (user=='X') user='O';
else if (user=='O') user='X';
winner = testwinner(board);
if (winner== 1)
{
System.out.println("winner is X");
gameend=true;
}
else if (winner== 2)
{
System.out.println("winner is O");
gameend=true;
}
else if(move ==9)
{ gameend=true;
System.out.println("No winner");
}
}
}
public static void displayboard(char board[][])
{
for(int i=0;i<3;i++)
{
System.out.print("|");
for(int j=0;j<3;j++)
{
System.out.print(board[i][j]+"|");
...