Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Problem Write a program that plays tic-tac-toe.Thetic-tac-toegameisplayedona3x3grid.The game is played by two players, who take turns. The first player marks move with a circle,

Java Problem

Write a program that plays tic-tac-toe.Thetic-tac-toegameisplayedona3x3grid.The game is played by two players, who take turns. The first player marks move 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 gameboard, ask the user for the coordinates of the next mark, change the player after every successful move, and pronounce the winner.

package TICTAC;

public class PlayTicTac { private int[][] board; public PlayTicTac() { this.board = new int[3][3]; } private boolean wonDiagonal( int player) { }

private boolean wonStraightLines( int player) { }

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 < 3; i++) { for (int j = 0; j < 3; j++) { if (board[i][j] == 1) { System.out.print("|X"); } else if (board[i][j] == 2) { System.out.print("|O"); } else { System.out.print("| "); } } System.out.println("| |-----|"); } } /** Choose a cell for player has won. @param r the row number chose @param c the column number chose @param player the player who choose a position @throws UnavailableCellException is the cell has been occupied (by either player) */ public void choose(int r, int c, int player) { this.board[r][c] = player; } }

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 ArrayOutOfBound- Exception on the screen, which is also not desirable.

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_2

Step: 3

blur-text-image_3

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

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

1. Discuss the four components of language.

Answered: 1 week ago

Question

f. What stereotypes were reinforced in the commercials?

Answered: 1 week ago