Sudoku Game (C++)
Sudoku Game (C++)
Sudoku Game (C++)
h> using namespace std; int i, j; char board[9][9]; char player[9][9]; void void void void void bool bool void void void filling(); except(); spaces(int run); display(); turn(); error(int row, int column, char userturn); check(); won(); errormsg(); clear_screen();
int main(void) { int level, run; char back = 'y'; while(back == 'y' || back == 'Y'){ clear_screen(); cout << "Welcome To Harsh Sudoku Game\n" << "Choose your level(1 - 3): "; cin >> level; switch(level) { case 1: run = 35; break; case 2: run = 45; break; case 3: run = 55; break; default: run = 55; break; } filling(); // Filling and Except will fill the whole board with correct d igits except(); spaces(run); // This will make spaces depending upon user's difficulty leve l
turn(); // This will let the user get into the real game cout << endl; cout << "Want to play sudoku again(y/n)? "; cin >> back; } } void turn() { for (i=0;i<9;i++) for (j=0;j<9;j++){ player[i][j] = board[i][j]; } char userturn; int row, column; bool gamewin = false; while(!gamewin){ clear_screen(); display(); // Nice Display Board cout << "\nEnter row , column number & your number with space in between th em: "; cin >> row >> column >> userturn; if(!(error(row, column, userturn))){ // Error Checking errormsg(); continue; } player[row-1][column-1] = userturn; if(board[row-1][column-1] != ' '){ // User can't edit the numbers given by computer already errormsg(); player[row-1][column-1] = board[row-1][column-1]; continue; } if (check()){ // Checking the winning condition for user won(); gamewin = true; } } } void filling() { char k = '0'; //Filling 1st Line of all the Boxes for (j = 0;j<9;j++){ board[0][j] = k+1; board[3][j] = k+2; board[6][j] = k+3; k++; } //Filling 2nd Line of all the Boxes k='0'; for (j = 0;j<6;j++){
board[1][j] = k+4; board[4][j] = k+5; board[7][j] = k+6; k++; } k='6'; for (j = 6;j<9;j++){ board[1][j] = k-5; board[4][j] = k-4; board[7][j] = k-3; k++; } //Filling 3rd Line of all the Boxes k='0'; for (j = 0;j<3;j++){ board[2][j] = k+7; board[5][j] = k+8; board[8][j] = k+9; k++; } k='3'; for (j=3;j<9;j++){ board[2][j] = k-2; board[5][j] = k-1; board[8][j] = k; k++; } } void except() { board[3][8] board[4][5] board[5][2] board[7][4] board[7][5] board[8][1] board[8][2] board[6][7] }
= = = = = = = =
'1'; '1'; '1'; '1'; '2'; '1'; '2'; '1'; board[6][8] = '2';
void spaces(int run) { time_t t; time(&t); srand(t); for (int a = 1;a<=run;a++){ i = 0 + rand() % 8; j = 0 + rand() % 8; board[i][j] = ' '; } } void display() { cout << " 1 2 3 | 4 5 6 | 7 8 9\n" << "--------------------------------\n\n"; for (int k = 3;k<=9;k+=3){ for (i = k-3;i<k;i++){
cout << i+1 << "| "; for (j = 0;j<3;j++){ cout << player[i][j] << " "; } cout << " | "; for (j=3;j<6;j++){ cout << player[i][j] << " "; } cout << " | "; for (j=6;j<9;j++){ cout << player[i][j] << " "; } cout << " | "; cout << endl; } cout << endl << "--------------------------------\n\n"; } } bool error(int row, int column, char userturn) { if(!(row <=9 && row >=1) || !(column <=9 && column >=1) ) return false; for (int j = 0;j<9;j++){ // Error if user try to write number already exist in the same line if(j != column-1){ if(userturn == board[row-1][j]){ return false; break; }}} for (int i = 0;i<9;i++){ if(i != row-1){ if(userturn == board[i][column-1]){ return false; break; }}} return true; } bool check() { for(i=0;i<9;i++){ for (j=0;j<9;j++){ if (player[i][j] == ' ') return false; }} return true; } void won() { cout << "\nCongratulation, You have completed the Sudoku Game"; }
void errormsg() { cout << "\nIncorrect Move\n"; system("pause"); } void clear_screen() { HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE); DWORD bytes_write, size; COORD coord = {0, 0}; CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(output_handle, &csbi); size = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter(output_handle, ' ', size, coord, &bytes_write ); SetConsoleCursorPosition(output_handle, coord); }