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

Message 4

Coding

Uploaded by

c9244mbqyc
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Message 4

Coding

Uploaded by

c9244mbqyc
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

*;
public class Main
{
public static char[][] board = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', '
'}};

public static boolean isWinner(char[][] board, char player)


{
int numInRow = 0;
for(char[] r : board)
{
numInRow = 0;
for(int i = 0; i < r.length; i++)
{
if(r[i] == player)
{
numInRow++;
if(numInRow == 3)
{
return true;
}
}
}
}

int numInCol = 0;
for(int c = 0; c < board[0].length; c++)
{
numInCol = 0;
for(int r = 0; r < board.length; r++)
{
if(board[r][c] == player)
{
numInCol++;
if(numInCol == 3)
{
return true;
}
}
}
}
if(numInCol == 3)
{
return true;
}

if(board[0][0] == player && board[1][1] == player && board[2][2] == player)


{
return true;
}

if(board[0][2] == player && board[1][1] == player && board[2][0] == player)


{
return true;
}
return false;
}
public static boolean isCat(char[][] board)
{
int full = 0;
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++)
{
if(board[i][j] != ' ')
{
full++;
}
}
}

if((full == 9) && (isWinner(board, 'X') == false) && (isWinner(board, 'O')


== false))
{
return true;
}
return false;
}

public static void printBoard()


{
System.out.println(" " + board[0][0] + " | " + board[0][1] + " | " +
board[0][2] + " ");
System.out.println("-----------");
System.out.println(" " + board[1][0] + " | " + board[1][1] + " | " +
board[1][2] + " ");
System.out.println("-----------");
System.out.println(" " + board[2][0] + " | " + board[2][1] + " | " +
board[2][2] + " ");
}

public static boolean isValid(char[][] board, int r, int c)


{
return((r >= 0 && r <= 2) && (c >= 0 && c <= 2) && (board[r][c] == ' '));
}

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
int play = 1;
while((isCat(board) == false) && (isWinner(board, 'X') == false) &&
(isWinner(board, 'O') == false))
{
printBoard();
//System.out.println();
if(play % 2 == 1)
{
boolean valid = false;
while(!valid)
{
System.out.println();
System.out.println("X enter the column for your move (0-2):");
int c = in.nextInt();
System.out.println("X enter the row for your move(0-2):");
int r = in.nextInt();
System.out.println();
valid = isValid(board, r, c);

if(!valid)
{
System.out.println("Invalid move, enter a new move.");
//System.out.println();
}
else
{
board[r][c] = 'X';
}
}
//System.out.println();
}
else
{
boolean valid = false;
while(!valid)
{
System.out.println();
System.out.println("O enter the column for your move (0-2):");
int c = in.nextInt();
System.out.println("O enter the row for your move(0-2):");
int r = in.nextInt();
System.out.println();

valid = isValid(board, r, c);

if(!valid)
{
System.out.println("Invalid move, enter a new move.");
//System.out.println();
}
else
{
board[r][c] = 'O';
}
//System.out.println();
}
}
play++;
}

printBoard();
System.out.println();
if(isCat(board))
{
System.out.println("Cats game.");
}
if(isWinner(board, 'X'))
{
System.out.println("X WINS!");
}
if(isWinner(board, 'O'))
{
System.out.println("O WINS!");
}
}
}

You might also like