The semester is ending and our programming #TC1017 C++ course will come to an end; but not before we turn in our final project, which kept us busy for the last couple weeks. Something pretty curious happened to me during this course. I thought challenges were huge until i started getting into them and breaking them into little pieces which didn´t took me long to figure out. After what might have been a couple intense days of thinking and planning, my teammate and I got our sudoku working.
I´ll try to explain, in general terms, how our program works.
Vectors may exist in two dimensions. In this case, our sudoku board is simply a two dimensional vector that is modified by the main function in two sections: WRITE & ERASE. But before writing and erasing we had to check 4 things.
1: You may not modify or erase and original number.
First, we solved this creating a paralel clone vector that was not going to be modified during the game; so everytime the user wanted to erase or to write a new value inside the vector, the program was going to check whether that given position contained or not an original number that belonged to the level. If no original number was found, then the program could continue and write a new value, otherwise, a warning message would print. After talking to ken about a bug, he saw our huge conditonal code creating a huge, 81-slot, vector everytime the Write section was called. He said that our program would run slow and that there was an easier way and more efficient solution to our problem. Negative numbers. The program basically writes every original extracted input as a negative value, but prints the absolute value. So it checks if the value is or not negative. If is, then it is an original number, if not, then… No.
2, 3 & 4: May not repeat a number inside the same cuadrant, on the same row or on the same column:
Columns and Rows were easy to solve. Write a couple nested for loops that walk the conditional (looking for input) through each row or column given and return a boolean if found… The challenge was cuadrants. Since cuadrants were never defined in our vector, we had to create a function that returns the numbered cuadrant the input was located in (1-9), and with this location, a second group of nested for loops would look for a coincidence with the input given. All these where written inside a boolean function that returned TRUE if repeated.
All this development was written after we had checked the input was a number between 1 and 9 and that we read the initial menu input from the user (W for write, E for erase, Q for quit… etc) the last step was writing a function that told the user he had won and aesthetic details.
I´ll include some pictures and the link for the code in gitHub.
Rodrigo and JuanCarlos´s Sudoku Final Project
How the main menu looks like.
You can read how to play sudoku and some history of how sudoku was created.
Here´s the writing interface. The program asks for Row and Column. Notice the new “2”.
When you complete a level, the program lets you know right away!
Feel free to download the code and play it in your computer.