Question: For this project you will implement the game Connect Four. Two players, represented by X and O, play on a grid of 5 rows and

For this project you will implement the game Connect Four.

Two players, represented by X and O, play on a grid of 5 rows and 8 columns. In turn, each player chooses the column they wish to play on, and their marker goes in the lowest open position in that column.

Game play continues until one player has four markers in a row (and wins the game), or the grid is filled (tied game).

Markers can be four-in-a-row vertically, horizontally, or diagonally.

The project zip file contains a starter file connect.py and some tests. Implement your solution in the file connect.py. Submit your solution to Gradescope.

Details

There are no command line arguments to connect.py

  • At the beginning of each turn, print the grid. Start with a blank line. Then print a column header. Then print the grid. Then print another column header.

  • State whose turn it is: X's turn or O's turn.

  • Prompt the current player with: Enter a column: .

  • The player enters a single digit between 0 and 7 corresponding to an unfilled column. The input should be validated. An input is invalid if:

    • It is not a single digit between 0 and 7
    • The corresponding column is full
  • If the input is invalid, inform the player with Invalid move. Enter a column number (0-7). and then prompt the player again (i.e. Enter a column: ).

  • A player wins when he or she has four markers in a row. These can be horizontal, vertical, or diagonal (just like tic-tac-toe, but 4-in-a-row).

  • When the game is over, print Game finished.

Example:

% python connect.py  01234567 ........ ........ ........ ........ ........ 01234567 X's turn Enter a column: 0  01234567 ........ ........ ........ ........ X....... 01234567 O's turn Enter a column: 7  01234567 ........ ........ ........ ........ X......O 01234567 X's turn Enter a column: 0  01234567 ........ ........ ........ X....... X......O 01234567 O's turn Enter a column: 6  01234567 ........ ........ ........ X....... X.....OO 01234567 X's turn Enter a column: 0  01234567 ........ ........ X....... X....... X.....OO 01234567 O's turn Enter a column: 5  01234567 ........ ........ X....... X....... X....OOO 01234567 X's turn Enter a column: 0  01234567 ........ X....... X....... X....... X....OOO 01234567 Game finished.

The game will end if there 4 in a row in these scenarios

  • Vertical win
  • Horizontal win
  • Diagonal up win
  • Diagonal down win
Or
  • Grid is filled with no winner (tied game)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!