Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the code (below) to be a human vs. computer game of Tic Tac Toe. As it currently stands, it's a human vs. human (2-player

Modify the code (below) to be a human vs. computer game of Tic Tac Toe.

As it currently stands, it's a human vs. human (2-player game) of Tic Tac Toe.

I need it to be a human playing a computer.

Please help! Thank you so much!

1 import java.util.Arrays;

2 import java.util.InputMismatchException; 3 import java.util.Scanner; 4 5 /* Jane's comments on the status if this one, works perfectly as a 2 person game of 6 tic-tac-toe but does not allow it to be a human versus computer game of tic tac toe*/ 7 8 public class TicTacToe3 { 9 static Scanner in; 10 static String[] board; 11 static String turn; 12 13 public static void main(String[] args) { 14 in = new Scanner(System.in); 15 board = new String[9]; 16 turn = "X"; 17 String winner = null; 18 populateEmptyBoard(); 19 20 System.out.println("Welcome to 2 Player Tic Tac Toe."); 21 System.out.println("--------------------------------"); 22 printBoard(); 23 System.out.println("X's will play first. Enter a slot number to place X in:"); 24 25 while (winner == null) { 26 int numInput; 27 try { 28 numInput = in.nextInt(); 29 if (!(numInput > 0 && numInput <= 9)) { 30 System.out.println("Invalid input; re-enter slot number:"); 31 continue; 32 } 33 } catch (InputMismatchException e) { 34 System.out.println("Invalid input; re-enter slot number:"); 35 continue; 36 } 37 if (board[numInput-1].equals(String.valueOf(numInput))) { 38 board[numInput-1] = turn; 39 if (turn.equals("X")) { 40 turn = "O"; 41 } else { 42 turn = "X"; 43 } 44 printBoard(); 45 winner = checkWinner(); 46 } else { 47 System.out.println("Slot already taken; re-enter slot number:"); 48 continue; 49 } 50 } 51 if (winner.equalsIgnoreCase("draw")) { 52 System.out.println("It's a draw! Thanks for playing."); 53 } else { 54 System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing."); 55 } 56 } 57 58 static String checkWinner() { 59 for (int a = 0; a < 8; a++) { 60 String line = null; 61 switch (a) { 62 case 0: 63 line = board[0] + board[1] + board[2]; 64 break; 65 case 1: 66 line = board[3] + board[4] + board[5]; 67 break; 68 case 2: 69 line = board[6] + board[7] + board[8]; 70 break; 71 case 3: 72 line = board[0] + board[3] + board[6]; 73 break; 74 case 4: 75 line = board[1] + board[4] + board[7]; 76 break; 77 case 5: 78 line = board[2] + board[5] + board[8]; 79 break; 80 case 6: 81 line = board[0] + board[4] + board[8]; 82 break; 83 case 7: 84 line = board[2] + board[4] + board[6]; 85 break; 86 } 87 if (line.equals("XXX")) { 88 return "X"; 89 } else if (line.equals("OOO")) { 90 return "O"; 91 } 92 } 93 94 for (int a = 0; a < 9; a++) { 95 if (Arrays.asList(board).contains(String.valueOf(a+1))) { 96 break; 97 } 98 else if (a == 8) return "draw"; 99 } 100 101 System.out.println(turn + "'s turn; enter a slot number to place " + turn + " in:"); 102 return null; 103 } 104 105 static void printBoard() { 106 System.out.println("/---|---|---\\"); 107 System.out.println("| " + board[0] + " | " + board[1] + " | " + board[2] + " |"); 108 System.out.println("|-----------|"); 109 System.out.println("| " + board[3] + " | " + board[4] + " | " + board[5] + " |"); 110 System.out.println("|-----------|"); 111 System.out.println("| " + board[6] + " | " + board[7] + " | " + board[8] + " |"); 112 System.out.println("/---|---|---\\"); 113 } 114 115 static void populateEmptyBoard() { 116 for (int a = 0; a < 9; a++) { 117 board[a] = String.valueOf(a+1); 118 } 119 } 120 }

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

Objects And Databases International Symposium Sophia Antipolis France June 13 2000 Revised Papers Lncs 1944

Authors: Klaus R. Dittrich ,Giovanna Guerrini ,Isabella Merlo ,Marta Oliva ,M. Elena Rodriguez

2001st Edition

3540416641, 978-3540416647

More Books

Students also viewed these Databases questions

Question

=+Where does the focus of labor relations lie? Is it collective

Answered: 1 week ago

Question

=+With whom does the firm have to negotiate?

Answered: 1 week ago