TakeTurns 1.0.3
dotnet add package TakeTurns --version 1.0.3
NuGet\Install-Package TakeTurns -Version 1.0.3
<PackageReference Include="TakeTurns" Version="1.0.3" />
<PackageVersion Include="TakeTurns" Version="1.0.3" />
<PackageReference Include="TakeTurns" />
paket add TakeTurns --version 1.0.3
#r "nuget: TakeTurns, 1.0.3"
#:package TakeTurns@1.0.3
#addin nuget:?package=TakeTurns&version=1.0.3
#tool nuget:?package=TakeTurns&version=1.0.3
Take Turns
A 2-Player, Turn-Base AI Engine
Take Turns is a fast, flexible, 2-player AI engine based on the classic minimax algorithm with alpha-beta pruning
- Compatible with Unity
- Fully customizable and suitable for any turn-based, 2-player game
- Simple, yet effective approach to turn-based AI
- Example project here
Installation
General Installation
This package is available in NuGet here or by running Install-Package TakeTurns -Version 1.0.3
in the Package manager console.
Unity Installation
Download TakeTurns.dll
from the release page and place anywhere inside your Assets directory.
Usage
See the <a href="https://github.com/ihawn/TicTacToe/tree/main/TicTacToe">example project</a> and more specifically <a href="https://github.com/ihawn/TicTacToe/blob/main/TicTacToe/Assets/Scripts/TakeTurnsOverloads.cs">this file</a> for a specific implementation example of this package.
This package requires 3 user-defined classes which will be specific to your game and are outlined as follows:
public class GameSpace
{
// Class to store the game
// Needs to have information on each game agent
// Should be as simple to make deep-copying fast
// For example, if you were to make a chess AI with this package you would probably want
// to have a matrix property to store each piece type and location
}
public class Agent
{
// Class to store each piece
// In the chess example, this could be as simple as an x, y coordinate to access the piecetype in GameSpace
}
public class Move
{
// Class to store each move
// Should contain enough information to modify GameSpace to reflect a unique movement of Agent
// In the chess example, this would just be an x, y coordinate to specify which square to move to
}
public class OverloadContainer : TakeTurns<GameSpace, Agent, Move>
{
// Class to store overloads
// An instance of this class will also be needed to call the GetBestMove() method
}
Place the following 3 overrides in OverloadContainer:
public override float GetGameEvaluation(GameSpace space)
{
// This method is very important and will be quite specific to your game
// It should calculate a float which defines the "state" of your game
// Player 1 will want to minimize this value while Player 2 will want to maximize it
// Returning 0 would mean the game is currently tied
// In the chess example, a simple evaluation calculation would be BlackPieceCount - WhitePieceCount
// Obviously, black would want to maximize this value and white would want to minimize it
// See https://en.wikipedia.org/wiki/Minimax for more info
// It is recommended to place an infinite penalty on the other player winning (using EndGameReached below)
// to ensure that neither player allows themselves to lose willingly
}
public override (bool, EndState) EndGameReached(GameSpace space)
{
// First return parameter:
// Returns true if space contains a finished game (there is a winner or a draw)
// Note that this is not measuring the current game space but rather that passed game space
// Second return parameter:
// Returns an EndState enum which can either be MaxPlayerWins, MinPlayerWins, Tie
}
public override IList<MinimaxInput<GameSpace, Agent, Move, float>> GetPositions(Gamespace space, bool isMaxPlayer)
{
// Generates a collection of MinimaxInput objects which represent each game state which is possible from the passed one (space)
// MinimaxInput contains the following fields
// - GameSpace -> the gamespace generated from space
// - Agent -> the agent which generated GameSpace
// - Move -> the move which Agent took to generate GameSpace (IMPORTANT: If a move is not possible from the current GameSpace, this needs to be null)
}
IMPORTANT: Inside GetPosition()
, when computing a branch of space, you must deep-copy space before assigning it to the GameSpace
property of the current MinimaxInput
object. If you do not, you will modify the passed parameter space which will result in nonsense output. This is why your GameSpace
class should contain the bare minimum amount of information needed. Preferrably, it should contain only primitives so that you may define the deep-copy in a constructor overload of GameSpace
.
And that's it! Obviously, games can be quite different which is why this user-supplied information is needed. The next move your computer player should make can be computed with the function
OverloadContainer.GetBestMove(GameSpace g, int depth, bool isMaximizingPlayer)
The isMaximizingPlayer
bool simply represents whose turn it is. It doesn't matter which player you choose to be your maximizing one as long as you are consistent when calling this function as well as when writing GetPositions
. Note too that depth must be odd (or else this function would return what move the other player should make).
GetBestMove()
returns a MinimaxOutput
object which contains the following parameters:
- Piece: The piece that should move
- Move: The move that the piece should make
- GameSpace: The space of your game after Piece performs Move
Now you have all the information you need to move your computer player's piece!
License
MIT
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net48 is compatible. net481 was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial Release