0% found this document useful (0 votes)
36 views4 pages

17F-8177 Lab2 Ooad

The document contains a Java program for a simple Tic Tac Toe game that allows two players to take turns inputting their names and choosing positions on a 3x3 game board. The game checks for valid moves, updates the board, and determines if a player has won after each turn. The program includes methods for printing the game board and checking for winning conditions.

Uploaded by

abdurahman saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views4 pages

17F-8177 Lab2 Ooad

The document contains a Java program for a simple Tic Tac Toe game that allows two players to take turns inputting their names and choosing positions on a 3x3 game board. The game checks for valid moves, updates the board, and determines if a player has won after each turn. The program includes methods for printing the game board and checking for winning conditions.

Uploaded by

abdurahman saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CODE:

package tic_tac_toe;
import [Link];
public class hello {

private static char[] c;

public static void main(String[] args) {

Scanner in = new Scanner([Link]);

[Link]("what is your name player 1? ");


String player1 =[Link]();
[Link]("what is your name player 2 ? ");
String player2 =[Link]();

char[][] gameboard = new char [3][3];

for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
gameboard [i][j]='-';

}
}

boolean player1_turn =true;

boolean gameEnded= false;

while (!gameEnded)
{

PrintingGameBoard(gameboard);
char symbol=' ';
if(player1_turn)
{
symbol='X';
}
else
{
symbol='O';
}

if(player1_turn) {
[Link](player1 + "'s turn: ");
}
else {
[Link](player2+ "'s turn: ");
}

int col=0;
int row=0;
while (true)
{
[Link]("choose a row please from the following: (0,1,2):");
row = [Link]();
[Link]("choose a column from the following:(0,1,2):");
col = [Link]();

if(row<0 || col<0 || row>2 || col>2)


{
[Link]("Row or Column out of bound/range !!!");
}
else if(gameboard[row][col] != '-')
{
[Link]("Box already filled !!!");
}
else {
break;
}

gameboard[row][col]=symbol;

PrintingGameBoard(gameboard);

if(winning_decision (gameboard)== 'X')


{
[Link]("player 1 has won congrats !!! ");
break;
}
else if (winning_decision(gameboard)== 'O')
{
[Link]("player 2 has won congrats !!! ");
break;
}
else
{
player1_turn=!player1_turn;
}
}
}

public static char winning_decision(char [][] gameboard)


{
for (int i=0 ;i<3;i++)
{
if(gameboard[i][0] == gameboard[i][1] && gameboard[i][1]==
gameboard[i][2] && gameboard[i][0] != '-')
{
return gameboard[i][0];
}
}
for (int j=0 ;j<3;j++)
{
if(gameboard[0][j] == gameboard[1][j] && gameboard[1][j]==
gameboard[2][j] && gameboard[0][j] != '-')
{
return gameboard[0][j];
}
}

if(gameboard[0][0] == gameboard[1][1] && gameboard[1][1]==


gameboard[2][2] && gameboard[0][0] != '-')
{
return gameboard[0][0];
}

if(gameboard[2][0] == gameboard[1][1] && gameboard[1][1]==


gameboard[0][2] && gameboard[2][0] != '-')
{
return gameboard[1][1];
}

return '-';
}

public static void PrintingGameBoard(char[][] gameboard) {


for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
[Link](gameboard[i][j]);

}
[Link]();
}
}
}

SCREENSHOTS:

You might also like