Please help in JAVA: I need to create a tic tac toe board and print it. The boar
ID: 3775324 • Letter: P
Question
Please help in JAVA:
I need to create a tic tac toe board and print it. The board is supposed to be 3x3 and it should be made up of 4 for loops. This is what I have so far but I am stuck. It is a player vs player game. Thanks.
public static void main(String[] args) {
//board is 3x3
char [] [] board = new char [3] [3];
//method to create board
createBoard(board);
//method to print board
printBoard(board);
}
public static void createBoard(char[][] board) {
for (int i=0; i<3;i++){
for (int j=0;j<3; j++)
board[i][j]=' ';
}
for (int i=0; i<3; i++){
board [i][3]='|';
board [i][7]='|';
}
}
public static void printBoard(char[][] board) {
System.out.println();
}
}
Explanation / Answer
This code contains board functions i.e it will create board ,initialize board.Afer entering move by player then that move will be marked on board by placeMove() function.
In secone part,this code will check winner or draw condition after each move.And Accordingly will declare winner or draw.
package org.junit;
public class TicTacToe {
private char[][] board;
private char currentPlayerMark;
public TicTacToe() {
board = new char[3][3];
currentPlayerMark = 'x';
createBoard();
}
// Set/Reset the board back to all empty values.
public void createBoard() {
// create board of 3x3 and initialize it to -
for (int i = 0; i < 3; i++) {
// Loop through columns
for (int j = 0; j < 3; j++) {
board[i][j] = '-';
}
}
}
// Print the Tic Tac toe board board
public void printBoard() {
System.out.println("-------------");
for (int i = 0; i < 3; i++) {
System.out.print("| ");
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j] + " | ");
}
System.out.println();
System.out.println("-------------");
}
}
// Loop through all cells of the board and if one is found to be empty (contains char '-') then return false.
// Otherwise the board is full.
public boolean isFull() {
boolean isFull = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
isFull = false;
}
}
}
return isFull;
}
// Returns true if there is a win, false otherwise.
// This will check the entire board to check the winner
public boolean checkWin() {
return (checkRows() || checkColumns() || checkDiagonals());
}
// Loop through rows and see if any are winners.
private boolean checkRows() {
for (int i = 0; i < 3; i++) {
if (checkRowCol(board[i][0], board[i][1], board[i][2]) == true) {
return true;
}
}
return false;
}
// Loop through columns and see if any are winners.
private boolean checkColumns() {
for (int i = 0; i < 3; i++) {
if (checkRowCol(board[0][i], board[1][i], board[2][i]) == true) {
return true;
}
}
return false;
}
// Check the two diagonals to see if either is a win. Return true if either wins.
private boolean checkDiagonals() {
return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true) || (checkRowCol(board[0][2], board[1][1], board[2][0]) == true));
}
// Check to see if all three values are the same (and not empty) indicating a win.
private boolean checkRowCol(char c1, char c2, char c3) {
return ((c1 != '-') && (c1 == c2) && (c2 == c3));
}
// Change player marks back and forth.
public void changePlayer() {
if (currentPlayerMark == 'x') {
currentPlayerMark = 'o';
}
else {
currentPlayerMark = 'x';
}
}
// Places a mark at the cell specified by row and col with the mark of the current player.
public boolean placeMove(int row, int col) {
// Make sure that row and column are in bounds of the board.
if ((row >= 0) && (row < 3)) {
if ((col >= 0) && (col < 3)) {
if (board[row][col] == '-') {
board[row][col] = currentPlayerMark;
return true;
}
}
}
return false;
}
}
//Main function to test program
package org.junit;
import java.util.Scanner;
public class TestGame {
public static void main(String[] args){
// Create game and initialize it.
Scanner sc=new Scanner(System.in);
TicTacToe tictactoe = new TicTacToe();
// First player will be 'x' and we will accept moves from user and then player will be changed to 'o' using change Player function
//We will accept all moves for the board 3x3 using for loop and it will check after each move that Whether any player won the game or not.
for(int l=0;l<8;l++)
{
System.out.println("Hey! Enter your move [0-2][0-2]::");
int i=sc.nextInt();
int j=sc.nextInt();
tictactoe.placeMove(i,j);
tictactoe.printBoard();
//Check for winner
if (tictactoe.checkWin()) {
System.out.println("We have a winner! ");
System.exit(0);
}
//Check if no one is winner
else if (tictactoe.isFull()) {
System.out.println(" we have a draw!");
System.exit(0);
}
tictactoe.changePlayer();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.