0% found this document useful (0 votes)
16 views6 pages

VGGD

hi

Uploaded by

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

VGGD

hi

Uploaded by

breakerboards
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

import java.u l.

Scanner;

class Main {

public sta c void main(String[] args) {

// Variable to represent the size of our c tac toe board

int n = 3;

// nxn array that represents our c tac toe board

char[][] board = new char[n][n];

//Ini alize our board with dashes (empty posi ons)

for(int i = 0; i < n; i++) {

for(int j = 0; j < n; j++) {

board[i][j] = '-';

//Create a Scanner for asking the players for their names

Scanner in = new Scanner(System.in);

System.out.println("Tic Tac Toe is ready for play!\n");

System.out.print("What is your name?, player 1: ");

String p1 = in.nextLine();

System.out.print(" What is your name?, player 2:");

String p2 = in.nextLine();

//Create a player1 boolean that is true if it is player 1's turn and false if it is player 2's turn

boolean player1 = true;

//Create a gameEnded boolean and use it as the condi on in the while loop

boolean gameEnded = false;

while(!gameEnded) {
//Draw the board

drawBoard(board);

//Print whose turn it is

if(player1) {

System.out.println(p1 + "'s Turn (x):");

} else {

System.out.println(p2 + "'s Turn (o):");

//Create a char variable that stores either 'x' or 'o' based on what player's turn it is

char c = '-';

if(player1) {

c = 'x';

} else {

c = 'o';

//Create row and col variables which represent indexes that correspond to a posi on on our board

int row = 0;

int col = 0;

//Only break out of the while loop once the user enters a valid posi on

while(true) {

//Ask the user for what posi on they want to place their x or o

System.out.print("Enter a row number: ");

row = in.nextInt();

System.out.print("Enter a column number: ");

col = in.nextInt();

//Check if the row and col are outside of the board


if(row < 0 || col < 0 || row >= n || col >= n) {

System.out.println("This posi on is off the bounds of the board! Try again.");

//Check if the posi on on the board the user entered is empty (has a -) or not

} else if(board[row][col] != '-') {

System.out.println("Someone has already made a move at this posi on! Try again.");

//Otherwise, the posi on is valid so break out of the while loop

} else {

break;

//Set the posi on on the board at row, col to c

board[row][col] = c;

//Check to see if either player has won

if(playerHasWon(board) == 'x') {

System.out.println(p1 + " has won!");

gameEnded = true;

} else if(playerHasWon(board) == 'o') {

System.out.println(p2 + " has won!");

gameEnded = true;

} else {

//If neither player has won, check to see if there has been a e (if the board is full)

if(boardIsFull(board)) {

System.out.println("It's a e!");

gameEnded = true;

} else {

//If player1 is true, make it false, and vice versa; this way, the players alternate each turn

player1 = !player1;

}
}

//Draw the board at the end of the game

drawBoard(board);

//Make a func on to draw the c tac toe board

public sta c void drawBoard(char[][] board) {

System.out.println("Board:");

for(int i = 0; i < board.length; i++) {

//The inner for loop prints out each row of the board

for(int j = 0; j < board[i].length; j++) {

System.out.print(board[i][j]);

System.out.println();

public sta c char playerHasWon(char[][] board) {

for(int i = 0; i < board.length; i++) {

boolean inARow = true;

char value = board[i][0];

if(value == '-') {

inARow = false;

} else {

for(int j = 1; j < board[i].length; j++) {

if(board[i][j] != value) {

inARow = false;

break;

if(inARow) {

return value;

}
}

for(int j = 0; j < board[0].length; j++) {

boolean inACol = true;

char value = board[0][j];

if(value == '-') {

inACol = false;

} else {

for(int i = 1; i < board.length; i++) {

if(board[i][j] != value) {

inACol = false;

break;

if(inACol) {

return value;

boolean inADiag1 = true;

char value1 = board[0][0];

if(value1 == '-') {

inADiag1 = false;

} else {

for(int i = 1; i < board.length; i++) {

if(board[i][i] != value1) {

inADiag1 = false;

break;

if(inADiag1) {

return value1;

boolean inADiag2 = true;

char value2 = board[0][board.length-1];


if(value2 == '-') {

inADiag2 = false;

} else {

for(int i = 1; i < board.length; i++) {

if(board[i][board.length-1-i] != value2) {

inADiag2 = false;

break;

if(inADiag2) {

return value2;

return ' ';

public sta c boolean boardIsFull(char[][] board) {

for(int i = 0; i < board.length; i++) {

for(int j = 0; j < board[i].length; j++) {

if(board[i][j] == '-') {

return false;

return true;

You might also like