0% found this document useful (0 votes)
31 views

Name: Divyanshu Kumar Enrollment No.: 191160107017 Class: 4PC1 Batch: C

The document is a lab manual for an Object Oriented Programming course. It includes a program written by Divyanshu Kumar that displays a tic-tac-toe board with random X's and O's. The program takes input of player names and their marker (X or O), displays the board, and checks for a winner after each turn until the game is complete. It then prints the winner or a draw message.

Uploaded by

Mr. Vipul
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)
31 views

Name: Divyanshu Kumar Enrollment No.: 191160107017 Class: 4PC1 Batch: C

The document is a lab manual for an Object Oriented Programming course. It includes a program written by Divyanshu Kumar that displays a tic-tac-toe board with random X's and O's. The program takes input of player names and their marker (X or O), displays the board, and checks for a winner after each turn until the game is complete. It then prints the winner or a draw message.

Uploaded by

Mr. Vipul
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

Object Oriented Programming -1[3140705]

Lab Manual

Name: Divyanshu
Kumar
Enrollment No.:
191160107017
Class:
4PC1
Batch :
C

Subject Program No. Date of Submission Faculty


Object Oriented Prof. Pratiti
Programming -1 17 09-05-2021 Mankodi
[3140705]

1 Program Definition – Write a program that displays a tic-tac-toe board. A


cell may be X, O, or empty. What to display at each cell is randomly
decided. The X and O are images in the files X.gif and O.gif.
/*
Author : Divyanshu Kumar
Date : 09-05-2021
File name : Prac17.java
*/

import java.util.Scanner;
public class Prac17 {
public static void main(String[] args){
String[] playerName = new String[2];
char[][] game = new char[3][3];
char sign;
int currPlayer = 0,row,col,times=0,wins=-1;
Scanner input = new Scanner(System.in);

System.out.println("=============================");
System.out.println("Welcome to the game TicTacToe");

System.out.println("=============================");
System.out.print("Please enter Player 1 name: ");
playerName[0] = input.next();
System.out.println("Hi, "+playerName[0]+". Your sign is:
X.");
System.out.println("-----------------------------");
System.out.print("Please enter Player 2 name: ");
playerName[1] = input.next();
System.out.println("Hi, "+playerName[1]+". Your sign is:
O.");
Object Oriented Programming -1[3140705]
Lab Manual

Name: Divyanshu
Kumar
Enrollment No.:
191160107017
Class:
4PC1
Batch :
C

System.out.println("=============================");

System.out.println("Current canvas of game is empty.");


System.out.println("Let us start game now.");
System.out.println();
System.out.println("-----------------------------");
while(true){
if(currPlayer==1) sign='x'; else sign='o';
System.out.println(playerName[currPlayer]+"
("+sign+") to enter position:");
System.out.print("Row: ");
row = input.nextInt()-1;
System.out.print("Col: ");
col = input.nextInt()-1;
if(row>=3 || row<0 || col>=3 || col<0 || game[row]
[col]!='\0'){
System.out.println("Please enter valid
position. (3x3) empty cell.");
continue;
}
times++;
wins=setGame(game,row,col,currPlayer,sign);
System.out.println("Game Status:");
printGame(game);
System.out.println("-----------------------------");
if(wins!=-1 || times==9)
break;
if(currPlayer==0) currPlayer=1; else
currPlayer=0;
}
System.out.println("=========End of
Game=========");
if(wins==-1)
System.out.println("No one wins.");
else
System.out.println(playerName[wins]+" wins.");

System.out.println("=============================");
}
public static int setGame(char[][] game,int row,int col,int
Object Oriented Programming -1[3140705]
Lab Manual

Name: Divyanshu
Kumar
Enrollment No.:
191160107017
Class:
4PC1
Batch :
C

currPlayer,char sign) {
game[row][col]=sign;

if(game[row][0]==game[row][1] && game[row]


[1]==game[row][2])
return currPlayer;
if(game[0][col]==game[1][col] && game[1]
[col]==game[2][col])
return currPlayer;
if(row==col)
if(game[0][0]==game[1][1] && game[1]
[1]==game[2][2])
return currPlayer;
if(row==2-col)
if(game[0][2]==game[1][1] && game[1]
[1]==game[2][0])
return currPlayer;
return -1;
}
public static void printGame(char[][] game){
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++){
if(game[i][j]!='\0')
System.out.printf("%2c",game[i][j]);
else
System.out.printf("%2c",' ');
if(j!=2)
System.out.print(" |");
}
System.out.println();
if(i!=2)
System.out.println("---+---+---");
}
}
}
/*
Compilation : javac Prac17.java
Execution : java Prac17
*/
Object Oriented Programming -1[3140705]
Lab Manual

Name: Divyanshu
Kumar
Enrollment No.:
191160107017
Class:
4PC1
Batch :
C

Output

You might also like