Browse Source

Merge branch 'main' into 'branchJulia'

# Conflicts:
#   src/main/c/main.c
remotes/origin/branchJulia
fdai8032 11 months ago
parent
commit
cf34045834
  1. 0
      src/main/c/GameTic_Tac_Toe/tictactoe.c
  2. 1
      src/main/c/GameTic_Tac_Toe/tictactoe.h
  3. 255
      src/main/c/Minesweeper/minesweeper_start.c
  4. 20
      src/main/c/Minesweeper/minesweeper_start.h
  5. 28
      src/main/c/Snake/snake_start.c
  6. 8
      src/main/c/Snake/snake_start.h
  7. 12
      src/main/c/main.c
  8. 40
      test/Minesweeper/test_bomb_in_array.c
  9. 97
      test/Minesweeper/test_number_of_bombs.c
  10. 4
      test/Snake/test_collision.c

0
src/main/c/GameTic_Tac_Toe/game.c → src/main/c/GameTic_Tac_Toe/tictactoe.c

1
src/main/c/GameTic_Tac_Toe/tictactoe.h

@ -0,0 +1 @@

255
src/main/c/Minesweeper/minesweeper_start.c

@ -0,0 +1,255 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include "minesweeper_start.h"
#pragma region defines
#define BLOCK '#'
#define FLAG 'x'
#define EMPTY ' '
#define SMALL 10
#define MIDDLE 15
#define LARGE 20
#define EASY 7
#define NORMAL 5
#define HARD 3
#pragma endregion
#pragma region Funktion_heads
void main_menu_minesweeper();
void game_minesweeper();
void options_minesweeper();
Minesweeper_Board initialize_minesweeper();
void place_bombs(Minesweeper_Board *board);
bool array_contains_value(int *array, int value, int length);
void draw_minesweeper(Minesweeper_Board *board);
int open_tile(Minesweeper_Board *board, int tile);
int number_of_bombs(Minesweeper_Board *board, int tile);
void open_empty_space(Minesweeper_Board *board, int tile, int index);
#pragma endregion
#pragma region Global
unsigned int width = MIDDLE;
unsigned int height = MIDDLE;
int num_bombs = MIDDLE * MIDDLE / NORMAL;
#pragma endregion //Global
void minesweeper_start(){
system("clear");
main_menu_minesweeper();
}
void main_menu_minesweeper(){
bool running = true;
while (running){
int option = 0;
system("clear");
printf("Waehlen Sie eine Option:\n");
printf("\t1.Start\n");
printf("\t2.Options\n");
printf("\t3.Exit\n");
scanf("%d", &option);
getchar();
system("clear");
switch (option){
case 1:
game_minesweeper();
break;
case 2:
options_minesweeper();
break;
case 3:
running = false;
break;
default:
break;
}
}
}
void game_minesweeper(){
bool running = true;
int x, y, t;
Minesweeper_Board board = initialize_minesweeper();
place_bombs(&board);
while (running){
system("clear");
draw_minesweeper(&board);
printf("Next turn (x, y, t(0 = open, 1 = flag)): ");
scanf("%d %d %d", &x, &y, &t);
getchar();
if(x < board.width && x > -1 && y < board.height && y > -1){
x = x + y * board.width;
if(t == 0){
int bombs = open_tile(&board, x);
if(bombs == -1){running = false;}
else if(bombs == 0){open_empty_space(&board, x, 0);}
else{board.tiles[x] = bombs + 48;}
}
if(t == 1){board.tiles[x] = board.tiles[x] == FLAG ? BLOCK : board.tiles[x] == BLOCK ? FLAG : board.tiles[x];}
}
if (t == 2){break;}
}
}
void options_minesweeper(){
bool running = true;
while (running){
int option = 0;
system("clear");
printf("Waehlen Sie eine Option:\n");
printf("\t1.Schwierigkeit\t\t%s\n", num_bombs < 2 * width * height / (NORMAL + EASY) ? "Einfach" : num_bombs < 2 * width * height / (NORMAL + HARD) ? "Normal" : "Schwer");
printf("\t2.Groesse\t\t%s\n", width * height < MIDDLE * SMALL ? "Klein" : width * height < MIDDLE * LARGE ? "Mittel" : "Gross");
printf("\t3.Custom\n");
printf("\t4.Exit\n");
scanf("%d", &option);
getchar();
switch (option){
case 1:
printf("Bitte neu Schwierigkeit eingeben (1 - 3): ");
scanf("%d", &num_bombs);
getchar();
num_bombs = num_bombs == 1 ? EASY : num_bombs == 2 ? NORMAL : HARD;
num_bombs = width * height / num_bombs;
break;
case 2:
num_bombs = num_bombs < width * height / NORMAL ? EASY : num_bombs == width * height / NORMAL ? NORMAL : HARD;
printf("Bitte neu Groesse eingeben (1 - 3): ");
scanf("%d", &width);
getchar();
width = width == 1 ? SMALL : width == 2 ? MIDDLE : LARGE;
height = width;
num_bombs = width * height / num_bombs;
break;
case 3:
printf("Bitte Breite des Spielfeld eingeben: ");
scanf("%d", &width);
getchar();
printf("Bitte Hoehe des Spielfeld eingeben: ");
scanf("%d", &height);
getchar();
printf("Bitte Anzahl der Bomben eingeben: ");
scanf("%d", &num_bombs);
getchar();
break;
case 4:
running = false;
break;
default:
break;
}
}
}
Minesweeper_Board initialize_minesweeper(){
Minesweeper_Board board;
board.width = width;
board.height = height;
board.tiles = (int*) malloc(width * height * sizeof(int));
for(int i = 0; i < width * height; i++){board.tiles[i] = BLOCK;}
board.marked = (int*) malloc(width * height * sizeof(int));
for(int i = 0; i < width * height; i++){board.marked[i] = 0;}
board.num_bombs = num_bombs;
board.bombs = (int*) malloc(board.num_bombs * sizeof(int));
for(int i = 0; i < num_bombs; i++){board.bombs[i] = -1;}
return board;
}
void place_bombs(Minesweeper_Board *board){
srand(time(NULL));
int r;
for(int i = 0; i < board->num_bombs; i++){
while (1){
r = rand() % (board->width * board->height);
if(!array_contains_value(board->bombs, r, board->num_bombs)){break;}
}
board->bombs[i] = r;
}
}
bool array_contains_value(int *array, int value, int length){
for(int i = 0; i < length; i++){
if(array[i] == value){return true;}
}
return false;
}
void draw_minesweeper(Minesweeper_Board *board){
printf(" ");
for(int i = 0; i < board->width; i++){printf("%d", i / 10);}
printf("\n ");
for(int i = 0; i < board->width; i++){printf("%d", i % 10);}
printf("\n +");
for(int i = 0; i < board->width; i++){printf("-");}
printf("+\n");
for(int i = 0; i < board->height; i++){
printf("%d", i / 10);
printf("%d|", i % 10);
for(int j = 0; j < board->width; j++){printf("%c", board->tiles[i * width + j]);}
printf("|%d", i / 10);
printf("%d\n", i % 10);
}
printf(" +");
for(int i = 0; i < board->width; i++){printf("-");}
printf("+\n ");
for(int i = 0; i < board->width; i++){printf("%d", i / 10);}
printf("\n ");
for(int i = 0; i < board->width; i++){printf("%d", i % 10);}
printf("\n");
}
int open_tile(Minesweeper_Board *board, int tile){
if(array_contains_value(board->bombs, tile, board->num_bombs)){return -1;}
int num = number_of_bombs(board, tile);
if(1){return num;}
}
int number_of_bombs(Minesweeper_Board *board, int tile){
int sum = 0;
for(int i = 0; i < 8; i++){
int check_tile = tile;
if(i < 3){if(tile % board->width == 0){continue;}
else{check_tile -= 1;}}
if(i > 4){if(tile % board->width + 1 == board->width){continue;}
else{check_tile += 1;}}
if(i % 3 == 0){if(i != 0 && tile / board->width == 0){continue;}
else{check_tile -= board->width;}}
if(i % 3 == 1){if(tile / board->width + 1 == height){continue;}
else{check_tile += board->width;}}
if(array_contains_value(board->bombs, check_tile, board->num_bombs)){sum++;}
}
return sum;
}
void open_empty_space(Minesweeper_Board *board, int tile, int index){
board->tiles[tile] = EMPTY;
board->marked[index++] = tile;
for(int i = 0; i < 8; i++){
int check_tile = tile;
if(i < 3){if(tile % board->width == 0){continue;}
else{check_tile -= 1;}}
if(i > 4){if(tile % board->width + 1 == board->width){continue;}
else{check_tile += 1;}}
if(i % 3 == 0){if(i != 0 && tile / board->width == 0){continue;}
else{check_tile -= board->width;}}
if(i % 3 == 1){if(tile / board->width + 1 == height){continue;}
else{check_tile += board->width;}}
int sum = open_tile(board, check_tile);
if(sum != 0){board->tiles[check_tile] = sum + 48;}
else if(!array_contains_value(board->marked, check_tile, index)){open_empty_space(board, check_tile, index);}
}
}

