Answered step by step
Verified Expert Solution
Question
1 Approved Answer
// APPLICATION // TicTacToe.java import java.util.ArrayList; import java.util.Objects; public class TicTacToe { public enum Turn { X , O } private static final int DEFAULT_BOARD_SIZE
// APPLICATION //
TicTacToe.java
import java.util.ArrayList; import java.util.Objects; public class TicTacToe { public enum Turn { X , O } private static final int DEFAULT_BOARD_SIZE = 3; private int boardSize; private TicTacToeTile [][] board; private Boolean isOver; private Turn currentTurn; private String result; public TicTacToeTile[][] getBoard() { return board; } public TicTacToe() { this(DEFAULT_BOARD_SIZE); } public TicTacToe(int boardSize) { // initialize the board this.boardSize = boardSize; this.board = new TicTacToeTile[boardSize][boardSize]; currentTurn = Turn.X; isOver = false; result = null; for (int row = 0; row results = new ArrayList(); results.add(checkDiagonalWin()); results.add(checkVerticalWin()); results.add(checkHorizontalWin()); result = results.stream().filter(Objects::nonNull).findFirst().orElse(null); if(result == null) result = isTie() ? "tie": null; if(result != null){ isOver = true; } } private boolean isTie() { // if non of the tiles are empty and no winner, then it's a tie for (int row = 0; row
TicTacToeConsoleRunner.java
import java.util.Scanner; public class TicTacToeConsoleRunner { public static void main(String[] args) { Scanner in = new Scanner(System.in); boolean done = false; TicTacToe game = new TicTacToe(); while (!done) { System.out.print(game.toString()); int row = SafeInput.getRangedInt(in, "Row for " + game.getCurrentTurn().name() + " (-1 to exit): ", -1, 2); if (row
TicTacToeTile.java
public class TicTacToeTile { private int row; private int column; private String value; public TicTacToeTile(int row, int column) { this.row = row; this.column = column; this.value = " "; } public int getRow() { return row; } public int getColumn() { return column; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
SafeInput.java
import java.util.ArrayList; import java.util.Scanner; import java.util.regex.Pattern; import javax.swing.JOptionPane; /** * @author Wulft */ public class SafeInput { /** * @param console - Scanner instance to read the data System.in in most cases * @param prompt - input prompt msg should not include range info * @param low - low end of inclusive range * @param high - high end of inclusive range * @return - int value within the inclusive range */ public static int getRangedInt(Scanner console, String prompt, int low, int high) { int retVal = 0; String trash = ""; boolean done = false; do { System.out.print(" " + prompt + " [" + low + "-" + high + "]: "); if (console.hasNextInt()) { retVal = console.nextInt(); console.nextLine(); if (retVal >= low && retVal = low && retVal getArrayOfStrings(Scanner console, String prompt) { Boolean doneEntering = false; ArrayListretval = new ArrayList(); System.out.println(" " + prompt + ": "); do { if (console.hasNext()) { retval.add(console.nextLine()); } doneEntering = !getYNConfirm(console, "Add more? "); } while (!doneEntering); return retval; } public static int getRangedIntDialog(String prompt, int low, int high) { int retVal = 0; String inputString = ""; boolean done = false; do { inputString = JOptionPane.showInputDialog(null, prompt + " [" + low + "-" + high + "]: "); try { retVal = Integer.parseInt(inputString); if (retVal >= low && retVal = low && retVal
2 Create TicTacTorama.juva that inherites from a frame. 3 Create TicTac Toe Rummer.jave which should have a main method that initializes the Frame class. 4 in the TicTacTorano dass start by creating the constructor that sets the Frame title. (use the inherited super constructor for this) . sets the Frame size to (700 500). . sets the Default Close Operation to terminate the application on dose. makes the frame visible. 5 Create the necessary components to produce the following: (the dashed lines represent Parals) Tic Tac Toe Tic Tac Toe Ties: main Panel mainLabel gameBoardPanel . All the game is buttons button Panel Quit Button the button also shows an icon resultsPanel XWins Label WinsLabel TiesLabel 6 Create the Layout Manager abjects to produce Border Layout mainPanel btnPanel Grid Layout gameBoardPanel resultsPanel 7 Create the necessary Font Objects as such Tant ordrant Tontillvatica, ont.DOLD, 2); Tonttutto new Font("Helvatice", Tont.BOLD, 3); Tant mini beantw Font("Helvatice", Font.DOLD, ); 8 Modify the TicTac Towels dass such that it extends the button class. This will allow us to use the same class for bath Console and GUI applications. 9 Mucify the SetVulet) method of the TieTACTOTILO so it sets the text to the value passed public void atal(String Valu this.value; satust value): 10 In the Tee TaeTafrane dass, Instantiate the following objects to track the game state and game results across multiple attempts. TicTacTon TicTacTor) TicTacTool board Board(); int i; int win; int ties
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