GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Feb-2007, 02:32
MiZaRiZ MiZaRiZ is offline
New Member
 
Join Date: Feb 2007
Posts: 3
MiZaRiZ is on a distinguished road

Sudoku - Lite version


Hey first post on the board. Also one of my first programs.
I don't include code, and i have read the "read this before posting"

Anyhow, i was wondering if any of you have done a lite version or like it before or know of one. And yes i have been goggling a lot, also did a search of this board. Got a recommendation from a friend.

I have made a fixed solution from the start, but from here on, i don't know what to do.

int sudoku[][]={8,5,4,9,6,4,7,2,1},{6,2,9,7,5,1,3,8,4},{7,3,1,2, 8,4,6,9,5},{4,7,5,6,1,9,8,3,2},{3,9,8,5,2,7,4,1,6} ,{1,6,2,4,3,8,5,7,9},{9,8,6,3,4,2,1,5,7},{5,1,7,8, 9,6,2,4,3},{2,4,3,1,7,5,9,6,8}

Its a exam project and would welcome any help from you guys, but sure i also understand if you don't want to help me
  #2  
Old 28-Feb-2007, 09:02
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sudoku - Lite version


Quote:
Originally Posted by MiZaRiZ
I have made a fixed solution from the start, but from here on, i don't know what to do.

I don't know what you are supposed to do.

Assuming that the quantities inside the braces represent the rows, you have not posted a valid solution sudoku. Here is a representation in ascii. Note that the digit 4 appears twice on the first row.

Code:
+---+---+---+ +---+---+---+ +---+---+---+ | 8 | 5 | 4 | | 9 | 6 | 4 | | 7 | 2 | 1 | +---+---+---+ +---+---+---+ +---+---+---+ | 6 | 2 | 9 | | 7 | 5 | 1 | | 3 | 8 | 4 | +---+---+---+ +---+---+---+ +---+---+---+ | 7 | 3 | 1 | | 2 | 8 | 4 | | 6 | 9 | 5 | +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ | 4 | 7 | 5 | | 6 | 1 | 9 | | 8 | 3 | 2 | +---+---+---+ +---+---+---+ +---+---+---+ | 3 | 9 | 8 | | 5 | 2 | 7 | | 4 | 1 | 6 | +---+---+---+ +---+---+---+ +---+---+---+ | 1 | 6 | 2 | | 4 | 3 | 8 | | 5 | 7 | 9 | +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ | 9 | 8 | 6 | | 3 | 4 | 2 | | 1 | 5 | 7 | +---+---+---+ +---+---+---+ +---+---+---+ | 5 | 1 | 7 | | 8 | 9 | 6 | | 2 | 4 | 3 | +---+---+---+ +---+---+---+ +---+---+---+ | 2 | 4 | 3 | | 1 | 7 | 5 | | 9 | 6 | 8 | +---+---+---+ +---+---+---+ +---+---+---+

Or, are the things in braces supposed to represent the numbers in the "little squares"? (Still not valid, since the number 4 appears twice in the first group.) Or what?

What is your program supposed to do?

Is the program supposed to print puzzles, generate puzzles, solve puzzles or validate proposed solutions? Or what?


Have you ever solved sudoku puzzles? (By "hand", I mean.)


Regards,

Dave
Last edited by davekw7x : 28-Feb-2007 at 10:17.
  #3  
Old 28-Feb-2007, 09:23
MiZaRiZ MiZaRiZ is offline
New Member
 
Join Date: Feb 2007
Posts: 3
MiZaRiZ is on a distinguished road

Re: Sudoku - Lite version


Thanks for your replay.

Quote[

What is your program supposed to do?

Is the program supposed to print puzzles, generate puzzles, solve puzzles or validate proposed solutions? Or what?
]

Its supposed to give a player a game of sort, trying to solve this puzzled. And no never played sudoku before, just got the assignment.

Quote[
For example, if I swap the first two digits on the first row, is the program supposed to test whether it still valid? (To me, it obviously is not.) Or what?
]

I don't know how to do this but as its a fixed set of numbers, given by myself, don't think it has to. But sure would be nice to put in, given time.

----
Jesper
  #4  
Old 28-Feb-2007, 10:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sudoku - Lite version


First of all, notice that my first response was posted too hastily. I edited it to show that the puzzle that I printed out is not valid. I'm sorry that I got it wrong at first.

Quote:
Originally Posted by MiZaRiZ
Its supposed to give a player a game of sort, trying to solve this puzzled. And no never played sudoku before, just got the assignment.

Then I respectfully suggest that you learn the rules of the "game" and try to solve a few "by hand" before even thinking about how to write a program to do whatever it is that you are supposed to do.

Regards,

Dave
  #5  
Old 02-Mar-2007, 03:02
MiZaRiZ MiZaRiZ is offline
New Member
 
Join Date: Feb 2007
Posts: 3
MiZaRiZ is on a distinguished road

Re: Sudoku - Lite version


Quote:
Originally Posted by davekw7x
Then I respectfully suggest that you learn the rules of the "game" and try to solve a few "by hand" before even thinking about how to write a program to do whatever it is that you are supposed to do.

Done a few now and a thing that worry me are that you write down a number that it could be, that you cant do on the computer, it wants to know the "true" answer. Therefore it can be kind of hard doing it on a computer, but also easyere, you can just type and hope for the correct answer.

And about what i am supposed to do. I have to make this "game" so that my player can play one game, a fixed one. And then i need to understand whats going on in my source code. Thats about all. I have been told that grafik i shouldn't care about.

Jesper
  #6  
Old 02-Mar-2007, 13:58
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Sudoku - Lite version


You need some kind of input to set up the initial board. You could type in something like 000030000 to load a row with a 3 in the center of the center box. Accept 8 more rows to load the starting position.
Then accept a value and a location 4,2,7 (put a 4 in 2nd row, 7th col) then using the rules the program verifies that the number and location are valid.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #7  
Old 03-Mar-2007, 07:40
davis
 
Posts: n/a

Re: Sudoku - Lite version


What I think WaltP is trying to say is that you need a C++ design that encapsulates the data representation of a Sudoku puzzle and provides appropriate interfaces to that data.

Here is a rough approximation of a class that may possibly provide some of the features that you might expect to see in such an implementation:

Sudoku.h

CPP / C++ / C Code:
#ifndef _Sudoku_h_
#define _Sudoku_h_ 1

#include <iostream>
#include <fstream>
#include <string>

class Sudoku
{
public:
    Sudoku();
    Sudoku( const int max_rows, const int max_columns );
    Sudoku( const int max_rows, const int max_columns, const char* input_filename );
    Sudoku( const int max_rows, const int max_columns, std::string const& input_filename );
    ~Sudoku();
    friend std::ostream& operator<<( std::ostream& os, Sudoku const& puzzle );
    friend std::istream& operator>>( std::istream& is, Sudoku& puzzle );
    int getValue( const int row, const int column );
    void setValue( const int row, const int column, const int value );
    void displayPuzzle();
protected:
private:
    int m_max_rows;
    int m_max_columns;
    int** m_board;
    std::ifstream m_default_sudoku_file;
    void init();
    std::string m_default_sudoku_input_filename;
};

#endif // ! _Sudoku_h_

Sudoku.cpp

CPP / C++ / C Code:
#ifndef _Sudoku_h_
#include <Sudoku.h>
#endif

Sudoku::Sudoku() :
m_max_rows( 9 ),
m_max_columns( 9 ),
m_board( 0 ),
m_default_sudoku_input_filename( "/tmp/sudoku.txt" )
{
    init();
}

Sudoku::Sudoku( const int max_rows, const int max_columns ) :
m_max_rows( max_rows ),
m_max_columns( max_columns ),
m_board( 0 ),
m_default_sudoku_input_filename( "/tmp/sudoku.txt" )
{
    init();
}

Sudoku::Sudoku( const int max_rows, const int max_columns, const char* input_filename ) :
m_max_rows( max_rows ),
m_max_columns( max_columns ),
m_board( 0 ),
m_default_sudoku_input_filename( input_filename )
{
    init();
}

