How to Use EasyAI for Two-Player Games

Jun 14, 2022 | Educational

Welcome to the world of EasyAI, a powerful Python framework designed specifically for two-player abstract games like Tic Tac Toe, Connect 4, and Reversi. In this article, we will guide you through the installation process, how to set up a game, and even how to employ artificial intelligence to enhance your gaming experience!

Installation Steps

Before diving into the gaming aspect, let’s get EasyAI installed on your system:

  • If you have pip installed, run the following command in your terminal:
  • sudo pip install easyAI
  • If you don’t have pip, you can download the source code from Github, unzip it into a folder, and execute:
  • sudo python setup.py install
  • Lastly, you will also need to install Numpy for running some examples. This can be done with:
  • sudo pip install numpy

Creating a Game of Bones

Now that you have EasyAI installed, let’s define the rules of a game and start a match against the AI. Think of this as building a simple board game, where you set the stage and rules before diving into the action.

Below is an example of how to create a game of bones where two players take turns removing 1 to 3 bones from a pile until one player loses by taking the last bone:

from easyAI import TwoPlayerGame, Human_Player, AI_Player, Negamax

class GameOfBones(TwoPlayerGame):
    def __init__(self, players=None):
        self.players = players
        self.pile = 20  # Start with 20 bones
        self.current_player = 1  # Player 1 starts

    def possible_moves(self):
        return [1, 2, 3]

    def make_move(self, move):
        self.pile -= int(move)  # Remove bones

    def win(self):
        return self.pile == 0  # Check if opponent took the last bone

    def is_over(self):
        return self.win()  # Game stops when someone wins

    def show(self):
        print("%d bones left in the pile" % self.pile)

    def scoring(self):
        return 100 if self.win() else 0  # Score for the AI

# Start a match
ai = Negamax(13)  # AI thinks 13 moves ahead
game = GameOfBones([Human_Player(), AI_Player(ai)])
history = game.play()

Understanding the Code with an Analogy

Imagine you are an architect laying out the blueprint for a new game. Each segment of code serves as a building block ensuring that your game functions correctly:

  • Class Definition: The GameOfBones is the architectural blueprint where you define the structure of the game.
  • Pile of Bones: The pile itself (20 bones) can be likened to the foundational material of a building; it determines how the game will progress.
  • Possible Moves: Each possible move represents options available to players, just like openings in a design that allow for different construction pathways.
  • Scoring: The scoring method ensures players understand the outcomes of their actions, similar to grading a completed structure based on its purpose.

Solving the Game

To take your game to the next level, let’s see how we can solve it: Determine the optimal moves that lead to a guaranteed win!


from easyAI import solve_with_iterative_deepening

r, d, m = solve_with_iterative_deepening(
    game=GameOfBones(),
    ai_depths=range(2, 20),
    win_score=100
)

Here, r indicates if the first player can force a win, d gives the depth of moves required, and m shows how many bones to remove first. This simplifies the process of determining the outcome, creating a strategic approach akin to a chess player contemplating their next move.

Troubleshooting Tips

If you encounter any issues during installation or while using EasyAI, consider these troubleshooting ideas:

  • Ensure pip is installed and up-to-date.
  • Check that your Python and Numpy installations are configured correctly.
  • Refer to the EasyAI documentation available here for detailed explanations.
  • If you experience technical challenges, feel free to discuss them on Github.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Now that you know how to use EasyAI, go ahead and challenge the AI in a game of bones or design even more complex games to showcase your creativity!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox