To capture pieces you need to configure your grid and board, and also set the moves and turns for your pieces. In this following tutorial we will make a game with the following configuration:
We will look at three different types of capture:
- a simple capture that involves moving onto another piece.
- a hop capture that involves hopping over a piece, and
- a surround capture that involves surrounding an enemy piece on the left and right, or top and bottom, and results in the piece being replaced by a piece of the capturing player, rather than being taken.
The other rules of this game are:
- Every player has two pieces.
- Red player starts.
- Pieces can move only one cell at a time.
- Pieces can move in any direction.
- Capturing works as explained above.
To implement the four first features follow the “Setting up a grind and board” and the “Moving a piece and changing turns” tutorials.
To implement capturing, you need to do the following.
- Set the line directions on the Grid Game Rules script on the GameBoard GameObject as shown below.These directions are relative and is necessary to tell the game which directions are valid for making or moving in lines.
- Your logical piece prefabs need to have a Relative Move Rule script with the Valid Directions configured with the same values as the line directions above.
- Logical pieces prefab need to have a Hope Capture Rule script with the same Valid directions and a Capture Player ID of the enemy.
This script implements the rules to capture a piece when you hop over an enemy piece. It sets the valid directions from which you can capture, and set the ID of the enemy piece.
- Add a Relative Capture Move Script to the Logical piece prefab to allow simple captures, and configure it as follows:
The final step is to add the main game script.
The Capture Game Script does the following things:
- Start the game by initializing the game and registering the players.
- Allows to select different pieces
- Allows to move a piece to a valid position.
- Allows to capture an enemy piece.
The first items are explained in other tutorials. The capturing happens on the OnClick method, the way to implemented is after a move is done but when the move is not valid.
if (manager.CommitMove(move)) { TransformEnemyPieces(point); DeselectPiece(); } else { CapturePiece(selectedPos, point); }
The CapturePiece method uses the manhattan distance between the point of the actual piece and the target destination to check whether a capture is possible. For the hop capture the legal distance is distance is 2 or 4, and for the relative capture it is 1.
The third type of capture is implemented fully in code. It checks whether the piece can be captured, and then transforms that piece into a piece of the capturing player.
Update:
There is a new example on this tutorial, it is called Full Surround Capture and it transform a piece only when its fully surrounded by enemy pieces. The Implementation is on code following the TransformEnemyPieces and it work like this:
To check out this example:
- Import Abstract Strategy in a new project.
- Import the package below into the same project.
- Open the CaptureEnemyPieces scene.