Difference between revisions of "Python - Connect Four"

From WLCS
(Drop Chip Function)
 
(6 intermediate revisions by the same user not shown)
Line 26: Line 26:
 
*# Check if a drop is valid
 
*# Check if a drop is valid
 
*# Determine who's turn it is
 
*# Determine who's turn it is
*# In this function, you will need to determine where a chip should be dropped, and use the functions below to drop and draw the chip for the appropriate turn
+
*# In this function, you will need to determine where a chip should be dropped, and use the dropChip() function described below to drop and draw the chip for the appropriate turn
 
*# Check for wins and/or full board
 
*# Check for wins and/or full board
 
=== Drawing Functions: ===
 
* drawBoard()
 
*# You'll want to use a nested for loop to go through all the [r][c] combinations
 
*# You could then call the drawChip() function below to draw a white circle inside the nested for loop
 
*# Test the drawBoard() function to make sure that it works
 
* drawChip(r, c, color)
 
*# Use the color parameter to set the fill color
 
*# Draw the chip at a particular row, column location (note: you'll need to convert the row, column to x, y-coordinates using a couple math equations)
 
  
 
=== Row and Column Functions: ===
 
=== Row and Column Functions: ===
Line 49: Line 40:
 
** Look at drawChip() to see how you convert from column to x-coordinate.  It looks similar, but instead, solve for the column variable to convert from x-coordinate to row
 
** Look at drawChip() to see how you convert from column to x-coordinate.  It looks similar, but instead, solve for the column variable to convert from x-coordinate to row
 
** Be sure to round down to an integer
 
** Be sure to round down to an integer
 +
* goToCenter(r, c)
 +
** Go to the x, y-coordinate center of a particular row and column
  
 
=== Drop Chip Function ===
 
=== Drop Chip Function ===
* dropChip(c, color)
+
* dropChip(board, c, turn)
*# Takes a column '''c''', and a color string as parameters
+
*# Takes a, board, column '''c''', and a turn color string as parameters
*# Use a loop to find the lowest available opening in that column.  The best loop would iterate from the bottom row (numRows-1) through 0.
+
*# Use a loop to find the lowest available opening in that column.  The best loop would iterate and start from the bottom row number through 0.
 
*# Check if board[r][c] matches the empty string:
 
*# Check if board[r][c] matches the empty string:
*## Place the color in the board '''and'''
+
*## Place the turn/color in the board matrix (save the move)
*## Draw the chip at that location using a call to drawChip()
+
*## Draw the chip at that location using a call to goToCenter(r, c)
* IMPORTANT: Test your Connect Four program to see if dropChip() works when you click anywhere in a column.  You will need to add the mouse click event to your main() function and call dropChip() inside your the mouse-click function
+
*## break/exit the loop
 +
* IMPORTANT: Test your Connect Four program to see if dropChip() works when you click anywhere in a column
  
 
=== Check Functions ===
 
=== Check Functions ===
* checkValidDrop(c)
+
* checkValidDrop(board, c)
 
*# Takes a column number and returns true if a chip can be dropped in that column, and false otherwise
 
*# Takes a column number and returns true if a chip can be dropped in that column, and false otherwise
 
*# This can be simply be done in one line of code (THINK about what the board looks like)
 
*# This can be simply be done in one line of code (THINK about what the board looks like)

Latest revision as of 14:50, 12 February 2015

Objective(s)

  • You will be able to create a Connect Four game using Python
  • You will be able to draw using the Python turtle
  • You will be able to use loops and if-statements to check for valid moves and wins

Directions

  • The Connect Four game must be broken up into manageable pieces. The variables and functions specified below will help guide your development. We will develop the game in the following order:
    1. Write a function to draw the board
    2. Write a function to draw a chip (circle)
    3. Write the functions to translate our mouse-click coordinates to row and column numbers
    4. Write the function to place a chip in a column (i.e. the chip has to drawn at the lowest possible place)
    5. Write the Boolean check functions to check if a move is valid, to check if the board is full, and to check if anybody has won

