Algorithm for Connect 4

Givens:

  1. Connect 4 game board code
  2. Rules of connect 4
    1. First to get 4 across, down, diagonally wins
    2. Players take turns
  3. Player is red, computer is black

Assumptions:

  1. Player knows how to play connect 4

Algorithm:

  1. Load the connect 4 set up.
  2. Count the number of cycles that the game has gone through.
  3. Allow the player to make a move.  If the player tries to put a piece in an full column, doesn’t make a choice, or is out of bounds, go to step 4.  If it is a valid move, go to step 5.
  4. This yields an error.  Prompt the user to return to step 3.
  5. The player has placed the piece.  The computer places a piece automatically to block the row of the player’s piece.
  6. This turn has been completed.  If the player both the player and the computer have less than four in a row, return to step 2.  If the board is full, go to step 7. If the player has four in a row, go to step 8. If the computer has four in a row, go to step 9.
  7. The game is tied and finished.
  8. The player has won!
  9. The computer has won.