Tic Tac Toe programming assignment

User Generated

Nyyvrqfvatu

Computer Science

Description

I need help with this programming assignment, which is a Tic Tac Toe game. All the details are give about the assignment in the file the I will upload.

Unformatted Attachment Preview

CSC 15 HW #4 For this assignment you are to implement a tic-tac-toe game. The high-level design and algorithm have been given to you. Also, you are given the methods to implement but it is up to you to chose the appropriate return types and parameters. Please make sure that you program output matches the output sample since you will be graded on the output formatting as well as the behavior. You will need to ensure that the data the user enters is validated: • • • • Player 1 and 2 cannot have the same name. Valid names only contain characters and no spaces (first names only). You may use the Character.isLetter(char letter) method to check to see if a char is a letter. Check to ensure that row or column number entered is a number, not a letter and that it is a valid row or column number. When prompted to play again they must answer with a “y” or an “n.” You will also need to check for a win condition (3 across, vertically or diagonally). Below is the algorithm. Your main method will call your run method: 1. run: Main algorithm. a. Declare a new scanner object and print welcome message. b. Loop as long as they want to continue playing i. Use getName() to get the name of each player ii. Check to make sure that the second player has a unique name, loop until they do iii. Flip a count to see who goes first using the flipCoin() method and then determine who the first player will be using the chooseFirstPlayer() method. iv. Use the initializeBoard() method to initialize a char[][] array for the board (3x3 char array). v. Set a String to store who’s turn it is and print out that player one is x and player two is o. vi. Print the board using printBoard(). vii. While there is no winner and less than 9 moves have been taken 1. Update the number of moves 2. Depending on whose turn it is, use the doUserMove() method to take that person’s turn. 3. Use the isWinner() method to check to see if there is a winner. 4. Print the board using printBoard(). 5. If there is a winner print the congratulations message 6. If there are 9 moves taken and there isn’t a winner then print out “Cats Game” viii. Ask if they would like to play again using the yesTo() method. 2. chooseFirstPlayer(): Receives a coin value (0 or 1) and two names. Returns the player name that will be going first based on the value of the coin. 3. flipCoin(): Returns a random number 0 or 1. 4. getName(): Receives a Scanner object and a String (“first” or “second”) and returns a name that contains only letters, no spaces. a. Loop while an invalid name is input i. Prompt for the player’s name ii. Loop for each character in the input 1. If the character is not a letter or if it is a space then the name is invalid. 5. initializeBoard(): Doesn’t receive any parameters but does return a char[][] (3 x 3 char array) that has been initialized with each value set to a space. 6. doUserMove(): Receives as parameters the Scanner object, your char[][] board array, the name of the player that is taking their turn and the character x or o for that player’s token. a. Loop while the move chosen is not valid i. Prompt them to enter a row number using the getPos() method. Prompt again for the column number. ii. If that position is not available then the move is invalid and display error. Otherwise it is a valid move. 7. getPos(): Receives as parameters the Scanner object, the prompt specifier (“row” or “column”) for the users and the name of the player to be used in the prompt. It should return a number between 1 and 3 a. While the number entered is out of range. i. Prompt for user input ii. While the input is not a number 1. Skip the bad input 2. Prompt again iii. Read in the input as a number 8. printBoard(): Receives the char[][] board array and prints the tic-tac-toe board game to the screen. 9. yesTo(): Receives the Scanner object and a prompt for the user and returns true if they entered “y” and false otherwise. It should continue to prompt until a valid answer is input. 10. isWinner(): Receives the char[][] board array and returns a char. It should return ‘x’ if x is the winner, ‘o’ if o is the winner and a space ‘ ‘ if there is none. a. Check rows i. Traverse each row and count x’s and o’s. If either is 3 then set that to the winner b. Check columns i. Traverse each column and count x’s and o’s. If either is 3 then set that to the winner. c. Check diagonal i. Check the diagonals and if either one is all the same then set that to the winner Let's play tic tac toe Enter the first Player's name--> Alb3rt Enter the first Player's name--> Albert Enter the second Player's name--> Albert You cannot play against yourself Enter the second Player's name--> Jane Wait, flipping the coin to see who goes first Hit enter to see the result The player Albert will start the game ------------| | | | ------------| | | | ------------| | | | ------------Albert, you will be 'x' Jane, you will be 'o' Albert enter the row--> 0 Albert enter the row--> 1 Albert enter the column--> 2 ------------| | x | | ------------| | | | ------------| | | | ------------Jane enter the row--> 3 Jane enter the column--> 1 ------------| | x | | ------------| | | | ------------| o | | | ------------Albert enter the row--> 3 Albert enter the column--> 2 ------------| | x | | ------------| | | | ------------| o | x | | ------------Jane enter the row--> 1 Jane enter the column--> 3 ------------| | x | o | ------------| | | | ------------| o | x | | ------------Albert enter the row--> 2 Albert enter the column--> 2 ------------- | | x | o | ------------| | x | | ------------| o | x | | ------------Congratulations Albert, you are the winner!!!!!!!!!!! Want to play again? yes Please answer y or n. Want to play again? 1 Please answer y or n. Want to play again? y Enter the first Player's name--> Beverly Enter the second Player's name--> Juan Wait, flipping the coin to see who goes first Hit enter to see the result The player Beverly will start the game ------------| | | | ------------| | | | ------------| | | | ------------Beverly, you will be 'x' Juan, you will be 'o' Beverly enter the row--> 2 Beverly enter the column--> 2 ------------| | | | ------------| | x | | ------------| | | | ------------Juan enter the row--> j enter a valid row number 1 Juan enter the column--> 1 ------------| o | | | ------------| | x | | ------------| | | | ------------Beverly enter the row--> 3 Beverly enter the column--> 1 ------------| o | | | ------------| | x | | ------------| x | | | ------------Juan Juan enter the row--> 1 enter the column--> 3 ------------| o | | o | ------------| | x | | ------------| x | | | ------------Beverly enter the row--> 1 Beverly enter the column--> 2 ------------| o | x | o | ------------| | x | | ------------| x | | | ------------Juan enter the row--> 3 Juan enter the column--> 2 ------------| o | x | o | ------------| | x | | ------------| x | o | | ------------Beverly enter the row--> 2 Beverly enter the column--> 1 ------------| o | x | o | ------------| x | x | | ------------| x | o | | ------------Juan enter the row--> 2 Juan enter the column--> 3 ------------| o | x | o | ------------| x | x | o | ------------| x | o | | ------------Beverly enter the row--> 3 Beverly enter the column--> 3 ------------| o | x | o | ------------| x | x | o | ------------| x | o | x | ------------Cat's game Want to play again? no Please answer y or n. Want to play again? n
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Now the program is ready. It plays tic ta...


Anonymous
Awesome! Made my life easier.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags