Jump to content

Help me overcome a problem with my C++ project


Iantiger

Recommended Posts

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

newsig4ty.jpg

Retired Tip It Moderator | Zybez Radio DJ - Listen Here

Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.