Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions