Gamelogic
  • Home
  • Blog
  • Tools
    • Grids 2 (for Unity)
      • Features
      • Documentation
    • Grids 1 (Legacy, for Unity)
      • 30 games in 30 days
      • Features
      • Tutorials, Documentation, and FAQ
      • Games made with Grids for Unity
      • Examples
      • What is new?
      • Grid Prototypes and Previews
      • Buy
    • Extensions
    • Colors
      • Features
      • Documentation and tutorials
      • Buy
    • Words
      • Features
      • Documentation
      • Buy
    • Abstract Strategy
      • Features
      • Documentation and tutorials
      • Examples
      • Buy
    • Match
      • Features
    • Grids (for GameMaker)
      • 30 games in 30 days
      • Features
      • Documentation
      • Buy
  • Buy
  • Articles
  • Support
    • Knowledge Base
    • Email Support
  • About
    • Meet our team

Capturing an enemy piece

Capture680x340

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:

Capturing1

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:

  1. Every player has two pieces.
  2. Red player starts.
  3. Pieces can move only one cell at a time.
  4. Pieces can move in any direction.
  5. 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.

Capturing2

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.

Capturing3

  • 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.

Capturing4

  • Logical pieces prefab need to have a Hope Capture Rule script with the same Valid directions and a Capture Player ID of the enemy.

Capturing5

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:

Capturing6

The final step is to add the main game script.

Capturing7

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.

C#
1
2
3
4
5
6
7
8
9
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.

Capturing8

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:

Capturing9

To check out this example:

  1. Import Abstract Strategy in a new project.
  2. Import the package below into the same project.
  3. Open the CaptureEnemyPieces scene.

CaptureEnemyPieces.unitypackage

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Related

  1. Creating your own piece rules  In this tutorial we will look at how to implement special rules...
  2. Moving a piece and changing turns  Setup a game board with pieces as explained in this...
  3. Creating your own victory rules  On this tutorial we will cover how to create a...
  4. Creating your own grid game rules  In this tutorial we will focus on implementing Basic Draughts...

  • Setting up a grid and board
  • Moving a piece and changing turns
  • Using victory conditions
  • Animating a piece in the board
  • Home
  • Features
  • Blog
  • About
  • 14 Eglin Road, Sunninghill, Johannesburg, South Africa
  • support@gamelogic.co.za
Stay Connected
  • Facebook
  • Google
  • Linkedin
  • Rss
  • Twitter
  • Gamelogic © 2013