Author Topic: 21_Game Rosetta Code  (Read 3983 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
21_Game Rosetta Code
« on: August 09, 2020, 10:20:56 pm »
Hurry and play and win! before I figure out how to make AI smarter!
Code: QB64: [Select]
  1. DEFINT A-Z
  2. _TITLE "21_Game from Rosetta Code" 'b+ just saw this at Rosetta 2020-08-09 today
  3. ' ref:   http://rosettacode.org/wiki/21_Game
  4.  
  5. ' Take turns adding 1, 2, or 3 until someone makes the total = 21, they are winner.
  6.  
  7.     Total = 0 'reset
  8.     PRINT "21_Game Rosetta Code:" 'title for total
  9.     LOCATE 1, 60: PRINT "Running Total:"; Total
  10.     IF RND < .5 THEN human = 1 ELSE human = 0 'who goes first
  11.     DO
  12.         human = 1 - human 'toggle player
  13.         IF human THEN
  14.             getHuman: 'in case human plays 22, 23...
  15.             INPUT "Human: enter 1, 2, 3 for your turn, any other quits > "; add
  16.             IF add < 1 OR add > 3 THEN PRINT "Human quits, goodbye!": END
  17.             IF add + Total > 21 THEN PRINT "Too much to add, try again...": GOTO getHuman
  18.         ELSE 'ai turn
  19.             IF Total >= 18 THEN
  20.                 add = 21 - Total
  21.             ELSEIF Total = 17 THEN 'lost
  22.                 add = 1
  23.             ELSEIF Total = 16 THEN
  24.                 add = 1
  25.             ELSEIF Total = 15 THEN
  26.                 add = 2
  27.             ELSEIF Total = 14 THEN
  28.                 add = 3
  29.             ELSE
  30.                 add = 3 ' AI not that smart ;(
  31.             END IF
  32.         END IF
  33.         IF human THEN LOCATE CSRLIN - 1, 60 ELSE LOCATE CSRLIN, 60
  34.         IF human THEN PRINT "    Human adds "; add ELSE PRINT "       AI adds "; add
  35.         Total = Total + add
  36.         LOCATE CSRLIN, 60: PRINT "Running Total:"; Total
  37.         IF Total = 21 THEN
  38.             LOCATE CSRLIN, 60
  39.             IF human THEN PRINT "*** Human wins ***" ELSE PRINT " *** AI wins ***"
  40.             EXIT DO
  41.         END IF
  42.     LOOP
  43.     PRINT "Press any for next game..."
  44.     SLEEP
  45.     CLS
  46.     _KEYCLEAR
  47.  
« Last Edit: August 09, 2020, 10:22:16 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: 21_Game Rosetta Code
« Reply #1 on: August 09, 2020, 11:35:33 pm »
I suspect this is another version of NIM. :)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: 21_Game Rosetta Code
« Reply #2 on: August 10, 2020, 04:42:34 am »
OK this should be harder to beat:
Code: QB64: [Select]
  1. DEFINT A-Z
  2. _TITLE "21_Game from Rosetta Code" 'b+ saw this at Roseetta 2020-08-09
  3. ' ref:   http://rosettacode.org/wiki/21_Game
  4. ' 2020-08-10 stronger AI
  5.  
  6. ' Take turns adding 1, 2, or 3 until someone makes the total = 21, they are winner.
  7.  
  8.     Total = 0 'reset
  9.     PRINT "21_Game Rosetta Code:" 'title for total
  10.     LOCATE 1, 60: PRINT "Running Total:"; Total
  11.     IF RND < .5 THEN human = 1 ELSE human = 0 'who goes first
  12.     DO
  13.         human = 1 - human 'toggle player
  14.         IF human THEN
  15.             getHuman: 'in case human plays 22, 23...
  16.             INPUT "Human: enter 1, 2, 3 for your turn, any other quits > "; add
  17.             IF add < 1 OR add > 3 THEN PRINT "Human quits, goodbye!": END
  18.             IF add + Total > 21 THEN PRINT "Too much to add, try again...": GOTO getHuman
  19.         ELSE 'ai turn
  20.             IF Total >= 18 THEN
  21.                 add = 21 - Total
  22.             ELSE
  23.                 add = 0
  24.                 FOR i = 1 TO 3
  25.                     IF (Total + i) MOD 4 = 1 THEN add = i
  26.                 NEXT
  27.                 IF add = 0 THEN add = 1
  28.             END IF
  29.         END IF
  30.         IF human THEN LOCATE CSRLIN - 1, 60 ELSE LOCATE CSRLIN, 60
  31.         IF human THEN PRINT "    Human adds "; add ELSE PRINT "       AI adds "; add
  32.         Total = Total + add
  33.         LOCATE CSRLIN, 60: PRINT "Running Total:"; Total
  34.         IF Total = 21 THEN
  35.             LOCATE CSRLIN, 60
  36.             IF human THEN PRINT "*** Human wins ***" ELSE PRINT " *** AI wins ***"
  37.             EXIT DO
  38.         END IF
  39.     LOOP
  40.     PRINT "Press any for next game..."
  41.     SLEEP
  42.     CLS
  43.     _KEYCLEAR
  44.  
« Last Edit: August 10, 2020, 04:54:14 am by bplus »

Offline loudar

  • Newbie
  • Posts: 73
  • improve it bit by bit.
    • View Profile
Re: 21_Game Rosetta Code
« Reply #3 on: August 10, 2020, 06:18:09 am »
As far as I noticed; it's only beatable when AI starts. Any other way, the AI would just force you to lose :D
Check out what I do besides coding: http://loudar.myportfolio.com/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: 21_Game Rosetta Code
« Reply #4 on: August 10, 2020, 12:08:00 pm »
As far as I noticed; it's only beatable when AI starts. Any other way, the AI would just force you to lose :D

Yes just like NIM, but the only way to beat it is if you start. I'd like to see the screen shot if you can beat it if AI has first move.

Here is how to beat AI
21_Game.PNG
* 21_Game.PNG (Filesize: 14.96 KB, Dimensions: 642x424, Views: 349)