A brief description of the Connect Four Game AI

In this task, we will discuss the general mechanism of the game. We will discuss the function calls used, game flow, and a general idea of the game mechanism. It would be a good idea to follow this code review along with the game code available in this project.

Prepare for lift off

We are building this arcade game based on the code from Making Games with Python & Pygame by Al Sweigart (http://inventwithpython.com/makinggames.pdf). This book is an interesting read and it should enable us to build our own arcade game (not necessarily the Connect Four game).

Engage thrusters

  1. Let's get started with reviewing the general structure of the game using the flowchart shown in the following diagram:
    Engage thrusters
  2. The first step is to import the modules required to launch and play the game. This includes the pygame, pygame.mixer, piface, and serial modules.
  3. After importing the requisite modules, game parameters such as game difficulty, board size in terms of width and height, background color, window size, colors of the computer, and player coins are initialized.
  4. The game menu is initialized and rendered on the game screen to await a button press by the player.

Main game loop

  1. When the start button is pressed, the runGame function is called to start the game. If the game is being launched for the first time, the isFirstGame variable is set to true. The computer always plays first for the very first time after the game is launched and the first turn is randomized for consecutive plays.
  2. When a new game is initialized, the getNewBoard method is called to create an empty board object to get started with the game. All game moves by the computer and the human player are recorded to this object. This is used to determine whether there is a winner as well as the computer to determine potential moves to play the game.
  3. Once we enter the main game loop, the game loop is divided into two tasks, the computer and the human's turn.
  4. When it is the player's turn, we wait for the player to play their turn using the buttons. When a button press is detected, using the animatePlayerMoving method, a coin drop is simulated. The game's board object is updated with the latest move.
  5. This is followed by the computer's turn. The getComputerMove method calls the getPotentialMoves function to determine the best possible moves available for the computer's turn. Then, the computer's coin drop is simulated using the animateComputerMoving()method.
  6. The game AI runs a check after each turn has been played to determine whether the player or the computer is a winner using the isWinner method.
  7. If either of them has won the game, an image declaring the winner is chosen. The game also checks whether the game was a tie. When either of the earlier mentioned three events occur, the program breaks out of the game loop and displays the winner (or a tie) and waits for the start button event to return to the main menu. Consequently, this will enable you to start a new game.

Objective complete – mini debriefing

Now that the code review is complete, let's move on to the next task to implement a simple marquee and wire up the input buttons.

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

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