School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,154 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,982 people online right now. Registration is fast and FREE... Join Now!



Sodoku Solver and Checker

Sodoku Solver and Checker Rate Topic: -----

#1 xUSAFxPJx  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 2
  • Joined: 30-May 09


Dream Kudos: 0

Posted 30 May 2009 - 12:41 PM

Hey guys, I am writing a C++ Sodoku Solver and Checker. This is the code I have so far
#include <iostream>
#include <string>
#include <math.h>
#include <conio.h>
#include <windows.h>
#include <ctime> 
#include <cstdlib>
#pragma comment(lib,"kernel32.lib")
using namespace std;
unsigned char currPlayer = 1, p=179;
int firstPrint = 1;
bool moveLegal(char board[9][9], int row, int column)
{
	return NULL;
}
void printBoard(char board[9][9])
{
	for (int Alpha=0; Alpha<9; Alpha++)
	{
		for (int  Bravo=0; Bravo<9; Bravo++)
		{
			cout << board[Alpha][Bravo];
			if (Bravo<9)
				cout << p;
		}
		cout << "\n";
		if (Alpha<10)
		{
			for (int Delta=0; Delta<18; Delta++)
				cout << "_";
			cout << "\n";
		}
	}
}

bool spaceEmpty(char board[9][9], int row, int column)
{
	if (board[(row-1)][(column-1)]==NULL)
		return true;
	return false;
}
void inputStuff(char board[9][9], int row, int column)
{
	system("CLS");
	if (spaceEmpty(board, row, column))
	{
		board[(row-1)][(column-1)]=currPlayer;
		printBoard(board);
	}
	else
		cout<<"Space is filled\n";
}

void main()
{
	char board[9][9]={ ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
	int row, column, playAgain=1, numTie=0;
	while (playAgain==1)
	{
		for (int Zulu=0; Zulu<9; Zulu++)
		{
			for(int Romeo=0; Romeo<9; Romeo++)
				board[Zulu][Romeo]=NULL;
		}
		while (playAgain == 1)
		{
			if(firstPrint == 1)
			{
				printBoard(board);
				firstPrint = 2;
			}
			cout <<"What number is next?";
			cin >> currPlayer;
			cout <<"\nWhere would you like to put the next number?\n";
			cin >> row >> column;
			inputStuff(board, row, column);
		}
	}
}



this is what it has to do

Sudoku Checker
There are two parts to the program that you must design: the setup and the checker.
Setup: Your program will start by setting up a Sudoku board that the player has by asking for the placement of the numbers.  Ask to place all 1’s first then all 2’s and so on. When they are done with each number output the board so far and confirm that the most recent numbers input are  in the correct position.
For example:
Do you have anymore 1’s? (Y or N) Y 
Column? 3
Row? 5
Do you have anymore 1’s? (Y or N) N
   |   |
   |   |
   |   |
__________
   |   |
  5|   |
   |   |
__________
   |   |
   |   |
   |   |
Are all 1’s in correct position? (Y or N) Y

Checker: Once all numbers have been put into position ask the user what number and where they would like to play. Check and make sure that the move is legal so far for the row, column, and 3X3 box. If the move is legal place it on the board. If not tell the user why the move is illegal. Then give the user the option of moving on from there or clearing the board back to the original and start placing numbers again. Your program should stop and congratulate the player when all spaces are filled and there are no conflicts in any row, column, or 3X3 box.
For example:
What number would you like to place next? 4
In what row would you like to place a 4? 2
In what column would you like to place a 4? 7
Move is legal!
   |   |
   |   |4
   |   |
__________
   |   |
  5|   |
   |   |
__________
   |   |
   |   |
   |   |
What number would you like to place next? 5
In what row would you like to place a 5? 9
In what column would you like to place a 5? 3
Illegal move. There is already a 5 in that column. Would you like to clear the board and start guessing again? Y
 



I have been working on this for a while and have yet to discover a way I can check if the move is legal. But the worst part is I don't know how to play sodoku. lol... If anyone could help, Thanks.
Was This Post Helpful? 0
  • +
  • -


#2 stayscrisp  Icon User is offline

  • Cheek-> Insert(Tongue);
  • Icon
  • View blog
  • Group: Author w/DIC++
  • Posts: 1,539
  • Joined: 14-February 08


Dream Kudos: 300

Posted 30 May 2009 - 02:19 PM

Hey there

I think you should read up on how to play sudoku before attempting something like this as the logic is very much rooted in the game itself.

This should help.

:^:
Was This Post Helpful? 0
  • +
  • -

#3 xUSAFxPJx  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 2
  • Joined: 30-May 09


Dream Kudos: 0

Posted 31 May 2009 - 08:37 AM

View Poststayscrisp, on 30 May, 2009 - 02:19 PM, said:

Hey there

I think you should read up on how to play sudoku before attempting something like this as the logic is very much rooted in the game itself.

This should help.

:^:

Thanks, I learned the game now but still I don't understand what i need to do check it.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • Icon
  • View blog
  • Group: Expert w/DIC++
  • Posts: 4,727
  • Joined: 16-October 07


Dream Kudos: 575

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

Posted 31 May 2009 - 10:30 AM

View PostxUSAFxPJx, on 31 May, 2009 - 10:37 AM, said:

Thanks, I learned the game now but still I don't understand what i need to do check it.


Every one of the 81 positions on the board belongs to a particular row, column, and box. If the position has a value, then that value must not exist elsewhere in any of those three domains. If it does, the sudoku is invalid.

If you know a board is valid, you can try to solve it by finding all valid values for a particular empty position. If the position has only one valid value, then that's what it has to be. If you can not solve the entire thing using this logic, you can either get creative and test for logical states or just brute force search all possibles.

Good luck.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month