CMSC 201

Project 1 - Due 11/24

This homework must be submitted by 5pm on the due date to get any credit.

Connect Four

We will be programming an interface to simulate a connect four game. Those unfamiliar with connect four should look here for more details. The entire game will be text based, with players taking turns entering moves. Your connect four program should first prompt the user for a number of rows, then a number of columns. If either of these values is less than five, the program should continue to ask the user for a row or column value until the user enters a number five or greater. If the user enters something that isn't a number, the program should continue to prompt them until they enter a number. Once a number of rows and number of columns has been successfully been entered, you should continue to prompt each player for a move in turn until a player has won or drawn. Once the game has ended, you should display the winner and present an option to play again by typing y, or to quit by typing n. Your output should look EXACTLY as follows:
Welcome to Connect Four! 
Please enter a number of rows: 6
Please enter a number of columns: 1
Please enter a valid choice: Tacocat
Please enter a valid choice: 5
_____
_____
_____
_____
_____
_____

Player 1's turn:
Please enter a move: 1
_____
_____
_____
_____
_____
x____

Player 2's turn:
Please enter a move: 2
_____
_____
_____
_____
_____
xo___

Player 1's turn:
Please enter a move: 1
_____
_____
_____
_____
x____
xo___

Player 2's turn:
Please enter a move: -1
Please enter a valid choice: 2
_____
_____
_____
_____
xo___
xo___

Player 1's turn:
Please enter a move: 1
_____
_____
_____
x____
xo___
xo___

Player 2's turn:
Please enter a move: 2
_____
_____
_____
xo___
xo___
xo___

Player 1's turn:
Please enter a move: 1
_____
_____
x____
xo___
xo___
xo___

Player 1 wins!

Would you like to play again (y/n): y

Please enter a number of rows: 6
Please enter a number of columns: 5

From here, the output should continue as it was before. For a draw, it should print "The game is a draw!". Should the player enter an "n" at the end, the program may simply terminate. If a player tries to move in a row that is already full, the program should prompt them to "Please enter a valid choice: ".

What you need

The first step is not to panic! We are going to break what is an intimidating program down into something simple. For this project, you should represent the board as a 2D array of 0s, 1s, and -1s (where a 0 is a blank space, 1 is player 1's move, and a -1 is player 2's move). You will also need a way of keeping track of whose turn it is. Here is a list of methods you should have:

  1. move(myboard, col, player)--myboard is your 2D array that is storing your board, col is the column a player is TRYING to move, and player is the player to move. If it is a valid move, your program should go ahead and change my board so that it now has the appropriate move in it, and return True. If it is not a valid move, myboard should return False. For example:
    myboard = [ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
    move(myboard, 0, 1) will alter myboard so that it looks like this after the method runs:
    [ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 0, 0, 0, 0]]
    
  2. printboard(myboard)--prints myboard to the screen, in the format shown above (you will have to change the zeros to underscores, etc).
  3. checkWin(myboard, lastColPlayed)--check win returns a 1 if player 1 has won, a -1 if player 2 has won, and 0 otherwise. lastColPlayed is the last valid move that was made. Knowing this makes detecting a win way easier (you ONLY have to look at four-in-a-row made by the piece that was just played, since otherwise the game would have ended earlier). Feel free to write helper functions if you want to break it down into horiziontal, vertical, and diagonals.
  4. isDraw(myboard)--returns true if it's a draw

You are also allowed to reuse any functions from previous homeworks if they work!

Submitting

When you've finished your homework, use the submit command to submit the file. You must be logged into your account and you must be in the same directory as the file you're trying to submit. At the Linux prompt, type

submit cs201 PROJ1 proj1.py

After entering the submit command shown above, you should get a confirmation that submit worked correctly:

Submitting proj1.py...OK

If not, check your spelling and that you have included each of the required parts and try again.

You can check your submission by entering:

submitls cs201 PROJ1

You should see the name of the file that you just submitted, in this case proj1.py