Author Topic: Mastermind Code Challenge  (Read 2818 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Mastermind Code Challenge
« on: December 23, 2018, 12:15:42 AM »
Here is the BaCon mastermind game example. This might be a good example to try and convert to MBC. I'm going to give it a shot in Script BASIC trying to change as little code as I can to show compatibility.

Code: BaCon
Code: ScriptBasic
  1. REM
  2. REM The mastermind game - requires ANSI terminal for colors
  3. REM http://en.wikipedia.org/wiki/Mastermind_(board_game)
  4. REM
  5. REM June 8, 2009 - (c) PvE - GPL.
  6. REM Revised May 2010.
  7. REM
  8.  
  9. REM Generate secret code
  10. SUB Generate_Code
  11.  
  12.     LOCAL i
  13.  
  14.     FOR i = 0 TO 3
  15.         REPEAT
  16.             code[i] = RND & 7
  17.         UNTIL code[i] < 7 AND code[i] > 0
  18.     NEXT
  19.  
  20. END SUB
  21.  
  22. REM Show the board
  23. SUB Print_Board
  24.  
  25.     LOCAL i, j
  26.  
  27.     PRINT
  28.  
  29.     FOR i = 0 TO 11
  30.  
  31.         PRINT TAB$(1);
  32.  
  33.         FOR j = 0 TO 3
  34.  
  35.             COLOR INTENSE
  36.             COLOR BG TO BLACK
  37.  
  38.             SELECT board[i][j]
  39.                 CASE 0
  40.                     COLOR FG TO WHITE
  41.                     PRINT " . ";
  42.                 CASE 1
  43.                     COLOR FG TO WHITE
  44.                     PRINT "[W]";
  45.                 CASE 2
  46.                     COLOR FG TO RED
  47.                     PRINT "[R]";
  48.                 CASE 3
  49.                     COLOR FG TO GREEN
  50.                     PRINT "[G]";
  51.                 CASE 4
  52.                     COLOR FG TO YELLOW
  53.                     PRINT "[Y]";
  54.                 CASE 5
  55.                     COLOR FG TO BLUE
  56.                     PRINT "[B]";
  57.                 CASE 6
  58.                     COLOR FG TO CYAN
  59.                     PRINT "[C]";
  60.             END SELECT
  61.  
  62.         NEXT
  63.  
  64.         COLOR RESET
  65.         PRINT " - ";
  66.  
  67.         FOR j = 0 TO 3
  68.             COLOR BG TO WHITE
  69.             IF hint[i][j] EQ 0 THEN
  70.                 COLOR FG TO BLACK
  71.                 PRINT ".";
  72.             ELIF hint[i][j] EQ 1 THEN
  73.                 COLOR INTENSE
  74.                 COLOR FG TO WHITE
  75.                 PRINT "O";
  76.                 COLOR NORMAL
  77.             ELIF hint[i][j] EQ 2 THEN
  78.                 COLOR FG TO BLACK
  79.                 PRINT "O";
  80.             END IF
  81.         NEXT
  82.  
  83.         COLOR RESET
  84.         PRINT
  85.  
  86.     NEXT
  87.  
  88. END SUB
  89.  
  90. REM Check and produce answer
  91. SUB Generate_Answer
  92.  
  93.     LOCAL i, j, pos
  94.     LOCAL check[4], line[4] TYPE NUMBER
  95.  
  96.     pos = 0
  97.  
  98.     REM Reset checked spots
  99.     FOR i = 0 TO 3
  100.         check[i] = code[i]
  101.         line[i] = board[row][i]
  102.     NEXT
  103.  
  104.     REM Check equals
  105.     FOR i = 0 TO 3
  106.         IF line[i] EQ check[i] THEN
  107.             hint[row][pos] = 2
  108.             check[i] = 0
  109.             line[i] = -1
  110.             INCR pos
  111.         END IF
  112.     NEXT
  113.  
  114.     REM Check occurence
  115.     FOR i = 0 TO 3
  116.         FOR j = 0 TO 3
  117.             IF line[i] EQ check[j] THEN
  118.                 hint[row][pos] = 1
  119.                 check[j] = 0
  120.                 line[i] = -1
  121.                 INCR pos
  122.             END IF
  123.         NEXT
  124.     NEXT i
  125.  
  126.     REM Check status
  127.     status = 0
  128.  
  129.     FOR i = 0 TO 3
  130.         INCR status, hint[row][i]
  131.     NEXT
  132.  
  133. END SUB
  134.  
  135. REM Set the correct colors based on input
  136. SUB Parse_Input
  137.  
  138.     LOCAL i, wrong
  139.  
  140.     wrong = 0
  141.  
  142.     IF LEN(col$) EQ 4 THEN
  143.         FOR i = 1 TO LEN(col$)
  144.             SELECT UCASE$(MID$(col$, i, 1))
  145.                 CASE "W"
  146.                     board[row][i-1] = 1
  147.                 CASE "R"
  148.                     board[row][i-1] = 2
  149.                 CASE "G"
  150.                     board[row][i-1] = 3
  151.                 CASE "Y"
  152.                     board[row][i-1] = 4
  153.                 CASE "B"
  154.                     board[row][i-1] = 5
  155.                 CASE "C"
  156.                     board[row][i-1] = 6
  157.                 DEFAULT
  158.                     FOR j = 0 TO 3
  159.                         board[row][j] = 0
  160.                     NEXT
  161.                     wrong = 1
  162.             END SELECT
  163.         NEXT
  164.     ELSE
  165.         wrong = 1
  166.     END IF
  167.  
  168.     IF wrong EQ 1 THEN
  169.         COLOR FG TO RED
  170.         PRINT NL$, TAB$(1), "Invalid input!"
  171.         COLOR RESET
  172.     ELSE
  173.         Generate_Answer
  174.         INCR row
  175.     END IF
  176.  
  177. END SUB
  178.  
  179. REM Main program starts here
  180. DECLARE code[4], board[12][4], hint[12][4] TYPE NUMBER
  181.  
  182. REM Keep track of turn
  183. row = 0
  184.  
  185. REM Guessed status
  186. status = 0
  187.  
  188. Generate_Code
  189.  
  190. PRINT "+------------------------------------------------------------+"
  191. PRINT "|                   M A S T E R M I N D                      |"
  192. PRINT "+------------------------------------------------------------+"
  193. PRINT "|                      - Usage -                             |"
  194. PRINT "| Enter colorcombination using one of the following letters: |"
  195. PRINT "| (W)hite (R)ed (G)reen (Y)ellow (B)lue (C)yan - (S) to quit |"
  196. PRINT "|                      - Hints -                             |"
  197. PRINT "| A white O means color occurs, a black O means correct spot.|"
  198. PRINT "+------------------------------------------------------------+"
  199.  
  200. WHILE NOT(EQUAL(col$, "stop")) AND row < 12 AND status < 8 DO
  201.     INPUT NL$, "Enter combination... ", col$
  202.     IF EQUAL(UCASE$(col$), "S") THEN END
  203.     Parse_Input
  204.     Print_Board
  205. WEND
  206.  
  207. IF status EQ 8 THEN
  208.     PRINT NL$, "Congratulations, guessed!"
  209. ELSE
  210.     PRINT NL$, "You lost! The code was: ";
  211.     FOR i = 0 TO 3
  212.         SELECT code[i]
  213.             CASE 1
  214.                 COLOR FG TO WHITE
  215.                 PRINT "W";
  216.             CASE 2
  217.                 COLOR FG TO RED
  218.                 PRINT "R";
  219.             CASE 3
  220.                 COLOR FG TO GREEN
  221.                 PRINT "G";
  222.             CASE 4
  223.                 COLOR FG TO YELLOW
  224.                 PRINT "Y";
  225.             CASE 5
  226.                 COLOR FG TO BLUE
  227.                 PRINT "B";
  228.             CASE 6
  229.                 COLOR FG TO CYAN
  230.                 PRINT "C";
  231.         END SELECT
  232.     NEXT
  233.     COLOR RESET
  234.     PRINT
  235. END IF
  236.  
  237. PRINT
  238. END
  239.  

« Last Edit: December 23, 2018, 12:17:20 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: Mastermind Code Challenge
« Reply #1 on: December 25, 2018, 01:17:51 AM »

       /\          /\          /\
    /\//\\/\    /\//\\/\    /\//\\/\
 /\//\\\///\\/\//\\\///\\/\//\\\///\\/\
//\\\//\/\\///\\\//\/\\///\\\//\/\\///\\
\\//\/                            \/\\//
 \/                                  \/
 /\      I'm wishing you all a       /\
//\\     joyous holiday season      //\\
\\//     and a Happy New Year!      \\//
 \/                                  \/
 /\                                  /\
//\\/\                            /\//\\
\\///\\/\//\\\///\\/\//\\\///\\/\//\\\//
 \/\\///\\\//\/\\///\\\//\/\\///\\\//\/
    \/\\//\/    \/\\//\/    \/\\//\/
       \/          \/          \/