Sudoku::Sudoku( const int max_rows, const int max_columns, std::string const& input_filename ) :
m_max_rows( max_rows ),
m_max_columns( max_columns ),
m_board( 0 ),
m_default_sudoku_input_filename( input_filename )
{
    init();
}

Sudoku::~Sudoku()
{
    if( m_board )
    {
        for( int i = 0; i < m_max_rows; i++ )
        {
            delete [] m_board[i];
        }
        delete [] m_board;
    }
}
std::ostream& operator<<( std::ostream& os, Sudoku const& puzzle )
{
    for( int rows = 0; rows < puzzle.m_max_rows; rows++ )
    {
        for( int columns = 0; columns < puzzle.m_max_columns; columns++ )
        {
            os << puzzle.m_board[rows][columns] << " ";
        }
        os << std::endl;
    }
    os << std::endl;
    return os;
}

std::istream& operator>>( std::istream& is, Sudoku& puzzle )
{
    for( int rows = 0; rows < puzzle.m_max_rows; rows++ )
    {
        for( int columns = 0; columns < puzzle.m_max_columns; columns++ )
        {
            is >> puzzle.m_board[rows][columns];
        }
    }
    return is;
}

int
Sudoku::getValue( const int row, const int column )
{
    int result = 0;
    if( row < m_max_rows && column < m_max_columns )
    {
        result = m_board[row][column];
    }
    return result;
}

void
Sudoku::setValue( const int row, const int column, const int value )
{
    int set_value = 0;
    if( row < m_max_rows && column < m_max_columns )
    {
         set_value = value;
    }
    m_board[row][column] = set_value;
}

void 
Sudoku::init()
{
    // generate a ¿unique? or fixed Sudoku puzzle...
    std::cout << "Sudoku Menu\n\n1: Enter 9x9 Sudoku Puzzle Givens (Use Zero for Blank)\n2: Use Fixed 9x9 Sudoku Puzzle Givens\n3: Enter Custom Sudoku Puzzle Size\n\nQ: Quit\n";
    char c;
    std::cin >> c;
    switch( c )
    {
    case '1':
        std::cout << "Enter 9x9 Sudoku Puzzle Givens (Use Zero for Blank)\n";
        m_max_rows = 9;
        m_max_columns = 9;
        m_board = new int*[m_max_rows];
        for( int rows = 0; rows < m_max_rows; rows++ )
        {
            m_board[rows] = new int[m_max_columns];
        }
        std::cin >> *this;
        break;
    case '2':
        m_max_rows = 9;
        m_max_columns = 9;
        m_board = new int*[m_max_rows];
        for( int rows = 0; rows < m_max_rows; rows++ )
        {
            m_board[rows] = new int[m_max_columns];
        }
        std::cout << "Fixed 9x9 Sudoku Puzzle Generated!" << std::endl;
        m_default_sudoku_file.open( m_default_sudoku_input_filename.c_str() );
        if( m_default_sudoku_file.good() && !m_default_sudoku_file.eof() && m_default_sudoku_file.is_open() )
        {
            m_default_sudoku_file >> *this;
            m_default_sudoku_file.close();
        }
        break;
    case '3':
        std::cout << "Enter number of rows: ";
        unsigned char rows;
        std::cin >> rows;
        std::cout << "Enter number or columns: ";
        unsigned char columns;
        std::cin >> columns;
        m_board = new int*[m_max_rows];
        for( int rows = 0; rows < m_max_rows; rows++ )
        {
            m_board[rows] = new int[m_max_columns];
        }
        std::cin >> *this;
        break;
    case 'Q':
    case 'q':
        break;
    default:
        break;
    }
}

