home *** CD-ROM | disk | FTP | other *** search
/ MikeOS 4.5 / mikeos.iso / mikeos.flp / sudoku.bas < prev    next >
Encoding:
BASIC Source File  |  2014-12-21  |  6.7 KB  |  450 lines

  1. rem *** MikeOS Sudoku 1.0 ***
  2.  
  3.  
  4. cls
  5.  
  6. a = 0
  7. b = 0
  8. c = 78
  9. d = 24
  10. gosub draw_box
  11.  
  12. $1 = "Easy,Medium,Hard"
  13. $2 = "MikeOS Sudoku"
  14. $3 = "Choose a difficulty level..."
  15. listbox $1 $2 $3 z
  16.  
  17. if z = 0 then goto finish
  18.  
  19.  
  20. rem *** Copy level information into RAM ***
  21. d = RAMSTART
  22. for c = 1 to 81
  23.   if z = 1 then read level_easy c a
  24.   if z = 2 then read level_medium c a
  25.   if z = 3 then read level_hard c a
  26.   poke a d
  27.   d = d + 1
  28. next c
  29.  
  30.  
  31. rem *** X and Y vars are cursor position ***
  32. x = 0
  33. y = 0
  34.  
  35.  
  36. cursor off
  37. gosub setup_screen
  38.  
  39. main_loop:
  40.   gosub update_screen
  41.  
  42.   rem *** Move cursor according to keypresses, skipping divider lines ***
  43.   waitkey k
  44.   if k = 27 then goto finish
  45.   if k = 4 and x < 16 then x = x + 2
  46.   if k = 3 and x > 0 then x = x - 2
  47.   if k = 2 and y < 10 then y = y + 1
  48.   if k = 2 and y = 3 then y = y + 1
  49.   if k = 2 and y = 7 then y = y + 1
  50.   if k = 1 and y > 0 then y = y - 1
  51.   if k = 1 and y = 3 then y = y - 1
  52.   if k = 1 and y = 7 then y = y - 1
  53.   if k = 13 then gosub enter_pressed
  54.   if k = 104 then gosub show_help
  55.   goto main_loop
  56.  
  57.  
  58. finish:
  59.   cursor on
  60.   cls
  61.   end
  62.  
  63.  
  64. enter_pressed:
  65.   move 40 14
  66.   print "Enter a number: " ;
  67.   input a
  68.  
  69.   if a > 9 then goto invalid
  70.  
  71.   rem *** These lines convert our coordinates in the grid into the ***
  72.   rem *** corresponding point in the Sudoku numbers in RAM ***
  73.   b = x / 2
  74.   c = RAMSTART
  75.   c = c + b
  76.   d = y * 9
  77.   c = c + d
  78.   if y > 2 and y < 7 then c = c - 9
  79.   if y > 7 then c = c - 18
  80.   poke a c
  81.  
  82.   move 40 14
  83.   print "                          "
  84.  
  85.   gosub check_completed
  86. return
  87.  
  88. invalid:
  89.   move 40 16
  90.   print "Invalid number!"
  91.   waitkey a
  92.   move 40 14
  93.   print "                          "
  94.   move 40 16
  95.   print "                          "
  96. return
  97.  
  98.  
  99. check_completed:
  100.   d = 0
  101.   u = 0
  102.  
  103.   rem *** First, go through the rows, looking for uniques ***
  104.   a = RAMSTART
  105.  
  106.   for p = 0 to 8
  107.     z = a
  108.  
  109.     for b = 1 to 9
  110.       for c = 0 to 8
  111.         peek m a
  112.         if m = b then d = d + 1
  113.         if m = b then goto donehere
  114.         a = a + 1
  115.       next c
  116.       donehere:
  117.       a = z
  118.     next b
  119.  
  120.     if d = 9 then u = u + 1
  121.     d = 0
  122.     a = a + 9
  123.   next p
  124.  
  125.  
  126.   rem *** And then through columns ***
  127.   a = RAMSTART
  128.   d = 0
  129.  
  130.   for p = 0 to 8
  131.     z = a
  132.  
  133.     for b = 1 to 9
  134.       for c = 0 to 8
  135.         peek m a
  136.         if m = b then d = d + 1
  137.         if m = b then goto bdonehere2
  138.         a = a + 9
  139.       next c
  140.  
  141.       bdonehere2:
  142.       a = z
  143.     next b
  144.  
  145.     if d = 9 then u = u + 1
  146.     d = 0
  147.     a = a + 1
  148.   next p    
  149.  
  150.  
  151.   rem *** And through first column of boxes ***
  152.  
  153.   for g = 0 to 2
  154.     a = RAMSTART
  155.     d = 0
  156.     b = g * 27
  157.     a = a + b
  158.  
  159.     z = a
  160.  
  161.     for b = 1 to 9
  162.       for c = 0 to 8
  163.         if c = 3 then a = a + 6
  164.         if c = 6 then a = a + 6
  165.         peek m a
  166.         if m = b then d = d + 1
  167.         if m = b then goto bdonehere3
  168.         a = a + 1
  169.       next c
  170.  
  171.       bdonehere3:
  172.       a = z
  173.     next b
  174.  
  175.     if d = 9 then u = u + 1
  176.     d = 0
  177.     a = a + 1
  178.   next g
  179.  
  180.  
  181.   rem *** Second column ***
  182.   for g = 0 to 2
  183.     a = RAMSTART
  184.     d = 0
  185.     b = g * 27
  186.     a = a + b
  187.     a = a + 3
  188.  
  189.     z = a
  190.  
  191.     for b = 1 to 9
  192.       for c = 0 to 8
  193.         if c = 3 then a = a + 6
  194.         if c = 6 then a = a + 6
  195.         peek m a
  196.         if m = b then d = d + 1
  197.         if m = b then goto bdonehere4
  198.         a = a + 1
  199.       next c
  200.  
  201.       bdonehere4:
  202.       a = z
  203.     next b
  204.  
  205.     if d = 9 then u = u + 1
  206.     d = 0
  207.     a = a + 1
  208.   next g
  209.  
  210.  
  211.   rem *** Third column ***
  212.   for g = 0 to 2
  213.     a = RAMSTART
  214.     d = 0
  215.     b = g * 27
  216.     a = a + b
  217.     a = a + 6
  218.  
  219.     z = a
  220.  
  221.     for b = 1 to 9
  222.       for c = 0 to 8
  223.         if c = 3 then a = a + 6
  224.         if c = 6 then a = a + 6
  225.         peek m a
  226.         if m = b then d = d + 1
  227.         if m = b then goto bdonehere5
  228.         a = a + 1
  229.       next c
  230.  
  231.       bdonehere5:
  232.       a = z
  233.     next b
  234.  
  235.     if d = 9 then u = u + 1
  236.     d = 0
  237.     a = a + 1
  238.   next g
  239.  
  240.   if u = 27 then goto win
  241. return
  242.  
  243.  
  244. win:
  245.   move 45 12
  246.   ink 14
  247.   print "PUZZLE COMPLETE!"
  248.   move 44 14
  249.   ink 7
  250.   print "Press a key to end"
  251.   waitkey x
  252.   cursor on
  253.   cls
  254.   end
  255.  
  256.  
  257. setup_screen:
  258.   cls
  259.  
  260.   rem *** Outer border ***
  261.   a = 0
  262.   b = 0
  263.   c = 78
  264.   d = 24
  265.   gosub draw_box
  266.  
  267.  
  268.   rem *** Sudoku grid lines ***
  269.   ink 8
  270.  
  271.   a = 13
  272.   b = 8
  273.   c = 31
  274.   d = 20
  275.   gosub draw_box
  276.   a = 13
  277.   b = 8
  278.   c = 31
  279.   d = 12
  280.   gosub draw_box
  281.   a = 19
  282.   b = 8
  283.   c = 25
  284.   d = 12
  285.   gosub draw_box
  286.   a = 13
  287.   b = 12
  288.   c = 31
  289.   d = 16
  290.   gosub draw_box
  291.   a = 19
  292.   b = 12
  293.   c = 25
  294.   d = 16
  295.   gosub draw_box
  296.   a = 19
  297.   b = 16
  298.   c = 25
  299.   d = 20
  300.   gosub draw_box
  301.  
  302.   ink 7
  303.   move 3 2
  304.   print "MikeOS Sudoku - Press H for help, or Esc to quit"
  305.   move 3 4
  306.   print "Cursor keys navigate, Enter inputs a number, zero blanks a square"
  307. return
  308.  
  309.  
  310. update_screen:
  311.   rem *** Cursor ***
  312.   ink 14
  313.  
  314.   rem *** First, blank out area where cursor could be ***
  315.   move 14 6
  316.   print "                    "
  317.   move 14 7
  318.   print "                    "
  319.  
  320.   for a = 9 to 21
  321.     move 11 a
  322.     print "  "
  323.   next a
  324.  
  325.   a = x + 14
  326.   move a 6
  327.   print "|" ;
  328.   move a 7
  329.   print "v" ;
  330.   b = y + 9
  331.   move 11 b
  332.   print "->" ;
  333.   ink 7
  334.  
  335.  
  336.   rem *** Numbers ***
  337.   a = 14
  338.   b = 9
  339.  
  340.   m = RAMSTART
  341.  
  342.   for c = 1 to 81
  343.     peek d m
  344.  
  345.     move a b
  346.     if d > 0 then print d ;
  347.     if d = 0 then print " " ;
  348.     a = a + 2
  349.  
  350.     rem *** Handle wrapping ***
  351.     if a = 32 then b = b + 1
  352.     if a = 32 then a = 14
  353.     if b = 12 then b = 13
  354.     if b = 16 then b = 17
  355.  
  356.     m = m + 1
  357.   next c
  358. return
  359.  
  360.  
  361. show_help:
  362.   cls
  363.  
  364.   print ""
  365.   print ""
  366.   print "   Sudoku is a puzzle game. You have a grid of 9 x 9 squares,"
  367.   print "   which is divided into smaller 3 x 3 sections. Your goal is"
  368.   print "   to fill the grid so that every row has the numbers 1 to 9,"
  369.   print "   every column has the numbers 1 to 9, and every small 3 x 3"
  370.   print "   section has the numbers 1 to 9."
  371.   print ""
  372.   print "   Press a key to return to the game..."
  373.  
  374.   a = 0
  375.   b = 0
  376.   c = 78
  377.   d = 24
  378.   gosub draw_box
  379.  
  380.   waitkey z
  381.   gosub setup_screen
  382.   return
  383. return
  384.  
  385.  
  386. rem *** Level data ***
  387. level_easy:
  388. 0 5 0 0 8 1 0 0 7
  389. 4 6 0 0 0 0 3 5 0
  390. 0 0 1 3 4 0 0 6 0
  391. 0 0 4 8 0 6 0 0 9
  392. 8 0 7 0 5 0 2 0 6
  393. 6 0 0 1 0 2 7 0 0 
  394. 0 1 0 0 3 4 6 0 0
  395. 0 8 6 0 0 0 0 2 3
  396. 2 0 0 7 6 0 0 9 0
  397.  
  398. level_medium:
  399. 0 7 0 0 2 0 0 3 0
  400. 8 0 0 0 0 0 0 0 9
  401. 0 0 5 0 9 0 4 0 0
  402. 0 5 0 0 8 0 0 4 0
  403. 3 0 1 9 0 7 6 0 2
  404. 0 9 0 0 6 0 0 8 0
  405. 0 0 9 0 7 0 8 0 0
  406. 1 0 0 0 0 0 0 0 6
  407. 0 4 0 0 5 0 0 7 0
  408.  
  409. level_hard:
  410. 0 0 0 1 6 8 0 0 0
  411. 0 0 0 4 7 9 1 0 0
  412. 0 0 0 0 0 0 0 0 0 
  413. 2 0 0 0 0 6 4 0 7
  414. 1 0 0 3 0 0 5 0 9
  415. 0 0 9 0 0 7 0 8 0
  416. 0 0 0 0 0 0 2 0 0
  417. 0 0 8 0 0 0 7 0 3
  418. 0 0 5 6 4 3 0 0 0
  419.  
  420.  
  421. rem *** draw_box routine ***
  422. rem *** Takes coords in A, B, C and D vars. Uses Z internally ***
  423. draw_box:
  424.   move a b
  425.   for z = a to c
  426.     print "-" ;
  427.   next z
  428.   move a d
  429.   for z = a to c
  430.     print "-" ;
  431.   next z
  432.   for z = b to d
  433.     move a z
  434.     print "|" ;
  435.   next z
  436.   for z = b to d
  437.     move c z
  438.     print "|" ;
  439.   next z
  440.   move a b
  441.   print "+" ;
  442.   move a d
  443.   print "+" ;
  444.   move c b
  445.   print "+" ;
  446.   move c d
  447.   print "+" ;
  448. return
  449.  
  450.