Help with Sudoku in C++

Programming a Sudoku in C++

Page 1 of 1

7 Replies - 4072 Views - Last Post: 16 November 2008 - 02:38 PM Rate Topic: -----

#1 Ertxz18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-November 08

Help with Sudoku in C++

Posted 15 November 2008 - 08:03 PM

Hey everyone! I am currently trying to program a Sudoku in C++. What this program will do is it will read from a file called sudoku.txt that will look something like this
6 5 9 4 1 3 2 8 7
7 4 3 2 9 8 1 6 5
1 8 2 5 6 7 9 4 3
9 1 4 3 2 5 8 7 6
3 7 8 6 4 9 5 1 2
2 6 5 7 8 1 4 3 9
4 3 7 1 5 2 6 9 8
5 9 1 8 3 6 7 2 4
0 0 0 0 0 0 0 0 0
Of course this is a simple sudoku, its just a test, but anyways, after reading from the txt file, the program will put this into the 9X9 grid, and the zeros represent blank squares that need to be filled in.
The program will allow the player to add numbers, and check to see if it is still possible to win after each addition. When all 81 cells are filled in, the program will say whether the player has won or not. The Sudoku class should have a constructor, and a “read” function that reads in the file “Sudoku_game.txt,” a 9 by 9 matrix of digits from 0 to 9. (Zero indicates that no number has been put in that cell.) The class should also have a print function, a test for whether the current state of the game is one that cannot work (violating some of the constraints, such as two 3‟s in the same box), a test to tell whether all the cells have been filled with a number, and a function that would allow a player to enter numbers in the open cells of the game. The main function should create an empty game, read a file into the game, print the current state of the game board, and then allow the player to add numbers (using row and column numbers) to complete the puzzle. After each number entered, the game should decide if the game is still winnable (that is, whether any of the constraints have already been violated.) If the last number entered won‟t work, print an error message and change the number back to zero. If the player successfully completes the game, entering the last number without violating the rules, the program should print a congratulatory message and quit. If the last cell is filled with a number, but the solution is not correct, the game should print a sympathetic message.
I really need some help on this, so any help will be appreciated. Thanks!
#include <iostream>
#include <cstdlib>
using namespace std;
//class
class Sudoku {
private:

public:
	int value;
	ofstream Numbers;
	Numbers.open("sudoku.txt");
	Numbers >> value;
	void Print(char game[0][9]){
		cout << "\t+---+---+---+---+---+---+---+---+---+" << endl;
		for (int a=0; a<9; a++) {
			cout << "\t| ";
			for (int b=0; b<9; b++)
				cout << game[a][b] << " | ";
			cout << endl;
			cout << "\t+---+---+---+---+---+---+---+---+---+" << endl;
		}
	}
	void generation(char game[9][9]) //generation function with arg
	{
		int randV;
		int r,c;
		for(int a=0;a<81;a++)
		{
			for( r=0;r<9;r++)
			{
				for(c=0;c<9;c++)
				{
					randV=(rand()%9)+1;
					if(game[r][c]==0)
					{
						game[r][c]=randV;
						if(!isLegal()) //check for invalid then
						{
							game[r][c]=0; //make sqr=0;
						}
					}
				}
			}
		}
	}
	};

	void MakeMove(char game[][9], int, int, char);


	int main ( ) {
		char game[9][9];
		for (int a=0; a<9; a++)	 // initialize board
			for (int b=0; b<9; b++)
				game[a][b] = ' ';
		int done = 0, count = 0, row, col, num;
		while ( done == 0 ) {
			if (count%2 == 0) {
				cout << "Enter a number (r,c,n): ";
				cin >> row >> col >> num;
				MakeMove (game, row, col, num);   }
			//else {
			//	cout << "Enter move (r c) for O : ";
			//	cin >> row >> col;
			//	MakeMove (game, row, col, 'O');   }
			//done = CheckWinner (game);
			//PrintBoard (game);
			Print (game);
			count++;
		}
		if (done == 1)
			cout << "Winner" << endl;
		else if (done == 2)
			cout << "Winner" << endl;
		else   // done == 3
			cout << "Tie – cat’s game" << endl;
		return 0;
	}

	void MakeMove (char game[ ][3], int r, int c, char ch) {
		if (game[r][c] == ' ')
			game[r][c] = ch;
		else
			cout << "Illegal move,	turn is lost!" << endl;
	}



This post has been edited by Ertxz18: 15 November 2008 - 09:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with Sudoku in C++

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Help with Sudoku in C++

Posted 16 November 2008 - 05:03 AM

As we're not going to write the program for you, it would help if you told us what parts you specifically have problems with. A little help on the way however,

To check if the latest number input by the user is violating any constraint, you need to check 3 things.
* Is there another instance of the same digit on the same row
* Is there another instance of the same digit in the same column
* Is there another instance of the same digit in the same 3x3 box
I would have a function control this, let the function return a boolean (true/false). Simply find out where the digit is put (row, column and box) and loop over the area and count the number of the instance-digit in the area.

To check if the game is completed, simply have a counter that is incremented for each digit that is put into the game. The total should be 81 when the game is completed, the initial value should be the number of digits that are given from the start. When the counter reaches 81 you can simply check that there are no digits in the wrong places. scan each row, column and box to make sure every digit occur exactly once.
Was This Post Helpful? 0
  • +
  • -

#3 Ertxz18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-November 08

Re: Help with Sudoku in C++

Posted 16 November 2008 - 11:21 AM

View PostGloin, on 16 Nov, 2008 - 04:03 AM, said:

As we're not going to write the program for you, it would help if you told us what parts you specifically have problems with. A little help on the way however,

To check if the latest number input by the user is violating any constraint, you need to check 3 things.
* Is there another instance of the same digit on the same row
* Is there another instance of the same digit in the same column
* Is there another instance of the same digit in the same 3x3 box
I would have a function control this, let the function return a boolean (true/false). Simply find out where the digit is put (row, column and box) and loop over the area and count the number of the instance-digit in the area.

To check if the game is completed, simply have a counter that is incremented for each digit that is put into the game. The total should be 81 when the game is completed, the initial value should be the number of digits that are given from the start. When the counter reaches 81 you can simply check that there are no digits in the wrong places. scan each row, column and box to make sure every digit occur exactly once.

Thanks for that! I appreciate it, and I'm sorry I didn't specify which areas I needed help on. But what you said has helped me and I'm about to write some more code for it. Also, to check if the game is completed with a counter, how could I make that counter add the numbers that have alredy been inputted by the .txt file? Also how can I make the program read from the .txt file and put it in a grid? Is my code correct the way i wrote it? Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Help with Sudoku in C++

Posted 16 November 2008 - 11:35 AM

View PostErtxz18, on 16 Nov, 2008 - 10:21 AM, said:

Thanks for that! I appreciate it, and I'm sorry I didn't specify which areas I needed help on. But what you said has helped me and I'm about to write some more code for it. Also, to check if the game is completed with a counter, how could I make that counter add the numbers that have alredy been inputted by the .txt file? Also how can I make the program read from the .txt file and put it in a grid? Is my code correct the way i wrote it? Thanks!


To read values from a file, you'll have to read up on. In C, you create a filepointer and then there are various functions to read data depending on the format. While you read the data, simply count the number of digits that are given. You don't need to keep track of which digits has been counted, only how many..
Was This Post Helpful? 0
  • +
  • -

#5 Ertxz18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-November 08

Re: Help with Sudoku in C++

Posted 16 November 2008 - 11:42 AM

View PostGloin, on 16 Nov, 2008 - 10:35 AM, said:

To read values from a file, you'll have to read up on. In C, you create a filepointer and then there are various functions to read data depending on the format. While you read the data, simply count the number of digits that are given. You don't need to keep track of which digits has been counted, only how many..

Thanks! This is my updated code for keeping count, its under public, is that correct?
#include <iostream>
#include <cstdlib>
using namespace std;
//class
class Sudoku {
private:
		int value, count=0;

public:
	ofstream Numbers;
	Numbers.open("sudoku.txt");
	Numbers >> value;
	while (Numbers >> value){ //counter here
		count++;
	}
	void Print(char game[0][9]){
		cout << "\t+---+---+---+---+---+---+---+---+---+" << endl;
		for (int a=0; a<9; a++) {
			cout << "\t| ";
			for (int b=0; b<9; b++)
				cout << game[a][b] << " | ";
			cout << endl;
			cout << "\t+---+---+---+---+---+---+---+---+---+" << endl;
		}
	}
	void generation(char game[9][9]) //generation function with arg
	{
		int randV;
		int r,c;
		for(int a=0;a<81;a++)
		{
			for( r=0;r<9;r++)
			{
				for(c=0;c<9;c++)
				{
					randV=(rand()%9)+1;
					if(game[r][c]==0)
					{
						game[r][c]=randV;
						if(!isLegal()) //check for invalid then
						{
							game[r][c]=0; //make sqr=0;
						}
					}
				}
			}
		}
	}
	};

	void MakeMove(char game[][9], int, int, char);

	int main ( ) {
		char game[9][9];
		for (int a=0; a<9; a++)	 // initialize board
			for (int b=0; b<9; b++)
				game[a][b] = ' ';
		int done = 0, count = 0, row, col, num;
		while ( done == 0 ) {
			if (count%2 == 0) {
				cout << "Enter a number (r,c,n): ";
				cin >> row >> col >> num;
				MakeMove (game, row, col, num);   }
			Print (game);
			count++;
		}
		if (done == 1)
			cout << "Winner" << endl;
		else if (done == 2)
			cout << "Winner" << endl;
		else   // done == 3
			cout << "Tie – cat’s game" << endl;
		return 0;
	}

	void MakeMove (char game[ ][9][9], int r, int c, int n, char ch) {
		if (game[r][c][n] == ' ')
			game[r][c][n]= ch;
		else
			cout << "Illegal move,	turn is lost!" << endl;
	}



Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Help with Sudoku in C++

Posted 16 November 2008 - 11:51 AM

You could define the ofstream in your class but the actual reading needs to take place in a function that can be public.

edit: more specifically:

   ofstream Numbers;
    Numbers.open("sudoku.txt");
    Numbers >> value;
    while (Numbers >> value){ //counter here
        count++;



Needs to be in a function of some sort.

This post has been edited by KYA: 16 November 2008 - 11:54 AM

Was This Post Helpful? 0
  • +
  • -

#7 Ertxz18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-November 08

Re: Help with Sudoku in C++

Posted 16 November 2008 - 12:03 PM

View PostKYA, on 16 Nov, 2008 - 10:51 AM, said:

You could define the ofstream in your class but the actual reading needs to take place in a function that can be public.

edit: more specifically:

   ofstream Numbers;
    Numbers.open("sudoku.txt");
    Numbers >> value;
    while (Numbers >> value){ //counter here
        count++;



Needs to be in a function of some sort.

Ohh allright thanks! So it would look something like this
	void Read{
	ofstream Numbers;
	Numbers.open("sudoku.txt");
	Numbers >> value;
	while (Numbers >> value){
		count++;
	}
	}


And i would simply call that function later on as Read;
is that correct?
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Help with Sudoku in C++

Posted 16 November 2008 - 02:38 PM

Yes, that's the idea. However, without having comprehensive knowledge of your program as you do, I cannot say for sure if that would work the first time without further modification.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1