Working with the game-tree class

The game state can be represented in a lot of different ways, but you will learn how to create extendible classes in order to use the high-level board AI algorithms for different circumstances.

Getting ready…

It is important to be clear on object-oriented programming, specifically on inheritance and polymorphism. This is because we'll be creating generic functions that can be applied to a number of board game decisions and then writing specific subclasses that inherit and further specify these functions.

How to do it…

We will build two classes in order to represent game-tree with the help of the following steps:

  1. Create the abstract class Move:
    using UnityEngine;
    using System.Collections;
    
    public abstract class Move
    {
        
    }
  2. Create the pseudo-abstract class Board:
    using UnityEngine;
    using System.Collections;
    
    public class Board
    {
        protected int player;
        //next steps here
    }
  3. Define the default constructor:
    public Board()
    {
        player = 1;
    }
  4. Implement the virtual function for retrieving the next possible moves:
    public virtual Move[] GetMoves()
    {
        return new Move[0];
    }
  5. Implement the virtual function for playing a move on the board:
    public virtual Board MakeMove(Move m)
    {
        return new Board();
    }
  6. Define the virtual function for testing whether the game is over:
    public virtual bool IsGameOver()
    {
        return true;
    }
  7. Implement the virtual function for retrieving the current player:
    public virtual int GetCurrentPlayer()
    {
        return player;
    }
  8. Implement the virtual function for testing the board's value for a given player:
    public virtual float Evaluate(int player)
    {
        return Mathf.NegativeInfinity;
    }
  9. Also, implement the virtual function for testing the board's value for the current player:
    public virtual float Evaluate()
    {
        return Mathf.NegativeInfinity;
    }

How it works…

We have created the stepping stones for the next algorithms. The Board class works as a node in order to represent the current game state, and the Move class represents an edge. When the GetMoves function is called, we model the function for getting the edges in order to reach the neighbors of the current game state.

See also

For more theoretical insights about the techniques in this chapter, please refer to Russel and Norvig's Artificial Intelligence: a Modern Approach (adversarial search) and Ian Millington's Artificial Intelligence for Games (board games).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.186