Question: Write a program to play one-player Tic-Tac-Toe. However, this time you must use arrays and at least three methods. Attached is some code to get

 

Write a program to play one-player Tic-Tac-Toe. However, this time you must use arrays and at least three methods.

Attached is some code to get you started. However, there are several strategies in using arrays and methods for Tic-Tac-Toe, and you don't need to follow this approach at all. You can start from scratch or modify the code any way you like.

 
 
 
 
 
 
import java.util.Scanner; 
 public class Main { enum Square {O, X, E}; // Initialize the Tic-Tac-Toe board so all squares are "Empty". final static Square[] board = { Square.E, Square.E, Square.E, Square.E, Square.E, Square.E, Square.E, Square.E, Square.E }; // lines[][] provides the indices for the 8 ways to win in the board array. final static int[][] lines = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6} }; // Counts the number of the squares, s, in the line specified. Returns the result. // Example: a = countLine(0, Square.X); Counts the number of X's in first row. public static int countLine(int line, Square s) { int count = 0; for(int i=0; i<3; i++) { if (board[ lines[line][i] ] == s) { count++; } } return count; } // Checks to see if there is a winning move for player specified by square s, and // makes the win. Returns true if a winning move was played. Otherwise, false. // Example: move = makeWin(Square.O); public static boolean makeWin(Square s) { // Add code here. } // Checks to see if there is a blocking move for player s, and makes the block. // Returns true if a blocking move was played. Otherwise, false. // Example: move = makeBlock(Square.O); Checks if there is a line with 2 X's // and one E, and will place an O in the E. public static boolean makeBlock(Square s) { // Add code here. } // Checks to see if player s won the game. Looks for a line with three squares for // the player. Returns true if player won. Otherwise, false. // Example: playerWin = checkWin(Square.X); public static boolean checkWin(Square s) { // Add code here. } // Displays the current board of Tic-Tac-Toe. public static void displayBoard() { if (board[0] == Square.E) System.out.print("0 |"); else System.out.print(board[0] + " |"); if (board[1] == Square.E) System.out.print(" 1 "); else System.out.print(" " + board[1] + " "); if (board[2] == Square.E) System.out.print("| 2"); else System.out.print("| " + board[2]); System.out.print(" --------- "); // FINISH displaying other two rows... Add code here. } public static void main(String[] args) { // Add code here. } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!