Author Topic: Checkers with Mouse  (Read 6822 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Checkers with Mouse
« on: February 03, 2020, 10:37:26 pm »
I think I have multiple jumps solved but you have to click checker again after jumping to signal you are done.

Code: QB64: [Select]
  1. _TITLE "Checkers" 'b+ 2020-02-03
  2.  
  3. CONST xmax = 500, ymax = 500, SQ = 50
  4. CONST WHITE = &HFFFFFFFF, GOLD = &HFFFF9900, BLACK = &HFF000000, GRAY = &HFF444444, GRAY2 = &HFF999999, RED = &HFFFF0000
  5. CONST dRed = &HFFDD0000, dGray = &HFF222222
  6. DIM SHARED b(1 TO 8, 1 TO 8) AS INTEGER, turn, bx, bx2, by, by2
  7. SCREEN _NEWIMAGE(xmax, ymax, 32)
  8. loadB
  9. showB
  10.     CLS
  11.     IF turn THEN yCP 5, "Red's turn" ELSE yCP 5, "Black's turn"
  12.     showB
  13.     bx = -1: by = -1: bx2 = -1: by2 = -1: turnF = 0
  14.     WHILE bx2 = -1
  15.         WHILE _MOUSEINPUT: WEND
  16.         mx = _MOUSEX: my = _MOUSEY
  17.         IF _MOUSEBUTTON(1) AND oldMouse = 0 THEN 'new mouse down
  18.             IF bx = -1 THEN bx = mx \ SQ: by = my \ SQ ELSE bx2 = mx \ SQ: by2 = my \ SQ
  19.             'LOCATE 1, 1: PRINT SPACE$(50)
  20.             'LOCATE 1, 1: PRINT bx, by, bx2, by2
  21.             IF bx <> -1 THEN CIRCLE (bx * SQ + 23, by * SQ + 23), 23, &HFFFFFF00
  22.             WHILE legalJump%
  23.                 turnF = -1
  24.                 bx = bx2: by = by2: bx2 = -1: by2 = -1
  25.                 showB
  26.                 yCP 25, "Finish jump(s) by clicking piece again."
  27.                 CIRCLE (bx * SQ + 23, by * SQ + 23), 23, &HFFFFFF00
  28.             WEND
  29.         END IF
  30.         oldMouse = _MOUSEBUTTON(1)
  31.     WEND
  32.     IF turnF = 0 THEN
  33.         IF legalMove% = 0 THEN BEEP ELSE turn = 1 - turn
  34.     ELSE
  35.         turn = 1 - turn
  36.     END IF
  37.     _LIMIT 60
  38.  
  39. SUB showB
  40.     n = 0
  41.     LINE (46, 46)-STEP(400 + 4, 400 + 4), GOLD, BF
  42.     FOR y = 1 TO 8
  43.         FOR x = 1 TO 8
  44.             n = (n + 1) MOD 2
  45.             IF n MOD 2 THEN colore~& = RED ELSE colore~& = BLACK
  46.             LINE (x * SQ, y * SQ)-STEP(SQ - 4, SQ - 4), colore~&, BF
  47.             IF b(x, y) > 0 THEN fcirc x * SQ + 23, y * SQ + 23, 20, RED: fcirc x * SQ + 23, y * SQ + 23, 15, dRed: CIRCLE (x * SQ + 23, y * SQ + 23), 15, GRAY2
  48.             IF b(x, y) > 1 THEN COLOR GOLD, dRed: _PRINTSTRING (x * SQ + 23 - 3, y * SQ + 23 - 8), "K": COLOR WHITE, BLACK
  49.             IF b(x, y) < 0 THEN fcirc x * SQ + 23, y * SQ + 23, 20, GRAY: fcirc x * SQ + 23, y * SQ + 23, 15, dGray: CIRCLE (x * SQ + 23, y * SQ + 23), 15, GRAY2
  50.             IF b(x, y) < -1 THEN COLOR GOLD, dGray: _PRINTSTRING (x * SQ + 23 - 3, y * SQ + 23 - 8), "K": COLOR WHITE, BLACK
  51.         NEXT
  52.         n = (n + 1) MOD 2 '< for even amount of squares in a row
  53.     NEXT
  54.  
  55. SUB loadB
  56.     FOR y = 1 TO 8
  57.         FOR x = 1 TO 8
  58.             n = (n + 1) MOD 2
  59.             IF n MOD 2 = 0 THEN
  60.                 b(x, y) = 1: b(9 - x, 9 - y) = -1
  61.                 i = i + 1
  62.                 IF i = 12 THEN EXIT SUB
  63.             END IF
  64.         NEXT
  65.         n = (n + 1) MOD 2
  66.     NEXT
  67.  
  68. FUNCTION legalMove% 'this is just a move no jumping
  69.     IF bx < 1 OR bx > 8 OR bx2 < 0 OR bx2 > 8 OR by < 1 OR by > 8 OR by2 < 1 OR by2 > 8 THEN EXIT FUNCTION ' in board
  70.     IF b(bx2, by2) = 0 THEN 'must be empty
  71.         IF turn = 0 AND b(bx, by) >= 0 THEN 'black that is neg
  72.             EXIT FUNCTION
  73.         ELSE
  74.             IF b(bx, by) = -1 THEN 'pean
  75.                 IF by - 1 = by2 AND (bx - 1 = bx2 OR bx + 1 = bx2) THEN ELSE EXIT FUNCTION
  76.             ELSEIF b(bx, by) = -2 THEN 'king
  77.                 IF (by - 1 = by2 OR by + 1 = by2) AND (bx - 1 = bx2 OR bx + 1 = bx2) THEN ELSE EXIT FUNCTION
  78.             END IF
  79.         END IF
  80.         IF turn AND b(bx, by) <= 0 THEN 'red
  81.             EXIT FUNCTION
  82.         ELSE
  83.             IF b(bx, by) = 1 THEN 'pean
  84.                 IF by + 1 = by2 AND (bx - 1 = bx2 OR bx + 1 = bx2) THEN ELSE EXIT FUNCTION
  85.             ELSEIF b(bx, by) = 2 THEN 'king
  86.                 IF (by - 1 = by2 OR by + 1 = by2) AND (bx - 1 = bx2 OR bx + 1 = bx2) THEN ELSE EXIT FUNCTION
  87.             END IF
  88.         END IF
  89.         b(bx2, by2) = b(bx, by): b(bx, by) = 0
  90.         IF turn = 0 AND by2 = 1 THEN b(bx2, by2) = -2 'Black King
  91.         IF turn AND by2 = 8 THEN b(bx2, by2) = 2 'Red King
  92.         legalMove% = -1
  93.     END IF
  94.  
  95. FUNCTION legalJump% 'this checks one jump move and removes piece if legal
  96.     IF bx < 1 OR bx > 8 OR bx2 < 0 OR bx2 > 8 OR by < 1 OR by > 8 OR by2 < 1 OR by2 > 8 THEN EXIT FUNCTION ' in board
  97.     IF b(bx2, by2) = 0 THEN 'must be empty
  98.         IF turn = 0 AND b(bx, by) >= 0 THEN 'black that is neg
  99.             EXIT FUNCTION
  100.         ELSE
  101.             IF b(bx, by) = -1 THEN 'Black pean
  102.                 IF by - 2 = by2 AND bx - 2 = bx2 THEN
  103.                     IF b(bx - 1, by - 1) > 0 THEN b(bx - 1, by - 1) = 0 ELSE EXIT FUNCTION
  104.                 ELSEIF by - 2 = by2 AND bx + 2 = bx2 THEN
  105.                     IF b(bx + 1, by - 1) > 0 THEN b(bx + 1, by - 1) = 0 ELSE EXIT FUNCTION
  106.                 ELSE
  107.                     EXIT FUNCTION
  108.                 END IF
  109.             ELSEIF b(bx, by) = -2 THEN 'Black king
  110.                 IF by - 2 = by2 AND bx - 2 = bx2 THEN
  111.                     IF b(bx - 1, by - 1) > 0 THEN b(bx - 1, by - 1) = 0 ELSE EXIT FUNCTION
  112.                 ELSEIF by - 2 = by2 AND bx + 2 = bx2 THEN
  113.                     IF b(bx + 1, by - 1) > 0 THEN b(bx + 1, by - 1) = 0 ELSE EXIT FUNCTION
  114.                 ELSEIF by + 2 = by2 AND bx - 2 = bx2 THEN
  115.                     IF b(bx - 1, by + 1) > 0 THEN b(bx - 1, by + 1) = 0 ELSE EXIT FUNCTION
  116.                 ELSEIF by + 2 = by2 AND bx + 2 = bx2 THEN
  117.                     IF b(bx + 1, by + 1) > 0 THEN b(bx + 1, by + 1) = 0 ELSE EXIT FUNCTION
  118.                 ELSE
  119.                     EXIT FUNCTION
  120.                 END IF
  121.             END IF
  122.         END IF
  123.         IF turn AND b(bx, by) <= 0 THEN 'red
  124.             EXIT FUNCTION
  125.         ELSE
  126.             IF b(bx, by) = 1 THEN 'Red pean
  127.                 IF by + 2 = by2 AND bx - 2 = bx2 THEN
  128.                     IF b(bx - 1, by + 1) < 0 THEN b(bx - 1, by + 1) = 0 ELSE EXIT FUNCTION
  129.                 ELSEIF by + 2 = by2 AND bx + 2 = bx2 THEN
  130.                     IF b(bx + 1, by + 1) < 0 THEN b(bx + 1, by + 1) = 0 ELSE EXIT FUNCTION
  131.                 ELSE
  132.                     EXIT FUNCTION
  133.                 END IF
  134.             ELSEIF b(bx, by) = 2 THEN 'Red king
  135.                 IF by - 2 = by2 AND bx - 2 = bx2 THEN
  136.                     IF b(bx - 1, by - 1) < 0 THEN b(bx - 1, by - 1) = 0 ELSE EXIT FUNCTION
  137.                 ELSEIF by - 2 = by2 AND bx + 2 = bx2 THEN
  138.                     IF b(bx + 1, by - 1) < 0 THEN b(bx + 1, by - 1) = 0 ELSE EXIT FUNCTION
  139.                 ELSEIF by + 2 = by2 AND bx - 2 = bx2 THEN
  140.                     IF b(bx - 1, by + 1) < 0 THEN b(bx - 1, by + 1) = 0 ELSE EXIT FUNCTION
  141.                 ELSEIF by + 2 = by2 AND bx + 2 = bx2 THEN
  142.                     IF b(bx + 1, by + 1) < 0 THEN b(bx + 1, by + 1) = 0 ELSE EXIT FUNCTION
  143.                 ELSE
  144.                     EXIT FUNCTION
  145.                 END IF
  146.             END IF
  147.         END IF
  148.         b(bx2, by2) = b(bx, by): b(bx, by) = 0
  149.         IF turn = 0 AND by2 = 1 THEN b(bx2, by2) = -2 'Black King
  150.         IF turn AND by2 = 8 THEN b(bx2, by2) = 2 'Red King
  151.         legalJump% = -1
  152.     END IF
  153.  
  154. 'from Steve Gold standard
  155. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  156.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  157.     DIM X AS INTEGER, Y AS INTEGER
  158.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  159.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  160.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  161.     WHILE X > Y
  162.         RadiusError = RadiusError + Y * 2 + 1
  163.         IF RadiusError >= 0 THEN
  164.             IF X <> Y + 1 THEN
  165.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  166.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  167.             END IF
  168.             X = X - 1
  169.             RadiusError = RadiusError - X * 2
  170.         END IF
  171.         Y = Y + 1
  172.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  173.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  174.     WEND
  175.  
  176. SUB yCP (y, s$) 'for xmax pixel wide graphics screen Center Print at pixel y row
  177.     _PRINTSTRING ((_WIDTH - LEN(s$) * 8) / 2, y), s$
  178.  
Checkers.PNG
* Checkers.PNG (Filesize: 20.85 KB, Dimensions: 504x532, Views: 190)
« Last Edit: February 03, 2020, 10:39:56 pm by bplus »

Offline dajan

  • Newbie
  • Posts: 41
    • View Profile
Re: Checkers with Mouse
« Reply #1 on: February 04, 2020, 03:00:44 am »
Nice one, bplus. How can I play against computer?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Checkers with Mouse
« Reply #2 on: February 04, 2020, 12:24:38 pm »
Nice one, bplus. How can I play against computer?

Working on that one. Just wanted to rough out the game first.

Think I need to modify my legalxxx subs, to give only an array of legal moves that can be made from each position and kind of stuck if I need to say whose turn it is with the sub. Don't need a bunch of subs for legal moves for every different piece since there is only one kind but the multiple jumping thing is most different from chess.

Just learned a new toggle today from SmallBASIC thread at Syntax Bomb:
Code: QB64: [Select]
  1. FOR i = 1 TO 10
  2.     j = NOT j
  3.     PRINT j
  4.  
That will be useful for whose turn it is in recursive subs.

Probably will use Mem routine for copying board, need sub for counting pieces, what else?
This is new territory for me.

Ha! If you can't wait, there is plenty of games on Internet I found last night.
« Last Edit: February 04, 2020, 12:25:59 pm by bplus »

Offline dajan

  • Newbie
  • Posts: 41
    • View Profile
Re: Checkers with Mouse
« Reply #3 on: February 04, 2020, 05:24:37 pm »
Well, you can allways start with programming the difficulty level "I'm too young to lose", where the computer just makes random legal moves, unless it has a chance to jump over your stone. That I would be able to beat at least :o)  Good luck.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Checkers with Mouse
« Reply #4 on: February 04, 2020, 05:29:52 pm »
Yeah, at least review all possible replies to a possible move to avoid completely random moves and bonehead mistakes.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Checkers with Mouse
« Reply #5 on: February 04, 2020, 06:59:12 pm »
Awesome job B+! I wish I could have made mine that good. I did a whole game but it doesn't finish. What I did with mine was check on every move to see if there's any of either the red pieces or black pieces left on the board to see if the game is finished and who wins, using POINT. It's just 2 quick loops in each other scanning the whole screen. Just an idea.
« Last Edit: February 04, 2020, 07:00:44 pm by SierraKen »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Checkers with Mouse
« Reply #6 on: February 04, 2020, 08:16:04 pm »
Someone double check  - but I just made a double-jump with a king, and while the second piece disappeared, the king stated on the middle square.
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Checkers with Mouse
« Reply #7 on: February 04, 2020, 08:29:28 pm »
Someone double check  - but I just made a double-jump with a king, and while the second piece disappeared, the king stated on the middle square.
stayed on middle square? I will check:

Start game with 4 Kings:
Code: QB64: [Select]
  1. SUB loadB
  2.     FOR y = 1 TO 8
  3.         FOR x = 1 TO 8
  4.             n = (n + 1) MOD 2
  5.             IF n MOD 2 = 0 THEN
  6.                 b(x, y) = 2: b(9 - x, 9 - y) = -2
  7.                 i = i + 1
  8.                 IF i = 4 THEN EXIT SUB
  9.             END IF
  10.         NEXT
  11.         n = (n + 1) MOD 2
  12.     NEXT
  13.  

test dbl jump twice on Black, here is 2nd round
dbl jump p1.PNG
* dbl jump p1.PNG (Filesize: 17.55 KB, Dimensions: 499x522, Views: 182)
dbl jump p2.PNG
* dbl jump p2.PNG (Filesize: 19.52 KB, Dimensions: 504x529, Views: 191)
« Last Edit: February 05, 2020, 01:01:33 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Checkers with Mouse
« Reply #8 on: February 04, 2020, 08:34:31 pm »
continue until it's Black's turn but where is Black? ;-)) 

The 4th frame is when I click the piece where it is to signal finish of the move, highlight is then turned off.

EDIT adding this paragraph: OK, I've tried to replicate or find a bug with King jumping and haven't managed to get one. I was concerned when you have to click the piece a final time to signal the end of jumping that the piece might be moved again which is why the opponent's piece gets removed immediately so that a jump back is not possible. I think the only possible thing that could happen is another legal jump and again you would need a final click for the turn to pass to the other player.

