Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Connect 4 assigment
#1
Hello team,

I have this assignment to do:
Quote:
Have you ever played "Connect 4"? It's a popular kid's game by the Hasbro company. In this project, your task is create a Connect 4 game in Python. Before you get started, please watch this video on the rules of Connect 4:

https://youtu.be/utXzIFEVPjA

Once you've got the rules down, your assignment should be fairly straightforward. You'll want to draw the board, and allow two players to take turns placing their pieces on the board (but as you learned above, they can only do so by choosing a column, not a row). The first player to get 4 across or diagonal should win!

Normally the pieces would be red and black, but you can use X and O instead.
I broke it down into three steps:

1. Draw the board
2. Update the board with user input
3. Verify if there is 4 across or diagonal to end the game

So far I just manage to come up with the first step, it looks like that:

'''

012345678
 | | | |  0
--------- 1
 | | | |  2
--------- 3
 | | | |  4
--------- 5
 | | | |  6
--------- 7
 | | | |  8

'''

def drawField():
    for row in range(9):
        if row % 2 == 0:
            for column in range(9):
                if column % 2 == 0:
                    if column != 8:
                        print(' ', end='')
                    else:
                        print(' ')
                else:
                    print('|', end='')
        else:
            print('-'*9)

drawField()

player = 1
currentField = [' ', ' ', ' ', ' ', ' ']
drawField()
while(True):
    print(f'Players {player} turn')
    move = int(input('Column: '))
    if player == 1:
        currentField[move] == 'X'
        player = 2
    else:
        currentField[move] == 'O'
    drawField(currentField)
What I am struggling with is to update the board with the user's input.

Firstly I want to make the function add an 'X' or 'O' to the bottom row , or the 8th row, of the column the user input. Then check if it is not ' ', and add to the 7th row and so on.

I have it clear in my mind but I cannot code it.

Any thoughts are most welcome.
Reply


Messages In This Thread
Connect 4 assigment - by Milan - Jan-13-2021, 02:21 PM
RE: Connect 4 assigment - by jefsummers - Jan-13-2021, 05:47 PM
RE: Connect 4 assigment - by DPaul - Jan-14-2021, 07:31 AM
RE: Connect 4 assigment - by Milan - Jan-14-2021, 02:29 PM
RE: Connect 4 assigment - by DPaul - Jan-14-2021, 04:12 PM
RE: Connect 4 assigment - by deanhystad - Jan-14-2021, 07:48 PM
RE: Connect 4 assigment - by Milan - Jan-15-2021, 05:08 PM
RE: Connect 4 assigment - by deanhystad - Jan-16-2021, 05:23 AM
RE: Connect 4 assigment - by Milan - Jan-20-2021, 10:02 PM
RE: Connect 4 assigment - by deanhystad - Jan-20-2021, 10:37 PM
RE: Connect 4 assigment - by deanhystad - Jan-22-2021, 05:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting list - Homework assigment ranbarr 1 2,307 May-16-2021, 04:45 PM
Last Post: Yoriz
  Python homework assigment makisha 3 3,366 Feb-28-2019, 10:21 PM
Last Post: Yoriz
  Display school assigment Vittya 2 3,562 Nov-09-2017, 02:13 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020