Last active
November 27, 2022 07:57
-
-
Save oriapp/1fb4636d433764cb0737ecdbfd5d2023 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <memory.h> | |
#include <stdbool.h> | |
#define MIN_CELLS 10 | |
#define MAX_CELLS 25 | |
#define COINS 6 | |
// | |
void draw_line (int length); | |
void place_coins(int *coins, int cellnum); | |
bool has_on_block(int coins[], int block); | |
// | |
void draw_board (int coins[], int cellnum) | |
{ | |
int arrTaken = 0; | |
draw_line(cellnum * COINS + 2); | |
for (int i = 0; i < cellnum; i++) | |
{ | |
printf(" | "); | |
if(coins[arrTaken] == i) | |
{ | |
printf(" * "); | |
arrTaken++; | |
} else { | |
printf(" "); | |
} | |
} | |
printf(" |\n"); | |
draw_line(cellnum * COINS + 2); | |
printf(" "); | |
for (int i = 0; i < cellnum; i++) | |
{ | |
printf(" %02d ", i); | |
} | |
printf("\n"); | |
} | |
void print_welcome_message() | |
{ | |
printf("\n- - - - - - - - - - - - - - - - -\n\n"); | |
printf("Welcome to the blah blah game!\n"); | |
printf("\n- - - - - - - - - - - - - - - - -\n\n"); | |
} | |
/// @brief Gets from stdin the number of blocks in game | |
int get_cellnum() | |
{ | |
int count; | |
do { | |
printf("How many blocks would you want to have: "); | |
scanf("%d", &count); | |
} while (count < 1 || count < (COINS + 1)); | |
return count; | |
} | |
/// @brief Input from the user each location of the coins on the board | |
void place_coins(int *coins, int cellnum) | |
{ | |
int coin, count = 0; | |
do { | |
do { | |
printf("Input coin location (%d): ", count); | |
scanf("%d", &coin); | |
} while (!(coin >= 0) || !(coin <= cellnum - 1)); | |
if(coin > coins[count]) | |
coins[count] = coin; | |
} while (++count < COINS); | |
} | |
/// @brief Given array of the coin locations, checks if the game has ended. | |
/// @return 1 if game ended else 0 | |
bool gameover(int coins[]) | |
{ | |
int inArow = 0; | |
for (int i = 0; i < COINS; i++) | |
{ | |
if(coins[i] == i) | |
{ | |
inArow++; | |
} | |
} | |
return inArow >= COINS; | |
} | |
// Index 0 is not found!!! blocks starts from 1!!! | |
/// @brief Indexing value of a block by given value | |
/// @param coins | |
/// @param value | |
/// @return block index in coins | |
int index_by_value(int coins[], int value) | |
{ | |
for (int i = 0; i < COINS; i++) | |
{ | |
if (coins[i] == value) | |
{ | |
return i; | |
} | |
} | |
return 0; | |
} | |
/// @brief Checks if block has a coin inside. | |
/// @param coins | |
/// @param block | |
/// @return | |
bool has_on_block(int coins[], int block) | |
{ | |
bool has = false; | |
for (int i = 0; i < COINS; i++) | |
{ | |
if(coins[i] == block) | |
has = true; | |
} | |
return has; | |
} | |
/// @brief Moves the coin one to the left | |
/// @param coins | |
/// @param index | |
void move(int coins[], int index) | |
{ | |
if(coins[index] - 1 >= 0) | |
{ | |
coins[index] -= 1; | |
} | |
} | |
/// @brief Gets as an input the current block and and how much steps. by these he moves the coin if found | |
/// @param coins | |
/// @param player | |
void make_move(int coins[], int player) | |
{ | |
int block, steps, index; | |
printf("Block: "); | |
scanf("%d", &block); | |
index = index_by_value(coins, block); | |
printf("Steps: "); | |
scanf("%d", &steps); | |
while (!(has_on_block(coins, --block)) && steps--) | |
{ | |
move(coins, index); | |
} | |
} | |
/// @brief Draws a line in n length. | |
/// @param length | |
void draw_line (int length) | |
{ | |
for (int i = 0; i < length; i++) | |
{ | |
printf("-"); | |
} | |
printf("\n"); | |
} | |
void print_game_summary(int player) | |
{ | |
printf("Congrats!!! Player %d has won!\n", player); | |
exit(0); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
int player_num = 1; | |
int coins[COINS] = {0}; | |
int cellnum; | |
cellnum = get_cellnum(); | |
place_coins(coins, cellnum); | |
print_welcome_message(); | |
draw_board(coins, cellnum); | |
while (!(gameover(coins))) { | |
printf("%d's turn!\n", player_num); | |
make_move(coins, player_num); | |
draw_board(coins, cellnum); | |
player_num = 3 - player_num; | |
} | |
print_game_summary(3-player_num); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment