Browse Source

adding sudoku.h, changing the game to include the header

main^2
Lucas Heil 11 months ago
committed by Peter Wiebe
parent
commit
729733ccfd
  1. 63
      src/main/c/sudoku.c
  2. 41
      src/main/c/sudoku.h

63
src/main/c/sudoku.c

@ -2,7 +2,7 @@
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "sudoku.h"
#define SIZE_OF_GAMEBORD_AXIS_X 9
#define SIZE_OF_GAMEBORD_AXIS_Y 9
@ -14,6 +14,11 @@
int selected_difficulty;
int selected_level;
bool check_solved;
bool test_help = false;
int test_row_e;
int test_col_e;
int test_num;
void Game_loop(); //is instead of main
@ -29,6 +34,7 @@ void check_if_Sudoku_solved(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAM
int availableLevels[AVAILABLE_DIFFICULTIES][AVAILABLE_LEVELS][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y] = {
{ //easy
{{0, 0, 3, 0, 2, 1, 8, 0, 0},
@ -234,6 +240,7 @@ void Game_loop() {
printf("1. Easy\n2. Medium\n3. Hard\n");
printf("Enter the corresponding number or type 'quit' to exit: ");
if(!test_help){
char input[10];
scanf("%s", input);
@ -243,6 +250,7 @@ void Game_loop() {
selected_difficulty = input[0] - '0'; // Convert the first character to an integer
if ((selected_difficulty >= 1 && selected_difficulty <= 3) && input[1] == '\0') {
Level_Pool(selected_difficulty);
Level_Selection(Sudoku_grid);
@ -250,7 +258,11 @@ void Game_loop() {
} else {
printf("Invalid input. Please enter a number between 1 and 3.\n");
}
}
if(test_help){
Level_Pool(selected_difficulty);
break;
}
}
}
@ -267,7 +279,8 @@ void Level_Selection(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_A
int level = 0;
printf("\nSelect a level:\n");
while (true){
char level_select[10];
if(!test_help){
char level_select[10];
scanf("%s", level_select);
@ -284,6 +297,13 @@ void Level_Selection(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_A
printf("Invalid input. Please enter a number between 1 and 3.\n");
}
}
if(test_help){
selected_level = level;
level--;
create_playing_field(Sudoku_grid, selected_difficulty, selected_level);
break;
}
}
@ -299,11 +319,13 @@ void initializeGrid(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AX
void create_playing_field(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y], int selected_difficulty, int level) {
initializeGrid(Sudoku_grid);
if(!test_help){
for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
Sudoku_grid[i][j] = availableLevels[selected_difficulty - 1][level - 1][i][j];
}
}
}
for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
printf("%d", Sudoku_grid[i][j]);
@ -324,8 +346,14 @@ void Player_actions_for_playing(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF
printf("5. Select level\n");
printf("6. Quit\n");
int action;
int action;
if(test_help){
printGrid(Sudoku_grid);
check_if_Sudoku_solved(Sudoku_grid);
break;
}
if(!test_help){
while (true){
char action_str[10];
scanf("%s", action_str);
@ -361,6 +389,7 @@ void Player_actions_for_playing(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF
default:
printf("Invalid input. Please enter a number between 1 and 6.\n");
}
}
}
}
@ -372,9 +401,18 @@ void giving_hints_to_player(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAM
printf("2. Set the user-specified 3x3 field to the right values\n");
printf("3. Solve the entire puzzle for the current level\n");
if(test_help){
initializeGrid(Sudoku_grid);
for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
Sudoku_grid[i][j] = solutionLevels[selected_difficulty - 1][selected_level - 1][i][j];
}
}
printf("Puzzle solved. \n");
}
int option = 0;
if(!test_help){
while (true){
char tip_str[10];
scanf("%s", tip_str);
@ -452,6 +490,7 @@ void giving_hints_to_player(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAM
default:
printf("Invalid option. Please enter a number between 1 and 3.\n");
}
}
}
void write_userinput_into_Sudoku(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
@ -459,8 +498,14 @@ void write_userinput_into_Sudoku(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_O
printf("1. Insert value in an empty field\n");
printf("2. Clear an already filled field\n");
int action;
if(test_help){
initializeGrid(Sudoku_grid);
Sudoku_grid[test_row_e -1][test_col_e -1] = test_num;
printGrid(Sudoku_grid);
}
int action;
if(!test_help){
while (true){
char in_str[10];
scanf("%s", in_str);
@ -541,6 +586,7 @@ void write_userinput_into_Sudoku(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_O
printf("Invalid input. Please enter 1 or 2.\n");
}
printGrid(Sudoku_grid);
}
}
@ -561,16 +607,17 @@ void check_if_Sudoku_solved(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAM
for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
if (Sudoku_grid[i][j] != solutionLevels[selected_difficulty - 1][selected_level - 1][i][j]) {
printf("Incorrect solution. Keep trying!\n");
check_solved = false;
return;
}
}
}
check_solved = true;
printf("Congratulations! Sudoku is solved correctly.\n");
}
int main(){
int mainn(){
Game_loop();
return 0;
}

41
src/main/c/sudoku.h

@ -0,0 +1,41 @@
#ifndef SUDOKU_H
#define SUDOKU_H
#include <stdbool.h>
//constants
#define SIZE_OF_GAMEBORD_AXIS_X 9
#define SIZE_OF_GAMEBORD_AXIS_Y 9
#define AVAILABLE_DIFFICULTIES 3
#define AVAILABLE_LEVELS 3
#define EMPTY 0
#define LEVEL_NUMBER 3
extern int selected_difficulty;
extern int selected_level;
extern int solutionLevels[AVAILABLE_DIFFICULTIES][AVAILABLE_LEVELS][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y];
extern int availableLevels[AVAILABLE_DIFFICULTIES][AVAILABLE_LEVELS][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y];
extern bool check_solved;
extern bool test_help;
extern int test_row_e;
extern int test_col_e;
extern int test_num;
//functions
void Game_loop(); //is instead of main
void Level_Pool(int selected_difficulty);
void Level_Selection(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
void initializeGrid(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
void create_playing_field(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y], int selected_difficulty, int level);
void Player_actions_for_playing(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
void giving_hints_to_player(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
void write_userinput_into_Sudoku(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
void printGrid(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
void check_if_Sudoku_solved(int Sudoku_grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
int mainn();
#endif // SUDOKU_H
Loading…
Cancel
Save