Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Java Task

Uploaded by

afnanfiverr17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Task

Uploaded by

afnanfiverr17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Three Musketeers

We will make use of the Three Musketeers Board game. This game is played on a 5x5 square board:

• Starter Board
| A B C D E
− +− − − − −
1 | O O O O X
2 | O O O O O
3 | O O X O O
4 | O O O O O
5 | X O O O O

• MUSKETEER - A musketeer moves to an adjacent (just orthogonal) cell occupied by a guard (cap-
turing it).

• GUARD - A guard moves to an adjacent (just orthogonal) empty cell.

• TURNS - On the first turn the musketeer side moves. Then, turns alternate.

• GOAL - The musketeer wins if they cannot make a valid move and the 3 musketeers are not on the
same row or column. The guard wins if the 3 musketeers are on the same row or column.
Example: Guard’s turn
| A B C D E
− +− − − − −
1 | X
2 | O
3 | X O O
4 | O
5 | O O X

– Guard moves D4 → E4

1
| A B C D E
− +− − − − −
1 | X
2 | O
3 | X O O
4 | O
5 | O O X

– Musketeer must capture D5 → C5


| A B C D E
− +− − − − −
1 | X
2 | O
3 | X O O
4 | O
5 | O X

– Guard moves A1 → B1
| A B C D E
− +− − − − −
1 | X
2 | O
3 | X O O
4 | O
5 | O X

– Musketeer must capture C5 → B5


| A B C D E
− +− − − − −
1 | X
2 | O
3 | X O O
4 | O
5 | X

– Guard wins because 3 musketeers are all on the B column

2
Tasks
Starter code is provided with classes and functions to implement

Your implementation will include a whole collection of features, including:


• implement Human VS Human play
• implement Human VS Computer (with both Greedy and Random strategies)
• investigate the effectiveness of your Greedy and Random strategies.

In your implementation you are allowed/required to:


• may add more private methods (no removal of the provided methods though!)
• may add additional classes
• use static and instance variables/methods as appropriate, with appropriate protections
• appropriately document your code

• make use of a stack to undo moves (or really any basic data structure as you see fit)
• use inheritance, composition, and polymorphism where possible

TODO

ThreeMusketeers.java

• move(Agent agent):
Gets a valid move from the given agent (Human/Random/Greedy), adds a copy of the move
using the Move copy constructor to the moves stack for undoing later, then does the move
on the board.

• undoMove():
Removes a move from the top of the moves stack and undoes the move on the board.

Board.java

• getCell(Coordinate coordinate):
Gets the cell on the board at the given coordinate.

• getMusketeerCells():
Gets all the musketeer cells on the board.

3
• getGuardCells():
Gets all the guard cells on the board.

• move(Move move):
Executes the given valid move on the board.

• undoMove(Move move):
Undo the given move on the board. The given move is a copy of the original move. So the
cells in the copy are not the same as the cells on the board and the copy has the pieces that
were originally in the fromCell and toCell.

• isValidMove(Move move):
Checks if the given move is valid according to the rules of the game. (1) the toCell is next
to the fromCell. (2) the fromCell piece can move onto the toCell piece.

• getPossibleCells():
Gets all the cells that have pieces that can be moved this turn. Needs to check if the cell has
at least one possible destination.

• getPossibleDestinations(Cell fromCell):
Gets all the cells that the piece in the given fromCell can move to.

• getPossibleMoves():
Gets all the possible moves that can happen this turn. This function can use the getPossible-
Cells and getPossibleDestinations functions to help get the list of possible moves.

• isGameOver():
Checks if the game is over and if it is, sets the winner attribute in the Board class to the
winner Piece Type.

Guard.java

• canMoveOnto(Cell cell):
Returns true if the Guard can move onto the given cell according to the rules of the game,
false otherwise.

Musketeer.java

• canMoveOnto(Cell cell):
Returns true if the Musketeer can move onto the given cell according to the rules of the game,
false otherwise.

4
RandomAgent.java

• getMove():
Gets a valid random move that can be done on the board.

HumanAgent.java

• getMove():
Asks the human for a move to be done. This function needs to validate the human input and
make sure the move is valid for the piece type that is moving.

BoardEvaluatorImpl.java

• evaluateBoard(Board board)
: Returns a score for the given board. This function is used by the GreedyAgent. A higher
score means the Musketeer side is winning and a lower score means the Guard side is winning.
Add heuristics to generate a score given a board. The GreedyAgent will go through possible
moves and pick the best move for the current side based on this score.

5
Setup

Install

Install Java 16.


Download an IDE e.g. IntelliJ.

Running

Open the project in IntelliJ then configure the SDK by going to File -> Project Structure. Select Java 16
as the project SDK.
The main function in ThreeMusketeers.java can be used to run the game. Boards are located in the boards
folder and boards are saved in text files with the current piece turn type on the first line followed by the 5
by 5 board (”X” = Musketeer, ”O” = Guard, ” ” = Empty cell).

Testing

Unit tests can be written in the test folder to test each class and the functions you implement. Example
tests are given.

You might also like