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

Creating your own victory rules

OwnVictoryRules

On this tutorial we will cover how to create a custom victory rule for our game, so to make it clear we will create a very simple game:

OwnVictoryRules1

This game has the following rules:

  • Red starts.
  • Only one turn per player.
  • You can only move to adjacent cells.
  • You can’t capture enemy pieces.
  • You win when you have a piece has three or more pieces of the same color in neighboring cells.

This last condition is not a common game rule, so we will make a script to validate this condition and make the game end when it happens.

1Configure the game board as shown in the image above, and make sure your pieces have a “Relative Move Rule” Script with 8 relative valid directions:

OwnVictoryRules2

We will also use a Canvas Text to show a message when the game end. To know how to do this refer to this tutorial.

Make a new script that will drive the game (YourOwnVictoryRule), and add it to the GameBoard Also add a VictoryOtherPlayer component to the game board, which is a default victory condition that can be configure to make a player lose if the other wins like this:

OwnVictoryRules3

2Implement the custom victory rule class, and call it CustomVictoryRule and add it to the game board. Make it extend from VictoryRules and use a RectPoint and GridGamePieceSettings class as types:
C#
1
public class CustomVictoryRule : VictoryRules<RectPoint, GridGamePieceSettings>

Implement the the CheckVictory method with the rule. To do this, iterate over every point on the board using “currentState.GetAllPoints()”. If the cell is not empty and if the piece belongs to the current player, then we count how many neighbors of the same type the piece has. If we count 4 then we set the current player as a winner with the following line.

currentPlayer.SetVictoryState(VictoryType.Victory);

Since we use the “Victory Other Player” script, making a player win means the other player lose, and this make the game finish.

Try out the game and change the number of pieces, add piece to the board and see how it all behaves.

OwnVictoryRules4

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 YourOwnVictoryRules scene.

YourOwnVictoryRules.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. Creating your own pieces  This tutorial will show you how to create your own pieces....
  3. Using victory conditions  Add a default Victory condition on the GameBoard GameObject. Configure...
  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