Variables:

  • Initialize the following variables to starting default values at the beginning of your Python file
  • height – the height of the screen
  • width – the width of the screen
  • board – a 2-dimensional list of strings that store all the locations of chips ("", "red", "black")
    • We will fill board with lists of empty strings ("")
  • turn – the current player’s turn ("red" or "black")

Clicked function

  • clicked(x, y) - this function is called and linked by onscreenclick()
    1. Put global board at the beginning of the function body
    2. Check if a drop is valid
    3. Determine who's turn it is
    4. In this function, you will need to determine where a chip should be dropped, and use the dropChip() function described below to drop and draw the chip for the appropriate turn
    5. Check for wins and/or full board

Row and Column Functions:

  • getRow(y)
    • Takes a y-coordinate and returns the corresponding row number
    • Do NOT use any if-statements. This should be a very simple math equation
    • Look at drawChip() to see how you convert from row to y-coordinate. It looks similar, but instead, solve for the row variable to convert from y-coordinate to row
    • Be sure to round down to an integer
  • getCol(x)
    • Takes an x-coordinate and returns the corresponding column number
    • Do NOT use any if-statements. This should be a very simple math equation
    • Look at drawChip() to see how you convert from column to x-coordinate. It looks similar, but instead, solve for the column variable to convert from x-coordinate to row
    • Be sure to round down to an integer
  • goToCenter(r, c)
    • Go to the x, y-coordinate center of a particular row and column

Drop Chip Function

  • dropChip(board, c, turn)
    1. Takes a, board, column c, and a turn color string as parameters
    2. Use a loop to find the lowest available opening in that column. The best loop would iterate and start from the bottom row number through 0.
    3. Check if board[r][c] matches the empty string:
      1. Place the turn/color in the board matrix (save the move)
      2. Draw the chip at that location using a call to goToCenter(r, c)
      3. break/exit the loop
  • IMPORTANT: Test your Connect Four program to see if dropChip() works when you click anywhere in a column

Check Functions

  • checkValidDrop(board, c)
    1. Takes a column number and returns true if a chip can be dropped in that column, and false otherwise
    2. This can be simply be done in one line of code (THINK about what the board looks like)
  • checkFullBoard(board)
    1. Use a for loop to iterate through the top row to check if there are any empty spots.
    2. Return false if the board is not full, and return true if the board is full
  • checkRows(board)
    1. Use nested for loops to go through the row and columns to check if there are 4 horizontally matching chips
    2. The for loop for rows will need to go through every row
    3. The nested for loop for columns does NOT need to go through every column. Be sure to bound the column variable of the for loop so that your internal if-statement does not check out of bounds.
    4. If you see 4 in a row, then return the string at board[r][c]
    5. If there are none to be found, then return empty string "" to signify no horizontals
  • checkColumns(board)
    1. Use nested for loops to go through the row and columns to check if there are 4 vertically matching chips
    2. Our outer for loop in checkColumn() will iterate through every column, while the inner for loop will iterate through the rows
    3. The for loop for columns will need to go through every column
    4. The nested for loop for rows does NOT need to go through every row. Be sure to bound the row variable of the for loop so that your internal if-statement does not check out of bounds.
    5. If you see 4 in a column, then return the string at board[r][c]
    6. If there are none to be found, then return empty string "" to signify no verticals
  • checkDiagonals(board)
    1. There are two different sets of diagonals to check for
    2. You will need nested for loops for each set of diagonals that you are checking
  • checkWin(board)
    1. Check if there are horizontal row winners, and return the winner's string
    2. Check if there are vertical column winners, and return the winner's string
    3. Check if there are diagonal winners, and return the winner's string
    4. Check if the board is full and there is a tie. Return "full"
    5. There must be no winner or tie, so return empty string ""