Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java Tic Tac Toe game need help. import java.util.Random; import java.util.Scanner; /** * INSTRUCTION FOR LAB: this program is designed for a user to *
Java Tic Tac Toe game need help. import java.util.Random; import java.util.Scanner; /** * INSTRUCTION FOR LAB: this program is designed for a user to * play Tic-Tac-Toe game with computer. The main, oneGame, and * some other methods have been implemented. * * Your job is to read and understand the workflow, and then complete * the missing parts to make the game fully functional. * You can find the parts you need to work on marked by * >>>>>>>>>>>>your code here >>>>>>>>>>>>>>>>>>>>>>>>>>> * * @author: */ public class TicTacToe { // Our keyboard scanner static Scanner in = new Scanner(System.in); static Random rand = new Random(); public static void main(String[] args) { boolean nextGame = true; while (nextGame) { // Play one game oneGame(); // Check if we want to continue System.out.print("Shall we play one more (y/n)? "); String next = in.nextLine(); if ((next.charAt(0) == 'y') || (next.charAt(0) == 'Y')) nextGame = true; else nextGame = false; } System.out.println("Goodbye!"); } public static void oneGame() { //create a 2D array as the board char[][] playField = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}} ; printBoard(playField); // Game loop while (true) { //user player move****************************** getUserInput(playField); printBoard(playField); if (findWin('X',playField)) { System.out.println("Congratulations, you won!"); break; } if (findTie(playField)) { System.out.println("It's a draw!"); break; } //Computer player move****************************** System.out.println("Computer move:"); getComputerInput(playField); printBoard(playField); if (findWin('O',playField)) { System.out.println("Computer won! Better luck next time!"); break; } if (findTie(playField)) { System.out.println("It's a draw!"); break; } // Next round System.out.println("New round!"); } } public static void getUserInput(char[][] playField) { boolean valid = false; int row = 0; int col = 0; while (!valid) { System.out.print("Please enter your move (row and col, no space): "); String move = in.nextLine(); if (move.length() == 2) { row = (int) move.charAt(0) - 97;//'a' --> 0, 'b'--> 1, 'c' --> 2 col = (int) move.charAt(1) - 49;//'1' --> 0, '2'--> 1, '3' --> 2 if ((row >= 0 && row < playField.length) && (col >= 0 && col < playField[0].length)) { if (playField[row][col] == 0 || playField[row][col] == ' ') { playField[row][col] = 'X'; valid = true; } else { System.out.println("This field is already taken, choose another!"); } } else { System.out.println("This field is invalid!"); } } else { System.out.println("Please only enter the right field (a1,...) for the play!"); } } } /** * Get computer input to a empty spot in the 2D character array * @param playField */ public static void getComputerInput(char[][] playField) { //>>>>>>>>>>>>your code here >>>>>>>>>>>>>>>>>>>>>>>>>>> } /** * If there is NO empty spot left, it's a tie * @param playField * @return true if tie, false if not */ public static boolean findTie(char[][] playField) { //>>>>>>>>>>>>your code here >>>>>>>>>>>>>>>>>>>>>>>>>>> return false; } /** * Return the winning status. Win at: * playerChar in a whole row; * playerChar in a whole column; * playerChar form diagonal line; * @param playerChar * @param playField * @return true if won */ public static boolean findWin(char playerChar, char[][] playField) { int winSum = playField.length; int sum = 0; // win in a row for (int i = 0; i < playField.length; i++) { sum = 0; for (int j = 0; j < playField[i].length; j++) { if( playField[i][j] == playerChar) { sum ++; } } if (sum == winSum)//if any row wins return true; } //win in a column //>>>>>>>>>>>>your code here >>>>>>>>>>>>>>>>>>>>>>>>>>> // win diagonally from top-left to bottom-right sum = 0; for (int i = 0, j = 0; i < playField.length && j < playField[0].length; i++, j++) { if(i == j && playField[i][j] == playerChar) sum ++; } if (sum == winSum) return true; //win diagonally from top-right to bottom-left //>>>>>>>>>>>>your code here >>>>>>>>>>>>>>>>>>>>>>>>>>> //Not winning return false; } public static void printBoard(char[][] playField) { // Print the board System.out.println(" 1 2 3"); System.out.println(" +-----+-----+-----+"); System.out.println("a | " + playField[0][0] + " | " + playField[0][1] + " | " + playField[0][2] + " |"); System.out.println(" |-----+-----+-----|"); System.out.println("b | " + playField[1][0] + " | " + playField[1][1] + " | " + playField[1][2] + " |"); System.out.println(" |-----+-----+-----|"); System.out.println("c | " + playField[2][0] + " | " + playField[2][1] + " | " + playField[2][2] + " |"); System.out.println(" +-----+-----+-----+"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started