Difference between revisions of "Python - Connect Four"

From WLCS
(Created page with "== 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-stateme...")
 
(Drop Chip Function)
 
(8 intermediate revisions by the same user not shown)
Line 11: Line 11:
 
*# Write the function to place a chip in a column (i.e. the chip has to drawn at the lowest possible place)
 
*# Write the function to place a chip in a column (i.e. the chip has to drawn at the lowest possible place)
 
*# 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
 
*# 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
*# Integrate our functions to the canvas’s mouse-click event handler
 
  
 
=== Variables:===
 
=== Variables:===
 
* Initialize the following variables to starting default values at the beginning of your Python file
 
* Initialize the following variables to starting default values at the beginning of your Python file
  
* cHeight – the height of the canvas
+
* height – the height of the screen
* cWidth – the width of the canvas
+
* width – the width of the screen
* board – a 2-dimensional array of strings that store all the locations of chips ("", "red", "black")
+
* board – a 2-dimensional list of strings that store all the locations of chips ("", "red", "black")
** Example: var board = []
 
 
** We will fill board with lists of empty strings ("")
 
** We will fill board with lists of empty strings ("")
 
* turn – the current player’s turn ("red" or "black")
 
* turn – the current player’s turn ("red" or "black")
  
=== Main function ===
+
=== Clicked function ===
* main() - the main function that sets up the board and runs the game
+
* clicked(x, y) - this function is called and linked by onscreenclick()
*# Populate '''board''' with empty strings ("")
+
*# Put '''global board''' at the beginning of the function body
*## Write a nested for loop that pushes an empty list/array into board
+
*# Check if a drop is valid
*## After you push the empty list, push an empty string into that row
+
*# Determine who's turn it is
*# Draw the board graphically using drawBoard()
+
*# 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
* clicked()
+
*# Check for wins and/or full board
** The clicked function is defined in the '''main()''' function that you have already defined
 
** After the clicked function definition, add the event listening line so that mouse clicks are detected: '''canvas.addEventListener('click', clicked);'''
 
 
 
=== Drawing Functions: ===
 
* drawBoard(ctx)
 
*# Takes a context variable ctx as a parameter
 
*# Use the ctx variable to draw all the white circles for the yellow board
 
*# 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(ctx, color, r, c)
 
*# 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 34:
 
** Do NOT use any if-statements.  This should be a very simple math equation
 
** 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
 
** 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 use Math.floor() to round down to an integer
+
** Be sure to round down to an integer
 
* getCol(x)
 
* getCol(x)
 
** Takes an x-coordinate and returns the corresponding column number
 
** Takes an x-coordinate and returns the corresponding column number
 
** Do NOT use any if-statements.  This should be a very simple math equation
 
** 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
 
** 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 use Math.floor() 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(ctx, c, color)
+
* dropChip(board, c, turn)
*# Takes a context, a column '''c''', and a color string
+
*# 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)
* checkFullBoard()
+
* checkFullBoard(board)
 
*# Use a for loop to iterate through the top row to check if there are any empty spots.   
 
*# Use a for loop to iterate through the top row to check if there are any empty spots.   
 
*# Return false if the board is not full, and return true if the board is full
 
*# Return false if the board is not full, and return true if the board is full
* checkRows()
+
* checkRows(board)
 
*# Use nested for loops to go through the row and columns to check if there are 4 horizontally matching chips
 
*# Use nested for loops to go through the row and columns to check if there are 4 horizontally matching chips
 
*# The for loop for rows will need to go through every row
 
*# The for loop for rows will need to go through every row
Line 78: Line 66:
 
*# If you see 4 in a row, then return the string at board[r][c]
 
*# If you see 4 in a row, then return the string at board[r][c]
 
*# If there are none to be found, then return empty string "" to signify no horizontals
 
*# If there are none to be found, then return empty string "" to signify no horizontals
* checkColumns()
+
* checkColumns(board)
 
*# Use nested for loops to go through the row and columns to check if there are 4 vertically matching chips
 
*# Use nested for loops to go through the row and columns to check if there are 4 vertically matching chips
 
*# Our outer for loop in checkColumn() will iterate through every column, while the inner for loop will iterate through the rows
 
*# Our outer for loop in checkColumn() will iterate through every column, while the inner for loop will iterate through the rows
Line 85: Line 73:
 
*# If you see 4 in a column, then return the string at board[r][c]
 
*# If you see 4 in a column, then return the string at board[r][c]
 
*# If there are none to be found, then return empty string "" to signify no verticals
 
*# If there are none to be found, then return empty string "" to signify no verticals
* checkDiagonals()
+
* checkDiagonals(board)
 
*# There are two different sets of diagonals to check for
 
*# There are two different sets of diagonals to check for
 
*# You will need nested for loops for each set of diagonals that you are checking
 
*# You will need nested for loops for each set of diagonals that you are checking
* checkWin()
+
* checkWin(board)
 
*# Check if there are horizontal row winners, and return the winner's string
 
*# Check if there are horizontal row winners, and return the winner's string
 
*# Check if there are vertical column winners, and return the winner's string
 
*# Check if there are vertical column winners, and return the winner's string

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 ""