BYU logo Computer Science

To start this lab, download this zip file.

Project 4 - Connect Four

Before doing this lab, make sure you have byu-pytest-utils version 0.3.6 or later.

conda activate cs110
pip install -U byu-pytest-utils=0.3.6

Instructions

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.

Tests

When testing, you need to be sure your program prints lines exactly as they are shown in the assignment. If you miss a space or misspell a word, the test will fail. Check the output of the test and click on <Click to see difference> to see how your program differs from what we are expecting.

The tests cover the following scenarios:

  • Vertical win
  • Horizontal win
  • Diagonal up win
  • Diagonal down win
  • Basic invalid moves
  • Harder invalid moves
  • O wins (instead of X)
  • Grid is filled with no winner (tied game)

As you write your program, play through different scenarios that exercise these cases. Verify that your program does what you expect it to.

It may also be helpful to look at the tests in test_connect.py. In each test, you will find a line that includes something like

as_stdin(5, 4, 3, 2, 4, 3, 2, 2, 3, 1, 2)

These numbers indicate the series of inputs to the program. You can play the program in the terminal and use this sequence of inputs (i.e. enter each number in turn) in order to play through the tested scenario.

Grading

Each test is worth the number of points indicated above the function definition.

For example:

@max_score(10)
def test_foobar():
    assert True

Would be worth 10 points.

Above and beyond

If you want an extra challenge, after submitting, make a copy of your program and add the following features:

  • Indicate the winner of the game, if any, or state that the game was a tie.
  • Use lowercase x and o for game play, and then indicate the winning sequence using uppercase letters.
  • Allow the user to indicate the size of the grid from the commandline.