April 26, 200620 yr 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
April 27, 200620 yr 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
April 27, 200620 yr Author 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
April 27, 200620 yr ahh i wasnt even really looking at that, more along the line that said "error" :P Current Goals80/80 Fletching60/75 Woodcutting97/100 Combat
April 27, 200620 yr 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 :)
Create an account or sign in to comment