Question
Advanced Java Programming with Object-Oriented Programming Design Can you please edit the following Java code that I currently have so that it has AT LEAST
Advanced Java Programming with Object-Oriented Programming Design
Can you please edit the following Java code that I currently have so that it has AT LEAST 2 CLASSES, GETTERS AND SETTERS?
Here's what I have so far:
import java.util.*;
public class Tic_Tac_Toe { //Declaring the strings for the X's, O's and the board. private String[][] board; static String X = "X"; static String O = "O"; /** * Constructor for objects of class TicTacToe */ public Tic_Tac_Toe() { // initialize instance variables board = new String[3][3]; }
/** * Print out the tic-tac-toe board */ public void printBoard() { System.out.println(); for (int m = 0; m < board.length; m++) { for (int k = 0; k < board[m].length; k++) { if (board[m][k] == null) { System.out.print("_"); } else { System.out.print(board[m][k]); } if (k < 2) { System.out.print("|"); } else { System.out.println(); } } } System.out.println(); } /* Checks if player wins. Checks right after X makes a play. * */ public Boolean checkWinner(String play) { int playInRow = 0; int playD1 = 0; int playD2 = 0; int[] playInColumn = new int[board[0].length]; for (int h = 0; h < board.length; h++) { playInRow = 0; for (int e = 0; e < board[h].length; e++) { if (null == board[h][e]) { continue; } if (board[h][e].equals(play)) { playInRow++; playInColumn[e]++; if (h == e) { playD1++; } else if (2 == h + e) { playD2++; } } } if (playInRow == 3) { return true; } } if (3 == playD1 || 3 == playD2) { return true; } for (int i = 0; i < playInColumn.length; i++) { if (playInColumn[i] == 3) { return true; } } return false; } /* * makeMove gets a legal coordinate for the move that is not occupied * and marks it with the play string */ public void makeMove(Scanner stdin, String play) { int g; int l; Boolean niceInput = false; while(!niceInput) { g = -1; l = -1; /*VERY IMPORTANT!: Answer with 2 numbers between 0 and 2!! Example: 1 then ENTER then 2 then ENTER. *Also, 0,0 = first spot, 0,1 = second spot, 0,2 = third spot,1,0 = fourth spot, 1,1 = fifth spot,1,2 = sixth spot, 2,0 = seventh spot, 2,1 = eighth spot and 2,2 = ninth spot. Hopefuly, this helps. */
System.out.println ("Please input coordinates to play your " + play+ ":"); // Must be integers. if (stdin.hasNextInt()) { g = stdin.nextInt(); } if (stdin.hasNextInt()) { l = stdin.nextInt(); } else { // Consume a line without an integer. stdin.nextLine(); // Consume a line without an integer. System.out.println("Both inputs must be integers between 0 and 2."); continue; } // Must be in the right coordinate range. if ((g < 0) || (g > 2) || (l < 0) || (l > 2)) { System.out.println("Both inputs must be integers between 0 and 2."); continue; } // Make sure the space is not occupied. else if (board[g][l] != null ) { System.out.println("That location is occupied"); continue; } else { board[g][l] = play; return; } } return; } public static void main(String[] args) { // Allocate a board. Tic_Tac_Toe ttt = new Tic_Tac_Toe(); Scanner stdin = new Scanner(System.in); int moves = 0; System.out.println("Let's play TicTacToe -- X goes first"); ttt.printBoard(); while (moves < 9) { ttt.makeMove(stdin, ttt.X); moves++; if (moves > 4) { if (ttt.checkWinner(X)) { System.out.println(X + " You Win!!!"); break; } } ttt.printBoard(); ttt.makeMove(stdin, ttt.O); moves++; if (moves > 4) { if (ttt.checkWinner(O)) { System.out.println(O + " You Win!!!"); break; } } ttt.printBoard(); } } }
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