Win 5 (Current: v4)


Game was updated. The current version now is v4

Outdated Post

Here is the code: I’ll update it, and you get news in this topic. [OUTDATED]

No comments? Is my game so bad :frowning:

Update v2

  • Added in-game players count change
  • Added Undo
  • Build in AI coming soon! Playing against a bot!

@TokOut Sorry for not commenting. I have been playing your game with one of my siblings and it’s pretty fun! Is the bot going to have different difficulties?

First of all: I don’t know how to write a very perfect bot. First I am going to understand how to make one, and the difficulties could be changed. Currently I am working on two projects: This game and another. Checkers. I am first only making the blocks move, then I develop rules etc.

Sorry for not commenting

You made me so happy :slight_smile:

Nice Game
Thanks for sharing

Update v3

  • Added last missing Orange color
  • Added ‘Themes’, so you can now chose the design from the game. Only one Theme currently with sound.

Suggest anything to me please :slight_smile:

You should consider using more tables and loops so you don’t have to write everything out 5+ times. For a bot, you could make it look for 3 in a rows and sto them (it’s a start).

It’s good to see you producing a game, well done.

You will find the code much easier to manage if you use tables as @MattthewLXXIII suggests.

Thanks. I will start using more Tables. And how to write the bot? Any ideas?

I did say to check for 3 in a row. Another thing could be to stop the longest chain, like the way you check for wins, but then use it to check for any 4 in a rows, if none then check for 3 and et cetera. This way the bot should usually have a place to put, if not you could just make it play randomly.

But if I let the bot place blocks randomly, then the bot will not be able to win. He will just stop the others place wins.

Please Help!

I need a function which will definitely place a block random where it’s not filled.

Here is the function which gets stack overflow:

function placeRandom(colour)
    local n = vec2(math.random(1, 18), math.random(1, 13))
    if tab[n.x][n.y] == 0 then
        tab[n.x][n.y] = colour

I guess you could use a while loop, something like

local n = randomthing
while tab thing ~= 0 do
n = randomthing
tab thing = colour

EDIT: added one more ‘~’

Can you explain me what the difference between tab thing and random thing should be?

Edit: I have never worked with while loops.

@TokOut Instead of doing recursive code that causes a stack overflow, you can do this since your game isn’t time intensive.

function placeRandom(colour)
    local tbl={}
    for x=1,18 do
        for y=1,13 do
            if tab[x][y] == 0 then
                table.insert(tbl,vec2(x,y))    -- table of empty squares
    if #tbl==0 then
        gameOver=true        -- game over, no empty squares
        local r=math.random(#tbl)
        tab[tbl[r].x][tbl[r].y] = colour

It places the blocks randomly, like it should, sure?

@TokOut Your recursive code works, you just need a way to not call it if there aren’t any open squares left. One way to do that is to have a counter that increments every time you set a square to a color. If that counter is equal to the total number of squares, the game is over and then don’t call the placeRandon function.

I think the bot should prioritize it’s options. Here’s how I think the bot should work:

if (there's a 4 in a row of the opposing player) then
 -- block it
elseif (there's a 3 in a row of the opposing player) then
 -- block it
    if not placingRegion then
        placingRegion = vec2(math.random(18),math.random(13)) -- This is the area that the bot will focus on placing blocks in (because you don't want the bot to place blocks randomly)
    if (there's a block in the 9x9 area (@Tokout or whatever sized area you want) around placingRegion) then
        if (that block is not blocked by the other player) then
            -- place a block next to the block
            placingRegion = (wherever the bot placed the block)
            placingRegion = vec2(math.random(18),math.random(13))
            -- place a block at placingRegion
        placingRegion = placingRegion + vec2(math.random(-4,4),math.random(-4,4))
        -- place a block at placingRegion
-- And it does all that on each turn.

Unfortunately, though, this might be difficult to program, but I’ll help whenever I can :smile:
Edit: I have fixed the instructions so they would actually work (I think, anyway)

Wait, I just realized that my instructions wouldn’t always work. Let me re-write that…