In: Computer Science
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below:
The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players after every successful move, and pronounce the winner.
This is the the code so far:
public static void main(String[] args) {
Scanner in = new
Scanner(System.in);
int[][] board = new
int[3][3];
}
So, here is the solution of the given problem.
I am attaching the well-commented source code
along with the Screenshots of the code as well as
output.
Source Code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[][] board = new int[3][3];
int x,y;
boolean flag = true;
//function to print instruction
printInstruction();
//run game infinitely till one player won or game ties
while (true){
//function to print board
print(board);
//to print player whose chance is there
System.out.println("Player" + ((flag)?"1":"2"));
//taking co-ordinated
x = in.nextInt();
y = in.nextInt();
//checking if is vacant or not
if (board[x-1][y-1] == 0){
board[x-1][y-1] = ((flag)? 1 : 2 );
flag = !flag;
}
else
System.out.println("Invalid Move!");
//check if player won or not
if(check(board)){
System.out.println("Player " + ((flag)?"2":"1") + " won.");
break;
}
//to check if game ties or not
if(completeFill(board)) {
System.out.println("Game Tie");
break;
}
}
}
//to check board is completely filled and no player won
private static boolean completeFill(int[][] board){
for(int i = 0;i<3;i++){
for(int j = 0;j<3;j++){
if(board[i][j] != 0)
return false;
}
}
return true;
}
//to print instructions
private static void printInstruction() {
System.out.println("1. You have to enter row number (1-3) and column number (1-3) to place your element: ");
System.out.println("2. Game will start from player 1 and one by one each player's turn will come.");
}
//function to check row is matched
private static boolean rowCrossed(int[][] board)
{
for (int i=0; i<3; i++)
{
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] != 0)
return true;
}
return false;
}
//function to check column is matched
private static boolean columnCrossed(int[][] board)
{
for (int i=0; i<3; i++)
{
if (board[0][i] == board[1][i] &&
board[1][i] == board[2][i] &&
board[0][i] != 0)
return true;
}
return false;
}
//function to check diagonal is matched
private static boolean diagonalCrossed(int[][] board)
{
if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] &&
board[0][0] != 0)
return true;
return board[0][2] == board[1][1] &&
board[1][1] == board[2][0] &&
board[0][2] != 0;
}
//function to check if any of the condition is satisfied i.e row,column,diagonal
private static boolean check(int[][] board){
return rowCrossed(board) || columnCrossed(board) || diagonalCrossed(board);
}
//function to print board
private static void print(int[][] board){
for(int i = 0;i<3;i++) {
for(int j = 0;j<3;j++)
System.out.print(" | " + board[i][j]);
System.out.println();
if(i<2)
System.out.println("--------------");
}
}
}
Screenshot of Source Code:
Output:
So, this was the solutions of the problem.
I hope I am able to solve your problem, if yes then do give it a
like.
It really helps :)