void
Sudoku::displayPuzzle()
{
    std::string sHeader;
    for( int i = 0; i < m_max_columns; i++ )
    {
        sHeader += "+---";
    }
    sHeader += "+";
    for( int i = 0; i < m_max_rows; i++ )
    {
        std::cout << sHeader << std::endl;
        for( int j = 0; j < m_max_columns; j++ )
        {
            std::cout << "| ";
            if( m_board[i][j] != 0 )
            {
                std::cout << m_board[i][j];
            }
            else
            {
                std::cout << " ";
            }
            std::cout << " ";
        }
        std::cout << "|" << std::endl;
    }
    std::cout << sHeader << std::endl;
}

main.cpp

CPP / C++ / C Code:
#ifndef _Sudoku_h_
#include <Sudoku.h>
#endif


int main()
{
    Sudoku sudoku;
    sudoku.displayPuzzle();
    std::cout << std::endl;
    std::cout << sudoku << std::endl;

    sudoku.setValue( 0, 2, 4 );
    sudoku.setValue( 0, 3, 6 );
    sudoku.setValue( 0, 5, 8 );
    sudoku.setValue( 0, 6, 9 );
    sudoku.setValue( 0, 7, 1 );
    sudoku.setValue( 0, 8, 2 );

    sudoku.setValue( 1, 1, 7 );
    sudoku.setValue( 1, 2, 2 );
    sudoku.setValue( 1, 6, 3 );
    sudoku.setValue( 1, 7, 4 );
    sudoku.setValue( 1, 8, 8 );

    sudoku.setValue( 2, 0, 1 );
    sudoku.setValue( 2, 3, 3 );
    sudoku.setValue( 2, 4, 4 );
    sudoku.setValue( 2, 5, 2 );
    sudoku.setValue( 2, 6, 5 );
    sudoku.setValue( 2, 8, 7 );

    sudoku.setValue( 3, 1, 5 );
    sudoku.setValue( 3, 2, 9 );
    sudoku.setValue( 3, 3, 7 );
    sudoku.setValue( 3, 5, 1 );
    sudoku.setValue( 3, 6, 4 );
    sudoku.setValue( 3, 7, 2 );

    sudoku.setValue( 4, 1, 2 );
    sudoku.setValue( 4, 2, 6 );
    sudoku.setValue( 4, 4, 5 );
    sudoku.setValue( 4, 6, 7 );
    sudoku.setValue( 4, 7, 9 );

    sudoku.setValue( 5, 1, 1 );
    sudoku.setValue( 5, 2, 3 );
    sudoku.setValue( 5, 3, 9 );
    sudoku.setValue( 5, 5, 4 );
    sudoku.setValue( 5, 6, 8 );
    sudoku.setValue( 5, 7, 5 );

    sudoku.setValue( 6, 0, 9 );
    sudoku.setValue( 6, 2, 1 );
    sudoku.setValue( 6, 3, 5 );
    sudoku.setValue( 6, 4, 3 );
    sudoku.setValue( 6, 5, 7 );
    sudoku.setValue( 6, 8, 4 );

    sudoku.setValue( 7, 0, 2 );
    sudoku.setValue( 7, 1, 8 );
    sudoku.setValue( 7, 2, 7 );
    sudoku.setValue( 7, 6, 6 );
    sudoku.setValue( 7, 7, 3 );

    sudoku.setValue( 8, 0, 3 );
    sudoku.setValue( 8, 1, 4 );
    sudoku.setValue( 8, 2, 5 );
    sudoku.setValue( 8, 3, 2 );
    sudoku.setValue( 8, 5, 6 );
    sudoku.setValue( 8, 6, 1 );

    sudoku.displayPuzzle();
    std::cout << std::endl;
    std::cout << sudoku << std::endl;


    return 0;
}

/tmp/sodoku.txt file contents:

Code:
5 3 0 0 7 0 0 0 0 6 0 0 1 9 5 0 0 0 0 9 8 0 0 0 0 6 0 8 0 0 0 6 0 0 0 3 4 0 0 8 0 3 0 0 1 7 0 0 0 2 0 0 0 6 0 6 0 0 0 0 2 8 0 0 0 0 4 1 9 0 0 5 0 0 0 0 8 0 0 7 9

Output:

Code:
Sudoku Menu 1: Enter 9x9 Sudoku Puzzle Givens (Use Zero for Blank) 2: Use Fixed 9x9 Sudoku Puzzle Givens 3: Enter Custom Sudoku Puzzle Size Q: Quit 2 Fixed 9x9 Sudoku Puzzle Generated! +---+---+---+---+---+---+---+---+---+ | 5 | 3 | | | 7 | | | | | +---+---+---+---+---+---+---+---+---+ | 6 | | | 1 | 9 | 5 | | | | +---+---+---+---+---+---+---+---+---+ | | 9 | 8 | | | | | 6 | | +---+---+---+---+---+---+---+---+---+ | 8 | | | | 6 | | | | 3 | +---+---+---+---+---+---+---+---+---+ | 4 | | | 8 | | 3 | | | 1 | +---+---+---+---+---+---+---+---+---+ | 7 | | | | 2 | | | | 6 | +---+---+---+---+---+---+---+---+---+ | | 6 | | | | | 2 | 8 | | +---+---+---+---+---+---+---+---+---+ | | | | 4 | 1 | 9 | | | 5 | +---+---+---+---+---+---+---+---+---+ | | | | | 8 | | | 7 | 9 | +---+---+---+---+---+---+---+---+---+ 5 3 0 0 7 0 0 0 0 6 0 0 1 9 5 0 0 0 0 9 8 0 0 0 0 6 0 8 0 0 0 6 0 0 0 3 4 0 0 8 0 3 0 0 1 7 0 0 0 2 0 0 0 6 0 6 0 0 0 0 2 8 0 0 0 0 4 1 9 0 0 5 0 0 0 0 8 0 0 7 9 +---+---+---+---+---+---+---+---+---+ | 5 | 3 | 4 | 6 | 7 | 8 | 9 | 1 | 2 | +---+---+---+---+---+---+---+---+---+ | 6 | 7 | 2 | 1 | 9 | 5 | 3 | 4 | 8 | +---+---+---+---+---+---+---+---+---+ | 1 | 9 | 8 | 3 | 4 | 2 | 5 | 6 | 7 | +---+---+---+---+---+---+---+---+---+ | 8 | 5 | 9 | 7 | 6 | 1 | 4 | 2 | 3 | +---+---+---+---+---+---+---+---+---+ | 4 | 2 | 6 | 8 | 5 | 3 | 7 | 9 | 1 | +---+---+---+---+---+---+---+---+---+ | 7 | 1 | 3 | 9 | 2 | 4 | 8 | 5 | 6 | +---+---+---+---+---+---+---+---+---+ | 9 | 6 | 1 | 5 | 3 | 7 | 2 | 8 | 4 | +---+---+---+---+---+---+---+---+---+ | 2 | 8 | 7 | 4 | 1 | 9 | 6 | 3 | 5 | +---+---+---+---+---+---+---+---+---+ | 3 | 4 | 5 | 2 | 8 | 6 | 1 | 7 | 9 | +---+---+---+---+---+---+---+---+---+ 5 3 4 6 7 8 9 1 2 6 7 2 1 9 5 3 4 8 1 9 8 3 4 2 5 6 7 8 5 9 7 6 1 4 2 3 4 2 6 8 5 3 7 9 1 7 1 3 9 2 4 8 5 6 9 6 1 5 3 7 2 8 4 2 8 7 4 1 9 6 3 5 3 4 5 2 8 6 1 7 9

Note that due to time constraints in preparing this limited example, I did not test several elements of the code, so your mileage may vary. I also didn't format the output as sexy as Dave's output, but hopefully it will give you some ideas for your own effort.

Obviously, one would need to implement some kind of puzzle validation routine and provide for user input beyond the basic interfaces provided by the extremely basic Sudoku class provided in this example.


:davis:
 
 

Recent GIDBlogI?m Home by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
versions onauc C++ Forum 13 21-Nov-2004 00:24
Apache perl version mismatch - can I fix it? chinagirl Apache Web Server Forum 1 25-May-2004 02:13
How do I detect flash version on my computer system? rhino1616 Graphics Forum 2 23-Oct-2003 10:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:52.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.