The goal of this project is to introduce us to a functional perspective when it comes to programming by developing a Hex-Game clone
- Pure Random Move: the functions playCPU and GetRandomInput return a valid random coordinate and a new RandomState for a possible next use
- Play: Check documentation below on BoardState's function play.
- Visual Representation: Check documentation below on BoardState's function draw.
- Check for a Continuous Line: The game ends when there is a continuous vertical or horizontal line across the board, check the Documentation below for more information about our solution to this problem.
- Undo'ing a move: In the Main object, there are some recursive functions (updatePvP, etc...), that are used to make the game flow. Each one of those has an argument history, that represents all the BoardStates until then. This makes for an easy implementation of an undo option.
- Developing a TUI: Once we run our code, a simple TUI menu will be displayed in the terminal with some game modes and other options, for each game mode the user will be capable of defining the board size.
- Developing a GUI: TODO
This case class / companion object is used to represent a GameState.
Each GameState is composed by a Board and a UnionFind.
Unit/String functions that allow for a visual representation of the Board via Text User Interface.
draw uses 2 tail recursive nested functions while drawFold uses a really extended foldRight. Although the latter one occupies fewer lines of code, it is a lot slower.
This function returns a valid coordinate chosen by the user
This function returns a new BoardState with piece played at coordinates, playRecursive does the same but using recursion
This function returns a valid random coordinate
This function returns a valid coordinate adjacent to oldCoord, if no valid coordinate is found then it returns a random valid coordinate
Used to check if coordinates corresponds to an empty slot on the Board and is within the boundaries
Uses a UnionFind data structure to check for a possible winner
A companion object function to initialize the BoardState of length size
This case class / companion object represents a simple Union Find data structure, it is useful to check for a possible winner while having a low time complexity
This function returns a new UnionFind where the tile coordinates gets connected to the adjacent tiles that have matching pieces
This function uses foldRights to check for a possible winner in board with a quadratic time complexity.
Note that it would be possible for a constant time complexity if we had implemented the Quick-Find algorithm, however we agreed that a quadratic time complexity would already be a good enough optimization since the brute-force/naive approach would be an estimate of O(6^n)
This companion object function initializes the base data for the Union Find structure