Four-in-a-row challenge!

connect4In this blog post you will use Python Code to complete this simulation of the game of connect 4. (Rules of the game)

You will first need to reverse-engineer the code provided. The code so far is used to:

  • Display the 6×7 grid using Python Turtle.
  • Randomly place tokens on the gird, taking turns (yellow and red).
  • Refresh the grid on the screen using the drawGrid() function.

The code provided uses a variable called connect4, use to store a two-dimensional array (6×7) of integer values. In Python, a 2D-array is a list of lists. Within this array, a 0 represents an empty place, a 1 represents a yellow token and a 2 represents a red token.

connect4-2d-array

Your Task:


Complete the checkIfWinner() function (from line 39) to check if after placing the token the game continues or if the player has aligned 4 tokens either in a row, column or diagonal. If so the function will return the color value of the winner (1 for Yellow, 2 for Red, 0 if no winner)


unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 3.6 / 5. Vote count: 12

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: