C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 307,084 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,261 people online right now. Registration is fast and FREE... Join Now!




Sodoku Solver and Checker

 

Sodoku Solver and Checker

xUSAFxPJx

30 May, 2009 - 12:41 PM
Post #1

New D.I.C Head
*

Joined: 30 May, 2009
Posts: 2

Hey guys, I am writing a C++ Sodoku Solver and Checker. This is the code I have so far
CODE

#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

CODE

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.

User is offlineProfile CardPM
+Quote Post


stayscrisp

RE: Sodoku Solver And Checker

30 May, 2009 - 02:19 PM
Post #2

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,403



Thanked: 53 times
Dream Kudos: 300
My Contributions

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.

icon_up.gif



User is offlineProfile CardPM
+Quote Post

xUSAFxPJx

RE: Sodoku Solver And Checker

31 May, 2009 - 08:37 AM
Post #3

New D.I.C Head
*

Joined: 30 May, 2009
Posts: 2

QUOTE(stayscrisp @ 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.

icon_up.gif

Thanks, I learned the game now but still I don't understand what i need to do check it.
User is offlineProfile CardPM
+Quote Post

baavgai

RE: Sodoku Solver And Checker

31 May, 2009 - 10:30 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,348



Thanked: 411 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(xUSAFxPJx @ 31 May, 2009 - 10:37 AM) *

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.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/21/09 10:42AM

Live C++ Help!

Be Social

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

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month