Still need to get a sub that will tell when the game is over. :)

I just played a game with another checkers program and there you are required to jump a piece if a jump exits, it won't let you move another piece.
dbl jump p3.PNG
* dbl jump p3.PNG (Filesize: 19.25 KB, Dimensions: 503x527, Views: 183)
dbl jump p4.PNG
* dbl jump p4.PNG (Filesize: 18.51 KB, Dimensions: 505x529, Views: 174)
« Last Edit: February 05, 2020, 01:09:39 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Checkers with Mouse
« Reply #9 on: February 05, 2020, 12:58:21 am »
Awesome job B+! I wish I could have made mine that good. I did a whole game but it doesn't finish. What I did with mine was check on every move to see if there's any of either the red pieces or black pieces left on the board to see if the game is finished and who wins, using POINT. It's just 2 quick loops in each other scanning the whole screen. Just an idea.

Hey Ken, thanks for compliment. You know the title of your checkers program was helpful for me dividing the job of taking a turn into moving or jumping. It made sense to handle them independently because it's one or the other. Then the jumping, that was a stumper, still is. Not real happy having to signal the end of jumping with another click, I can see why you restricted your program to 1 jump. Big holdup on how to handle that for an AI player, don't see the way yet. And then there is a new question about forced jumping. Sure if you can jump 3 of your opponents pieces you'd go for it but should more than 1 jump be forced? What if you can jump one piece here and 3 there, are you forced to do the 3? (Is it possible to have 2 different jumping opportunities at once?)

