Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a version of TicTacToe with two differences: (1) three-in-a-row in a diagonal does not count and (2) instead of a 3-by-3 grid, the

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

This is a version of TicTacToe with two differences: (1) three-in-a-row in a diagonal does not count and (2) instead of a 3-by-3 grid, the board is a string of nine characters. At the start the board is "123456789". As the game is played, the numbers are replaced with X's and o's. Picture the string as three separate strings stacked on top of each other. For example, imagine as "X200X60X9 as X20 OX6 OX9 This is a relatively big program. You need to complete four methods. There are 9 other methods besides these four. Each of the methods you work on need to call other methods in the class. There is a summary of the 13 methods in the Unit 5B Program packet that is on Google Classroom. (5 points) Here is a sample run of the finished program. Enter your name: Jimmy Enter the computer's name: Robotron 123456789 Where do you want to place an X? 1 X23456789 Robotron places an 0 at spot 9 X23456780 Where do you want to place an X? 2 XX3456780 Robotron places an 0 at spot 3 XX0456780 Here is a sample run of the finished program. Enter your name: Jimmy Enter the computer's name: Robotron 123456789 Where do you want to place an X? 1 X23456789 Robotron places an 0 at spot 9 X23456780 Where do you want to place an X? 2 XX3456780 Robotron places an 0 at spot 3 XX0456780 Where do you want to place an X? 4 XXOX56780 Robotron places an 0 at spot 7 XXOX56080 Where do you want to place an X? 5 XXOXX6080 Robotron places an 0 at spot 8 XXOXX6000 Robotron wins!! Main.java Tictactoe.java: 1 import java.util.Scanner; 2 3 public class Main 4- { 5 public static void main(String[] args) 6 - { 7 Scanner sc = new Scanner( system.in); 8 System.out.print("Enter your name: "); 9 String name = sc.nextLine(); 10 System.out.print("Enter the computer's name: "); 11 String computer = sc.nextLine(); 12 System.out.println(); 13 14 TicTac Toe ttt = new Tictactoe( name, computer ); 15 System.out.println( ttt.tostring() ); 16 17 while ( ttt.keepPlaying() ) 18 { 19 if ( ttt.humansTurn() ) 20 { 21 System.out.print("Where do you want to place an X? "); 22 int location = sc.nextInt(); 23 if ( ttt.isValid( location ) ) 24 { 25 ttt.humanMove( location); 26 System.out.println( ttt.tostring() ); 27 ttt.nextPlayer(); 28 } 29 else System.out.println( "You cannot place an X at spot! 31 32 } 30 + location); 33 34 - 35 36 37 38 39 40 41 42 43 44 else Il computer's humans turn { ttt.computerMove(); System.out.println( ttt.tostring() ); ttt.nextPlayer(); } System.out.println(); // end of the while loop } String result = ttt.getResult(); System.out.println( result ); } 45 } public void computerMove() { /* call the getopenIndices helper method randomly pick one character out of the string */ String spot = "0"; // replace with randomly selected character System.out.println(computer + " places an o at spot " + spot); * convert the randomly selected character into an int call the helper method update to change the board */ } 46 47 - 48 49 50 51 52 53- 54 55 56 57 58 59 60 61 62- 63 64 65 66 public boolean isValid( int num) { if (num 9 ) return false; /* call getOpenIndices helper method if num is found in that string then return true otherwise return false */ return true; // placeholder } 67 ****** ***** ***** ******** 68 - 69 70 71 72 73 74 75 * The methods below this point are completed. You will need to call getOpenIndices, update, and check3 in the above methods * The other methods are either called from the main method or are used for testing solutions */ * * ***** ***** ******** *** 76 77- 78 79 80- 81 82 83 84 85 86 87 /* getopenIndices returns a string of integers of the spots that are open. For example, in the beginning it returns "123456789" because all the spots are open. If it returns an empty string, there are no open spots. */ public String getopen Indices() { String openSpots = ""; for (int k = 0; k

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

Students also viewed these Databases questions