Author Topic: Flip It  (Read 6293 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Flip It
« Reply #15 on: November 05, 2019, 04:42:07 pm »
Here’s the video where I first say the lady explaining this little exercise:
&t=381s

Oh man! my strategy for play for guaranteed win is S+0! much simpler than that (hers in the video).

I am pretty sure it will never fail PLUS it has a symmetric opposite.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Flip It
« Reply #16 on: November 05, 2019, 09:27:49 pm »
Flip It Squared, same read, same guaranteed strategy for success when possible, only now it is symmetric 4 ways:
Code: QB64: [Select]
  1. _TITLE "Flip It Squared" 'b+ started 2019-11-05
  2. ' inspired by Steve's recent QB64 thread:
  3. ' https://www.qb64.org/forum/index.php?topic=1831.msg110717#msg110717
  4. ' from Quick Flip It experiments, add another dimemsion for complexity.
  5.  
  6. 'Objective: to remove all cards from board
  7. 'Play: can only remove face up cards (lighter color), cards adjacent to removed card will be toggled up/down (dark/light).
  8.  
  9. CONST across = 10, down = 7, sqSide = 50, sqSpace = 60, marg = 30 'should be enough to get the gist of what is going on
  10. CONST cardUp = &HFFBBBBBB, cardDown = &HFF000066, cardOut = &HFF000000
  11. xmax = across * sqSpace + 2 * marg: ymax = down * sqSpace + 2 * marg
  12.  
  13. DIM SHARED state(1 TO across, 1 TO down) AS INTEGER, gameover AS INTEGER, ups AS INTEGER, downs AS INTEGER
  14. SCREEN _NEWIMAGE(xmax, ymax, 32)
  15. _SCREENMOVE 300, 100
  16. gameover = 1
  17. WHILE _KEYDOWN(27) = 0
  18.     IF gameover THEN initRound
  19.     c = 0: r = 0
  20.     getCardClicked c, r 'this card is removed and cards adjacent (if any) are toggled up/down
  21.     IF c > 0 AND r > 0 THEN
  22.         LOCATE 1, 1: PRINT c, r
  23.         IF state(c, r) = 1 THEN
  24.             IF c > 1 THEN
  25.                 IF state(c - 1, r) <> -1 THEN state(c - 1, r) = 1 - state(c - 1, r)
  26.             END IF
  27.             IF c < across THEN
  28.                 IF state(c + 1, r) <> -1 THEN state(c + 1, r) = 1 - state(c + 1, r)
  29.             END IF
  30.             IF r > 1 THEN
  31.                 IF state(c, r - 1) <> -1 THEN state(c, r - 1) = 1 - state(c, r - 1)
  32.             END IF
  33.             IF r < down THEN
  34.                 IF state(c, r + 1) <> -1 THEN state(c, r + 1) = 1 - state(c, r + 1)
  35.             END IF
  36.             state(c, r) = -1
  37.         END IF
  38.     END IF
  39.     updateScreen
  40.     _DISPLAY
  41.     _LIMIT 60
  42.  
  43. SUB getCardClicked (col AS INTEGER, row AS INTEGER)
  44.     DIM mb AS INTEGER, m AS INTEGER, mx AS INTEGER, my AS INTEGER
  45.     mb = _MOUSEBUTTON(1) '            left button down
  46.     IF mb THEN '                      get last place mouse button was down
  47.         WHILE mb '                    wait for mouse button release as a "click"
  48.             m = _MOUSEINPUT: mb = _MOUSEBUTTON(1): mx = _MOUSEX: my = _MOUSEY
  49.         WEND
  50.         FOR row = 1 TO down
  51.             FOR col = 1 TO across
  52.                 IF mx > (col - 1) * sqSpace + marg AND mx < (col - 1) * sqSpace + marg + sqSide THEN
  53.                     IF my > (row - 1) * sqSpace + marg AND my < (row - 1) * sqSpace + marg + sqSide THEN
  54.                         EXIT SUB 'found it
  55.                     END IF
  56.                 END IF
  57.             NEXT
  58.         NEXT
  59.         col = 0: row = 0
  60.     END IF
  61.  
  62. SUB updateScreen 'and count the ups and downs of cards
  63.     DIM ups AS INTEGER, downs AS INTEGER, y AS INTEGER, x AS INTEGER, clr~&
  64.     CLS
  65.     ups = 0: downs = 0 'count cards while displaying
  66.     FOR y = 1 TO down
  67.         FOR x = 1 TO across
  68.             IF state(x, y) = 1 THEN
  69.                 ups = ups + 1: clr~& = cardUp
  70.             ELSEIF state(x, y) = 0 THEN
  71.                 downs = downs + 1: clr~& = cardDown
  72.             ELSEIF state(x, y) = -1 THEN
  73.                 clr~& = cardOut
  74.             END IF
  75.             LINE ((x - 1) * sqSpace + marg, (y - 1) * sqSpace + marg)-STEP(sqSide, sqSide), clr~&, BF
  76.         NEXT
  77.     NEXT
  78.     yCP 10, SPACE$(60): yCP 10, "Ups, Downs: " + TS$(ups) + ", " + TS$(downs)
  79.     'check win
  80.     IF ups = 0 THEN
  81.         gameover = 1
  82.         IF downs > 0 THEN
  83.             yCP 10, SPACE$(60): yCP 10, "No win"
  84.         ELSE
  85.             yCP 10, SPACE$(60): yCP 10, "Win"
  86.         END IF
  87.         _DISPLAY: SLEEP 5
  88.     END IF
  89.  
  90. SUB initRound 'reassign letters and hide them all
  91.     DIM x AS INTEGER, y AS INTEGER
  92.     FOR y = 1 TO down
  93.         FOR x = 1 TO across
  94.             IF RND < .5 THEN state(x, y) = 1 ELSE state(x, y) = 0
  95.         NEXT
  96.     NEXT
  97.     gameover = 0: updateScreen
  98.  
  99. SUB yCP (y, s$) 'for xmax pixel wide graphics screen Center Print at pixel y row
  100.     _PRINTSTRING ((_WIDTH - LEN(s$) * 8) / 2, y), s$
  101.  
  102.     TS$ = _TRIM$(STR$(n))
  103.  
  104.  
« Last Edit: November 05, 2019, 09:30:24 pm by bplus »