20
src/main/c/Minesweeper/minesweeper_start.h

@ -0,0 +1,20 @@
#ifndef MINESWEEPER_START_H
#define MINESWEEPER_START_H
typedef struct Minesweeper_Board{
unsigned int width;
unsigned int height;
int *tiles;
int *marked;
int num_bombs;
int *bombs;
}Minesweeper_Board;
void minesweeper_start();
Minesweeper_Board initialize_minesweeper();
bool array_contains_value(int *array, int bomb, int length);
int number_of_bombs(Minesweeper_Board *board, int tile);
#endif // MINESWEEPER_START_H

28
src/main/c/Snake/snake_start.c

@ -5,19 +5,21 @@
#include <stdlib.h>
#include "snake_start.h"
#include "get_character.h"
#define WIDTH 15
#define HEIGHT 15
#define TURN 0.5
#define START_LENGTH 3
#define START_DIRECTION 1
#define START_TILE 8 * 16 + 8
#pragma region Funktion_heads
void main_menu();
void game();
void options();
void main_menu_snake();
void game_snake();
void options_snake();
Snake initialize_snake();
void get_next_move(double limit, Snake *snake, bool *running);
void move_snake(Snake *snake);
void draw(Snake *snake, unsigned char fruit);
void draw_snake(Snake *snake, unsigned char fruit);
int part_of_snake(Snake *snake, unsigned char tile);
bool check_if_dead(Snake *snake);
unsigned char spawn_fruit(Snake *snake);
@ -30,10 +32,10 @@ double TIME_TURN = 0.5;
void snake_start(){
system("clear");
main_menu();
main_menu_snake();
}
void main_menu(){
void main_menu_snake(){
bool running = true;
while (running){
int option = 0;
@ -51,10 +53,10 @@ void main_menu(){
switch (option){
case 1:
game();
game_snake();
break;
case 2:
options();
options_snake();
break;
case 3:
running = false;
@ -66,7 +68,7 @@ void main_menu(){
}
}
void game(){
void game_snake(){
Snake snake = initialize_snake();
bool running = true;
clock_t t = clock();
@ -74,7 +76,7 @@ void game(){
while (running){
system("clear");
draw(&snake, fruit);
draw_snake(&snake, fruit);
if(check_if_dead(&snake)){break;}
t = clock() - t;
get_next_move(TIME_TURN - (double)t / CLOCKS_PER_SEC, &snake, &running);
@ -84,12 +86,12 @@ void game(){
}
}
void options(){
void options_snake(){
int difficulty = 1;
system("clear");
printf("Please select a difficulty(1 - 10): ");
scanf("%d", &difficulty);
TIME_TURN = TURN / difficulty;//(11 - difficulty) * TURN / 10;
TIME_TURN = TURN / difficulty;
}
Snake initialize_snake(){
@ -139,7 +141,7 @@ void move_snake(Snake *snake){
snake->segments[0] += snake->direction;
}
void draw(Snake *snake, unsigned char fruit){
void draw_snake(Snake *snake, unsigned char fruit){
printf("Score:%d Speed:%f\n", snake->length - START_LENGTH, TIME_TURN);
printf("+");
for(int i = 0; i < WIDTH; i++){printf("-");}

8
src/main/c/Snake/snake_start.h

@ -1,14 +1,12 @@
#ifndef SNAKE_START_H
#define SNAKE_START_H
#define HEIGHT 15
#define WIDTH 15
#define AREA HEIGHT * WIDTH
#include <stdbool.h>
typedef struct{
typedef struct Snake{
signed char direction;
char length;
unsigned char segments[AREA];
unsigned char segments[225];
}Snake;

12
src/main/c/main.c

@ -2,8 +2,8 @@
#include <stdlib.h>
#include <stdbool.h>
#include "Template/game100.h"
#include "Snake/snake_start.h"
#include "Minesweeper/minesweeper_start.h"
#include "Pong/pong.h"
int main(){
@ -19,8 +19,8 @@ int main(){
printf("\t2.Pong starten\n");
printf("\t3.Snake starten\n");
printf("\t4.Spiel4 starten\n");
printf("\t100.Template starten\n");
printf("\t6.Exit\n");
printf("\t7.Minesweeper starten\n");
printf("\t10.Exit\n");
scanf("%d", &option);
getchar();
@ -38,10 +38,10 @@ int main(){
case 4:
//start_game4();
break;
case 100:
start_game100();
case 7:
minesweeper_start();
break;
case 6:
case 10:
system("clear");
running = false;
break;

40
test/Minesweeper/test_bomb_in_array.c

@ -0,0 +1,40 @@
#ifdef TEST
#include "unity.h"
#include <stdbool.h>
#include "../../src/main/c/Minesweeper/minesweeper_start.h"
void setUp(void){}
void tearDown(void){}
void test_bomb_in_array(void){
/* arrange */
bool result;
int array[] = {5, 9, 42, 6, 87, 95, 202, 13, 45 ,78};
int bomb = 42;
int length = 10;
/* act */
result = array_contains_value(array, bomb, length);
/* assert */
TEST_ASSERT_TRUE(result);
}
void test_bomb_not_in_array(void){
/* arrange */
bool result;
int array[] = {5, 9, 42, 6, 87, 95, 202, 13, 45 ,78};
int bomb = 0;
int length = 10;
/* act */
result = array_contains_value(array, bomb, length);
/* assert */
TEST_ASSERT_FALSE(result);
}
#endif // TEST

97
test/Minesweeper/test_number_of_bombs.c

@ -0,0 +1,97 @@
#ifdef TEST
#include "unity.h"
#include <stdbool.h>
#include "../../src/main/c/Minesweeper/minesweeper_start.h"
void setUp(void){}
void tearDown(void){}
void test_no_bombs_placed_around_tile(void){
/* arrange */
int result;
Minesweeper_Board board = initialize_minesweeper();
int tile = 0 + board.width * 1;
/* act */
result = number_of_bombs(&board, tile);
/* assert */
TEST_ASSERT_EQUAL_INT(0, result);//no bombs placed
}
void test_searching_for_bombs_on_correct_tiles(void){
/* arrange */
int result;
Minesweeper_Board board = initialize_minesweeper();
int tile = 0 + board.width * 1;
board.bombs[0] = 0 + board.width * 3;
board.bombs[1] = 2 + board.width * 1;
board.bombs[2] = board.width - 1 + board.width * 1;
board.bombs[3] = 0 + board.width * (board.height - 1);
/* act */
result = number_of_bombs(&board, tile);
/* assert */
TEST_ASSERT_EQUAL_INT(0, result);//no bombs placed around tile
}
void test_2_bombs_around_tile(void){
/* arrange */
int result;
Minesweeper_Board board = initialize_minesweeper();
int tile = 0 + board.width * 1;
board.bombs[0] = 0 + board.width * 2;
board.bombs[1] = 1 + board.width * 1;
board.bombs[2] = board.width - 1 + board.width * 1;
board.bombs[3] = 0 + board.width * (board.height - 1);
/* act */
result = number_of_bombs(&board, tile);
/* assert */
TEST_ASSERT_EQUAL_INT(2, result);//2 bombs placed around tile
}
void test_5_bombs_around_tile(void){
/* arrange */
int result;
Minesweeper_Board board = initialize_minesweeper();
int tile = 1 + board.width * 0;
board.bombs[0] = 0 + board.width * 0;
board.bombs[1] = 2 + board.width * 0;
board.bombs[2] = 0 + board.width * 1;
board.bombs[3] = 1 + board.width * 1;
board.bombs[4] = 2 + board.width * 1;
/* act */
result = number_of_bombs(&board, tile);
/* assert */
TEST_ASSERT_EQUAL_INT(5, result);//5 bombs placed around tile
}
void test_8_bombs_around_tile(void){
/* arrange */
int result;
Minesweeper_Board board = initialize_minesweeper();
int tile = 7 + board.width * 7;
board.bombs[0] = 6 + board.width * 6;
board.bombs[1] = 7 + board.width * 6;
board.bombs[2] = 8 + board.width * 6;
board.bombs[3] = 6 + board.width * 7;
board.bombs[4] = 8 + board.width * 7;
board.bombs[5] = 6 + board.width * 8;
board.bombs[6] = 7 + board.width * 8;
board.bombs[7] = 8 + board.width * 8;
/* act */
result = number_of_bombs(&board, tile);
/* assert */
TEST_ASSERT_EQUAL_INT(8, result);//2 bombs placed around tile
}
#endif // TEST

4
test/Snake/test_collision.c

@ -1,8 +1,8 @@
#ifdef TEST
#include "unity.h"
#include <stdbool.h>
#include "../../src/main/c/Snake/snake_start.h"
#include "../../src/main/c/Snake/get_character.h"
#include "snake_start.h"
#include "get_character.h"
void setUp(void){}

Loading…
Cancel
Save