Iantiger Posted April 26, 2006 Share Posted April 26, 2006 Hi everyone, My coursework involves creating a sudoku-oriented game in C++ Everything's going swimmingly - I'm using SDL Libraries to generate a sudoku board on the screen, and then using event handling to allow interaction with the user. However, I'm currently programming the logic which is the last step. I've managed to get the program to evaluate every column/row and make sure that there is only one of each value (1-9) which appears in each column/row. Now I'm trying to evaluate every 3x3 square. Here's my code: //checks every 3x3 square for (int x = 0 ; x < 9 ; x+3) { for (int y = 0 ; y < 9 ; y+3) { //checks for every value 1-9 ERROR HERE: for (int number = 1 ; number < 10 ; number++)[/color] { //creates variable to store the number of occurences int count = 0; //checks each individual square of 3x3 for (int n = 0 ; n < 3 ; n++) { for (int m = 0 ; m < 3 ; m++) { //increments count if the number is in that square if (pBitmaps[x+n][y+m] == number) { count ++; } } } //ensures there is exactly one of each number per 3x3 square if (count != 1) { threebythree = false; } } } } I've got another statement further down which concludes the program if threebythree is true, however, whenever I run my program, it just freezes up. It didn't do this before I added the above code. Any ideas what's up? EDIT: I've identified the flagged line of code to be the issue. When number gets to 10, it automatically resets to 1, and x and y don't increment. Erego I'm stuck in an infinite loop. Much thanks, ~Ian Retired Tip It Moderator | Zybez Radio DJ - Listen Here Link to comment Share on other sites More sharing options...
tunaboy692004 Posted April 27, 2006 Share Posted April 27, 2006 Hey, i added you on msn i think i can help you, but in return i have a question for you Current Goals80/80 Fletching60/75 Woodcutting97/100 Combat Link to comment Share on other sites More sharing options...
Iantiger Posted April 27, 2006 Author Share Posted April 27, 2006 SOLVED Problem's in the FOR loops for x and y... x+3 isn't a valid increment it should be "x+=3" D'oh! ~Ian Retired Tip It Moderator | Zybez Radio DJ - Listen Here Link to comment Share on other sites More sharing options...
tunaboy692004 Posted April 27, 2006 Share Posted April 27, 2006 ahh i wasnt even really looking at that, more along the line that said "error" :P Current Goals80/80 Fletching60/75 Woodcutting97/100 Combat Link to comment Share on other sites More sharing options...
Hannibal Posted April 27, 2006 Share Posted April 27, 2006 The code seems very inefficient though. You loop over each square 9 times... A more efficient way would be to use lists and splice those, but I don't know how easy that is in C++. Another way would be to have a fixed number 9! (which happens to be 3628800) and divide by the number in each field. In the end you should have 1 remaining, and if you don't, something is wrong :) . You would need to take care to catch errors or allow for floating point numbers in the variable though (so not just integers), or the division screws up if it goes bad :) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now