Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please make the code below answer the following prompt exactly --- split into TWO separate classes, TicTacToe.java (driver class) and Board.java (class without main method).

Please make the code below answer the following prompt exactly --- split into TWO separate classes, TicTacToe.java (driver class) and Board.java (class without main method). And include ALL the parts mentioned in the prompt. Thank you!! Much appreciated

Prompt:

"Your task is to create a game of Tic-Tac-Toe using a 2-Dimensional String array as the game board. Start by creating a Board class that holds the array. The constructor should use a traditional for-loop to fill the array with "blank" Strings (eg. "-"). You may want to include other instance data... Include the follwoing methods: public void setSpace(int row, int col, String s) will change a specific space to have either "X" or "O", but only if the space is available. public boolean hasThree(String s) will check to see if there are 3 in a row, column, or diagonal. public String toString() will generate a String that displays the Board with appropriate formatting. Create a driver class called TicTacToe that instantiates a Board object and allows 2 players to go head to head in a the game.

import java.util.Scanner;

import java.io.*;

class rr{

//intialinzing 3*3 array with empty values

static String values[][] = { {" "," "," "},

{" "," "," "},

{" "," "," "} };

public static void main(String args[]){

Scanner in = new Scanner(System.in);

int step=1;

while(true){

//assigning symbols to tiles according to players...

//if odd number player 1 move...

if(step%2==1){

System.out.println("~~~~~~First Player move~~~~~~");

System.out.println("Row No:");

int row=in.nextInt();

System.out.println("Column No:");

int col=in.nextInt();

//if tile is empty

if(!(row>=0 && row<=3)&&(col>=0 && col<=3)){

System.out.println("select valid tile!");

step--;

}

else{

if(values[row-1][col-1]==" "){

setspace(row,col,"X");

//checking for winning condition after successfully applied symbol on tile...

if(hasThree("X")){

System.out.println("Player 1 wins!!");

return;

}

}

//if tile is already pre-occupied

else{

System.out.println("Already selected select anoter tile!!");

step--;

}

}

}

//if even number player 2 move...

if(step%2==0){

System.out.println("~~~~~~Second Player move~~~~~");

System.out.println("Row No:");

int row=in.nextInt();

System.out.println("Column No:");

int col=in.nextInt();

if(!(row>=0 && row<=3)&&(col>=0 && col<=3)){

System.out.println("select valid tile!");

step--;

}

else{

//if tile is empty

if(values[row-1][col-1]==" "){

setspace(row,col,"O");

//checking for winning condition after successfully applied symbol on tile...

if(hasThree("O")){

System.out.println("Player 2 wins!!");

return;

}

}

//if tile is already pre-occupied

else{

System.out.println("Already selected select anoter tile!!");

step--;

}

}

}

step++;

//for printing board..(interface)

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

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

System.out.print("|"+values[i][j]+"|");

}

System.out.println();

}

}

}

//function to set symbol on a tile for player..

public static void setspace(int row,int col,String s){

values[row-1][col-1]=s;

}

//function to check for winning..

public static boolean hasThree(String s){

//winning conditions..

if( (values[0][0]==s && values[0][1]==s && values[0][2]==s )||

(values[1][0]==s && values[1][1]==s && values[1][2]==s )||

(values[2][0]==s && values[2][1]==s && values[2][2]==s )||

(values[0][0]==s && values[1][0]==s && values[2][0]==s )||

(values[0][1]==s && values[1][1]==s && values[2][1]==s )||

(values[0][2]==s && values[1][2]==s && values[2][2]==s )||

(values[0][0]==s && values[1][1]==s && values[2][2]==s )||

(values[2][0]==s && values[1][1]==s && values[0][2]==s ) )

return true;

else{

return false;

}

}

}

This is my teacher's toString class:

public String toString() { String result = ""; result += " | | "; result += " " + board[0][0] + " | " + board[0][1] + " | " + board[0][2] + " "; result += "_____|_____|_____ "; result += " | | "; result += " " + board[1][0] + " | " + board[1][1] + " | " + board[1][2] + " "; result += "_____|_____|_____ "; result += " | | "; result += " " + board[2][0] + " | " + board[2][1] + " | " + board[2][2] + " "; result += " | | "; return result; } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

Define promotion.

Answered: 1 week ago

Question

Write a note on transfer policy.

Answered: 1 week ago

Question

Discuss about training and development in India?

Answered: 1 week ago

Question

Explain the various techniques of training and development.

Answered: 1 week ago