*If you don't know how to play Sudoku, this might not make much sense.*
I'm relatively new to programming and I need some help with this program I'm writing. It's being compiled in Microsoft Visual C++ 2008 Express.
This program will hopefully be able to beat Sudoku puzzles when I'm done with it.
In it, the user inputs the numbers that are on a sudoku game into the program. They put in "0," if the spot is blank.
After the user puts in all 81 spots, the program starts trying different combinations of numbers in the blank spots until it finds a working answer.
Now...I haven't gotten too far into writing this, but as of now, the user can input all numbers and the program can read through and spit them out. I have coded the part of the program that deciphers the blank ("0") spots, but the program only, so far, is basing it off what's in the x-axis of the 0.
Also, that last part doesn't seem to work...but it should! I think...
I'm just going to past the whole code, because as of now, the program's not *too* large and I'm not sure where the actual problem might be.
And before anyone says anything, yes I'm using the "goto" function...yeah yeah... I've heard....I know how to not use it, but as far as I can tell, that's not what's causing the problem.
There are a lot of extra bits in the code that I used to test the program with. All of the comments were applied before I made this post, while writing the program, so if any of it doesn't make sense, just ask.
It outputs all of the numbers back correctly, but it can't seem to find the right number for zero, unless the user types in the numbers in order from one to nine.
Line 74 is where it outputs the chosen number for the blank spot.
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
|
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int a [9][10]; int b =0; int c =0; int d =0; int e; int f; int testb; int br;
//user puts in known sudoku numbers and this program systematically
//tries combinations of numbers until it finds a solution.
int main()
{
//PART ONE where the user inputs the given numbers
cout << "Tell me where each of the numbers are that you know of. \n";
while (c < 9)
{
cout << "What is in ["; cout << b+1; cout << ", "; cout << c+1; cout << "]? ";
d = 0;
cin >> d;//user inputs given number in slot
a [b][c] = d;
/*TEST*/cout << d;
/*TEST*/cout << a [b][c];
/*TEST*/cout << a [8][0];
/*TEST*/cout << a [0][1];
b = b+1;//we move to the next spot on the row...
if (b == 9)//checking if it's the end of the row...
{c = c+1;//it is, so we move down one spot on the y-axis
b = 0;}//spot next checked will be (0,(last + 1))
}//-
//PART TWO where the computer deciphers what the missing numbers are
b = 0;
c = 0;
loop2:
//A NUMBER WAS GIVEN
if (a [b][c] > 0)
{
//user had a number already in in this spot, so the program skips over it.
cout << a [b][c];
b += 1;
if (b > 8)
{
c += 1;
b = 0;
}
if (c == 9)
{/*TEST*/cout << "hello";}
else
{goto loop2;}
}
//A ZERO WAS GIVEN
else
{
//user had a zero in this spot, so the program tests possible numbers.
cout << "hi";
//--+
testb = 1;
br = b;
b = 0;
startb://-for the x-axis
if (a [b][c] == testb)
{
testb = testb + 1;
goto startb;
}
else
{
b = b + 1;
if (b > 8)
{
b = br;
a [b][c] = testb;
cout << a [b][c]; cout << "is b ";
b = b + 1;
goto loop2;
}
goto startb;
}
}
/*TEST*/cout << a [0][0];
/*TEST*/cout << a [0][5];
/*TEST*/cout << a [3][0];
/*TEST*/cout << a [8][8];
cout << "You Win!\n \n Press any key and then <enter> to continue!";
int z;
cin >> z;
cin.get();
return 0;
}//end of int(main)
|