Implement a single-player board game in Haskell where the player flips the color of pegs on a board shaped like a '+'. The game starts with all pegs black except one white peg, and the goal is to turn all pegs white by flipping neighboring black pegs.
The board is shaped like a '+' with the center at position (0,0). Peg positions are relative to this center.
- All pegs start as black except one randomly placed white peg.
- A black peg can be flipped to white if it has at least one white neighboring peg (up, down, left, or right, but not diagonally).
- The player wins by flipping all pegs to white.
type Position = (Int, Int)
data Color = W | B deriving (Eq, Show)
data Peg = Peg Position Color deriving (Eq, Show)
data Move = M Position deriving (Eq, Show)
type Board = [Peg]
data State = S Move Board deriving (Eq, Show)
- Position: Represents the position of pegs on the board with (column, row) coordinates.
- Color: Represents the color of the pegs (W for white, B for black).
- Peg: Represents a piece on the board with its position and color.
- Move: Represents a move to flip a peg to white at a specific position.
- Board: Represents the game board as a list of pegs.
- State: Represents the game state with the current move and the board configuration.
-
createBoard :: Position -> Board
- Initializes the board with all black pegs except for one white peg at the specified position.
- Returns an error if the position is not on the board.
- Example:
> createBoard (1, 1) [Peg (-3,-1) B, Peg (-3,0) B, ..., Peg (1,1) W, ..., Peg (3,1) B]
-
isValidMove :: Move -> Board -> Bool
- Checks if a move is legal.
- A move is valid if the peg at the specified position is black and has at least one white neighboring peg.
- Example:
> isValidMove (M (1,0)) [Peg (0,0) W, Peg (1,0) B, ...] True
-
isGoal :: Board -> Bool
- Checks if all pegs on the board are white.
- Example:
> isGoal [Peg (0,0) W, Peg (1,0) W, ...] True
-
showPossibleNextStates :: Board -> [State]
- Generates all possible game states from the current board state by applying valid moves.
- Returns an error if the board is already in a winning state.
- Example:
> showPossibleNextStates [Peg (0,0) W, Peg (1,0) B, ...] [S (M (1,0)) [Peg (0,0) W, Peg (1,0) W, ...], ...]
- createBoard: Displays an error if the specified position is not valid.
> createBoard (-2,2) Program error: The position is not valid.
- showPossibleNextStates: Displays an error if the board is already in a winning state.
> showPossibleNextStates [Peg (0,0) W, Peg (1,0) W, ...] Program error: No Possible States Exist.
-
Clone the Repository:
git clone https://github.com/yehiarasheed/Peg-Reversal-Game.git cd Peg-Reversal-Game
This command downloads a copy of the repository to your local machine and navigates into the project directory.
-
Install WinHugs: Download and install WinHugs from the Haskell website. WinHugs is the recommended Haskell compiler/interpreter for this project.
-
Open the Project:
- Launch WinHugs.
- Load the project files by navigating to
File → Open
and selecting the Haskell file you want to run.
This project requires the following dependencies:
- WinHugs: The recommended Haskell compiler/interpreter for running the Peg Reversal Game.
Ensure WinHugs is installed and properly configured on your machine to run the Haskell code in this project.