Dairymilk2010
Badges: 4
Rep:
?
#1
Report Thread starter 1 year ago
#1
Does anyone know how to make a connect 4 game on python? I’ve got a list of tasks in order to do it but I can’t do python for the life of me
0
reply
winterscoming
Badges: 19
Rep:
?
#2
Report 1 year ago
#2
What are you using to write it with? PyGame? or as a plain GUI app using TKinter? or something else? It would be a good idea to spend some time looking at whatever you're going to use to display your game on the screen and figure out how you're going to visually represent the board and the counters to the users. For example, see if you can write a small sandbox/prototype program to draw them

A good way to approach it could be to write separate prototype programs for all the main pieces that you need to do and focus on each bit by itself. You can worry about joining it all together later; the main task to star with is all about divide-and-conquer by breaking it down into smaller managable pieces and solving each part on its own -- if a problem seems too complicated to do by itself, you probably just haven't broken it down enough.

Also, have you done a thorough analysis on what the rules are for Connect 4? Have you drawn out any UI mockups on the screen and described what the UI/UX is going to look like and how the user is going to interact with it? e.g. what do they press to start the game? what do they press to exit the game? what do they press to drop a counter into a column?

Before you write any code, Think about what the user manual would look like and how you would explain to someone what the controls of the game are, and what they should expect to see on the screen. It's a really good idea to write out alot of your functional tests before starting on the code because the functional tests will inform your design and how the code needs to work.

Also, Can you explain step-by-step in plain English how you would check all the counters on the board after a player move to decide whether a player has won?
(i.e. if you're looking at one counter at-a-time what's the logic to find out whether there are 4 counters in a row?)

Also, what about the step-by-step logic to decide whether the board is full and it's a draw?

Have you considered what data and data structures you will use to represent things in memory? for example, what data structure would you use to store the state of the board? what would you use to represent a counter and its colour?
(There's no absolute right/wrong answers to these, they're a bit open-ended although there might be some easy/obvious answers if you think about programming concepts like Arrays/Lists, Integers, Characters, Strings, Records, etc.)

it's a good idea to think about all your logic, algorithms, data structures and data before even attempting to write any real code. Also by clearly defining the rules of the game, that should prompt you to think about a lot of the same things that you will need to model in Python (e.g. imagine explaining it to a very young child who has just learned to count) -- The hardest part of programming usually isn't writing the code but spending time thinking about the computational "model" in terms of programming constructs that you need to use.

I would very strongly recommend you write an entirely separate program for your game rules/logic and don't attempt to write your game logic mixed in with any GUI or user input stuff -- if you can get the basic computational model working in terms of representing all the rules of the game and its behaviour just using in-memory data structures like lists/variables/records/strings/etc. Then that's an entirely separate problem on its own. Don't get it all muddled up with GUI and UI stuff. (You can test it with hard-coded "dummy" data to get it working)

Also (separately) consider things like:
- How are you going to handle user input? (that will probably depend on whether you use pygame/tkinter/etc.)
- What will you do to make sure that the user can't break the program by clicking/pressing the wrong thing?
- How will you display messages/feedback on the screen to the user?


Again, i would recommend writing separate programs for these things first -- don't mix your user input up with your core game rules/logic. The main thing first is to figure out how you're going to do it. when you know how all the code needs to work, you can start bringing it all together at the end. Doing this also forces you to write logic into separate functions and even separate classes, which is a good thing from an "OO" point of view. Object-Oriented programming is all about grouping related stuff together, and keeping clean, logical separation between things which don't belong together

For example, you might want some classes/functions whose responsibility are solely about drawing/updating the screen -- those classes/functions hopefully wouldn't contain any stuff about your game rules or input handling. Similarly, you don't want your game rules functions/classes being muddled up with graphics drawing code or mouse/keyboard handling, etc. (e.g. the code you write to calculate whether the user has won would ideally be separate from the code you write which draws something on the screen or reads a mouse/keyboard event to listen to where they've dropped their counter, etc.)


Lastly, for learning/using Python syntax itself, try following some of these programming lessons: https://www.py4e.com/
Some decent videos too: https://www.youtube.com/playlist?lis...6oAMk4JHw91mC_
Last edited by winterscoming; 1 year ago
1
reply
Dairymilk2010
Badges: 4
Rep:
?
#3
Report Thread starter 1 year ago
#3
winterscoming Wow thankyou so much for the reply! I mean you’ve probably gone and wasted 30 mins of your life for me (: which I’m grateful for. Your advice makes sense and I’ll what I can do from there. Thankyou again!
1
reply
winterscoming
Badges: 19
Rep:
?
#4
Report 1 year ago
#4
(Original post by Dairymilk2010)
winterscoming Wow thankyou so much for the reply! I mean you’ve probably gone and wasted 30 mins of your life for me (: which I’m grateful for. Your advice makes sense and I’ll what I can do from there. Thankyou again!
You're welcome - If it's useful then hopefully not a waste at all Good luck on the project!
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

How would you feel if uni students needed to be double vaccinated to start in Autumn?

I'd feel reassured about my own health (43)
14.93%
I'd feel reassured my learning may be less disrupted by isolations/lockdowns (93)
32.29%
I'd feel less anxious about being around large groups (36)
12.5%
I don't mind if others are vaccinated or not (24)
8.33%
I'm concerned it may disadvantage some students (17)
5.9%
I think it's an unfair expectation (72)
25%
Something else (tell us in the thread) (3)
1.04%

Watched Threads

View All