Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Assignment // TicTacToeModel.java package cs5004.tictactoe; import java.util.Arrays; import java.util.stream.Collectors; public class TicTacToeModel implements TicTacToe { // add your implementation here @Override public String toString()

JAVA Assignment

image text in transcribed

// TicTacToeModel.java

package cs5004.tictactoe; import java.util.Arrays; import java.util.stream.Collectors; public class TicTacToeModel implements TicTacToe { // add your implementation here @Override public String toString() { // Using Java stream API to save code: return Arrays.stream(getBoard()).map( row -> " " + Arrays.stream(row).map( p -> p == null ? " " : p.toString()).collect(Collectors.joining(" | "))) .collect(Collectors.joining(" ----------- ")); // This is the equivalent code as above, but using iteration, and still using // the helpful built-in String.join method. /********** List rows = new ArrayList(); for(Player[] row : getBoard()) { List rowStrings = new ArrayList(); for(Player p : row) { if(p == null) { rowStrings.add(" "); } else { rowStrings.add(p.toString()); } } rows.add(" " + String.join(" | ", rowStrings)); } return String.join(" ----------- ", rows); ************/ } }

// TicTacToe.java

package cs5004.tictactoe; /** * A single game of Tic Tac Toe, played on a three-by-three grid with two players, * with the object of the game to achieve three markers in a row either vertically, * horizontally, or diagonally. {@link Player} X goes first. */ public interface TicTacToe { /** * Execute a move in the position specified by the given row and column. * * @param r the row of the intended move * @param c the column of the intended move * @throws IllegalArgumentException if the space is occupied or the position is otherwise invalid * @throws IllegalStateException if the game is over */ void move(int r, int c); /** * Get the current turn, i.e., the player who will mark on the next call to move(). * * @return the {@link Player} whose turn it is */ Player getTurn(); /** * Return whether the game is over. The game is over when either the board is full, or * one player has won. * * @return true if the game is over, false otherwise */ boolean isGameOver(); /** * Return the winner of the game, or {@code null} if there is no winner. If the game is not * over, returns {@code null}. * * @return the winner, or null if there is no winner */ Player getWinner(); /** * Return the current game state, as a 2D array of Player. A {@code null} value in the grid * indicates an empty position on the board. * * @return the current game board */ Player[][] getBoard(); /** * Return the current {@link Player} mark at a given row and column, or {@code null} if the * position is empty. * * @param r the row * @param c the column * @return the player at the given position, or null if it's empty */ Player getMarkAt(int r, int c); }

//TicTacToeModelText.java

import org.junit.Test; import cs5004.tictactoe.Player; import cs5004.tictactoe.TicTacToe; import cs5004.tictactoe.TicTacToeModel; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; /** * Test cases for the tic tac toe model. Verifying that game state is properly managed, and * all game actions are properly validated. */ public class TicTacToeModelTest { private TicTacToe ttt1 = new TicTacToeModel(); @Test public void testMove() { ttt1.move(0, 0); assertEquals(Player.O, ttt1.getTurn()); } @Test public void testHorizontalWin() { ttt1.move(0, 0); // X takes upper left assertFalse(ttt1.isGameOver()); ttt1.move(1, 0); // O takes middle left ttt1.move(0, 1); // X takes upper middle assertNull(ttt1.getWinner()); ttt1.move(2, 0); // O takes lower left ttt1.move(0, 2); // X takes upper right assertTrue(ttt1.isGameOver()); assertEquals(Player.X, ttt1.getWinner()); assertEquals(" X | X | X " + "----------- " + " O | | " + "----------- " + " O | | ", ttt1.toString()); } @Test public void testDiagonalWin() { diagonalWinHelper(); assertTrue(ttt1.isGameOver()); assertEquals(Player.O, ttt1.getWinner()); assertEquals(" X | X | O " + "----------- " + " X | O | " + "----------- " + " O | | ", ttt1.toString()); } // set up situation where game is over, O wins on the diagonal, board is not full private void diagonalWinHelper() { ttt1.move(0, 0); // X takes upper left assertFalse(ttt1.isGameOver()); ttt1.move(2, 0); // O takes lower left ttt1.move(1, 0); // X takes middle left assertNull(ttt1.getWinner()); ttt1.move(1, 1); // O takes center ttt1.move(0, 1); // X takes upper middle ttt1.move(0, 2); // O takes upper right } @Test public void testInvalidMove() { ttt1.move(0, 0); assertEquals(Player.O, ttt1.getTurn()); assertEquals(Player.X, ttt1.getMarkAt(0, 0)); try { ttt1.move(0, 0); fail("Invalid move should have thrown exception"); } catch (IllegalArgumentException iae) { //assertEquals("Position occupied", iae.getMessage()); assertTrue(iae.getMessage().length() > 0); } try { ttt1.move(-1, 0); fail("Invalid move should have thrown exception"); } catch (IllegalArgumentException iae) { //assertEquals("Position occupied", iae.getMessage()); assertTrue(iae.getMessage().length() > 0); } } @Test(expected = IllegalStateException.class) public void testMoveAttemptAfterGameOver() { diagonalWinHelper(); ttt1.move(2, 2); // 2,2 is an empty position } @Test public void testCatsGame() { ttt1.move(0, 0); assertEquals(Player.O, ttt1.getTurn()); ttt1.move(1, 1); assertEquals(Player.X, ttt1.getTurn()); ttt1.move(0, 2); ttt1.move(0, 1); ttt1.move(2, 1); ttt1.move(1, 0); ttt1.move(1, 2); ttt1.move(2, 2); ttt1.move(2, 0); assertTrue(ttt1.isGameOver()); assertNull(ttt1.getWinner()); assertEquals( " X | O | X " + "----------- " + " O | O | X " + "----------- " + " X | X | O", ttt1.toString()); } @Test(expected = IllegalArgumentException.class) public void testInvalidGetMarkAtRow() { ttt1.getMarkAt(-12, 0); } @Test(expected = IllegalArgumentException.class) public void testInvalidGetMarkAtCol() { ttt1.getMarkAt(0, -30); } @Test public void testGetBoard() { diagonalWinHelper(); Player[][] bd = ttt1.getBoard(); assertEquals(Player.X, bd[0][0]); assertEquals(Player.O, bd[1][1]); assertEquals(Player.X, bd[0][1]); // attempt to cheat by mutating board returned by getBoard() // check correct preconditions assertEquals(Player.O, bd[2][0]); assertEquals(Player.O, ttt1.getMarkAt(2, 0)); bd[2][0] = Player.X; // mutate // check correct post conditions assertEquals(Player.O, ttt1.getMarkAt(2, 0)); Player[][] bd2 = ttt1.getBoard(); assertEquals(Player.O, bd2[2][0]); } // TODO: test case where board is full AND there is a winner }
1.1 A Model for Tic Tac Toe The purpose of this exercise is to give you practice with implementing the Model component of the Model-View-Controller design pattern. In the starter code, you are given an interface representing a game of Tic Tac Toe; your task is to implement the TicTacToe interface. You will need to define an enum Player, representing the players (X and O), with a toString() method that returns "X" and "0" accordingly. You will need to implement the public class named TicTacToeModel, with a single public constructor that takes no arguments. The class definition, with a toString() implementation to help with debugging, are provided to you in the starter code. You will fill in the fields and remaining method definitions as appropriate. You may also define other classes at your option as needed. The game grid cells are numbered by row and column starting from 0. For example, the upper left position is row 0, column 0 (or [0][0] in the 2D array returned by getBoard()), the upper middle position is row 0, column 1 ([0][1]), the lower right is [2][2]. 1.2 Testing We have supplied you with some basic JUnit tests as part of the starter code. Use these to verify that your implementation is correct, and write additional tests of your own. 1.3 Notes to keep in mind Avoid duplicating code as much as possible. Consider using non-public methods as means of creating reusable pieces of functionality. Be sure to use access modifiers, private and public, as well as final, appropriately. In your getters, be careful to return a copy of, and not a direct reference to, any mutable internal state in your model. Include JavaDoc for your classes and constructors as appropriate. You do not need to repeat JavaDoc already existing in a superclass or interface when you override a method. 1.1 A Model for Tic Tac Toe The purpose of this exercise is to give you practice with implementing the Model component of the Model-View-Controller design pattern. In the starter code, you are given an interface representing a game of Tic Tac Toe; your task is to implement the TicTacToe interface. You will need to define an enum Player, representing the players (X and O), with a toString() method that returns "X" and "0" accordingly. You will need to implement the public class named TicTacToeModel, with a single public constructor that takes no arguments. The class definition, with a toString() implementation to help with debugging, are provided to you in the starter code. You will fill in the fields and remaining method definitions as appropriate. You may also define other classes at your option as needed. The game grid cells are numbered by row and column starting from 0. For example, the upper left position is row 0, column 0 (or [0][0] in the 2D array returned by getBoard()), the upper middle position is row 0, column 1 ([0][1]), the lower right is [2][2]. 1.2 Testing We have supplied you with some basic JUnit tests as part of the starter code. Use these to verify that your implementation is correct, and write additional tests of your own. 1.3 Notes to keep in mind Avoid duplicating code as much as possible. Consider using non-public methods as means of creating reusable pieces of functionality. Be sure to use access modifiers, private and public, as well as final, appropriately. In your getters, be careful to return a copy of, and not a direct reference to, any mutable internal state in your model. Include JavaDoc for your classes and constructors as appropriate. You do not need to repeat JavaDoc already existing in a superclass or interface when you override a method

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions

Question

Methods of Delivery Guidelines for

Answered: 1 week ago