CS 111- Introduction to Computer Science Homework #13

Due: Friday 4/28 at the beginning of class

## Hex (Part 1)

0. Learn the game of Hex:

1. Implement HexState: Implement a class HexState.java according to this specification.  This will likely be your most significant and challenging homework assignment of the semester, so start early.  Note:

• Read the documentation carefully.  Your integer constants for PLAYER2, NONE, and PLAYER1 need to be -1, 0, and 1, respectively.  Don't just read the method summaries.  Some have additional necessary specification details beyond the summary tables.
• It is easiest to represent Hex's rhombic hexagonal grid board as a square grid, using  a 2D array of the aforementioned integer constants.  Let grid cells be adjacent if they are vertically adjecent, horizontally adjacent, or diagonally adjacent along the diagonal where the row and column both increase/decrease together.
• The most challenging method you'll implement will be the getWinner method.  Determining the winner will require you to devise an algorithm that is related to your 2D maze solving algorithm.  Think of it this way: Player 1, seeking to connect the top row to the bottom row, wins if there is an unbroken path of connected pieces running from the bottom row to the top row (or vice versa).  Finding that path is like finding a maze path from any player 1 piece on the bottom row through passages of same colored pieces and reaching the top row.  All player 2 pieces and empty spaces can be considered walls of the maze.  If there's a solution to this player 1 "maze", player 1 has won.  Similarly, player 2 seeks a path along their own pieces from the leftmost column to the rightmost column.  Study the maze code and adapt it as necessary (e.g. don't forget that you'll have 6 directions to search).
• The constructor that copies another HexState must make a "deep copy".  That means you need to not merely copy the reference to the given state's board.  You need to allocate a new, independent 2D array and copy the contents over to the new object.  Without a correct implementation of this constructor, the undo function will not work.
• The toString method is the second longest method you'll implement, but there are full details of the implementation in the specification.

2. Test HexState: Test your HexState class using the provided Hex.java Graphical User Interface (GUI).  Usage: "java Hex".  Single-click onto an empty hex grid cell to play a piece in that spot.  Press "u" to undo a move.  You can also use the "Game" menu to undo a move, restart a game, start a game with a different size board, or go to a website where people play Hex online.  Note that playing with this GUI prints a transcript of play to the standard output that will be useful for testing your text-based HexGame.java in the next homework assignment.