Question: java please help, i will appreciate your help and will upvote. Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3x3

java please help, i will appreciate your help and will upvote.

Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3x3 grid. The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark, change the player after every successful move, and pronounce the winner.

  • Implement the private method wonDiagonal() which checks if player has won tic-tac-toe along diagonal lines.
  • Implement the private method wonStraightLines() which checks if player has won tic-tac-toe along straight lines.
  • Implement the public method won(), which checks if player has won.

If implemented correctly, you can play the game now. But the program has bugs. Try to choose a position that has been chosen (either by the same player or the other player), you will see that the new value replaces the old value, which is not correct. Also try to enter a position that is out of the range, e.g., 3 0, or 5 8 and you will see ArrayOutOfBoundException on the screen, which is also not desirable.

  • Modify the method choose(), such that if the chosen cell has already been occupied, the method throws an exception UnavailableCellException.
  • Modify the code in main() so that it catches both the UnavailableCellException and ArrayOutOfBoundException exceptions. The program should now run in such a way that if a player chooses a position that has already been occupied, or choose an invalid position, prompts the player to enter again until a empty cell is chosen.

The tester file is provided so that you can check your output on the console as shown in Appendix A.

TicTac Class:

package TICTAC;

public class PlayTicTac { /** board a 3x3 array containing 0, 1, 2, values indicating blanks or player numbers */ private int[][] board; public PlayTicTac() { this.board = new int[3][3]; } /** Checks if player has won tic-tac-toe along diagonal lines. @param player the player to check for a winning sequence of marks @return true if player won, false otherwise */ private boolean wonDiagonal( int player) { }

/** Checks if player has won tic-tac-toe along straight lines. @param player the player to check for a winning sequence of marks @return true if player won, false otherwise */ private boolean wonStraightLines( int player) { }

/** Checks if player has won. @param player the player to check for a winning sequence of marks @return true if player won, false otherwise */ public boolean win(int player) { }

/** Draws gameboard, player 1 is X, player 2 is O. */ public void drawBoard() { System.out.println("|-----|"); for (int i = 0; i

TicTacDemo Class:

package TICTAC;

import java.util.Scanner;

public class TicTacdemo {

public static void main(String[] args) { Scanner in = new Scanner(System.in); int player = 2;

PlayTicTac ticTacToe = new PlayTicTac();

ticTacToe.drawBoard(); while (!ticTacToe.win( player)) { if (player == 1) { player = 2; } else { player = 1; }

if (player ==1) System.out.print("Player " + player + " (X) choose a row and column: "); else System.out.print("Player " + player + " (O) choose a row and column: "); int row = in.nextInt(); int column = in.nextInt(); ticTacToe.choose(row, column, player); ticTacToe.drawBoard(); } System.out.println("Player " + player + " wins!"); }

}

UnavailableCellException.java (file):

package TICTAC;

public class UnavailableCellException extends Exception {

public UnavailableCellException() { // TODO Auto-generated constructor stub }

public UnavailableCellException(String arg0) { super(arg0); // TODO Auto-generated constructor stub } }

java please help, i will appreciate your help and will upvote. Write

Appendix A Invalid Position. Try again. Player 2 (O) choose a row and column: 0 2 The sample output will look like this. XI 101 ----- | 1011 ----- |----- III I----- Player 1 (X) choose a row and column: 0 0 I----- XIII --- - Player 1 (x) choose a row and column: 00 Position Occupied. Try again. Player 1 (X) choose a row and column: 21 - -- XI 101 -- -- | 101 -- -- | XX1 -- -- Player 2 (O) choose a row and column: 30 Invalid Position. Try again. Player 2 (O) choose a row and column: 2 0 ---- XI 101 |-----| | 1011 Player 2 (0) choose a row and column: 11 |X1 | 101 101XX ----- Player 1 (x) choose a row and column: 00 Position Occupied. Try again. Player 1 (x) choose a row and column: 1 1 Position Occupied. Try again. Player 1 (x) choose a row and column: 2 2 |----- XIII Player 2 wins! | 101 |----- IX -- - Player 2 (0) choose a row and column: 2 2 Position Occupied. Try again. Player 2 (O) choose a row and column: 50 Invalid Position. Try again. Player 2 (0) choose a row and column: 4 3

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!