Jump to content

need help with C++


rakudave
 Share

Recommended Posts

I found a source and i don't understand it.

i know, "this is no general help forum", but

could someone translate this to autoit??? plz help!

//-------------------------------------------------------------------------------------
//
/// @file Pseudoku.cpp 
/// Simple Soduko Solver by Thomas Busser
//
//      @version        \$Revision: 1.0 $
//      @date           Creation: 06-August-2005
//
//-------------------------------------------------------------------------------------


#include "stdafx.h"

typedef unsigned char   u8;
typedef unsigned short  u16;
typedef unsigned long   u32;

typedef u8 Sudoku[9][9];

#define MEASURING_SUDOKU_PERF

#if defined(MEASURING_SUDOKU_PERF)
    u32 dbgHits = 0;
    u32 dbgEndTime = 0;
#endif

void Display( const char* str ) {
    OutputDebugString( str );
}


struct SudokuSolver {

    SudokuSolver( Sudoku source ) {
        ZeroMemory( this, sizeof(*this) );
        for( u8 i=0; i<9; i++ ) {
            for( u8 j=0; j<9; j++ ) {
                if( source[i][j] ) {
                    Set( i,j, source[i][j] );
                }
            }
        }

        for( u16 i=0; i<2048; i++ ) {
            u8 count = 0;
            for( u16 j=0; j<10; j++ ) {
                if( i & (1<<j) ) { 
                    count ++;
                }
            }
            BitCounts[ i ] = count;
        }

    }

    void Solve( ) {

        static const u8 NOT_SET = 255;

#       if defined(MEASURING_SUDOKU_PERF)
            dbgHits++;
#       endif

        u8      best_score  = NOT_SET,
                best_i      = 0,
                best_j      = 0;
        u16     best_mask   = 0;

        for( u8 i=0; i<9; i++ ) {
            for( u8 j=0; j<9; j++ ) {
                if( Grid[i][j] ) {
                    continue;
                }

                const u16   m = LineMask[i] | ColMask[j] | Block3x3Mask[ i/3 ][ j/3 ];
                const u8    score = BitCounts[m];

                if( best_score==NOT_SET || score>best_score ) {
                    best_score  = score;
                    best_i      = i;
                    best_j      = j;
                    best_mask   = m;
                }
            }
        }

        if( best_score==NOT_SET ) {
#           if defined(MEASURING_SUDOKU_PERF)
                if( dbgEndTime==0 ) {
                    dbgEndTime = u32( GetTickCount() );
                }
#           endif

            Show(); // replace with any code to notify a solution has been found (stored in the in current Grid)
        }
        else {
            for( u8 k=1; k<10; k++ ) {

                best_mask >>= 1;
                if( best_mask&1 ) {
                    continue;
                }

                Set( best_i,best_j, k );
                Solve( );
                Set( best_i,best_j, 0 );
            }
        }
    }

    void Show( ) {

        Display( "-------------------\n" );
        char line[ 20 ];
        strcpy( line, "123 456 789 \r\n" );

        for( u8 i=0; i<9; i++ ) {
            int col = 0;

            for( u8 j=0; j<9; j++ ) {
                u8 c = Grid[i][j];
                if( c==0 )
                    line[col] = '-';
                else
                    line[col] = '0' + c;

                if( (j%3)==2 )
                    col+=2;
                else
                    col++;
            }

            Display( line );
            if( (i%3)==2 ) {
                Display( "\r\n" );
            }
        }
    }


private:
    void Set( u8 i, u8 j, u8 value ) {
        if( value ) {
            const u32 m = 1<<value;
            LineMask[i] |= m;
            ColMask[j] |= m;
            Block3x3Mask[ i/3 ][ j/3 ] |= m;            
        }
        else {
            const u32 m = (1<<Grid[i][j]) ^ ~0;
            LineMask[i] &= m;
            ColMask[j] &= m;
            Block3x3Mask[ i/3 ][ j/3 ] &= m;            
        }

        Grid[i][j] = value;
    }


    Sudoku      Grid;
    u16         ColMask[ 9 ];
    u16         LineMask[ 9 ];
    u16         Block3x3Mask[ 3 ][ 3 ];
    u8          BitCounts[ 2048 ];
};



#define _ 0
Sudoku s1 =
{
       {_,_,_,1,3,_,_,_,5}, 
       {_,4,_,_,_,_,2,_,_}, 
       {8,_,_,9,_,_,_,_,_}, 
       {_,_,_,_,5,_,9,_,_}, 
       {_,_,2,_,_,_,4,_,_}, 
       {_,_,3,_,6,_,_,_,_}, 
       {_,_,_,_,_,3,_,_,6}, 
       {_,_,5,_,_,_,_,1,_}, 
       {7,_,_,_,2,8,_,_,_}
};

Sudoku s2 =
{
       {_,_,_,_,8,_,1,3,2}, 
       {_,_,_,_,_,_,_,_,_}, 
       {6,_,_,_,4,5,_,7,9}, 
       {_,_,2,8,_,7,6,_,_}, 
       {_,_,1,_,_,_,5,_,_}, 
       {_,_,4,5,_,1,9,_,_}, 
       {2,3,_,6,1,_,_,_,8}, 
       {_,_,_,_,_,_,_,_,_}, 
       {7,9,6,_,3,_,_,_,_}
};

Sudoku s3 =
{
        _,4,2,_,3,_,_,_,_, 
        _,_,5,_,_,_,_,_,_, 
        _,_,_,9,_,_,_,_,8, 
        1,_,_,8,_,_,_,_,_, 
        _,_,3,_,4,_,2,_,_, 
        _,_,_,_,_,7,_,_,6, 
        7,_,_,_,_,6,_,_,_, 
        _,_,_,_,_,_,4,6,_, 
        _,_,_,_,2,_,5,9,_
};

Sudoku s4 =
{
    8,_,_,_,_,_,_,_,1,
    _,7,_,1,_,9,_,6,_,
    6,1,2,_,_,_,4,5,9,
    _,_,_,7,_,5,_,_,_,
    _,4,_,_,_,_,_,7,_,
    _,_,_,4,_,2,_,_,_,
    7,5,9,_,_,_,3,4,8,
    _,3,_,8,_,4,_,1,_,
    1,_,_,_,_,_,_,_,6,
};
#undef _


void Sudoku_SolveAndShowStats( Sudoku s )
{
    SudokuSolver si( s );
    si.Show();

#   if defined(MEASURING_SUDOKU_PERF)
        dbgHits = 0;
        dbgEndTime = 0;
        u32 start = u32( GetTickCount() );
#   endif

    si.Solve( );

#   if defined(MEASURING_SUDOKU_PERF)
        u32 time = dbgEndTime - start;

        char str2[ 256 ];
        sprintf( str2, 
            "\r\n-------------------\r\n"
            "Duration : %d ms\r\n"
            "Number of steps: %d\n\r", 
            time, dbgHits );

        Display( str2 );
#   endif

}



void FindSudoku(  )
{
    Sudoku_SolveAndShowStats( s1 );
    Sudoku_SolveAndShowStats( s2 );
    Sudoku_SolveAndShowStats( s3 );
    Sudoku_SolveAndShowStats( s4 );
}

THX

Link to comment
Share on other sites

I found a source and i don't understand it.

i know, "this is no general help forum", but

could someone translate this to autoit??? plz help!

You don't understand what the c++ code does, and you ask somebody to translate it? WHY??? I could also post some parts of the linux kernel and ask for translation to autoit. Only it would make absolutely no sense. But, maybe you can explain your project a bit more detailed. I'm sure you will then find somebody to translate it!

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

i didn't expect anyone to translate it to autoit, i just wanted to understand the logic

of the script. it's a script to solve SuDoku's (japanese number "crossword")

i'd like to do the same in autoit, but i've no clue how to do so.

'cause my english isn't to good, the question was badly put, i guess...

cu

Link to comment
Share on other sites

i didn't expect anyone to translate it to autoit, i just wanted to understand the logic

of the script. it's a script to solve SuDoku's (japanese number "crossword")

Ah, O.K. then I mis-understood your post. Sorry. You might want to have a look at the following urls.

http://www.eddaardvark.co.uk/sudokusolver.html

http://www.scanraid.com/sudoku.htm

i'd like to do the same in autoit, but i've no clue how to do so.

It should'nt be too hard. Programming structures in AutoIT are very similar. However, they are working with bit fields and shift operations. You would have to "reinvent" some of those concepts for AutoIT.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

BitShift and BitRotate functions replace << and >> operators in C/C++. &= is a combination of Bitwise-And and assign.

In C,

a &= b;

In AutoIt,

$a = BitAnd($a, $b)

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...