mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-06 22:03:14 +02:00
Penguins Restoration Project 1.5%: feat: Added makefile, changed README, beautified interactive_mode from main.c
This commit is contained in:
parent
33150ba9d8
commit
feb7e11655
25
EPFU/README
25
EPFU/README
@ -1 +1,24 @@
|
|||||||
This is a WUT project about creating an AI that can play board game "Hey that's my fish"
|
This is a WUT project about creating an AI that can play board game "Hey that's my fish"
|
||||||
|
toDo:
|
||||||
|
|
||||||
|
feat:
|
||||||
|
Code to translate "Current board status" from terminal into GameState for bug fixing
|
||||||
|
|
||||||
|
fix: Game ending early despite one of the players having possible moves
|
||||||
|
Current board status:
|
||||||
|
0 1 2 3 4
|
||||||
|
0 P1 ~ F2 F3 F3
|
||||||
|
1 P2 ~ F3 F3 F2
|
||||||
|
2 P1 ~ F3 F2 F3
|
||||||
|
3 P2 F2 ~ F2 F2
|
||||||
|
4 P1 ~ F3 P2 ~
|
||||||
|
|
||||||
|
Player number 1 score: 10
|
||||||
|
Player number 2 score: 10
|
||||||
|
|
||||||
|
It's a tie!
|
||||||
|
Should have continued with player two still playing
|
||||||
|
|
||||||
|
fix: Picking a penguin that has no moves left changes this penguin into water (???) and then asks player to choose another penguin
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
BIN
EPFU/src/a.out
BIN
EPFU/src/a.out
Binary file not shown.
170
EPFU/src/main.c
170
EPFU/src/main.c
@ -15,59 +15,127 @@
|
|||||||
#define INTERACTIVE
|
#define INTERACTIVE
|
||||||
//#define AUTOMATIC
|
//#define AUTOMATIC
|
||||||
|
|
||||||
|
const char START_MESSAGE[] = "Hello again! Please enter the number of rows and columns \n";
|
||||||
|
const char PLACEMENT_TEXT[] = "================================\n \tPLACEMENT PHASE!\n================================\n";
|
||||||
|
const int BOTH_PLAYERS_HAVE_MOVES_FLAG = 0;
|
||||||
|
const int BOTH_PLAYERS_OUT_OF_MOVES_FLAG = 2;
|
||||||
|
const char PLAYER_ONE_WON[] = "Player ONE won!\n";
|
||||||
|
const char PLAYER_TWO_WON[] = "Player TWO won!\n";
|
||||||
|
const char TIE_MESSAGE[] = "It's a tie!\n";
|
||||||
|
|
||||||
|
void clearScreen()
|
||||||
|
{
|
||||||
|
system("clear || cls");
|
||||||
|
}
|
||||||
|
|
||||||
|
void enterBoardWidthAndHeight(GameState *Game)
|
||||||
|
{
|
||||||
|
printf(START_MESSAGE);
|
||||||
|
scanf("%d", &(Game -> board_width));
|
||||||
|
scanf("%d", &(Game -> board_height));
|
||||||
|
}
|
||||||
|
|
||||||
|
void prepareBoard(GameState *Game)
|
||||||
|
{
|
||||||
|
Game -> board = allocateMemory(*Game);
|
||||||
|
Game -> board = randomizeFields(*Game);
|
||||||
|
}
|
||||||
|
|
||||||
|
const int STARTING_SCORE = 0;
|
||||||
|
|
||||||
|
GameState initializePlayerScoreVariables(GameState Game)
|
||||||
|
{
|
||||||
|
Game.player_data[0].player_number = 1;
|
||||||
|
Game.player_data[1].player_number = 2;
|
||||||
|
Game.player_data[0].score = STARTING_SCORE;
|
||||||
|
Game.player_data[1].score = STARTING_SCORE;
|
||||||
|
return Game;
|
||||||
|
}
|
||||||
|
|
||||||
|
GameState setUpPenguinsPerPlayer(GameState Game)
|
||||||
|
{
|
||||||
|
Game.one_fish_fields = checkPlacement(Game);
|
||||||
|
Game.penguins_per_player = floor(Game.one_fish_fields / 2);
|
||||||
|
return Game;
|
||||||
|
}
|
||||||
|
|
||||||
|
GameState initializeGame(GameState Game)
|
||||||
|
{
|
||||||
|
enterBoardWidthAndHeight(&Game);
|
||||||
|
prepareBoard(&Game);
|
||||||
|
Game = initializePlayerScoreVariables(Game);
|
||||||
|
printBoard(Game);
|
||||||
|
Game = setUpPenguinsPerPlayer(Game);
|
||||||
|
Game.current_player = 1;
|
||||||
|
return Game;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
GameState insidePlacePenguinsLoop(GameState Game, int i)
|
||||||
|
{
|
||||||
|
while(!placePenguin(&Game));
|
||||||
|
printBoard(Game);
|
||||||
|
Game.current_player = (i + 1) % 2 + 1; // Since player numbers are 1 and 2 we need to make sure that this number is at least 1 and at max 2 as opposed to 0 and 1 if we just used i % 2
|
||||||
|
return Game;
|
||||||
|
}
|
||||||
|
|
||||||
|
GameState placePenguins(GameState Game)
|
||||||
|
{
|
||||||
|
int penguinsToPlace = 2 * Game.penguins_per_player;
|
||||||
|
for(int i = 0; i < penguinsToPlace; i++)
|
||||||
|
{
|
||||||
|
Game = insidePlacePenguinsLoop(Game, i);
|
||||||
|
}
|
||||||
|
return Game;
|
||||||
|
}
|
||||||
|
|
||||||
|
GameState setUpGameBeforePlayPhase(GameState Game)
|
||||||
|
{
|
||||||
|
clearScreen();
|
||||||
|
printBoard(Game);
|
||||||
|
Game.current_player = 1;
|
||||||
|
Game.flag = BOTH_PLAYERS_HAVE_MOVES_FLAG;
|
||||||
|
return Game;
|
||||||
|
}
|
||||||
|
|
||||||
|
GameState playPhase(GameState Game)
|
||||||
|
{
|
||||||
|
while(Game.flag != BOTH_PLAYERS_OUT_OF_MOVES_FLAG)
|
||||||
|
{
|
||||||
|
movePenguin(&Game);
|
||||||
|
clearScreen();
|
||||||
|
/*
|
||||||
|
if(movePenguin(&Game))
|
||||||
|
{
|
||||||
|
clearScreen();
|
||||||
|
} Old code, not sure why we check if movePenguin end successfuly?
|
||||||
|
*/
|
||||||
|
printBoard(Game);
|
||||||
|
}
|
||||||
|
return Game;
|
||||||
|
}
|
||||||
|
|
||||||
|
void calculateScorePhase(const GameState Game)
|
||||||
|
{
|
||||||
|
if(Game.player_data[0].score > Game.player_data[1].score)
|
||||||
|
{
|
||||||
|
printf(PLAYER_ONE_WON);
|
||||||
|
}else if(Game.player_data[0].score < Game.player_data[1].score)
|
||||||
|
{
|
||||||
|
printf(PLAYER_TWO_WON);
|
||||||
|
}else printf(TIE_MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
int interactive_mode(GameState Game)
|
int interactive_mode(GameState Game)
|
||||||
{
|
{
|
||||||
printf("Hello again! Please enter the number of rows and columns \n");
|
Game = initializeGame(Game);
|
||||||
scanf("%d", &Game.board_width);
|
printf(PLACEMENT_TEXT);
|
||||||
scanf("%d", &Game.board_height);
|
Game = placePenguins(Game);
|
||||||
// User enters values to the Game structure
|
Game = setUpGameBeforePlayPhase(Game);
|
||||||
// User Interaction - Telling user to input rows and columns
|
Game = playPhase(Game);
|
||||||
// Creating a board for our game using Field structure
|
calculateScorePhase(Game);
|
||||||
Game.board = allocateMemory(Game);
|
|
||||||
Game.board = randomizeFields(Game);
|
|
||||||
|
|
||||||
// Changed variables we use to Game structure and board
|
|
||||||
Game.player_data[0].player_number = 1;
|
|
||||||
Game.player_data[1].player_number = 2;
|
|
||||||
Game.player_data[0].score = 0;
|
|
||||||
Game.player_data[1].score = 0;
|
|
||||||
|
|
||||||
printBoard(Game);
|
|
||||||
Game.one_fish_fields = checkPlacement(Game);
|
|
||||||
Game.penguins_per_player = floor(Game.one_fish_fields / 2);
|
|
||||||
Game.current_player = 1;
|
|
||||||
|
|
||||||
|
|
||||||
printf("================================\n");
|
|
||||||
printf("\tPLACEMENT PHASE!\n");
|
|
||||||
printf("================================\n");
|
|
||||||
for(int i = 0; i < 2 * Game.penguins_per_player; i++)
|
|
||||||
{
|
|
||||||
while(!placePenguin(&Game));
|
|
||||||
system("clear || cls");
|
|
||||||
printBoard(Game);
|
|
||||||
Game.current_player = (i + 1) % 2 + 1;
|
|
||||||
|
|
||||||
}
|
|
||||||
system("clear || cls");
|
|
||||||
printBoard(Game);
|
|
||||||
Game.current_player = 1;
|
|
||||||
Game.flag = 0;
|
|
||||||
while(Game.flag != 2)
|
|
||||||
{
|
|
||||||
if(movePenguin(&Game))
|
|
||||||
{
|
|
||||||
system("clear || cls");
|
|
||||||
}
|
|
||||||
printBoard(Game);
|
|
||||||
}
|
|
||||||
if(Game.player_data[0].score > Game.player_data[1].score)
|
|
||||||
{
|
|
||||||
printf("Player ONE won!\n");
|
|
||||||
}else if(Game.player_data[0].score < Game.player_data[1].score)
|
|
||||||
{
|
|
||||||
printf("Player TWO won!\n");
|
|
||||||
}else printf("It's a tie!\n");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int main(
|
int main(
|
||||||
|
|||||||
2
EPFU/src/makefile
Normal file
2
EPFU/src/makefile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
penguins: main.c
|
||||||
|
gcc -Wall -Wextra -pedantic main.c
|
||||||
Loading…
Reference in New Issue
Block a user