Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Select the first cell in the set {A1 : I9}.
- -- Check if the cell is free.
- -- If that cell is not free, chose the next cell in the set {A1 : I9}.
- -- Repeat until you find an available cell.
- -- Set the available cell to be the active cell.
- -- Select with the lowest number if the set {1 : 9}.
- -- Check if that number is used in the current “row”, “column” or “3 x 3 cell”.
- -- If the number is used, chose the next lowest number and see if that is available.
- -- Repeat until you find an available number.
- -- Once you find an available number, check if there is more than one available number.
- -- If there is, populate the active cell with the lowest of them.
- -- If there is not, populate the active cell with the available number.
- -- 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.
- -- Repeat until there are no available cells.
- with ada.text_io, ada.integer_text_io, ada.command_line;
- use ada.text_io, ada.integer_text_io, ada.command_line;
- procedure sudoku is
- subtype counter is integer range 1..9;
- subtype row is integer range 1..9;
- subtype col is integer range 1..9;
- type board is array (row, col) of integer;
- input_file: file_type;
- placeholder: integer;
- puzzle: board := (others=>(others=> 0));
- function handle_arguments return boolean is
- begin
- if argument_count <1 then
- put("Please enter in atleast one argument into Cmd line!");
- return false;
- end if;
- if argument_count =1 then
- begin
- open(file=>input_file, name=>argument(1), mode=>in_file);
- exception
- when name_error=>
- put("File name provided will not open..");
- return false;
- end;
- end if;
- return true;
- end handle_arguments;
- --Check each row
- function check_row (data: in board; target_row: in row)
- return boolean is
- seen: array (row) of boolean :=(others=> false);
- current_value: integer;
- begin
- for c in Data'range (2) loop
- current_value := data(target_row, c);
- if current_value in row then
- if seen(current_value) then
- return false;
- end if;
- seen(current_value) := true;
- end if;
- end loop;
- return true;
- end check_row;
- --Check each col
- function check_col (data: in board; target_col: in col)
- return boolean is
- seen: array (col) of boolean :=(others=>false);
- current_value: integer;
- begin
- for r in data'range (1) loop
- current_value:= data(target_col, r);
- if current_value in col then
- if seen(current_value) then
- return false;
- end if;
- seen(current_value):= true;
- end if;
- end loop;
- return true;
- end check_col;
- --Check each box
- function check_box (data: in board; target_row: in row; target_col: in col)
- return boolean is
- seen: array(counter) of boolean := (others=> false);
- current_value: integer;
- row_offset: constant integer:= ((target_row-1) /3) *3;
- col_offset: constant integer:= ((target_col-1) /3) *3;
- begin
- for box_row in 1..3 loop
- for box_col in 1..3 loop
- current_value := data(row_offset + box_row, col_offset + box_col);
- if current_value in counter then
- if seen(current_value) then
- return false;
- end if;
- seen (current_value):= true;
- end if;
- end loop;
- end loop;
- return true;
- end check_box;
- --Check to make sure each value is valid
- -- function valid_value (data: in board) return boolean is
- -- result : boolean:= true;
- -- begin
- -- for r in data'range (1) loop
- -- result:= result and check_row(data,r);
- --Solve
- --procedure solve (data: in out board) is
- -- solved: boolean := false;
- --Ouput board
- procedure output_board (data: in board) is
- begin
- for r in data'range (1) loop
- for c in data'range (2) loop
- put(data(r,c));
- end loop;
- new_line;
- end loop;
- end output_board;
- begin
- --Handling the input arguments and opening
- if handle_arguments = true then
- put("Original puzzle: ");
- new_line;
- for r in row loop
- put("|");
- for c in col loop
- get(input_file, placeholder);
- if placeholder not in counter then
- puzzle(r,c):= 0;
- else
- puzzle(r,c) := placeholder;
- end if;
- put(placeholder);
- end loop;
- put("|");
- new_line;
- end loop;
- end if;
- end sudoku;
Add Comment
Please, Sign In to add comment