Scippy

SCIP

Solving Constraint Integer Programs

sudoku_utils.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2020 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file sudoku_utils.h
17  * @brief A set of utilities that are used to read the puzzle and display the puzzle
18  * @author Naga V C Gudapati
19 */
20 
21 #include <iostream>
22 #include <fstream>
23 #include <vector>
24 #include <string>
25 
26 namespace sudoku
27 {
28  /** reads in the sudoku puzzle from filepath
29  *
30  * Reads the string of sudoku puzzle into a 9x9 grid represented by a vector
31  * of a vector of ints. The actual number is stored as itself and the blanks are stored as -1.
32  *
33  */
34  inline std::vector<std::vector<int>> getSudokuPuzzle( std::string &filepath )
35  {
36  /* setting up a 9x9 grid forstoring the sudoku puzzle. */
37  std::vector<std::vector<int>> puzzle(9, std::vector<int>(9));
38 
39  /* Reading the puzzle into a stringstream */
40  std::ifstream infile(filepath);
41 
42  std::string puzzledata = "";
43 
44  if( infile.is_open() )
45  {
46  std::getline( infile, puzzledata );
47  if( puzzledata.length() != 81 ) /* The puzzle should have 81 characters */
48  {
49  std::cerr << "Please check the puzzle file forinconsistencies"
50  << "\n";
51  exit(1);
52  }
53  }
54 
55  int idx = 0; /* This variable will be used to access the numbers in the puzzle string */
56 
57  for( int i = 0; i < 9; ++i )
58  {
59  for( int j = 0; j < 9; ++j )
60  {
61  /* We will only convert the numeric string to an integer if it is not '.' or '0'. */
62  if( (puzzledata.substr(idx, 1) != ".") && (puzzledata.substr(idx, 1) != "0") )
63  {
64  puzzle[i][j] = std::stoi( puzzledata.substr(idx, 1) );
65  }
66  else
67  {
68  /* If we are currently reading a '.' or '0' make it -1. */
69  puzzle[i][j] = -1;
70  }
71  idx++;
72  }
73  }
74 
75  return puzzle;
76  }
77 
78  /** prints the sudoku puzzle to console */
79  inline void printSudoku( const std::vector<std::vector<int>> &sudokupuzzle )
80  {
81  std::cout << "+----------+-----------+-----------+" << "\n";
82  for( int i = 0; i < 9; ++i )
83  {
84  std::cout << "|";
85  for( int j = 0; j < 9; ++j )
86  {
87  if( sudokupuzzle[i][j] > 0 )
88  {
89 
90  if( j == 2 || j == 5 || j == 8 )
91  {
92  std::cout << sudokupuzzle[i][j] << " | ";
93  }
94  else
95  {
96  std::cout << sudokupuzzle[i][j] << " ";
97  }
98  }
99  else
100  {
101  if( j == 2 || j == 5 || j == 8 )
102  {
103  std::cout << "*" << " | ";
104  }
105  else
106  {
107  std::cout << "*" << " ";
108  }
109  }
110  }
111  std::cout << "\n";
112 
113  if( i == 2 || i == 5 || i == 8 )
114  {
115  std::cout << "+----------+-----------+-----------+" << "\n";
116  }
117  }
118  }
119 } /* namespace sudoku */
std::vector< std::vector< int > > getSudokuPuzzle(std::string &filepath)
Definition: sudoku_utils.h:34
void printSudoku(const std::vector< std::vector< int >> &sudokupuzzle)
Definition: sudoku_utils.h:79