Discuss Scratch

Blue-Monarch-3
Scratcher
10 posts

Connect 4...

My friend is doing a project in which he is making a connect 4 game with me. The problem is, I can't figure out how to get the gravity system to work. The project is due in a month and I am worried sick. I searched up some tutorials and onky found one that I couldn't even access. Please help…
legendary34678
Scratcher
1000+ posts

Connect 4...

It would help if you could post the link to their project so that I can take a look how they're doing it. Connect-4 can be quite tricky, after all!
deck26
Scratcher
1000+ posts

Connect 4...

This is an ideal project for a grid system. A new counter can only descend in it's column until it sees the next slot is occupied. Not sure you need anything fancy for gravity, just move the counter down at a fixed speed. But if you want to you could move n steps in a loop increasing the value by a small amount each time.
Blue-Monarch-3
Scratcher
10 posts

Connect 4...

deck26 wrote:

This is an ideal project for a grid system. A new counter can only descend in it's column until it sees the next slot is occupied. Not sure you need anything fancy for gravity, just move the counter down at a fixed speed. But if you want to you could move n steps in a loop increasing the value by a small amount each time.
Thanks, already figured I could use that method. How do you make it so that the counter can see if the slot below is occupied?
deck26
Scratcher
1000+ posts

Connect 4...

Blue-Monarch-3 wrote:

deck26 wrote:

This is an ideal project for a grid system. A new counter can only descend in it's column until it sees the next slot is occupied. Not sure you need anything fancy for gravity, just move the counter down at a fixed speed. But if you want to you could move n steps in a loop increasing the value by a small amount each time.
Thanks, already figured I could use that method. How do you make it so that the counter can see if the slot below is occupied?
That's where the grid system comes in. Say your grid is 9 columns wide and you're dropping a counter in column 4. If cell 4 is empty - eg list item contains a 0 - you can drop down to that grid cell. If it is not empty you'd check list item 13, 22, 31 in order until you found an empty cell. Once the counter is in position you change the corresponding list item - perhaps to 1 for player 1 or 2 for player 2. You can then look at the grid and work out if there is a winner.

This project https://scratch.mit.edu/projects/47150778 explains more about using a grid system. It is aimed at movement within a grid but a lot of it still relevant.
Spentine
Scratcher
1000+ posts

Connect 4...

I created a Connect 4 game, here’s how I made the falling work.

Let’s simplify a bit and assume that the board is only 1 column wide. Once that’s figured out, it shouldn’t be too difficult to extend the process to 7 columns.

First, the code must check if the column is full. To do that, it must look at the top position of the column and detect if it’s empty. If it is, then it can move onto the next step. Otherwise, it’s impossible to place a piece in that position.

Afterwards, the code must check the next token down. If it has a token, then place the token right above it. Otherwise, repeat the process until a token is in the checked position or if the checked token is below the board, which means that there are no tokens.

Once you figure out what I just said and you were able to implement it, you can then extend the code to work with 7 columns.

Powered by DjangoBB