C++ boolean arrays conditional long statement - c++

So, I can't search what the meaning of this let say for example I have
string TICTACTOE[3][3] = {
{"1","2","3"},
{"4","5","6"},
{"7","8","9"}
};
How can I make boolean passing the condition statement in something like this
bool trial = TICTACTOE[0][0] == TICTACTOE[0][1] == TICTACTOE[0][2]
if (trial) {
cout << "THIS PLAYER WINS: " <<playerWinner << endl;
}
Its working though if its only
bool trial = TICTACTOE[0][0] == TICTACTOE[0][1]
but when its three bool its not working..anyone can explain me this?
I'm just used to do with it "==" since I used to use javascript a lot.

You can do this
int check()
{
if (a[1] == a[2] && a[2] == a[3]) return 1; //GAME IS OVER WITH RESULT
else if (a[4] == a[5] && a[5] == a[6]) return 1;
else if (a[7] == a[8] && a[8] == a[9]) return 1;
else if (a[1] == a[4] && a[4] == a[7]) return 1;
else if (a[2] == a[5] && a[5] == a[8]) return 1;
else if (a[3] == a[6] && a[6] == a[9]) return 1;
else if (a[1] == a[5] && a[5] == a[9]) return 1;
else if (a[3] == a[5] && a[5] == a[7]) return 1;
if ( a[1] != '1' && a[2] != '2' && a[3] != '3' && a[4] != '4' &&
a[5] != '5' && a[6] != '6' && a[7] != '7' && a[8] != '8' && a[9] != '9')
return 0; //GAME IS OVER AND NO RESULT
else
return -1; //GAME IS IN PROGRESS
}

Related

smartmove() function to determine winning possibilities not functioning

