Guest User

Untitled

a guest
Feb 18th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. -- Select the first cell in the set {A1 : I9}.
  2. -- Check if the cell is free.
  3. -- If that cell is not free, chose the next cell in the set {A1 : I9}.
  4. -- Repeat until you find an available cell.
  5. -- Set the available cell to be the active cell.
  6. -- Select with the lowest number if the set {1 : 9}.
  7. -- Check if that number is used in the current “row”, “column” or “3 x 3 cell”.
  8. -- If the number is used, chose the next lowest number and see if that is available.
  9. -- Repeat until you find an available number.
  10. -- Once you find an available number, check if there is more than one available number.
  11. -- If there is, populate the active cell with the lowest of them.
  12. -- If there is not, populate the active cell with the available number.
  13. -- If there is no available number, backtrack to the last place where you have more than 1 available number and use the next highest number.
  14. -- Repeat until there are no available cells.
  15.  
  16.  
  17. with ada.text_io, ada.integer_text_io, ada.command_line;
  18. use ada.text_io, ada.integer_text_io, ada.command_line;
  19.  
  20.  
  21.  
  22. procedure sudoku is
  23. subtype counter is integer range 1..9;
  24. subtype row is integer range 1..9;
  25. subtype col is integer range 1..9;
  26. type board is array (row, col) of integer;
  27. input_file: file_type;
  28. placeholder: integer;
  29.  
  30. puzzle: board := (others=>(others=> 0));
  31.  
  32.  
  33. function handle_arguments return boolean is
  34. begin
  35. if argument_count <1 then
  36. put("Please enter in atleast one argument into Cmd line!");
  37. return false;
  38. end if;
  39. if argument_count =1 then
  40. begin
  41. open(file=>input_file, name=>argument(1), mode=>in_file);
  42.  
  43. exception
  44. when name_error=>
  45. put("File name provided will not open..");
  46. return false;
  47. end;
  48. end if;
  49. return true;
  50. end handle_arguments;
  51.  
  52. --Check each row
  53. function check_row (data: in board; target_row: in row)
  54. return boolean is
  55. seen: array (row) of boolean :=(others=> false);
  56. current_value: integer;
  57. begin
  58. for c in Data'range (2) loop
  59. current_value := data(target_row, c);
  60. if current_value in row then
  61. if seen(current_value) then
  62. return false;
  63. end if;
  64. seen(current_value) := true;
  65. end if;
  66. end loop;
  67. return true;
  68. end check_row;
  69.  
  70. --Check each col
  71. function check_col (data: in board; target_col: in col)
  72. return boolean is
  73. seen: array (col) of boolean :=(others=>false);
  74. current_value: integer;
  75. begin
  76. for r in data'range (1) loop
  77. current_value:= data(target_col, r);
  78. if current_value in col then
  79. if seen(current_value) then
  80. return false;
  81. end if;
  82. seen(current_value):= true;
  83. end if;
  84. end loop;
  85. return true;
  86. end check_col;
  87.  
  88. --Check each box
  89. function check_box (data: in board; target_row: in row; target_col: in col)
  90. return boolean is
  91. seen: array(counter) of boolean := (others=> false);
  92. current_value: integer;
  93. row_offset: constant integer:= ((target_row-1) /3) *3;
  94. col_offset: constant integer:= ((target_col-1) /3) *3;
  95. begin
  96. for box_row in 1..3 loop
  97. for box_col in 1..3 loop
  98. current_value := data(row_offset + box_row, col_offset + box_col);
  99. if current_value in counter then
  100. if seen(current_value) then
  101. return false;
  102. end if;
  103. seen (current_value):= true;
  104. end if;
  105. end loop;
  106. end loop;
  107. return true;
  108. end check_box;
  109.  
  110. --Check to make sure each value is valid
  111. -- function valid_value (data: in board) return boolean is
  112. -- result : boolean:= true;
  113. -- begin
  114. -- for r in data'range (1) loop
  115. -- result:= result and check_row(data,r);
  116. --Solve
  117. --procedure solve (data: in out board) is
  118. -- solved: boolean := false;
  119.  
  120.  
  121. --Ouput board
  122. procedure output_board (data: in board) is
  123. begin
  124. for r in data'range (1) loop
  125. for c in data'range (2) loop
  126. put(data(r,c));
  127. end loop;
  128. new_line;
  129. end loop;
  130. end output_board;
  131.  
  132. begin
  133. --Handling the input arguments and opening
  134. if handle_arguments = true then
  135. put("Original puzzle: ");
  136. new_line;
  137. for r in row loop
  138. put("|");
  139. for c in col loop
  140. get(input_file, placeholder);
  141. if placeholder not in counter then
  142. puzzle(r,c):= 0;
  143. else
  144. puzzle(r,c) := placeholder;
  145. end if;
  146. put(placeholder);
  147. end loop;
  148. put("|");
  149. new_line;
  150. end loop;
  151.  
  152. end if;
  153. end sudoku;
Add Comment
Please, Sign In to add comment