0% found this document useful (0 votes)
10 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)
10 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
You are on page 1/ 4

CODE:

package tic_tac_toe;
import java.util.Scanner;
public class hello {

private static char[] c;

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("what is your name player 1? ");


String player1 =in.nextLine();
System.out.print("what is your name player 2 ? ");
String player2 =in.nextLine();

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) {
System.out.print(player1 + "'s turn: ");
}
else {
System.out.print(player2+ "'s turn: ");
}

int col=0;
int row=0;
while (true)
{
System.out.print("choose a row please from the following: (0,1,2):");
row = in.nextInt();
System.out.print("choose a column from the following:(0,1,2):");
col = in.nextInt();

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


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

gameboard[row][col]=symbol;

PrintingGameBoard(gameboard);

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


{
System.out.println("player 1 has won congrats !!! ");
break;
}
else if (winning_decision(gameboard)== 'O')
{
System.out.print("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++)
{
System.out.print(gameboard[i][j]);

}
System.out.println();
}
}
}

SCREENSHOTS:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy