Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ /

ID: 3635797 • Letter: #

Question

// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//
// badsuduku.cpp
//
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//
// Play the popular game
// It keeps track of how long the player took, and how many times they
// asked for help.
//
// NOTE: all of the arrays are 1-9 to match user expectations.
// NOTE: this does not stop you from putting a wrong value in!!
//
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// General flow chart
//
// Start
// +-- initialize values
// | +-- blank tables (value and allowed)
// | +-- fill table
// | | +-- adjust help strings
// +-- start looping
// | +-- show the board
// | +-- ask for next play
// | +-- check if user wants to see answer
// | | +-- if so:
// | | | +-- put all values into board
// | | | +-- show board
// | | | +-- stop
// | +-- check if user wants help on a cell
// | | +-- if so:
// | | | +-- get location of cell
// | | | +- look up allowed values
// | | | +- show allowed values
// | | | +-- [start looping over]
// | +-- insert value into table
// | | +-- adjust help strings
// | ^^ end loop
//
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//
// Rev history
//
// rev date by reason
// --- ------ --------------- ------------------------------------------------
// 0 100106 PMoorhead Initial implementation.
//
//
//
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

// Compiler directives: includes - - - - - - - - - - - - - - - - - - - - - - -

#include <cstdlib>
#include <iostream>

#include <time.h>

using namespace std;

// Compiler directives: global typedefs - - - - - - - - - - - - - - - - - - -

// Compiler directives: global defs - - - - - - - - - - - - - - - - - - - - -

#define TEST 1

// Compiler directives: global constants - - - - - - - - - - - - - - - - - - -

const int iEXIT = 1 ;
const int iCONTINUE = 0 ;
const int iTRUE = 1 ;
const int iFALSE = 0 ;

// Compiler directives: global variables - - - - - - - - - - - - - - - - - - -

int giStartTime ;
int giHelpRequestCount ;

char gcaTable[ 10 ][ 10 ] ;
char gcaAnswer[ 10 ][ 10 ] ;

string gsaAllowed[ 10 ][ 10 ] ;

time_t gStartTime ;
time_t gCurTime ;

// Compiler directives: prototypes - - - - - - - - - - - - - - - - - - - - - -

void blank_tables( void ) ;
void initialize_table( int ) ;
void extract( int , string , string ) ;
int validate_input( string ) ;
void insert_entry( char , char , char ) ;
int convert_char_to_int( char ) ;
void show_board( void ) ;
void build_row_to_show( int ) ;
void show_prompt( void ) ;
int check_for_show_help( string ) ;
void show_help( string ) ;
string get_allowed_values( char , char ) ;
int check_for_show_answer( string ) ;
void populate_answer() ;
void adjust_help_strings( void ) ;
string process_quadrant( int , int , int , int , string ) ;

// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//
// M A I N P R O G R A M
//
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

int main(int argc, char *argv[])
{
int iRet ;
int iDone ;

string u ;

#ifdef TEST
cout << "Now starting main." << endl ;
#endif

time( &gStartTime ) ;
giHelpRequestCount = 0 ;

blank_tables() ;
initialize_table( 1 ) ;

iDone = iFALSE ;
while( iDone == iFALSE )
{
show_board() ;
show_prompt() ;
cin >> u ;

if( check_for_show_answer( u ) == iEXIT )
{
return EXIT_SUCCESS ;
}

if( check_for_show_help( u ) == iTRUE )
{
continue ;
}

if( validate_input( u ) == iFALSE )
{
continue ;
}

}

system("PAUSE");
return EXIT_SUCCESS;
}

// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// F U N C T I O N S
// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

void blank_tables()
{
int i ;
int j ;

#ifdef TEST
cout << "Now starting blank_tables." << endl ;
#endif

for( i = 1 ; i < 10 ; i++ )
{
for( j = 1 ; j < 10 ; j++ )
{
gcaTable[ i ][ j ] = '.' ;
gsaAllowed[ i ][ j ] = "........." ;
}
}
}

// ===========================================================================
void initialize_table( int i )
{
string s1 ;
string s2 ;

#ifdef TEST
cout << "Now starting initialize_table: " << i << endl ;
#endif

switch( i )
{
case 1: s1 = "619784532" ; s2 = "..9...53." ; extract( 1 , s1 , s2 ) ;
s1 = "254316789" ; s2 = "....16..9" ; extract( 2 , s1 , s2 ) ;
s1 = "837259164" ; s2 = "8..2.9..4" ; extract( 3 , s1 , s2 ) ;
s1 = "143698275" ; s2 = "..3.9.27." ; extract( 4 , s1 , s2 ) ;
s1 = "982547613" ; s2 = ".8.547.1." ; extract( 5 , s1 , s2 ) ;
s1 = "576132948" ; s2 = ".76.3.9.." ; extract( 6 , s1 , s2 ) ;
s1 = "795463821" ; s2 = "7..4.3..1" ; extract( 7 , s1 , s2 ) ;
s1 = "328971456" ; s2 = "3..97...." ; extract( 8 , s1 , s2 ) ;
s1 = "461825397" ; s2 = ".61...3.." ; extract( 9 , s1 , s2 ) ;
adjust_help_strings() ;
break ;
}
}

// ===========================================================================
void extract( int r , string s1 , string s2 )
{
int i ;

string s ;

#ifdef TEST
cout << "Now starting extract: " << r << " " << s1 << " " << s2 << endl ;
#endif

for( i = 1 ; i < 10 ; i++ )
{
gcaAnswer[ r ][ i ] = s1[ i - 1] ;
gcaTable[ r ][ i ] = s2[ i - 1 ] ;
}
}

// ===========================================================================
int validate_input( string s )
{

#ifdef TEST
cout << "Now starting validate_input: " << s << endl ;
#endif

if( s[ 0 ] < '1' ) return iFALSE ;
if( s[ 0 ] > '9' ) return iFALSE ;
if( s[ 1 ] < '1' ) return iFALSE ;
if( s[ 1 ] > '9' ) return iFALSE ;
if( s[ 2 ] < '1' ) return iFALSE ;
if( s[ 2 ] > '9' ) return iFALSE ;
return iTRUE ;
}

// ===========================================================================
void insert_entry( char r , char c , char v )
{
int row ;
int col ;

#ifdef TEST
cout << "Now starting insert_entry: " << r << " " << c << " " << v << endl ;
#endif

row = convert_char_to_int( r ) ;
col = convert_char_to_int( c ) ;

gcaTable[ row ][ col ] = v ;
adjust_help_strings() ;
}

// ===========================================================================
int convert_char_to_int( char x )
{
switch( x )
{
case '0' : return 0 ;
case '1' : return 1 ;
case '2' : return 2 ;
case '3' : return 3 ;
case '4' : return 4 ;
case '5' : return 5 ;
case '6' : return 6 ;
case '7' : return 7 ;
case '8' : return 8 ;
case '9' : return 9 ;
}
}

// ===========================================================================
void show_board()
{

#ifdef TEST
cout << "Now starting show_board." << endl ;
#endif

cout << "+-------|-------|-------+" << endl ;
build_row_to_show( 1 ) ;
build_row_to_show( 2 ) ;
build_row_to_show( 3 ) ;
cout << "+-------|-------|-------+" << endl ;
build_row_to_show( 4 ) ;
build_row_to_show( 5 ) ;
build_row_to_show( 6 ) ;
cout << "+-------|-------|-------+" << endl ;
build_row_to_show( 7 ) ;
build_row_to_show( 8 ) ;
build_row_to_show( 9 ) ;
cout << "+-------|-------|-------+" << endl ;
cout << endl ;
}

// ===========================================================================
void build_row_to_show( int r )
{
static string stringmaster = "| . . . | . . . | . . . |" ;

string s ;

s = stringmaster ;
s[ 2 ] = gcaTable[ r ][ 1 ] ;
s[ 4 ] = gcaTable[ r ][ 2 ] ;
s[ 6 ] = gcaTable[ r ][ 3 ] ;
s[ 10 ] = gcaTable[ r ][ 4 ] ;
s[ 12 ] = gcaTable[ r ][ 5 ] ;
s[ 14 ] = gcaTable[ r ][ 6 ] ;
s[ 18 ] = gcaTable[ r ][ 7 ] ;
s[ 20 ] = gcaTable[ r ][ 8 ] ;
s[ 22 ] = gcaTable[ r ][ 9 ] ;

cout << s << endl ;
}

// ===========================================================================
void show_prompt()
{
cout << "Enter row column value [example: 116] with no spaces" << endl ;
cout << "Enter row column ? [example 11?] for help" << endl ;
cout << "Enter 000 for answer" << endl ;
}

// ===========================================================================
int check_for_show_help( string s )
{

#ifdef TEST
cout << "Now starting check_for_show_help: " << s << endl ;
#endif

if( s[ 2 ] == '?' )
{
show_help( s ) ;
}
}

// ===========================================================================
void show_help( string s )
{

#ifdef TEST
cout << "Now starting show_help: " << s << endl ;
#endif

cout << "Allowed values for row: "
<< s[ 0 ]
<< " column: "
<< s[ 1 ]
<< " are: "
<< get_allowed_values( s[ 0 ] , s[ 1 ] )
<< endl
<< endl ;
system( "PAUSE" ) ;
}

// ===========================================================================
string get_allowed_values( char r , char c )
{
return gsaAllowed[ convert_char_to_int( r ) ]
[ convert_char_to_int( c ) ] ;
}

// ===========================================================================
int check_for_show_answer( string s )
{

#ifdef TEST
cout << "Now starting check_for_show_answer: " << s << endl ;
#endif

if( ( s[ 0 ] == '0' ) &&
( s[ 1 ] == '0' ) &&
( s[ 2 ] == '0' ) )
{
populate_answer() ;
system( "PAUSE" ) ;
return iTRUE ;
}
return iFALSE ;
}

// ===========================================================================
void populate_answer()
{
int i ;
int j ;

#ifdef TEST
cout << "Now starting populate_answer." << endl ;
#endif


for( i = 1 ; i < 10 ; i++ )
{
for( j = 1 ; j < 10 ; j++ )
{
gcaTable[ i ][ j ] = gcaAnswer[ i ][ j ] ;
}
}
show_board() ;
}

// ===========================================================================
int check_for_win()
{
int i ;
int j ;


#ifdef TEST
cout << "Now starting check_for_win." << endl ;
#endif

for( i = 1 ; i < 10 ; i++ )
{
for( j = 1 ; j < 10 ; j++ )
{
if( gcaTable[ i ][ j ] == '.' )
{
return iFALSE;
}
}
}
cout << "YOU WON!!" << endl << endl ;

system( "PAUSE" ) ;

return iTRUE ;
}

// ===========================================================================
void adjust_help_strings()
{
int r ;
int c ;
int quadrant_r ;
int quadrant_c ;

string s ;

#ifdef TEST
cout << "Now starting adjust_help_screens." << endl ;
#endif

for( quadrant_r = 0 ; quadrant_r < 3 ; quadrant_r++ )
{
for( quadrant_c = 0 ; quadrant_c < 3 ; quadrant_c++ )
{
for( r = 1 ; r < 4 ; r++ )
{
for( c = 1 ; c < 4 ; c++ )
{
s = gsaAllowed[ ( quadrant_r * 3 ) + r ]
[ ( quadrant_c * 3 ) + c ] ;
s = process_quadrant( quadrant_r * 3 ,
quadrant_c * 3 ,
r ,
c ,
s ) ;
gsaAllowed[ ( quadrant_r * 3 ) + r ]
[ ( quadrant_c * 3 ) + c ] = s ;
}
}
}
}
}

// ===========================================================================
string process_quadrant( int qr , int qc , int r , int c , string s )
{
int i ;
int j ;
int z ;

char thischar ;

#ifdef TEST
cout << "Now starting process_quadrant: "
<< qr
<< " "
<< qc
<< " "
<< r
<< " "
<< c
<< " "
<< s
<< endl ;

cout << "start ================= table" << endl ;
for( i = 1 ; i < 10 ; i++ )
{
cout << "| " ;
for( j = 1 ; j<10;j++)
cout << gcaTable[ i][j] << " " ;
cout << " |" << endl ;
}
cout << "end ================= table" << endl ;

#endif

for( i = 1 ; i < 4 ; i++ )
{
for( j = 1 ; j < 4 ; j++ )
{
#ifdef TEST
cout << "---- qr: " << qr ;
cout << " i: " << i ;
cout << " qc: " << qc ;
cout << " j: " << j ;
#endif

thischar = gcaTable[ qr + i ][ qc + j ] ;
#ifdef TEST
cout << " thischar: " << thischar ;
#endif

if( thischar != '.' )
{
z = convert_char_to_int( thischar ) ;
#ifdef TEST
cout << " z: " << z ;
cout << " s[z]: " << s[z] << " string: " << s << endl ;
#endif

s[ z ] = '.' ;
}
#ifdef TEST
cout << endl ;
#endif

}
}
for( i = 1 ; i < 10 ; i++ )
{
thischar = gcaTable[ i ][ qc + c ] ;
if( thischar != '.' )
{
z = convert_char_to_int( thischar ) ;
s[ z ] = '.' ;
}
}
for( j = 1 ; j < 10 ; j++ )
{
thischar = gcaTable[ qr + r ][ j ] ;
if( thischar != '.' )
{
z = convert_char_to_int( thischar ) ;
s[ z ] = '.' ;
}
}
return s ;
}

// $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// L A S T L I S T I N G L I N E

Explanation / Answer

/*********************************************************************** * Program: * Project 7, Sudoku * Author: * Don Page * Summary: * Sudoku Program that allows user to save his/her progress and also shows * the possible values when prompted to do so. Lets the user solve, rotate, * and clear the board ************************************************************************/ #include #include #include using namespace std; struct Board { int values[81]; //Stores board values bool isFixedValue[81]; //Keeps track of what numbers can be modified bool isPossibleValue[81][9]; int coordinates; }; void getFileName(char fileName[]); bool getBoard(char fileName[], Board &mainBoard); bool validateBoard(Board board); void interact(Board &mainBoard); void displayMenu(); void displayBoard(Board board); char getOption(); int getCoordinates(bool isFixedValue[]); int getNewValue(Board &board); bool canChange(Board mainBoard, int coordinates, int value); bool anyDuplicates(int board[], int coordinates, int value); void editSquare(int board[], int coordinates, int value); void showPossibles(Board &board); void save(Board mainBoard); void rotateBoard(Board &board); bool checkPossibles(Board &newBoard); float solve(Board &newBoard); bool bruteForce(Board &board); bool isSolved(Board board); void clearBoard(Board &board); /********************************************************************** * Gets the filename, loads board, and checks to see if opened properly * Then interacts with the user. ***********************************************************************/ int main(int argc,char *argv[]) { char fileName[256]; Board mainBoard; //Get FileName and read board getFileName(fileName); if (!getBoard(fileName, mainBoard)) cout