Question: Java Assignment: A Model for Tic Tac Toe The purpose of this exercise is to give you practice with implementing the Model component of the

Java Assignment:

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 "O" 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].

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.

________________________________________________________________

public interface TicTacToe { void playerMove(int row, int column); Player playerTurn(); boolean gameOver(); Player winner(); Player[][] gameBoard(); Player MarkAt(int row, int column); }

______________________________________________________________

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); ************/ } }

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!