Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm in java one and need help with this code. please follow the directions given and test your program to make sure it works as

I'm in java one and need help with this code. please follow the directions given and test your program to make sure it works as it is required. I will apreciate your help. thank you.please use the given code as it is just fill the missing information to complete the program. thank you

here is the code

cp ~lkoperski/1404_Lab/Assignments/TicTacToe.java .

image text in transcribed

image text in transcribed

image text in transcribed

Programming Concepts

2-D Arrays

Assignment Description

Write a program that simulates a tic-tac-toe game.

You may copy the partially completed program from my directory into yours. Issue the following command:

cp ~lkoperski/1404_Lab/Assignments/TicTacToe.java .

There will be a total of four methods

o main() Implements the main game logic o moveAI() Randomly marks a location for the AI

o printBoard() Prints out the game board

o checkWinner() Checks to see if 'X' or 'O' is the winner, or if the game is a tie

? Thismethodreturns'X' if 'X' is the winner

? Tismetho dreturns 'O' if 'O' is the winner ? This method returns 'T' if the game is a tie ? This method returns 'N' if the game is not finished

However, I will be implementing printBoard() and checkWinner().

You need to implement these two methods:

public static void main( String[] args )

o This method implements the game logic. o Print the board o While the game is not finished:

? Print the board ? Get the location from the user and validate it in a loop

>>The location cannot be out of bounds

>>The location also cannot be already filled

? Mark the position in the board with an 'X', according to the user's specified location

? Print the board ? Check to see if the game is finished. If it is, break out of the loop ? Have the AI make a move ? Print the board

o Print out the result of the game (if 'X' or 'O' wins, or if the game is a tie)

public static void moveAI( char[][] board )

o This method is the AI. It will randomly mark a spot on the board for the computer, who is player 'O'.

o The location needs to be a random row between 0 and 2 (inclusive), and a random column between 0 and 2 (inclusive).

o If the random location is invalid (the location is not empty), then keep recalculating the random location. This will need to be done in a loop.

o Mark the position in the board with an 'O', based on the random row and column

Assignment Suggestions

When you initially create a char array, it will initialize every value to the int value 0. You should then manually initialize every element in the array to a space:

So, to check to see if a spot in the board is empty, then check to see if that spot is equal to a space: ' '. As you play the game, the board will start to look like this:

Output Example 

User input is underlined

''

''

''

''

''

''

''

''

''

'X'

''

''

''

'O'

''

'X'

'O'

''

output Example

user@loki:~$ java TicTacToe

image text in transcribed

X's turn! Enter the row and column, separated by spaces: -1 0

Enter the row and column, separated by spaces: 43 19

Enter the row and column, separated by spaces: 0 0

image text in transcribed

O's turn!

image text in transcribed

X's turn! Enter the row and column, separated by spaces: 1 1

image text in transcribed

O's turn!

image text in transcribed

X's turn! Enter the row and column, separated by spaces: 2 2

Enter the row and column, separated by spaces: 2 0

image text in transcribed

O's turn!

image text in transcribed

O wins!

1 import java.util.Random; 2 import java.util.Scanner; 3 public class TicTacToe 4 5 public static void main (String[1 args) 7 Scanner in -new Scanner( System.in); char[][] board = new char [ ] [ ]; // Initialize the board to spaces // Print the game board //Keep playing while the game isn't finished 10 12 13 14 15 16 17 18 19 20 21 /Get the location from the user and validate it // Mark the position in the board according to the user's specified location // Print the board after the user plays //Check to see if the game is finished. If it is, break out of the loop /Have the AI make a move // Print the board after the AI plays 23 24 25 26 27 28 29 30 31 32 33 /x 34 Makes a move for the AI, and marks the board with an '0' 35 36 37 38 39 40 41 42 43 /I Check to see who the winner is //If the winner is 'X' or '0',print that, otherwise, it is a tie * @param boardThe game board public static void moveAIchar [][] board) // Validate that the random location generated is valid. // Keep recalculating the location if the one generated is not /7 an empty space //Be sure to mark the position in the board with an '0 45

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

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions