Browse Source

refactoring: 8

main^2
Lucas Heil 11 months ago
committed by Peter Wiebe
parent
commit
91b072e5f0
  1. 71
      src/main/c/sudoku.c

71
src/main/c/sudoku.c

@ -1,15 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define SIZE 9
#define SIZE_OF_GAMEBORD_AXIS_X 9
#define SIZE_OF_GAMEBORD_AXIS_Y 9
#define EMPTY 0
#define LEVEL_NUMBER 3
@ -20,18 +15,18 @@ int selected_level;
void Game_loop(); //is instead of main
void Level_Pool(int difficulty);
void selectLevel(int grid[SIZE][SIZE]);
void initializeGrid(int grid[SIZE][SIZE]);
void create_playing_field(int grid[SIZE][SIZE], int difficulty, int level);
void Player_actions_for_playing(int grid[SIZE][SIZE]);
void giving_hints_to_player(int grid[SIZE][SIZE]);
void write_userinput_into_Sudoku(int grid[SIZE][SIZE]);
void printGrid(int grid[SIZE][SIZE]);
void check_if_Sudoku_solved(int grid[SIZE][SIZE]);
void selectLevel(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
void initializeGrid(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
void create_playing_field(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y], int difficulty, int level);
void Player_actions_for_playing(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
void giving_hints_to_player(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
void write_userinput_into_Sudoku(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
void printGrid(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
void check_if_Sudoku_solved(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]);
int availableLevels[3][3][SIZE][SIZE] = {
int availableLevels[3][3][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y] = {
{ //easy
{{0, 0, 3, 0, 2, 1, 8, 0, 0},
{6, 0, 0, 0, 7, 0, 1, 3, 2},
@ -128,7 +123,7 @@ int availableLevels[3][3][SIZE][SIZE] = {
}
};
int solutionLevels[3][3][SIZE][SIZE] = {
int solutionLevels[3][3][SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y] = {
{ //easy
{{4, 7, 3, 6, 2, 1, 8, 5, 9},
@ -228,7 +223,7 @@ int solutionLevels[3][3][SIZE][SIZE] = {
void Game_loop() {
int grid[SIZE][SIZE];
int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y];
selected_level = 0;
while (1) {
@ -265,7 +260,7 @@ void Level_Pool(int difficulty) {
void selectLevel(int grid[SIZE][SIZE]) {
void selectLevel(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
int level = 0;
printf("\nSelect a level:\n");
while (true){
@ -291,23 +286,23 @@ void selectLevel(int grid[SIZE][SIZE]) {
}
void initializeGrid(int grid[SIZE][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
void initializeGrid(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
grid[i][j] = EMPTY;
}
}
}
void create_playing_field(int grid[SIZE][SIZE], int difficulty, int level) {
void create_playing_field(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y], int difficulty, int level) {
initializeGrid(grid);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
grid[i][j] = availableLevels[difficulty - 1][level - 1][i][j];
}
}
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; 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", grid[i][j]);
}
printf("\n");
@ -316,7 +311,7 @@ void create_playing_field(int grid[SIZE][SIZE], int difficulty, int level) {
}
void Player_actions_for_playing(int grid[SIZE][SIZE]) {
void Player_actions_for_playing(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
while (1) {
printf("\nTurn function - Choose an action:\n");
printf("1. Hints\n");
@ -368,7 +363,7 @@ void Player_actions_for_playing(int grid[SIZE][SIZE]) {
void giving_hints_to_player(int grid[SIZE][SIZE]) {
void giving_hints_to_player(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
printf("\nTip function - Choose an option:\n");
printf("1. Set the user-specified cell to the right value\n");
printf("2. Set the user-specified 3x3 field to the right values\n");
@ -444,8 +439,8 @@ void giving_hints_to_player(int grid[SIZE][SIZE]) {
break;
case 3:
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
grid[i][j] = solutionLevels[difficulty - 1][selected_level - 1][i][j];
}
}
@ -456,7 +451,7 @@ void giving_hints_to_player(int grid[SIZE][SIZE]) {
}
}
void write_userinput_into_Sudoku(int grid[SIZE][SIZE]) {
void write_userinput_into_Sudoku(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
printf("\nInput function - Choose an action:\n");
printf("1. Insert value in an empty field\n");
printf("2. Clear an already filled field\n");
@ -546,21 +541,21 @@ void write_userinput_into_Sudoku(int grid[SIZE][SIZE]) {
}
void printGrid(int grid[SIZE][SIZE]) {
void printGrid(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
printf("\nSudoku Grid:\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; 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 ", grid[i][j]);
}
printf("\n");
}
}
void check_if_Sudoku_solved(int grid[SIZE][SIZE]) {
void check_if_Sudoku_solved(int grid[SIZE_OF_GAMEBORD_AXIS_X][SIZE_OF_GAMEBORD_AXIS_Y]) {
printf("\nDone function - Checking if the solution is correct...\n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
for (int i = 0; i < SIZE_OF_GAMEBORD_AXIS_X; i++) {
for (int j = 0; j < SIZE_OF_GAMEBORD_AXIS_Y; j++) {
if (grid[i][j] != solutionLevels[difficulty - 1][selected_level - 1][i][j]) {
printf("Incorrect solution. Keep trying!\n");
return;

Loading…
Cancel
Save