Game ending is really easy just count the pieces left, I would just count them off the b for board array, not from POINT. But if a player can't move when it's his turn, that is also a defeat, a much harder condition to detect until start work on AI code.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Checkers with Mouse
« Reply #10 on: February 06, 2020, 02:41:20 pm »
LOL true I totally forgot about the possibility of being blocked with 1 left. I used to have a checkers game for Windows 98 ages ago so it is still possible. Although I'm sure they didn't use a BASIC language. :D

Marked as best answer by bplus on February 15, 2020, 05:28:40 pm

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Checkers with Mouse
« Reply #11 on: February 15, 2020, 10:24:46 pm »
Ok you can practice Forced Jumps Checkers, no more clicking piece to signal you are done jumping:
Code: QB64: [Select]
  1. _TITLE "Checkers Force Jump 2020-02-15" 'b+ start from checkers.bas 2020-02-03
  2. ' 2020-02-06 Checkers Force Jump.bas modify to enforce Forced jumps
  3. ' add sub MoveList:
  4. '    modify legalMove and legalJump to input start x, y and end x, y and whose turn it is and copy of board?
  5. ' for turn set constants Red and Black, oops content to just know Black's turn when = 0, Red's turn = 1.
  6. ' move sub << yes legalMove and one for jumping legalJump, if no jump then check move.
  7. ' copyArray sub - done << oops nope the mem copy works only with lists, 1 dim arrays. May not need it anyway.
  8.  
  9. ' 2020-02-13  create 2 types may only use MOVE type, the other is std XY type.
  10. ' Added moveList sub to create list of moves available and to begin forced jump enforcement.
  11. ' Now using that to determine a winner when no moves are available. The win function and it's use commented out.
  12. ' So far the only way to get the new sub moveList to work is to copy the board array b() before using it and then
  13. ' copy the saved copy back to board array after calling because something(s) in b() are getting lost but I don't see
  14. ' how??? Because nothing in the new sub or the subs it calls alters the b(). << wrong! 2020-02-14 I still had
  15. ' legalJump removing the piece! Dang! should have doubled checked on 2/13.
  16.  
  17. ' 2020-02-14 alter moveList sub to copy the board array, then check the board array after certain stages of
  18. ' it's code to see if can find what is causing the change in the b(). Aha! LegalJump WAS changing the b()!!!
  19. ' Reworked legalJump and legalMove to more efficient code? certainly much less lines and seem to still work.
  20. ' Test new legalXXX subs with moveList. Cleanup mess of finding bug.  Only MOVE Type left.
  21.  
  22. ' 2020-02-15 OK I think I have an idea to eliminate having to click the piece to signal the end of a move (if jumping has occurred).
  23. ' I will create a jumpAvailable function that takes a X, Y position and turn and just sees if a legal Jump can be found.
  24. ' Should be a piece of cake, just copy paste a little piece from the listMoves sub. I've been considering breaking that sub into
  25. ' pieces for using as needed for AI. So far my future thinking for AI has been to avoid having to keep and pass copies
  26. ' of the boards in various states, using the board array and one copy hopefully will be all that will be needed when evaluating
  27. ' moves for the future AI. More checks and beeps with clicks.
  28.  
  29.  
  30. DEFINT A-Z
  31. CONST xmax = 500, ymax = 500, SQ = 50
  32. 'for checkers
  33. CONST WHITE = &HFFFFFFFF, GOLD = &HFFFF9900, BLACK = &HFF000000, GRAY = &HFF444444, GRAY2 = &HFF999999, RED = &HFFFF0000
  34. CONST dRed = &HFFDD0000, dGray = &HFF222222
  35. TYPE MOVE
  36.     SX AS INTEGER
  37.     SY AS INTEGER
  38.     EX AS INTEGER
  39.     EY AS INTEGER
  40. DIM SHARED b(1 TO 8, 1 TO 8) AS INTEGER
  41. SCREEN _NEWIMAGE(xmax, ymax, 32)
  42. REDIM mList(0) AS MOVE
  43. restart:
  44. turn = 0
  45. loadB
  46. showB
  47.     CLS
  48.     listMoves turn, mList(), forceJump 'analyse board with listMoves  and call win or force jump for next player up
  49.     'see the how list is coming, it looks great
  50.     'FOR i = LBOUND(mList) TO UBOUND(mList)
  51.     '    PRINT i, mList(i).SX; ","; mList(i).SY; ","; mList(i).EX; ","; mList(i).EY
  52.     'NEXT
  53.     'INPUT "OK ..."; w$
  54.     'CLS
  55.     showB
  56.     IF UBOUND(mList) = 0 THEN 'no move possible, player lost
  57.         IF turn THEN
  58.             yCP 1, "Red has no move, Black Wins!"
  59.         ELSE
  60.             yCP 1, "Black has no move, Red Wins!"
  61.         END IF
  62.         _DELAY 4
  63.         GOTO restart
  64.     ELSEIF forceJump THEN 'is there a forced jump?
  65.         IF turn THEN yCP 1, "Red's turn and must jump." ELSE yCP 1, "Black's turn and must jump."
  66.     ELSE
  67.         IF turn THEN yCP 1, "Red's turn" ELSE yCP 1, "Black's turn"
  68.     END IF
  69.  
  70.     bx = -1: by = -1: bx2 = -1: by2 = -1: turnF = 0
  71.     WHILE bx2 = -1
  72.         IF bx <> -1 THEN CIRCLE (bx * SQ + 23, by * SQ + 23), 23, &HFFFFFF00 'highlite
  73.         WHILE _MOUSEINPUT: WEND
  74.         mx = _MOUSEX \ SQ: my = _MOUSEY \ SQ
  75.         IF _MOUSEBUTTON(1) AND oldMouse = 0 THEN 'new mouse down
  76.             IF mx > 0 AND mx < 9 AND my > 0 AND my < 9 THEN
  77.                 IF bx = -1 THEN
  78.                     IF forceJump THEN 'ONLY highlite a piece that can jump
  79.                         IF jumpAvailable(mx, my, turn) THEN bx = mx: by = my
  80.                     ELSE 'could check to see that bx, by has a piece whose color matches turn
  81.                         IF (turn AND b(mx, my) > 0) OR (turn = 0 AND b(mx, my) < 0) THEN bx = mx: by = my ELSE BEEP
  82.                     END IF 'force Jump or not
  83.                 ELSE
  84.                     IF b(mx, my) = 0 THEN bx2 = mx: by2 = my ELSE BEEP
  85.                 END IF '1st click or 2nd
  86.                 'LOCATE 1, 1: PRINT SPACE$(50)      'check our mouse position to borad
  87.                 'LOCATE 1, 1: PRINT bx, by, bx2, by2
  88.                 WHILE legalJump(bx, by, bx2, by2, turn) 'if legal jump allow for more jumps
  89.                     MovePiece bx, by, bx2, by2, turn
  90.                     turnF = -1
  91.                     IF jumpAvailable(bx2, by2, turn) THEN 'forceJump still in effect
  92.                         bx = bx2: by = by2: bx2 = -1: by2 = -1
  93.                         yCP 25, "Another jump to do..."
  94.                         showB
  95.                         CIRCLE (bx * SQ + 23, by * SQ + 23), 23, &HFFFFFF00
  96.                     ELSE 'done here
  97.                         showB
  98.                         forceJump = 0 'set flags that a jump move has been made
  99.                         EXIT WHILE 'bx2 is not -1 so will exit next while loop after
  100.                     END IF
  101.                 WEND
  102.             ELSE
  103.                 BEEP
  104.             END IF 'mouse click inside b() bounds
  105.         END IF 'click
  106.         oldMouse = _MOUSEBUTTON(1)
  107.         _LIMIT 100
  108.     WEND
  109.     IF forceJump THEN
  110.         BEEP
  111.     ELSE
  112.         IF turnF = 0 THEN
  113.             IF legalMove(bx, by, bx2, by2, turn) = 0 THEN BEEP ELSE MovePiece bx, by, bx2, by2, turn: turn = 1 - turn
  114.         ELSE
  115.             turn = 1 - turn
  116.         END IF
  117.     END IF
  118.     _LIMIT 60
  119.  
  120. SUB MovePiece (bbx, bby, newX, newY, turn) 'This is supposed to be the only routine that changes the b() array assume all has been prechecked!
  121.     IF legalJump(bbx, bby, newX, newY, turn) THEN 'jump remove piece in between
  122.         mx = (bbx + newX) / 2: my = (bby + newY) / 2
  123.         b(mx, my) = 0
  124.     END IF
  125.     b(newX, newY) = b(bbx, bby): b(bbx, bby) = 0
  126.     IF turn = 0 AND newY = 1 THEN b(newX, newY) = -2 'Black King
  127.     IF turn AND newY = 8 THEN b(newX, newY) = 2 'Red King
  128.  
  129. SUB listMoves (turn, L() AS MOVE, jF) 'stop making list if jumps are found
  130.     jF = 0 'jump Flag, reset in case set previously and reuse variable is jump Flag
  131.     REDIM L(0) AS MOVE 'also reset in case used previously
  132.  
  133.     FOR y = 1 TO 8 'scan board for legal jumps and list all found and set JF
  134.         FOR x = 1 TO 8
  135.             IF b(x, y) > 0 THEN 'red's
  136.                 IF turn THEN 'LL potential
  137.                     FOR y2 = y - 2 TO y + 2 STEP 4
  138.                         FOR x2 = x - 2 TO x + 2 STEP 4
  139.                             IF legalJump(x, y, x2, y2, turn) THEN
  140.                                 jF = -1
  141.                                 li = li + 1
  142.                                 REDIM _PRESERVE L(li) AS MOVE
  143.                                 L(li).SX = x
  144.                                 L(li).SY = y
  145.                                 L(li).EX = x2
  146.                                 L(li).EY = y2
  147.                             END IF
  148.                         NEXT
  149.                     NEXT
  150.                 END IF
  151.             ELSEIF b(x, y) < 0 THEN 'black's
  152.                 IF turn = 0 THEN
  153.                     FOR y2 = y - 2 TO y + 2 STEP 4
  154.                         FOR x2 = x - 2 TO x + 2 STEP 4
  155.                             IF legalJump(x, y, x2, y2, turn) THEN
  156.                                 jF = -1
  157.                                 li = li + 1
  158.                                 REDIM _PRESERVE L(li) AS MOVE
  159.                                 L(li).SX = x
  160.                                 L(li).SY = y
  161.                                 L(li).EX = x2
  162.                                 L(li).EY = y2
  163.                             END IF
  164.                         NEXT
  165.                     NEXT
  166.                 END IF
  167.             END IF
  168.         NEXT
  169.     NEXT
  170.     IF jF = 0 THEN 'no moves to force  so go over board again for regular moves
  171.         FOR y = 1 TO 8
  172.             FOR x = 1 TO 8
  173.                 IF b(x, y) > 0 THEN 'red
  174.                     IF turn THEN ' potential piece to move
  175.                         FOR y2 = y - 1 TO y + 1 STEP 2
  176.                             FOR x2 = x - 1 TO x + 1 STEP 2
  177.                                 IF legalMove(x, y, x2, y2, turn) THEN
  178.                                     li = li + 1
  179.                                     REDIM _PRESERVE L(li) AS MOVE
  180.                                     L(li).SX = x
  181.                                     L(li).SY = y
  182.                                     L(li).EX = x2
  183.                                     L(li).EY = y2
  184.                                 END IF
  185.                             NEXT
  186.                         NEXT
  187.                     END IF
  188.                 ELSEIF b(x, y) < 0 THEN 'black
  189.                     IF turn = 0 THEN ' potential piece to move
  190.                         FOR y2 = y - 1 TO y + 1 STEP 2
  191.                             FOR x2 = x - 1 TO x + 1 STEP 2
  192.                                 IF legalMove(x, y, x2, y2, turn) THEN
  193.                                     li = li + 1
  194.                                     REDIM _PRESERVE L(li) AS MOVE
  195.                                     L(li).SX = x
  196.                                     L(li).SY = y
  197.                                     L(li).EX = x2
  198.                                     L(li).EY = y2
  199.                                 END IF
  200.                             NEXT
  201.                         NEXT
  202.                     END IF
  203.                 END IF
  204.             NEXT
  205.         NEXT
  206.     END IF
  207.  
  208. FUNCTION jumpAvailable (x, y, turn) ' 0 or -1 a jump is possoble or not
  209.     IF b(x, y) > 0 AND turn THEN 'red piece and red's turn
  210.         FOR y2 = y - 2 TO y + 2 STEP 4
  211.             FOR x2 = x - 2 TO x + 2 STEP 4
  212.                 IF legalJump(x, y, x2, y2, turn) THEN jumpAvailable = -1: EXIT FUNCTION
  213.             NEXT
  214.         NEXT
  215.     ELSEIF b(x, y) < 0 AND turn = 0 THEN 'black's piece and turn
  216.         FOR y2 = y - 2 TO y + 2 STEP 4
  217.             FOR x2 = x - 2 TO x + 2 STEP 4
  218.                 IF legalJump(x, y, x2, y2, turn) THEN jumpAvailable = -1: EXIT FUNCTION
  219.             NEXT
  220.         NEXT
  221.     END IF
  222.  
  223. FUNCTION legalMove (bx, by, bx2, by2, turn) 'this is just a move no jumping
  224.     IF bx < 1 OR bx > 8 OR bx2 < 1 OR bx2 > 8 OR by < 1 OR by > 8 OR by2 < 1 OR by2 > 8 THEN EXIT FUNCTION ' in board
  225.     IF b(bx2, by2) = 0 THEN 'must be empty
  226.         IF turn = 0 THEN
  227.             IF b(bx, by) = -1 THEN 'pean
  228.                 IF by - 1 = by2 AND (bx - 1 = bx2 OR bx + 1 = bx2) THEN legalMove = -1
  229.             ELSEIF b(bx, by) = -2 THEN 'king
  230.                 IF (by - 1 = by2 OR by + 1 = by2) AND (bx - 1 = bx2 OR bx + 1 = bx2) THEN legalMove = -1
  231.             END IF
  232.         ELSE 'turn = 1
  233.             IF b(bx, by) = 1 THEN 'pean
  234.                 IF by + 1 = by2 AND (bx - 1 = bx2 OR bx + 1 = bx2) THEN legalMove = -1
  235.             ELSEIF b(bx, by) = 2 THEN 'king
  236.                 IF (by - 1 = by2 OR by + 1 = by2) AND (bx - 1 = bx2 OR bx + 1 = bx2) THEN legalMove = -1
  237.             END IF
  238.         END IF
  239.     END IF
  240.  
  241. FUNCTION legalJump (bx, by, bx2, by2, turn) 'this checks only one jump
  242.     IF bx < 1 OR bx > 8 OR bx2 < 1 OR bx2 > 8 OR by < 1 OR by > 8 OR by2 < 1 OR by2 > 8 THEN EXIT FUNCTION ' in board
  243.     IF b(bx2, by2) = 0 THEN 'must be empty
  244.         IF turn = 0 THEN 'black's move
  245.             IF b(bx, by) = -1 THEN 'Black pean
  246.                 IF by - 2 = by2 AND bx - 2 = bx2 THEN 'must jump red player
  247.                     IF b(bx - 1, by - 1) > 0 THEN legalJump = -1
  248.                 ELSEIF by - 2 = by2 AND bx + 2 = bx2 THEN
  249.                     IF b(bx + 1, by - 1) > 0 THEN legalJump = -1
  250.                 END IF
  251.             ELSEIF b(bx, by) = -2 THEN 'Black king
  252.                 IF by - 2 = by2 AND bx - 2 = bx2 THEN
  253.                     IF b(bx - 1, by - 1) > 0 THEN legalJump = -1
  254.                 ELSEIF by - 2 = by2 AND bx + 2 = bx2 THEN
  255.                     IF b(bx + 1, by - 1) > 0 THEN legalJump = -1
  256.                 ELSEIF by + 2 = by2 AND bx - 2 = bx2 THEN
  257.                     IF b(bx - 1, by + 1) > 0 THEN legalJump = -1
  258.                 ELSEIF by + 2 = by2 AND bx + 2 = bx2 THEN
  259.                     IF b(bx + 1, by + 1) > 0 THEN legalJump = -1
  260.                 END IF
  261.             END IF
  262.         ELSE 'red's move
  263.             IF b(bx, by) = 1 THEN 'Red pean
  264.                 IF by + 2 = by2 AND bx - 2 = bx2 THEN
  265.                     IF b(bx - 1, by + 1) < 0 THEN legalJump = -1
  266.                 ELSEIF by + 2 = by2 AND bx + 2 = bx2 THEN
  267.                     IF b(bx + 1, by + 1) < 0 THEN legalJump = -1
  268.                 END IF
  269.             ELSEIF b(bx, by) = 2 THEN 'Red king
  270.                 IF by - 2 = by2 AND bx - 2 = bx2 THEN
  271.                     IF b(bx - 1, by - 1) < 0 THEN legalJump = -1
  272.                 ELSEIF by - 2 = by2 AND bx + 2 = bx2 THEN
  273.                     IF b(bx + 1, by - 1) < 0 THEN legalJump = -1
  274.                 ELSEIF by + 2 = by2 AND bx - 2 = bx2 THEN
  275.                     IF b(bx - 1, by + 1) < 0 THEN legalJump = -1
  276.                 ELSEIF by + 2 = by2 AND bx + 2 = bx2 THEN
  277.                     IF b(bx + 1, by + 1) < 0 THEN legalJump = -1
  278.                 END IF
  279.             END IF
  280.         END IF
  281.     END IF
  282.  
  283. SUB showB
  284.     n = 0
  285.     LINE (46, 46)-STEP(400 + 4, 400 + 4), GOLD, BF
  286.     FOR y = 1 TO 8
  287.         FOR x = 1 TO 8
  288.             n = (n + 1) MOD 2
  289.             IF n MOD 2 THEN colore~& = RED ELSE colore~& = BLACK
  290.             LINE (x * SQ, y * SQ)-STEP(SQ - 4, SQ - 4), colore~&, BF
  291.             IF b(x, y) > 0 THEN fcirc x * SQ + 23, y * SQ + 23, 20, RED: fcirc x * SQ + 23, y * SQ + 23, 15, dRed: CIRCLE (x * SQ + 23, y * SQ + 23), 15, GRAY2
  292.             IF b(x, y) > 1 THEN COLOR GOLD, dRed: _PRINTSTRING (x * SQ + 23 - 3, y * SQ + 23 - 8), "K": COLOR WHITE, BLACK
  293.             IF b(x, y) < 0 THEN fcirc x * SQ + 23, y * SQ + 23, 20, GRAY: fcirc x * SQ + 23, y * SQ + 23, 15, dGray: CIRCLE (x * SQ + 23, y * SQ + 23), 15, GRAY2
  294.             IF b(x, y) < -1 THEN COLOR GOLD, dGray: _PRINTSTRING (x * SQ + 23 - 3, y * SQ + 23 - 8), "K": COLOR WHITE, BLACK
  295.         NEXT
  296.         n = (n + 1) MOD 2
  297.     NEXT
  298.  
  299. SUB loadB
  300.     ERASE b
  301.     FOR y = 1 TO 8
  302.         FOR x = 1 TO 8
  303.             n = (n + 1) MOD 2
  304.             IF n MOD 2 = 0 THEN
  305.                 b(x, y) = 1: b(9 - x, 9 - y) = -1
  306.                 i = i + 1
  307.                 IF i = 12 THEN EXIT SUB
  308.             END IF
  309.         NEXT
  310.         n = (n + 1) MOD 2
  311.     NEXT
  312.  
  313. SUB fcirc (CX AS INTEGER, CY AS INTEGER, R AS INTEGER, C AS _UNSIGNED LONG)
  314.     DIM Radius AS INTEGER, RadiusError AS INTEGER
  315.     DIM X AS INTEGER, Y AS INTEGER
  316.     Radius = ABS(R): RadiusError = -Radius: X = Radius: Y = 0
  317.     IF Radius = 0 THEN PSET (CX, CY), C: EXIT SUB
  318.     LINE (CX - X, CY)-(CX + X, CY), C, BF
  319.     WHILE X > Y
  320.         RadiusError = RadiusError + Y * 2 + 1
  321.         IF RadiusError >= 0 THEN
  322.             IF X <> Y + 1 THEN
  323.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), C, BF
  324.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), C, BF
  325.             END IF
  326.             X = X - 1
  327.             RadiusError = RadiusError - X * 2
  328.         END IF
  329.         Y = Y + 1
  330.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), C, BF
  331.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), C, BF
  332.     WEND
  333.  
  334. SUB yCP (y, s$) 'Center Print at pixel y row
  335.     _PRINTSTRING ((_WIDTH - LEN(s$) * 8) / 2, y), s$
  336.  
  337.  
  338.  

Offline gaslouk

  • Newbie
  • Posts: 29
    • View Profile
Re: Checkers with Mouse
« Reply #12 on: February 16, 2020, 12:02:11 am »
Very good effort, many more corrections still needed.
Congratulations.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Checkers with Mouse
« Reply #13 on: February 16, 2020, 12:45:45 am »
Very good effort, many more corrections still needed.
Congratulations.

Thanks :) I think, "many"?

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Checkers with Mouse
« Reply #14 on: February 16, 2020, 12:41:26 pm »
I like that one, even though I've never been much of a checkers player, it is a great presentation.