Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

simple Java program 5

BunkerHillBroad
BunkerHillBroad 


use files TicTacToe.java and TicTacToeBoard.java Write a program that allows two people to play a game of “tic-tac-toe”, with the computer acting as a “referee.” Here is what your program might look like in action: [11] Welcome! Tic-Tac-Toe is a two player game. Enter player one's name: Foo Enter player two's name: Bar Players take turns marking a square. Only squares not already marked can be picked. Once a player has marked three squares in a row, he or she wins! If all squares are marked and no three squares are the same, a tied game is declared. Have Fun! Game board: | | | | | | It is Foo's turn. Pick a row between 1 and 3: 1 Pick a column between 1 and 3: 1 Game board: | X | | | | | It is Bar's turn. Pick a row between 1 and 3: 2 Pick a column between 1 and 3: 2 Game board: | X | | O | | | It is Foo's turn. Pick a row between 1 and 3: 2 Pick a column between 1 and 3: 2 ILLEGAL CHOICE! TRY AGAIN... Pick a row between 1 and 3: 2 Pick a column between 1 and 3: 1 Game board: | X | | X O | | | It is Bar's turn. Pick a row between 1 and 3: 3 Pick a column between 1 and 3: 1 Game board: | X | | X O | | O | It is Foo's turn. Pick a row between 1 and 3: 3 Pick a column between 1 and 3: 3 Game board: | X | | X O | | O X| It is Bar's turn. Pick a row between 1 and 3: 1 Pick a column between 1 and 3: 3 Game board: | X O| | X O | | O X| Game Over - Bar WINS!!! In solving this problem, create a TicTacToeBoard class that contains a two- dimensional array representing the current state of a tic-tac-toe board. Initially, every element of this array could contain a space character, to indicate it is empty. Your TicTacToeBoard class should only have private variables and these and only these public methods: // constructor (optional, depending on implementation) public TicTacToeBoard() {...} // returns a string representation of the board public String toString() {...} // tries to play c (X or O) at row, col // returns true if square was legal and not occupied public boolean makeMove(int row, int col, char c) {...} // returns true if the board is a win for either X or O public boolean isWin() {...} Your toString method in this class will make it possible for the TicTacToe main program to print the board as shown in the above output. Note: Your TicTacToeBoard class is not allowed to print anything out to the terminal or to read in any input. That should all be handled by TicTacToe.java. Your main program will alternately let each of the two players make a move, but not allow any move that is illegal — either because a particular square is already occupied, or because a value less than 1 or greater than 3 has been input for the row or column position. After each turn is made the computer should check if any row, column or diagonal contains three ‘X’ marks or three ‘O’ marks; if so, a winner should be declared and the game should end at that point. Attachments
Tags Clarifications

Answers

TicTacToe - Solution

mariobeka
mariobeka 



Below the two java files containing the assignment solution. Attachments
TicTacToe.java (2 K) Download Preview
"not already marked can be picked. Once a player has\n" + "marked three squares in a row, he or she wins! If all squares\n" + "are marked and no three squares are the same, a tied game is declared.\n" + "HAVE FUN!"); play(players); } private static void play(String[] players) {

TicTacToeBoard.java (2 K) Download Preview
} return false; } private boolean checkHorizontal() { for (State[] aBoard : board) { if (aBoard[0].equals(State.EMPTY) || aBoard[1].equals(State.EMPTY) || aBoard[2].equals(State.EMPTY)) { // do nothing } else if (aBoard[0].equals(aBoard[1]) && aBoard[1].equals(aBoard[2])) { return true; } } return false; } public String toString() { StringBuilder sb = new StringBuilder(); for (int i

Screenshots
Purchase Answer