Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a board game
#11
(02-23-2024, 09:06 PM)Marcus Wrote:
(02-23-2024, 04:34 PM)aliensoldier Wrote: I'm going to share the code I've made because I'm at an impasse and I don't know how to continue.

I have three problems:

1-there is a cursor that moves around the board and if you stand on top of a player's piece and press "z" it picks it up and you can drop it on another square with the "x" key, but the problem is that it drops it anywhere box and I want it to only be able to be dropped in the dark boxes and I don't know how to do it.

2-when I grab a player token you can grab other player tokens at the same time and I only want it to grab one token at a time but I haven't been able to do it.

3-When I have a player token I should only be able to move one square per turn but it can be moved to any square, as could be done.

I made two posts but deleted them now, after looking up the rules of Checkers.

Hopefully this code can help you:

Code:
constant GRID = 8 ' number of rows and columns.
constant SQUARE = 53 ' size of each square.

set window "test", 640, 480
set redraw off

' create 8x8 array filled with zeroes.
board = fill(0, GRID, GRID)
' place pieces.
for y = 0 to 2  for x = 0 to GRID - 1
    if (y + x)%2 = 1  board[x][y] = 1
    if (GRID - 1 - y + x)%2 = 1  board[x][GRID - 1 - y] = 2
next
' center board in window.
boardX = (width(primary) - GRID*SQUARE)/2
boardY = (height(primary) - GRID*SQUARE)/2

' set to 1 or 2 when a piece has been picked up.
holdingPiece = 0
' original position of picked up piece.
originalPieceX = 0
originalPieceY = 0

' loop until user presses escape.
while not keydown(KEY_ESCAPE, true)
    ' check if mouse is inside board.
    mx = mousex()
    my = mousey()
    gridX = mx - boardX
    gridY = my - boardY
    if gridX >= 0 and gridX < GRID*SQUARE and gridY >= 0 and gridY < GRID*SQUARE
        ' convert to grid coordinates.
        gridX = int(gridX/SQUARE)
        gridY = int(gridY/SQUARE)
        ' click?
        if mousebutton(0, true)
            ' not holding a piece?
            if not holdingPiece
                ' any piece at that position?
                if board[gridX][gridY]
                    ' store information about the piece.
                    holdingPiece = board[gridX][gridY]
                    originalPieceX = gridX
                    originalPieceY = gridY
                    ' remove piece from board.
                    board[gridX][gridY] = 0
                endif
            ' already holding piece.
            else
                ' spot empty?
                if not board[gridX][gridY]
                    ' same position or valid distance for move? here both players can move up and
                    ' down. only "kings" should be able to do that.
                    dx = |gridX - originalPieceX|
                    dy = |gridY - originalPieceY|
                    if dx = 0 and dy = 0 or dx = 1 and dy = 1
                        ' put piece down.
                        board[gridX][gridY] = holdingPiece
                        holdingPiece = 0
                    endif
                endif
            endif
        endif
    endif

    ' draw.
    set color 0, 0, 0
    cls
       
    ' draw board.
    for y = 0 to GRID - 1
        for x = 0 to GRID - 1
            ' this formula gives a chess pattern.
            if (y + x)%2  set color 64, 64, 64
            else  set color 208, 208, 208
            draw rect boardX + x*SQUARE, boardY + y*SQUARE, SQUARE, SQUARE, true
            ' any piece?
            if board[x][y]
                DrawPiece(board[x][y], boardX + (x + 0.5)*SQUARE, boardY + (y + 0.5)*SQUARE)
            endif
        next
    next
   
    ' Draw picked up piece.
    if holdingPiece  DrawPiece(holdingPiece, mx, my)
   
    redraw
    fwait 60
wend

function DrawPiece(player, x, y)
    if player = 1  set color 240, 240, 240
    else  set color 32, 32, 32
    draw ellipse x, y, SQUARE/2 - 4, SQUARE/2 - 4, true
    set color 0, 0, 0
    draw ellipse x, y, SQUARE/2 - 4, SQUARE/2 - 4, false
endfunc

As it is now, both players can always move backwards or forwards diagonally. But according to the rules, only "kings" can move backwards. board[x][y] is now either: 0 = empty, 1 = player 1 piece or 2 = player 2 piece. I guess you can simply add two more values: 3 = player 1 king piece and 4 = player 2 king piece, and make it so that only kings can move backwards.

Edit  And I'm sorry for not basing my code on yours. I'm just showing you how I would do it myself, and hopefully it can help you modify your own code Smile

Thanks Marcus, I'll try to adapt it to my code. Smile
Reply


Messages In This Thread
How to create a board game - by aliensoldier - 02-11-2024, 09:09 PM
RE: How to create a board game - by Marcus - 02-13-2024, 06:56 PM
RE: How to create a board game - by aliensoldier - 02-13-2024, 07:54 PM
RE: How to create a board game - by johnno56 - 02-13-2024, 09:12 PM
RE: How to create a board game - by aliensoldier - 02-14-2024, 04:27 PM
RE: How to create a board game - by johnno56 - 02-14-2024, 07:10 PM
RE: How to create a board game - by aliensoldier - 02-14-2024, 07:49 PM
RE: How to create a board game - by johnno56 - 02-14-2024, 09:57 PM
RE: How to create a board game - by aliensoldier - 02-23-2024, 04:34 PM
RE: How to create a board game - by Marcus - 02-23-2024, 09:06 PM
RE: How to create a board game - by aliensoldier - 02-24-2024, 01:44 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)