forked from AlexIzydorczyk/sudoku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
140 lines (102 loc) · 3.3 KB
/
game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Matt Olson
Alex Izydorczyk
File to implement game play
*/
#include <iostream>
#include <regex>
#include <sstream>
#include "game.hpp"
#include "solver.hpp"
#include "altproj.hpp"
using namespace std;
void playGame(int size, int nobs);
//play game again
void playAgainPrompt(int size, int nobs){
string user_response;
do{
cout << "Would you like to play again? y/n" << endl;
getline(cin, user_response);
} while ((user_response != "y") && (user_response != "n"));
if (user_response == "y"){
playGame(size, nobs);
} else {
return;
}
}
void playGame(int size, int nobs){
string user_entry;
bool solverSucces = false;
Board board = generatePuzzle(size,nobs);
regex rgx("[0-9]{1,}"); // For parsing user input
smatch match;
int input_int[3]; // Array for user input
// Repeat until puzzle is solved
do {
board.printPuzzle(); // print puzzle
getline(cin, user_entry); // get user input
// Check for user commands
if (user_entry == "Solve" || user_entry == "solve"){
solverSucces = solve(board,0,0);
board.printPuzzle();
break;
}
if (user_entry == "Clear" || user_entry == "clear"){
board.clearPuzzle();
continue;
}
// Assuming no special command, attempt to parse user input
int count = 0;
for(sregex_iterator i = sregex_iterator(user_entry.begin(),
user_entry.end(), rgx);
i != sregex_iterator();
++i )
{
smatch m = *i;
input_int[count] = stoi(m.str());
++count;
}
// If parse values are out of range... throw error and continue
bool invalid = false;
for (int i=0; i<3; i++) {
if (!board.inBounds(input_int[i])){
invalid = true;
}
}
if (invalid){
cout << "Values entered must be between 1 and " << board.getSize() << endl;
continue;
}
// Assign board value if feasible move
if (board.feasibleUser((input_int[0]-1),(input_int[1]-1),input_int[2])==true){
if (board.checkImmutable((input_int[0]-1),(input_int[1]-1)) == false){
board.assignValue((input_int[0]-1),(input_int[1]-1),input_int[2]);
} else {
cout << "You can't change that value!" << endl;
}
} else {
cout << "Not feasible!" << endl;
}
// reset inputs to 0, will cause invalid message if unchanged on next round
for (int i=0; i<3; i++) {
input_int[i] = 0;
}
} while (!board.checkPuzzle());
string user_response;
if (solverSucces){
cout << "The puzzle is solved!" << endl;
playAgainPrompt(size, nobs);
} else {
cout << "Could not solve puzzle based on pre-filled values" << endl;
do {
cout << "Would you like to clear and try to solve? y/n" << endl;
getline(cin, user_response);
} while ((user_response != "y") && (user_response != "n"));
if (user_response == "y"){
board.clearPuzzle();
solve(board,0,0);
board.printPuzzle();
playAgainPrompt(size, nobs);
}
}
}