I'm making a tic tac toe program. Right now, the two player option hasn't been created, but I will have no problem with that. My current issue is working on the bot function. Essentially, it's got 2 main ways of going about: botmove() where a random number is generated and it places its piece based on that number, of course testing that it is unoccupies, and smartmove() which is the function im struggling with. I've essentially given it an algoritm to test any win conditions (horizontal, vertical, and diagonal) and told it that, if it detects one, to place its piece to prevent loss. I have a " ..." and pause for a second to give it some time as well as to see whether or not its using smartmove() or botmove(). When you run the program, if the bot encounters a possible smartmove, it simply does nothing and moves onto the user's next term.
Here's the whole program, but main() and smartmove() are here for convenience.
Here's my main():
int main() {
char replaya;
replaya = 'y';
intro();
while (replaya == 'y' || replaya == 'Y') {
char friendorbota;
friendorbota = friendorbot();
if (friendorbota == 'f' || friendorbota == 'F') {
cout << "friend";
}
else if (friendorbota == 'b' || friendorbota == 'B') {
char arr[9] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
//1
placepiece(arr);
if (determinewin(arr) == true) {
replaya = replay();
}
if (isFilled(arr) == true) {
replaya = replay();
}
//2
smartmove(arr);
if (determinewin(arr) == true) {
replaya = replay();
}
if (isFilled(arr) == true) {
replaya = replay();
}
//3
placepiece(arr);
if (determinewin(arr) == true) {
replaya = replay();
}
if (isFilled(arr) == true) {
replaya = replay();
}
//4
smartmove(arr);
if (determinewin(arr) == true) {
replaya = replay();
}
if (isFilled(arr) == true) {
replaya = replay();
}
//5
placepiece(arr);
if (determinewin(arr) == true) {
replaya = replay();
}
if (isFilled(arr) == true) {
replaya = replay();
}
//6
smartmove(arr);
if (determinewin(arr) == true) {
replaya = replay();
}
if (isFilled(arr) == true) {
replaya = replay();
}
//7
placepiece(arr);
if (determinewin(arr) == true) {
replaya = replay();
}
if (isFilled(arr) == true) {
replaya = replay();
}
//8
smartmove(arr);
if (determinewin(arr) == true) {
replaya = replay();
}
if (isFilled(arr) == true) {
replaya = replay();
}
//9
placepiece(arr);
if (determinewin(arr) == true) {
replaya = replay();
}
if (isFilled(arr) == true) {
replaya = replay();
}
}
cout << "\nThanks for playing!\n";
exit(0);
}
}
And my smartmove():
void smartmove(char arr[9]) {
this_thread::sleep_for(chrono::milliseconds(500));
cout << "\n\n . . . \n\n";
this_thread::sleep_for(chrono::milliseconds(500));
// Check if player has two in a row
for (int i = 0; i < 9; i += 3) {
if (arr[i] == 'X' && arr[i + 1] == 'X' && arr[i + 2] == ' ') {
arr[i + 2] = 'O';
return;
}
else if (arr[i] == 'X' && arr[i + 1] == ' ' && arr[i + 2] == 'X') {
arr[i + 1] = 'O';
return;
}
else if (arr[i] == ' ' && arr[i + 1] == 'X' && arr[i + 2] == 'X') {
arr[i] = 'O';
return;
}
}
// Check if player has two in a column
for (int i = 0; i < 3; i++) {
if (arr[i] == 'X' && arr[i + 3] == 'X' && arr[i + 6] == ' ') {
arr[i + 6] = 'O';
return;
}
else if (arr[i] == 'X' && arr[i + 3] == ' ' && arr[i + 6] == 'X') {
arr[i + 3] = 'O';
return;
}
else if (arr[i] == ' ' && arr[i + 3] == 'X' && arr[i + 6] == 'X') {
arr[i] = 'O';
return;
}
}
// Check if player has two in a diagonal
if (arr[0] == 'X' && arr[4] == 'X' && arr[8] == ' ') {
arr[8] = 'O';
}
else if (arr[0] == 'X' && arr[4] == ' ' && arr[8] == 'X') {
arr[4] = 'O';
}
else if (arr[0] == ' ' && arr[4] == 'X' && arr[8] == 'X') {
arr[0] = 'O';
}
else if (arr[2] == 'X' && arr[4] == 'X' && arr[6] == ' ') {
arr[6] = 'O';
}
else if (arr[2] == 'X' && arr[4] == ' ' && arr[6] == 'X') {
arr[4] = 'O';
}
else if (arr[2] == ' ' && arr[4] == 'X' && arr[6] == 'X') {
arr[2] = 'O';
}
else {
// Default to botmove() if no smart move is available
botmove(arr);
}
}
I've tested every function to ensure that everything is working fine, and I know that my main function could probably be organized a bit better but I wanted it as simple as possible while trying to solve this smartmove() issue. I've tested every function to ensure that everything is working fine other than smartmove(), so it seems like the only issue is with smartmove() or main() to my knowledge, although I've included all of the code in the link. And yes, I've done debugging by myself of every single section and both visual studio.

Why is iteration through a char vector is faster than the iteration through a string in C++

I am practicing a coding challenge where I have to reverse the vowels in a string.
My first approach failed because of exeeding Time limit. Here is my first approach using string iteration to reverse the vowels in a string.
string reverseVowels(string s) {
string str = "";
//storing the vowels from the string into another string
for (auto x : s)
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U')
str = str + x;
//swapping the vowels
int count = 0;
for (int i = s.size() - 1; i >= 0; i--)
{
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U')
{
s[i] = str[count];
count++;
}
}
return s;
}
My second approach using the char vector iteration had passed all the tests. Here is my second approach
class Solution {
public:
string reverseVowels(string s) {
vector<char> v;
//storing the vowels from the string into vector
for (auto x : s)
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U')
v.push_back(x);
//swapping the vowels
int count = 0;
for (int i = s.size() - 1; i >= 0; i--)
{
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U')
{
s[i] = v[count];
count++;
}
}
return s;
}
};
Could you explain why my first method failed the tests but second method passed the tests
Replace str = str + x; with str.push_back(x); or str += x;, and you'll likely see the same performance as with vector.
str = str + x; makes a copy of str, appends the character to that copy, then makes another copy when assigning back to str. As a result, your algorithm is quadratic, for no good reason.
It's because you're doing str = str + x, that creates an unnecessary copy of str, but std::vector::push_back or std::string::push_back appends a character to the vector or string, which is much faster than creating a copy of str.
str = str + x
this creates an additional copy of str while copying.
std::vector::push_back
this straight appends to the vector string

tictaktoe Array bound overflow

I am in need of help for this code that i am working on for a assignment. I am have the issue where if i have any X's on the board that is either in the left 2 columns it will display a X in the row above. I used my debugger and it seems that it is trying to access something outside the array bounds, but it shouldnt be. any advice on how to do this?
#include <iostream>
using namespace std;
void printTTT(char a[3][3]);
void insertX(/*PASS BY REFERENCE*/);
void insertO(char (&arr)[3][3]);
void checkForWin(/*PASS BY REFERENCE*/); // IGNORE THIS FOR NOW
int main() {
char TTTarray[3][3] = { { 'X','-','-' },
{ '-','-','-' },
{ 'X','-','-' } };
//char TTTarray[3][3] = { {'-','X','-'},
// {'-','X','-'},
// {'-','-','O'}};
//char TTTarray[3][3] = { {'-','-','-'},
// {'-','X','-'},
// {'-','O','-'}};
//char TTTarray[3][3] = { {'X','-','X'},
// {'-','-','-'},
// {'O','-','-'}};
//char TTTarray[3][3] = { {'X','-','X'},
// {'O','X','-'},
// {'O','-','O'}};
//insertX(/*CALL*/);
//OR
insertO(TTTarray);
printTTT(TTTarray);
/*****************
I have included the declaratoin of the array, initialized to - for each spot.
The '-' represents an empty position. You should fill it with either a
capital 'O' or a capital 'X'. I have also included a number of initialized arrays
to test; just comment out the ones you don't want for that moment
*****************/
return 0;
}
void printTTT(char a[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << a[i][j];
}
cout << endl;
}
}
void insertX(/*PASS BY REFERENCE*/) {
}
void insertO(char (&arr)[3][3])
{
int x1x;
int x1y;
//int x2x;
//int x2y;
for (int i = 0; i < 3; i++)
{
int go = 0;
for (int j = 0; j < 3; j++)
{
if (arr[i][j] == '-')
{
x1x = i;
x1y = j;
// looking for 2 x's for the block lol
if (x1x == 0 && go == 0)
{
if (arr[x1x][x1y + 1] == 'X' && arr[x1x][x1y + 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x + 1] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x - 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1x == 1 && go == 0)
{
if (arr[x1x][x1y + 1] == 'X' && arr[x1x][x1y + 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x + 1] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x - 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1x == 2 && go == 0)
{
if (arr[x1x][x1y + 1] == 'X' && arr[x1x][x1y + 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x + 1] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x - 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1y == 0 && go == 0)
{
if (arr[x1x + 1][x1y] == 'X' && arr[x1x + 2][x1y] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x + 1][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x - 2][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1y == 1 && go == 0)
{
if (arr[x1x + 1][x1y] == 'X' && arr[x1x + 2][x1y] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x + 1][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x - 2][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1y == 2 && go == 0)
181,1-8 83%
{
if (arr[x1x + 1][x1y] == 'X' && arr[x1x + 2][x1y] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x + 1][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x - 2][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
}
}
}
}
Take a look at these lines from your insertD function:
if (x1x == 0 && go == 0)
{
if (arr[x1x][x1y + 1] == 'X' && arr[x1x][x1y + 2] == 'X')
In this case you have checked that x1x is zero, but you haven't checked x1y. So in this case you will go out of bounds if x1y is non-zero.
A couple of lines below you have
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x + 1] == 'X')
This will go out of bounds too, when x1y is zero.
You need to add more checks, or rethink the logic.

Making C++ program to play quarto, having one issue with array elements

These functions in particular are what I'm having issues with, Piece is a struct implemented elsewhere in the file and only contains a 4 character string called "name". I'm trying to get my code so that it can compare 4 pieces in a row (in any direction), by comparing each of their 4 characters one at a time. The error that appears when I compile says that it cannot convert the string I am inputting into what it needs to operate further.
string comparePieces(string a, string b, string c, string d)
{
string sharedTrait;
if(a[0] == b[0] && a[0] == c[0] && a[0] == d[0])
sharedTrait = "true";
else if(a[1] == b[1] && a[1] == c[1] && a[1] == d[1])
sharedTrait = "true";
else if(a[2] == b[2] && a[2] == c[2] && a[2] == d[2])
sharedTrait = "true";
else if(a[3] == b[3] && a[3] == c[3] && a[3] == d[3])
sharedTrait = "true";
else
sharedTrait = "false";
return sharedTrait;
}
string getSpace(string space)
{
int r;
int c;
space == board[r][c].name;
return space;
}
string checkWinner()
{
string win = "false";
for(int i = 0; i<= 4; i++)
{
int r = i;
if( comparePieces( (getSpace(board[r][0].name)) , (getSpace(board[r][1].name)) , (getSpace(board[r][2].name)) , (getSpace(board[r][3].name)) ) == "true")
win = "true";
if( comparePieces( (getSpace(board[0][r].name)) , (getSpace(board[1][r].name)) , (getSpace(board[2][r].name)) , (getSpace(board[3][r].name)) ) == "true")
win = "true";
if( comparePieces( (getSpace(board[0][0].name)) , (getSpace(board[1][1].name)) , (getSpace(board[2][2].name)) , (getSpace(board[3][3].name)) ) == "true")
win = "true";
if( comparePieces( (getSpace(board[0][3].name)) , (getSpace(board[1][2].name)) , (getSpace(board[2][1].name)) , (getSpace(board[3][0].name)) ) == "true")
win = "true";
}
return win;
}

Sudoku Solving Function Explanation

I have been searching for a Sudoku Solving Algorithm for a while and I found this code. But I have some difficulties. I can't understand it. If there are conflicts with all numbers between 1 and 9 in a single cell, the program should stop, right? But it continues. Can somebody explain me how the code works, please? Here it is:
bool Sudoku::Help_Solve(int i, int j)
{
int nextrow, nextcol;
while(change[i][j] == 1) //We find the first cell in which we can change the number
{
j++;
if(j > 9)
{
j = 1;
i++;
}
if(i > 9) return true;
}
for(int p = 1; p <= 9; p++)
{
if(Game.Check_Conflicts(p, i, j)) //We are checking for conflicts
{
board[i][j] = p;
nextrow = i;
nextcol = j+1;
if(nextcol > 9)
{
nextcol = 1;
nextrow++;
}
if(nextcol == 1 && nextrow == 10) return true;
if(Game.Help_Solve(nextrow, nextcol)) return true;
}
}
board[i][j] = 0;
return false;
}
Not enough code to explain properly, what happens in Game.Check_Conflicts(p, i, j), is this function getting called recursively?
Here is the whole code if you want to see it:
#include <iostream>
#include <iomanip>
#include <time.h>
#include <cstdlib>
#include <windows.h>
using namespace std;
class Sudoku
{
private:
int board[9][9];
int change[9][9];
public:
Sudoku();
void Print_Board();
void Add_First_Cord();
bool Help_Solve(int i, int j);
bool Check_Conflicts(int p, int i, int j);
};
Sudoku Game;
void setcolor(unsigned short color) //The function that you'll use to
{ //set the colour
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}
Sudoku::Sudoku()
{
for(int i = 0; i <= 9; i++)
for(int j = 0; j <= 9; j++)
board[i][j] = 0;
}
void Sudoku::Print_Board()
{
for(int i = 1; i <= 9; i++)
{
for(int j = 1; j <= 9; j++)
{
if(change[i][j] == 1)
{
setcolor(12);
cout << board[i][j] << " ";
setcolor(7);
}
else cout << board[i][j] << " ";
if(j%3 == 0) cout << "| ";
}
cout << endl;
if(i%3 == 0) cout << "------+-------+--------" << endl;
}
}
void Sudoku::Add_First_Cord()
{
board[1][1] = 5; change[1][1] = 1;
board[1][2] = 3; change[1][2] = 1;
board[1][5] = 7; change[1][5] = 1;
board[2][1] = 6; change[2][1] = 1;
board[2][4] = 1; change[2][4] = 1;
board[2][5] = 9; change[2][5] = 1;
board[2][6] = 5; change[2][6] = 1;
board[3][2] = 9; change[3][2] = 1;
board[3][3] = 8; change[3][3] = 1;
board[3][8] = 6; change[3][8] = 1;
board[4][1] = 8; change[4][1] = 1;
board[4][5] = 6; change[4][5] = 1;
board[4][9] = 3; change[4][9] = 1;
board[5][1] = 4; change[5][1] = 1;
board[5][4] = 8; change[5][4] = 1;
board[5][6] = 3; change[5][6] = 1;
board[5][9] = 1; change[5][9] = 1;
board[6][1] = 7; change[6][1] = 1;
board[6][5] = 2; change[6][5] = 1;
board[6][9] = 6; change[6][9] = 1;
board[7][2] = 6; change[7][2] = 1;
board[7][7] = 2; change[7][7] = 1;
board[7][8] = 8; change[7][8] = 1;
board[8][4] = 4; change[8][4] = 1;
board[8][5] = 1; change[8][5] = 1;
board[8][6] = 9; change[8][6] = 1;
board[8][9] = 5; change[8][9] = 1;
board[9][5] = 8; change[9][5] = 1;
board[9][8] = 7; change[9][8] = 1;
board[9][9] = 9; change[9][9] = 1;
}
bool Sudoku::Check_Conflicts(int p, int i, int j)
{
for(int k = 1; k <= 9; k++)
if(board[i][k] == p) return false;
for(int q = 1; q <= 9; q++)
if(board[q][j] == p) return false;
/*
*00
000
000
*/
if((j == 1 || j == 4 || j == 7) && (i == 1 || i == 4 || i == 7))
{
if(board[i][j+1] == p || board[i][j+2] == p || board[i+1][j] == p ||
board[i+2][j] == p || board[i+1][j+1] == p || board[i+1][j+2] == p ||
board[i+2][j+1] == p || board[i+2][j+2] == p)return false;
}
/*
000
000
*00
*/
if((j == 1 || j == 4 || j == 7) && (i == 3 || i == 6 || i == 9))
{
if(board[i-1][j] == p || board[i-2][j] == p || board[i][j+1] == p ||
board[i][j+2] == p || board[i-1][j+1] == p || board[i-1][j+2] == p ||
board[i-2][j+1] == p || board[i-2][j+2] == p)return false;
}
/*
000
*00
000
*/
if((j == 1 || j == 4 || j == 7) && (i == 2 || i == 5 || i == 8))
{
if(board[i-1][j] == p || board[i-1][j+1] == p || board[i-1][j+2] == p ||
board[i][j+1] == p || board[i][j+2] == p || board[i+1][j] == p ||
board[i+1][j+1] == p || board[i+1][j+2] == p)return false;
}
/*
0*0
000
000
*/
if((j == 2 || j == 5 || j == 8) && (i == 1 || i == 4 || i == 7))
{
if(board[i][j-1] == p || board[i][j+1] == p || board[i+1][j+1] == p ||
board[i+1][j-1] == p || board[i+1][j] == p || board[i+2][j-1] == p ||
board[i+2][j] == p || board[i+2][j+1] == p)return false;
}
/*
000
0*0
000
*/
if((j == 2 || j == 5 || j == 8) && (i == 2 || i == 5 || i == 8))
{
if(board[i-1][j] == p || board[i-1][j-1] == p || board[i-1][j+1] == p ||
board[i][j+1] == p || board[i][j-1] == p || board[i+1][j+1] == p ||
board[i+1][j] == p || board[i+1][j-1] == p)return false;
}
/*
000
000
0*0
*/
if((j == 2 || j == 5 || j == 8) && (i == 3 || i == 6 || i == 9))
{
if(board[i][j-1] == p || board[i][j+1] == p || board[i-1][j] == p ||
board[i-1][j+1] == p || board[i-1][j-1] == p || board[i-2][j] == p ||
board[i-2][j+1] == p || board[i-2][j-1] == p) return false;
}
/*
00*
000
000
*/
if((j == 3 || j == 6 || j == 9) && (i == 1 || i == 4 || i == 7))
{
if(board[i][j-1] == p || board[i][j-2] == p || board[i+1][j] == p ||
board[i+1][j-1] == p || board[i+1][j-2] == p || board[i+2][j] == p ||
board[i+2][j-1] == p || board[i+2][j-2] == p) return false;
}
/*
000
00*
000
*/
if((j == 3 || j == 6 || j == 9) && (i == 2 || i == 5 || i == 8))
{
if(board[i-1][j] == p || board[i-1][j-1] == p || board[i-1][j-2] == p ||
board[i][j-1] == p || board[i][j-2] == p || board[i+1][j] == p ||
board[i+1][j-1] == p || board[i+1][j-2] == p) return false;
}
/*
000
000
00*
*/
if((j == 3 || j == 6 || j == 9) && (i == 3 || i == 6 || i == 9))
{
if(board[i][j-1] == p || board[i][j-2] == p || board[i-1][j] == p ||
board[i-1][j-1] == p || board[i-1][j-2] == p || board[i-2][j] == p ||
board[i-2][j-1] == p || board[i-2][j-2] == p) return false;
}
return true;
}
bool Sudoku::Help_Solve(int i, int j)
{
int nextrow, nextcol;
while(change[i][j] == 1)
{
j++;
if(j > 9)
{
j = 1;
i++;
}
if(i > 9) return true;
}
for(int p = 1; p <= 9; p++)
{
if(Game.Check_Conflicts(p, i, j))
{
board[i][j] = p;
nextrow = i;
nextcol = j+1;
if(nextcol > 9)
{
nextcol = 1;
nextrow++;
}
if(nextcol == 1 && nextrow == 10) return true;
if(Game.Help_Solve(nextrow, nextcol)) return true;
}
}
board[i][j] = 0;
return false;
}
int main()
{
Game.Add_First_Cord();
Game.Help_Solve(1, 1);
Game.Print_Board();
system("pause");
return 0;
}
It looks like Sudoku::Check_Conflicts returns true if the number CAN be placed there, or false if it CAN'T be placed there due to a simple conflict. A different function name could maybe better self-document the code.
The thing is rhat I can't understand why it continues if in the end it
returns false :/
It doesn't ALWAYS return at the bottom of the function tho':
bool Sudoku::Help_Solve(int i, int j)
{
int nextrow, nextcol;
while(change[i][j] == 1) //We find the first cell in which we can change the number
{
j++;
if(j > 9)
{
j = 1;
i++;
}
if(i > 9) return true;
-------------------------^^^^
returns true if we have filled all squares.
}
for(int p = 1; p <= 9; p++)
{
if(Game.Check_Conflicts(p, i, j)) //We are checking for conflicts
{
board[i][j] = p;
nextrow = i;
nextcol = j+1;
if(nextcol > 9)
{
nextcol = 1;
nextrow++;
}
if(nextcol == 1 && nextrow == 10) return true;
-----------------------------------------------------^^^^
returns when we have filled everything!
if(Game.Help_Solve(nextrow, nextcol)) return true;
---------------------------------------------------------^^^^
returns if we filled at the next level of solution.
}
}
board[i][j] = 0;
return false;
-----------^^^^^ returns if we failed to fill the whole thing.
}
As someone else mentioned in a comment, there are some trivial things that can be done to improve on the algorithm - such as looking for the "most suitable place to fill first" [which doesn't improve the worst case, but it does improve the typical case].
I have written a Sudoku solver that uses a similar algorithm, but it tries to find the cell with the lowest number of candidates (possible numbers to go in the that cell) and only tries recursively if there are multiple choices.