commit a1de1a93d747b2bec02065b3602f4a5a79a07a49 Author: PolishPigeon Date: Sun Dec 5 04:29:26 2021 +0100 Commiting whole project diff --git a/EPFU/README b/EPFU/README new file mode 100644 index 00000000..3d3c1729 --- /dev/null +++ b/EPFU/README @@ -0,0 +1 @@ +This is a WUT project about creating an AI that can play board game "Hey that's my fish" \ No newline at end of file diff --git a/EPFU/doc/.gitkeep b/EPFU/doc/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EPFU/doc/05.12/.gitkeep b/EPFU/doc/05.12/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EPFU/doc/05.12/report.pdf b/EPFU/doc/05.12/report.pdf new file mode 100644 index 00000000..ef30a300 Binary files /dev/null and b/EPFU/doc/05.12/report.pdf differ diff --git a/EPFU/doc/07.11/.gitkeep b/EPFU/doc/07.11/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EPFU/doc/07.11/Report b/EPFU/doc/07.11/Report new file mode 100644 index 00000000..0d5c35cf --- /dev/null +++ b/EPFU/doc/07.11/Report @@ -0,0 +1,7 @@ +Data Structures: We will use int, char, int 2D array for board, char array for names of the players for data structures + +Interaction with the user: Our plan is to show a lot of messages to user about what it happening, so for example at the start we will welcome user to play, we will +let users input their names and later refer to them by their names ( using char array ), we will also print out scoreboard with names of the players and scores +we will use printf for showing messages to the user and scanf for getting information from the user + +Main loop: Inside the main.c file \ No newline at end of file diff --git a/EPFU/doc/19.12/.gitkeep b/EPFU/doc/19.12/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EPFU/doc/19.12/report.pdf b/EPFU/doc/19.12/report.pdf new file mode 100644 index 00000000..804633ee Binary files /dev/null and b/EPFU/doc/19.12/report.pdf differ diff --git a/EPFU/doc/21.11/.gitkeep b/EPFU/doc/21.11/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EPFU/doc/21.11/boardgeneration.jpg b/EPFU/doc/21.11/boardgeneration.jpg new file mode 100644 index 00000000..3936abf5 Binary files /dev/null and b/EPFU/doc/21.11/boardgeneration.jpg differ diff --git a/EPFU/doc/21.11/checkcoordinates.jpg b/EPFU/doc/21.11/checkcoordinates.jpg new file mode 100644 index 00000000..ef766c6d Binary files /dev/null and b/EPFU/doc/21.11/checkcoordinates.jpg differ diff --git a/EPFU/doc/21.11/main.png b/EPFU/doc/21.11/main.png new file mode 100644 index 00000000..aedd3532 Binary files /dev/null and b/EPFU/doc/21.11/main.png differ diff --git a/EPFU/doc/21.11/movement.jpg b/EPFU/doc/21.11/movement.jpg new file mode 100644 index 00000000..0737e171 Binary files /dev/null and b/EPFU/doc/21.11/movement.jpg differ diff --git a/EPFU/doc/21.11/placement.jpg b/EPFU/doc/21.11/placement.jpg new file mode 100644 index 00000000..9fc3a66f Binary files /dev/null and b/EPFU/doc/21.11/placement.jpg differ diff --git a/EPFU/doc/21.11/report.jpg b/EPFU/doc/21.11/report.jpg new file mode 100644 index 00000000..3aa408f9 Binary files /dev/null and b/EPFU/doc/21.11/report.jpg differ diff --git a/EPFU/doc/21.11/userinteraction.jpg b/EPFU/doc/21.11/userinteraction.jpg new file mode 100644 index 00000000..f271bfdb Binary files /dev/null and b/EPFU/doc/21.11/userinteraction.jpg differ diff --git a/EPFU/doc/23.10/.gitkeep b/EPFU/doc/23.10/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EPFU/doc/AIflowchart.pdf b/EPFU/doc/AIflowchart.pdf new file mode 100644 index 00000000..d4fbd97c Binary files /dev/null and b/EPFU/doc/AIflowchart.pdf differ diff --git a/EPFU/doc/longerinteractiveversionFlowchart.pdf b/EPFU/doc/longerinteractiveversionFlowchart.pdf new file mode 100644 index 00000000..c7671a1a Binary files /dev/null and b/EPFU/doc/longerinteractiveversionFlowchart.pdf differ diff --git a/EPFU/doc/report.txt b/EPFU/doc/report.txt new file mode 100644 index 00000000..0457150b --- /dev/null +++ b/EPFU/doc/report.txt @@ -0,0 +1,8 @@ +Team- 101B +Krzysztof Rudnicki +Patryk Kalbarczyk +Shaunak Bhardwaj +Jasmine Armon + +Meeting every Monday – 12:00 +Also communicate on WhatsApp consistently. diff --git a/EPFU/doc/shorterinteractiveversionFlowchart.pdf b/EPFU/doc/shorterinteractiveversionFlowchart.pdf new file mode 100644 index 00000000..c27a067c Binary files /dev/null and b/EPFU/doc/shorterinteractiveversionFlowchart.pdf differ diff --git a/EPFU/src/.gitkeep b/EPFU/src/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EPFU/src/Old stuff/.gitkeep b/EPFU/src/Old stuff/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EPFU/src/Old stuff/board.c b/EPFU/src/Old stuff/board.c new file mode 100644 index 00000000..9ced0963 --- /dev/null +++ b/EPFU/src/Old stuff/board.c @@ -0,0 +1,45 @@ +#include +#include +#include + +//Defining struct Board +typedef struct +{ + int penguin_no; + int player_no; +}Board; + +//Board Dimensions +int board_width = 6; +int board_height =6; + +int main() +{ + srand(time(NULL)); + Board board[board_height][board_width]; // Creating 2D array of type Board + + for (int i = 0; i < board_height ; ++i) // Populating our board with penguins and players (player_no set to 0 as no player is present on the board) + { + for (int j = 0; j < board_width ; ++j) + { + board[i][j].player_no = 0; + board[i][j].penguin_no = rand() % 4; // Randomizing no of penguins on each cell ( Between 0 and 3 ) + + } + } + + for (int i = 0; i < board_height ; ++i) // Printing our board + { + for (int j = 0; j < board_width ; ++j) + { + printf("[Penguins = %d][PLayer no = %d] ",board[i][j].penguin_no,board[i][j].player_no ); + + + } + printf("\n"); + + } + + + return 0; +} \ No newline at end of file diff --git a/EPFU/src/Old stuff/src_main.c b/EPFU/src/Old stuff/src_main.c new file mode 100644 index 00000000..7edb78f4 --- /dev/null +++ b/EPFU/src/Old stuff/src_main.c @@ -0,0 +1,43 @@ +#include +#include +char P1,P2; int m,n; int peng; + +void welcome() +{ + printf("Heyy there!! I know you don't wanna do your assignment. Lets play HEY THAT'S MY FISH \n"); +} +void generate() +{ + printf("Hello again! Please enter the number of rows and columns \n"); + scanf("%d",&m); scanf("%d",&n); + int a[m][n]; + for (int i=0;i 0 && Coordinates.coordinate_1 + 1 < Game.board_height) + { + if(Coordinates.coordinate_2 > 0 && Coordinates.coordinate_2 < Game.board_width) + { + if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 - 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 - 1].player_no == 0) // LEFT + { + return 1; + }else if(Game.board[Coordinates.coordinate_1 - 1][Coordinates.coordinate_2].fish_no != 0 && Game.board[Coordinates.coordinate_1 - 1][Coordinates.coordinate_2].player_no == 0) // UP + { + return 2; + }else if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].player_no == 0) // DOWN + { + return 3; + }else if(Game.board[Coordinates.coordinate_1 + 1][Coordinates.coordinate_2].fish_no != 0 && Game.board[Coordinates.coordinate_1 + 1][Coordinates.coordinate_2].player_no == 0) //RIGHT + { + return 4; + }else return 0; + }else if(Coordinates.coordinate_2 == 0) + { + if(Game.board[Coordinates.coordinate_1 - 1][Coordinates.coordinate_2].fish_no != 0 && Game.board[Coordinates.coordinate_1 - 1][Coordinates.coordinate_2].player_no == 0) // UP + { + return 2; + }else if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].player_no == 0) // DOWN + { + return 3; + }else if(Game.board[Coordinates.coordinate_1 + 1][Coordinates.coordinate_2].fish_no != 0 && Game.board[Coordinates.coordinate_1 + 1][Coordinates.coordinate_2].player_no == 0) //RIGHT + { + return 4; + }else return 0; + }else if(Coordinates.coordinate_2 == Game.board_width - 1) + { + if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 - 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 - 1].player_no == 0) // LEFT + { + return 1; + }else if(Game.board[Coordinates.coordinate_1 - 1][Coordinates.coordinate_2].fish_no != 0 && Game.board[Coordinates.coordinate_1 - 1][Coordinates.coordinate_2].player_no == 0) // UP + { + return 2; + }else if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].player_no == 0) // DOWN + { + return 3; + }else return 0; + }else + { + printf("You missed a case!\n"); + return 0; + } + }else if(Coordinates.coordinate_1 == 0) + { + if(Coordinates.coordinate_2 > 0 && Coordinates.coordinate_2 < Game.board_width) + { + if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 - 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 - 1].player_no == 0) // LEFT + { + return 1; + }else if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].player_no == 0) // DOWN + { + return 3; + }else if(Game.board[Coordinates.coordinate_1 + 1][Coordinates.coordinate_2].fish_no != 0 && Game.board[Coordinates.coordinate_1 + 1][Coordinates.coordinate_2].player_no == 0) //RIGHT + { + return 4; + }else return 0; + }else if(Coordinates.coordinate_2 == 0) + { + if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].player_no == 0) // DOWN + { + return 2; + }else if(Game.board[Coordinates.coordinate_1 + 1][Coordinates.coordinate_2].fish_no != 0 && Game.board[Coordinates.coordinate_1 + 1][Coordinates.coordinate_2].player_no == 0) //RIGHT + { + return 4; + }else return 0; + }else if(Coordinates.coordinate_2 == Game.board_width) + { + if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 - 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 - 1].player_no == 0) // LEFT + { + return 1; + }else if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].player_no == 0) // DOWN + { + return 3; + }else return 0; + }else + { + printf("You missed a case!\n"); + return 0; + } + }else if(Coordinates.coordinate_1 + 1 == Game.board_height) + { + if(Coordinates.coordinate_2 > 0 && Coordinates.coordinate_2 < Game.board_width) + { + if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 - 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 - 1].player_no == 0) // LEFT + { + return 1; + }else if(Game.board[Coordinates.coordinate_1 - 1][Coordinates.coordinate_2].fish_no != 0 && Game.board[Coordinates.coordinate_1 - 1][Coordinates.coordinate_2].player_no == 0) // UP + { + return 2; + }else if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].player_no == 0) // RIGHT + { + return 4; + }else return 0; + }else if(Coordinates.coordinate_2 == 0) + { + if(Game.board[Coordinates.coordinate_1 - 1][Coordinates.coordinate_2].fish_no != 0 && Game.board[Coordinates.coordinate_1 - 1][Coordinates.coordinate_2].player_no == 0) // UP + { + return 2; + }else if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 + 1].player_no == 0) // RIGHT + { + return 4; + }else return 0; + }else if(Coordinates.coordinate_2 == Game.board_width) + { + if(Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 - 1].fish_no != 0 && Game.board[Coordinates.coordinate_1][Coordinates.coordinate_2 - 1].player_no == 0) // LEFT + { + return 1; + }else if(Game.board[Coordinates.coordinate_1 - 1][Coordinates.coordinate_2].fish_no != 0 && Game.board[Coordinates.coordinate_1 - 1][Coordinates.coordinate_2].player_no == 0) // UP + { + return 2; + }else return 0; + }else + { + printf("You missed a case!\n"); + return 0; + } + }else + { + printf("You missed a case!\n"); + return 0; + } +} + +int checkMovement(Movement_coordinates c, GameState Game) +{ + int i = 0; + if(c.coordinate_1 == c.coordinate_3) + { + if(c.coordinate_2 < c.coordinate_4) + { + for(i = c.coordinate_2 + 1; i <= c.coordinate_4; i++) + { + if(Game.board[c.coordinate_1][i].fish_no == 0 || Game.board[c.coordinate_1][i].player_no != 0) + { + printf("There is another penguin or water on field Board[%d][%d]!\n", c.coordinate_1, i); + return 0; + } + } + return 1; + }else if(c.coordinate_2 > c.coordinate_4) + { + for(i = c.coordinate_2 - 1; i >= c.coordinate_4; i--) + { + if(Game.board[c.coordinate_1][i].fish_no == 0 || Game.board[c.coordinate_1][i].player_no != 0) + { + printf("There is another penguin or water on field Board[%d][%d]!\n", c.coordinate_1, i); + return 0; + } + } + return 1; + } + }else if(c.coordinate_2 == c.coordinate_4) + { + if(c.coordinate_1 < c.coordinate_3) + { + for(i = c.coordinate_1 + 1; i <= c.coordinate_3; i++) + { + if(Game.board[i][c.coordinate_2].fish_no == 0 || Game.board[i][c.coordinate_2].player_no != 0) + { + printf("There is another penguin or water on field Board[%d][%d]!\n", i, c.coordinate_2); + return 0; + } + } + return 1; + }else if(c.coordinate_1 > c.coordinate_3) + { + for(i = c.coordinate_1 - 1; i >= c.coordinate_3; i--) + { + if(Game.board[i][c.coordinate_2].fish_no == 0 || Game.board[i][c.coordinate_2].player_no != 0) + { + printf("There is another penguin or water on field Board[%d][%d]!\n", i, c.coordinate_2); + return 0; + } + } + return 1; + } + }else + { + printf("You can't move the penguin diagonally!\n"); + return 0; + } +} +int ourPenguins(GameState Game) +{ + int i, j; // This function checks how many penguins we have and returns this number + int number_of_our_penguins; + number_of_our_penguins = 0; + for(i = 0; i < Game.board_height; i++) + { + for(j = 0; j < Game.board_width; j++) + { + if(Game.board[i][j].player_no == Game.our_player_number) + { + number_of_our_penguins++; + } + } + } + return number_of_our_penguins; +} + +int checkMove(GameState Game) +{ + Movement_coordinates Coordinates; + int i, j, anymove; + for(i = 0; i < Game.board_height; i++) // This goes through the whole board and checks if there are any moves possible for every penguin we find + { + for(j = 0; j < Game.board_width; j++) + { + if(Game.board[i][j].player_no != 0) + { + Coordinates.coordinate_1 = i; + Coordinates.coordinate_2 = j; + printf("We check if there is any move possible at board[%d][%d]\n", Coordinates.coordinate_1, Coordinates.coordinate_2); + anymove = anyMovePossible(Coordinates, Game); + if(anymove != 0) + { + printf("There is a possible move!\n"); + return 1; + } + } + } + } + printf("There are no possible moves!\n"); + return 0; +} +int checkPlayer(GameState Game) +{ + Movement_coordinates c; + for(int i = 0; i < Game.board_height; i++) + { + for(int j = 0; j < Game.board_width; j++) + { + //printf("Player number:%d\n", Game.board[i][j].player_no); + if(Game.board[i][j].player_no == Game.current_player) + { + c.coordinate_1 = i; + c.coordinate_2 = j; + if(anyMovePossible(c, Game) != 0) + { + //printf("Player %d, has at least one move left!\n", Game.current_player); + return 1; + } + } + } + } + return 0; +} + +int checkPlacement(GameState Game) +{ + int one_fish = 0; + for(int i = 0; i < Game.board_height; i++) + { + for(int j = 0; j < Game.board_width; j++) + { + if(Game.board[i][j].fish_no == 1) + { + one_fish++; + } + } + } + return one_fish; +} diff --git a/EPFU/src/command_line_parameters.c b/EPFU/src/command_line_parameters.c new file mode 100644 index 00000000..1df8f028 --- /dev/null +++ b/EPFU/src/command_line_parameters.c @@ -0,0 +1,116 @@ +#include "game_struct.h" +#include +#include +#include +#include +#include "handling_files.c" + +int numberOfPenguins(GameState Game, int command_number) +{ + char number[10]; + int len = strlen(Game.command_line[command_number]); + int j = 0; + for(int i = 9; i < len; i++) + { + number[j] = Game.command_line[command_number][i]; + j++; + } + Game.penguins = atoi(number); + return Game.penguins; +} + +int isItBoardParameter(GameState Game, int command_number) +{ + int len = strlen(Game.command_line[command_number]); + int j = 0, i; + char extension[4]; + int answer = 0; + memset(extension, 0, strlen(extension)); + for(i = len - 4; i < len; i++) + { + extension[j] = Game.command_line[command_number][i]; + j++; + } + if(!strcmp(extension, ".txt")) + { + answer = 1; + }else answer = 0; + return answer; +} + +char** handlingParameters(int argc, char *argv[], GameState Game) +{ + int i, j; + for(i = 1; i < argc; i++) + { + strcpy(Game.command_line[i - 1], argv[i]); + } + for(i = 0; i < argc - 1; i++) + { + printf("%s\n", Game.command_line[i]); + } + return Game.command_line; +} + +int checkParameterType(GameState *Game, int command_number) +{ + if(!strcmp(Game -> command_line[command_number], "phase=placement")) // Checks if the command line parameters says phase=placement + { + Game -> phase = 1; + printf("Game phase is placement!\n"); + return 1; + }else if(!strcmp(Game -> command_line[command_number], "phase=movement")) // Checks if the command line parameters says phase=movement + { + Game -> phase = 2; + printf("Game phase is movement!\n"); + return 1; + }else if(!strncmp(Game -> command_line[command_number], "penguins=", 9)) // Checks if the command line parameters says penguins + { + Game -> penguins = numberOfPenguins(*Game, command_number); + Game -> penguins_from_command = 1; + printf("There are %d penguins\n", Game -> penguins); + return 2; + }else if(isItBoardParameter(*Game, command_number)) // // Checks if the command line parameters gives us board file name + { + printf("It is a board file!\n"); + if(Game -> file_type == 0) // First file is input board file + { + printf("It is an input board file!\n"); + char line[255]; + Game -> file_type = Game -> file_type + 1; + printf("%s", Game -> command_line[command_number]); + Game -> input_board_command_number = command_number; + Game -> input_board = fopen(Game -> command_line[command_number], "r+"); + return 3; + }else if(Game -> file_type == 1) // Second file is output board file + { + printf("It is an output board file!\n"); + char line[255]; + if(!strcmp(Game -> command_line[Game -> input_board_command_number], Game -> command_line[command_number])) // If the input and board file are the same + // then we open it in r+ mode + { + Game -> output_board = fopen(Game -> command_line[command_number], "r+"); + } // Else we open it in w mode + else + { + Game -> output_board = fopen(Game -> command_line[command_number], "w"); + } + Game -> file_type = Game -> file_type + 1; + return 4; + }else if(Game -> file_type == 2) + { + printf("There are too many files!\n"); + return 0; + } + }else if(!strcmp(Game -> command_line[command_number], "id")) + { + printf("It asks for id!\n"); + Game -> check_for_id = 1; + return 5; + }else + { + printf("This: '%s' command line parameter is wrong!\n", Game -> command_line[command_number]); + return 0; + } + return 2137; +} diff --git a/EPFU/src/game_struct.h b/EPFU/src/game_struct.h new file mode 100644 index 00000000..8b5a9f7a --- /dev/null +++ b/EPFU/src/game_struct.h @@ -0,0 +1,53 @@ +#ifndef STRUCT_H_ +#define STRUCT_H_ +#include + +typedef struct // Struct for our board +{ + int fish_no; + int player_no; +}Field; + +typedef struct Coordinates +{ + int coordinate_1; // Coordinate of penguin's location. + int coordinate_2; // Coordinate of penguin's location. + int coordinate_3; // Coordinate of where we want to put the penguin. + int coordinate_4; // Coordinate of where we want to put the penguin. +}Movement_coordinates; + +struct Player +{ + char playerID[500]; + int player_number; + int score; +}; + +typedef struct Game // Struct containing all of the variables that the program needs. +{ + char** command_line; + Field** board; + FILE *input_board; + FILE *output_board; + int board_height; // The height of the board. + int board_width; // The width of the board. + int total_players; // Total number of players. + int one_fish_fields; + int current_player; // Player. + int penguins; + int penguins_per_player; + int file_type; // Flag that checks if the file from command line parameter is inputboardfile (0) or outputboardfile (1) + int phase; //decides the phase of the game 1 - placement; 2 - movement + int our_player_number; + int lines_number; + int input_board_command_number; + int penguins_from_command; + int check_for_id; + struct Player player_data[10]; + int flag; +}GameState; + + +#endif + + diff --git a/EPFU/src/generate.c b/EPFU/src/generate.c new file mode 100644 index 00000000..9f3f32f9 --- /dev/null +++ b/EPFU/src/generate.c @@ -0,0 +1,52 @@ +#include "game_struct.h" +#include +#include +#include +#include + +Field** allocateMemory(GameState Game) +{ + //This for loop assigns pointers from our array of pointer + // to the beginning of memory blocks with size of Field struct + // There are board_width number of smaller memory blocks + // And board_height number of bigger memory blocks + + Game.board = (Field **)malloc(Game.board_height * sizeof(Field*)); + for(int i = 0; i < Game.board_height; i++) + { + Game.board[i] = (Field*)malloc(Game.board_width*sizeof(Field)); + } + return Game.board; +} + +Field** randomizeFields(GameState Game) +{ + srand(time(0)); + + for(int i = 0; i < Game.board_height; i++) + { + for(int j = 0; j < Game.board_width; j++) + { + Game.board[i][j].fish_no = rand() % 3 + 1; + //upside of this method + // is that it does not change the way we refer to this array + // later on in the program + + Game.board[i][j].player_no = 0; + + } + } + return Game.board; +} + +char** generateCommandLine(GameState Game, int argc, char *argv[]) +{ + int i, j; + Game.command_line = (char **)malloc(argc * sizeof(char*)); + for(i = 0; i < argc; i++) + { + Game.command_line[i] = (char*)malloc(30 *sizeof(char)); + } + + return Game.command_line; +} diff --git a/EPFU/src/handling_files.c b/EPFU/src/handling_files.c new file mode 100644 index 00000000..a047f0a7 --- /dev/null +++ b/EPFU/src/handling_files.c @@ -0,0 +1,303 @@ +#include "game_struct.h" +#include +#include +#include +#include "generate.c" + +int samePlayerID(GameState Game, int k) +{ + + for(int i = 1; i < k; i++) + { + if(!strcmp(Game.player_data[i].playerID, Game.player_data[k].playerID)) + { + return 1; + } + } + return 0; +} + +int handleInputFile(GameState *Game) +{ + Game -> lines_number = 0; + char rows[30], columns[30]; // Stores rows number and columns number from the board file + char c; // Gets all the characters from the file + int rows_number, columns_number; // Stores int values of rows and columns number from the board file + int i = 0, j; + while((c = getc(Game -> input_board)) != ' ') // Getting rows number + { + if(c >= '0' && c <= '9' && c != ' ') // Checks if rows number is a number + { + rows[i] = c; + i++; + if(i >= 30) + { + printf("Row number is too big! \n"); + return 1; + } + }else + { + printf("row number is not a number! \n"); + return 1; + } + } + + i = 0; + while((c = getc(Game -> input_board)) != ' ' && c != '\n') // Gets columns number + { + if(c >= '0' && c <= '9' && c != ' ') // Checks if columns number is a number + { + columns[i] = c; + i++; + if(i >= 30) + { + printf("Columns number is too big! \n"); + return 1; + } + }else + { + printf("row number is not a number! \n"); + return 1; + } + } + + rows_number = atoi(rows); // Changes rows number from a string to an integer + printf("Rows number is: %d\n", rows_number); + columns_number = atoi(columns); + printf("Columns number is: %d\n", columns_number); + + Game -> board_height = rows_number; + Game -> board_width = columns_number; + + if(rows_number == 0 || columns_number == 0) // Checks if rows or columns number is equal to 0 + { + if(rows_number == 0) + { + printf("Number of rows is equal to 0!\n"); + return 1; + }else + { + printf("Number of columns is equal to 0!\n"); + return 1; + } + } + Game -> board = allocateMemory(*Game); // Allocates memory for our board + int check = 0; // flag that is either 0 if we get fish number of 1 if we get player number + int value = 0; // temporary value for fish number and player number + int three = 1; // Used to check if there are any extra characters in file between board files + int current_columns = 0; // Used to check is there are any extra columns + + for(int i = 0; i < Game -> board_height; i++) // This for loop gets fields from the board file + { + int j = 0; + while((c = getc(Game -> input_board)) != '\n') + { + if(three % 3 != 0 || (i == 0 && j == 0)) + { + if(c != ' ' && check == 0) + { + if(c >= '0' && c <= '3' && c != ' ') + { + value = c - '0'; // According to Stack Overflow it works and is valid with all standards + Game -> board[i][j].fish_no = value; + check = 1; + three++; + }else if(c > '3' && c <= '9') + { + printf("Number of fishes on field: [%d][%d] is too big!\n", i ,j); + return 1; + }else + { + printf("Number of fishes on field: [%d][%d] is not a number!\n", i, j); + return 1; + } + }else if(c != ' ' && check == 1) + { + if(c >= '0' && c <= '9') + { + value = c - '0'; + Game -> board[i][j].player_no = value; + check = 0; + j++; + three++; + current_columns++; + }else + { + printf("Number of players on field: [%d][%d] is not a number!\n", i, j); + return 1; + } + } + }else if(c == ' ') + { + three++; + }else + { + printf("There are more than two elements on field: [%d][%d]", i, j); + return 1; + } + } + if(current_columns != columns_number) + { + printf("There is different number of columns than there should be!\n"); + return 1; + }else current_columns = 0; + } + + i = 0; + int expected_player = 1; // Used to put player ID, score and number into right array and checks if player numbers are in order + // We use 1 because it is much simpler + while((c = getc(Game -> input_board)) != EOF) // Checks last m+2 and consecutive rows + { + j = 0; + do // checks player id + { + if(c != ' ' && c != '"' && c != '\n' && c != EOF) + { + if(j < 500) + { + Game -> player_data[expected_player].playerID[j] = c; + j++; + }else + { + printf("Id of this player is too long!\n"); + return 1; + } + }else if(c == ' ') + { + printf("Player ID contains a space! "); + return 1; + }else if(c == '"') + { + printf("Player ID contains a quotation mark! "); + return 1; + }else if(c == '\n') + { + while((c = getc(Game -> input_board)) != EOF) + { + if(c != ' ' && c != '\n' && c != EOF) + { + printf(":<"); + return 1; + } + } + Game -> total_players = expected_player - 1; + return 0; + }else + { + printf("Player ID contains end of file character!"); + return 1; + } + + }while((c = getc(Game -> input_board)) != ' '); + + + for(i = strlen(Game -> player_data[expected_player].playerID); i >= j; i--) // We clear playerID string characters after the characters we get from the file + { + Game -> player_data[expected_player].playerID[i] = 0; + } + + if(samePlayerID(*Game, expected_player) && expected_player != 1) // Checks if the players ID's are the same + { + printf("Two player ID's are the same!\n"); + return 1; + } + + c = getc(Game -> input_board); + if(c >= '1' && c <= '9') + { + Game -> player_data[expected_player].player_number = c - '0'; // Gets player number from the file + if(expected_player != Game -> player_data[expected_player].player_number) + { + printf("Player numbers from the file is not in order!"); + return 1; + } + + if(!strcmp(Game -> player_data[expected_player].playerID, "marcel")) // Checks if the player ID is our's player ID + { + Game -> our_player_number = Game -> player_data[expected_player].player_number; + } + + if( getc(Game -> input_board) == ' ') + { + char score[100]; + i = 0; + while((c = getc(Game -> input_board)) != '\n' && c != EOF) // Gets player score from the file + { + if(c >= '0' && c <= '9' && i < 100) + { + score[i] = c; + i++; + }else + { + printf("Player score is not a number!\n"); + return 1; + } + + } + + Game -> player_data[expected_player].score = atoi(score); + printf("Player ID is: %s\n", Game -> player_data[expected_player].playerID); + printf("Player number is: %d\n", Game -> player_data[expected_player].player_number); + printf("Player score is: %d\n", Game -> player_data[expected_player].score); + Game -> lines_number++; + expected_player++; + }else + { + printf("Player number is longer than 1 character! "); + return 1; + } + }else + { + printf("Player number is not a number or is equal to 0 \n"); + return 1; + } + + } + if (Game -> our_player_number == 0) + { + printf("There is no id for our player!\n"); + return 1; + } + Game -> total_players = expected_player - 1; + fclose(Game -> input_board); + return 0; +} + +int checkForStrayPenguins(GameState Game) +{ + printf("%d\n", Game.total_players); + for(int i = 0; i < Game.board_height; i++) + { + for(int j = 0; j < Game.board_width; j++) + { + if(Game.board[i][j].player_no > Game.total_players) + { + printf("There is a stray penguin on field [%d][%d]\n", i, j); + printf("%d\n", Game.board[i][j].player_no); + return 1; + } + } + } + return 0; +} + +int handleOutPutFile(GameState Game) +{ + int i, j; + fprintf(Game.output_board, "%d %d\n", Game.board_height, Game.board_width); + for(i = 0; i < Game.board_height; i++) + { + for(j = 0; j < Game.board_width; j++) + { + fprintf(Game.output_board, "%d%d ", Game.board[i][j].fish_no, Game.board[i][j].player_no); + } + fprintf(Game.output_board, "\n"); + } + printf("%d", Game.lines_number); + for(i = 1; i < Game.lines_number + 1; i++) + { + fprintf(Game.output_board, "%s %d %d\n", Game.player_data[i].playerID, Game.player_data[i].player_number, Game.player_data[i].score); + } + fclose(Game.output_board); + return 0; +} diff --git a/EPFU/src/main.c b/EPFU/src/main.c new file mode 100644 index 00000000..978644fb --- /dev/null +++ b/EPFU/src/main.c @@ -0,0 +1,189 @@ +#include +#include +#include +#include +#include +#include +#include "userinteraction.c" +#include "placement.c" +#include "movement.c" +#include "command_line_parameters.c" +#include "game_struct.h" // Header file for file where we store all our structures +#include +//#define INTERACTIVE +#define AUTOMATIC + +int interactive_mode(GameState Game) +{ + printf("Hello again! Please enter the number of rows and columns \n"); + scanf("%d", &Game.board_width); + scanf("%d", &Game.board_height); + // User enters values to the Game structure + // User Interaction - Telling user to input rows and columns + // Creating a board for our game using Field structure + 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; +} +int main(int argc, char *argv[]) +{ + GameState Game; + #ifdef AUTOMATIC + printf("Its automatic mode\n"); + + if(argc != 1) + { + // Creating a Game variable that will hold informations needed to run the program + Game.command_line = generateCommandLine(Game, argc, argv); // Generates 2D dynamic array of strings + handlingParameters(argc, argv, Game); // Stores all commands inside Game.command_line array + Game.file_type = 0; // We use this variable to check if the file we open is inputboardfile or outputboardfile + Game.phase = 0; // We use this variable to check if current game phase is placement (1) or movement(2) + Game.penguins_from_command = 0; // This variable checks if the user enter number of penguins in placement phase + Game.check_for_id = 0; // This variable checks if we got instruction to check id from the command line + Game.our_player_number = 0; // This variable will be used to determine our player number based on his ID + + for(int i = 0; i < argc - 1; i++) + { + if(checkParameterType(&Game, i) == 0) + { + printf("One of the parameters you entered is wrong!\n"); + return 2; + } // Inside this loop we run through each command and check what it does and assign proper values from those commands + } + + if(Game.check_for_id == 0) // Checks if the commands asked for ID + { + if(Game.file_type == 2) // Checks if we got boards files from the commands + { + if(!handleInputFile(&Game)) // handleInputFile returns 1 if something went wrong, this function handles input file + { + printf("Total number of players is %d\n", Game.total_players); + if(checkForStrayPenguins(Game) == 1) // Checks if there are any penguins that don't belong to any player + { + printf("There is a stray penguin!\n"); + return 2; + } + //printBoard(Game); + if(Game.phase == 1) + { + if(Game.penguins_from_command == 0) // Checks if we got any penguins from command + { + printf("We didn't get any penguins from command!\n"); + return 2; + } + if(Game.penguins == ourPenguins(Game)) // Checks if we put all our penguins + { + printf("All penguins have been placed"); + return 1; + }else + { + if(automaticPlacePenguin(&Game) == 0) // Tries to put penguin somewhere and if unable returns 0 + { + printf("You can't put penguin anywhere!\n "); + handleOutPutFile(Game); + return 1; + }else + { + printf("Program placed a penguin!"); + handleOutPutFile(Game); + return 0; + } + } + }else if(Game.phase == 2 && Game.penguins_from_command == 0) // Checks if it is a movement phase + { + if(checkMove(Game) == 0) // This function checks if there are any moves left for any player + { + printf("All penguins are blocked! "); + handleOutPutFile(Game); + return 1; + }else + { + automaticMovePenguin(&Game); // Moves our penguin automatically + handleOutPutFile(Game); + return 0; + } + }else + { + printf("You entered number of penguins even though it is a movement phase!\n"); + return 2; + } + }else + { + printf("There is a mistake in board file! "); // type of mistake is printed out in handleInputFile function + fclose(Game.input_board); + return 2; + } + }else if(Game.file_type == 1) + { + printf("You entered input board file name but did not enter output board file name!"); + return 2; + }else if(Game.phase != 0) + { + printf("You did not enter input board file and output board file"); + return 2; + } + + printf("Our player number is: %d\n", Game.our_player_number); + printf("Everything went great"); + + }else + { + printf("marcel"); + return 0; + } + }else + { + printf("You entered too few arguments! "); + return 2; + } + return 2137; + #endif + #ifdef INTERACTIVE + welcome(); // User interaction - showing communicate at the start of the program + interactive_mode(Game); + return 0; + #endif // INTERACTIVE +} diff --git a/EPFU/src/movement.c b/EPFU/src/movement.c new file mode 100644 index 00000000..9d1450ca --- /dev/null +++ b/EPFU/src/movement.c @@ -0,0 +1,119 @@ +#include "game_struct.h" +#include "checkmove.c" + +// Phase == 2 : Movement +/* In this phase we give each player a chance to move his penguin + 1. Inside of it we will have a do while loop that will execute until there would be no more moves + 2. First player chooses the penguin (For user interaction we print out his name and tell him to choose a penguin) + printf("%s choose a penguin", player_name[player_number][]; + scanf("%d %d", &coordinate_1, &coordinate_2) coordinate_1 - coordinate one, coordinate_2 - coordinate two + Now we check if the penguin he has chosen has a valid move + 3. Placing the penguin (For user interaction we print out his name and tell him to place a penguin) + printf("%s choose a penguin", player_name[player_number][]; + scanf("%d %d", &coordinate_3, &coordinate_4) coordinate_3 - coordinate three, coordinate_4 - coordinate four + Now we check if the move is valid + The move is made + We update the scoreboard */ + +int automaticMovePenguin(GameState *Game) +{ + Movement_coordinates Coordinates; + for(int i = 0; i < Game -> board_height; i++) + for(int j = 0; j < Game -> board_width; j++) + { + if(Game -> board[i][j].player_no == Game -> our_player_number) + { + Coordinates.coordinate_1 = i; + Coordinates.coordinate_2 = j; + if(anyMovePossible(Coordinates, *Game) == 1) + { + Game -> board[i][j].player_no = 0; + Game -> board[i][j].fish_no = 0; + Game -> player_data[Game -> our_player_number].score += Game -> board[i][j - 1].fish_no; + Game -> board[i][j - 1].player_no = Game -> our_player_number; + Game -> board[i][j - 1].fish_no = 0; + return 1; + }else if(anyMovePossible(Coordinates, *Game) == 2) + { + Game -> board[i][j].player_no = 0; + Game -> board[i][j].fish_no = 0; + Game -> player_data[Game -> our_player_number].score += Game -> board[i - 1][j].fish_no; + Game -> board[i - 1][j].player_no = Game -> our_player_number; + Game -> board[i - 1][j].fish_no = 0; + return 1; + }else if(anyMovePossible(Coordinates, *Game) == 3) + { + Game -> board[i][j].player_no = 0; + Game -> board[i][j].fish_no = 0; + Game -> player_data[Game -> our_player_number].score += Game -> board[i + 1][j].fish_no; + Game -> board[i + 1][j].player_no = Game -> our_player_number; + Game -> board[i + 1][j].fish_no = 0; + return 1; + }else if(anyMovePossible(Coordinates, *Game) == 4) + { + Game -> board[i][j].player_no = 0; + Game -> board[i][j].fish_no = 0; + Game -> player_data[Game -> our_player_number].score += Game -> board[i][j].fish_no; + Game -> board[i][j + 1].player_no = Game -> our_player_number; + Game -> board[i][j + 1].fish_no = 0; + return 1; + } + } + } + printf("We didn't manage to put penguin anywhere! \n"); + return 0; +} + +int movePenguin(GameState *Game) // Early version of moving the Penguin for interaction Phase +{ + int check = checkPlayer(*Game); + if(!check) + { + printf("You don't have any more moves left!\n"); + Game -> flag += 1; + return 0; + } + Movement_coordinates c; + printf("Player %d choose the coordinates of the penguin you want to move:\n", Game -> current_player); + scanf("%d", &c.coordinate_1); + scanf("%d", &c.coordinate_2); + if(Game -> board[c.coordinate_1][c.coordinate_2].player_no != Game -> current_player) + { + printf("This is not your penguin!\n"); + return 0; + } + + if(anyMovePossible(c, *Game) == 0) + { + printf("This penguin can't move anywhere!\n"); + Game -> board[c.coordinate_1][c.coordinate_2].fish_no = 0; + Game -> board[c.coordinate_1][c.coordinate_2].player_no = 0; + return 0; + } + + printf("Player %d, choose the coordinates of the place you want to put your penguin:\n", Game -> current_player); + scanf("%d", &c.coordinate_3); + scanf("%d", &c.coordinate_4); + if(c.coordinate_1 == c.coordinate_3 && c.coordinate_2 == c.coordinate_4) + { + printf("Coordinates of the move is the same as the coordinates of the penguin!\n"); + return 0; + } + if(checkMovement(c, *Game) == 0) + { + printf("You can't move this penguin there!\n"); + return 0; + } + Game -> board[c.coordinate_1][c.coordinate_2].fish_no = 0; + Game -> board[c.coordinate_1][c.coordinate_2].player_no = 0; + + Game -> board[c.coordinate_3][c.coordinate_4].player_no = Game -> current_player; + Game -> player_data[Game -> current_player - 1].score += Game -> board[c.coordinate_3][c.coordinate_4].fish_no; + Game -> board[c.coordinate_3][c.coordinate_4].fish_no = 0; + if(Game -> current_player == 2) + { + Game -> current_player = 1; + }else Game -> current_player = 2; + return 1; +} + diff --git a/EPFU/src/new/.gitkeep b/EPFU/src/new/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/EPFU/src/new/printing.c b/EPFU/src/new/printing.c new file mode 100644 index 00000000..f21b94b1 --- /dev/null +++ b/EPFU/src/new/printing.c @@ -0,0 +1,32 @@ +#include "game_struct.h" + +void print_board(GameState Game) +{ + printf(" "); + + for (int i=0;i board_height; i++) + { + for(j = 0; j < Game -> board_width; j++) + { + if(Game -> board[i][j].fish_no == 1 && Game -> board[i][j].player_no == 0) + { + Game -> board[i][j].fish_no = 0; + Game -> board[i][j].player_no = Game -> our_player_number; + Game -> player_data[Game -> our_player_number].score += 1; + printf("We put a penguin at coordinates: [%d][%d]\n", i, j); + return 1; + } + } + } + printf("You can't put any penguin!\n"); + return 0; +} + +int placePenguin(GameState *Game) // Early version for placing the penguin in placement phase for interactive mode +{ + int c1, c2; + printf("Enter where you want to put your penguin: \n"); + scanf("%d", &c1); + scanf("%d", &c2); + if(c1 < 0 || c1 > Game -> board_height || c2 < 0 || c2 > Game -> board_width) + { + printf("One of the coordinates is out of the board!\n"); + return 0; + } + if(Game -> board[c1][c2].fish_no != 1) + { + printf("Number of fishes on this field is not 1!\n"); + return 0; + } + Game -> board[c1][c2].player_no = Game -> current_player; + Game -> board[c1][c2].fish_no = 0; + Game -> player_data[Game -> current_player - 1].score += 1; + return 1; +} + +/* How the placement phase function is gonna work +void placePenguin ( Penguin *penguin , Field *field ) { + penguin -> field = field; + + for ( int i = 0; i < player_count; ++i) { + for ( int j = 0; j < players[i] -> penguinsCount; ++j ) { + if ( players[i] -> penguins[j] == penguin ){ + players[i] -> score += penguin-> field -> fish_count; + penguin -> field -> fish_count = 0; + break; + } + } + } +} */ diff --git a/EPFU/src/printing.c b/EPFU/src/printing.c new file mode 100644 index 00000000..3e588445 --- /dev/null +++ b/EPFU/src/printing.c @@ -0,0 +1,51 @@ +#include "game_struct.h" +#include + +int printBoard(GameState Game) +{ + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_SCREEN_BUFFER_INFO consoleInfo; + WORD saved_attributes; + + /* Save current attributes */ + GetConsoleScreenBufferInfo(hConsole, &consoleInfo); + saved_attributes = consoleInfo.wAttributes; + system("clear || cls"); + printf("\nCurrent board status:\n"); + printf(" "); + + for (int i = 0; i < Game.board_width; i++) + { + printf("%d ", i); // Here we print the number of columns + } + printf("\n"); + for(int i = 0; i < Game.board_height; i++) + { + printf("%d ", i); // Here we print the number of rows + for(int j = 0; j < Game.board_width; j++) + { + if ((Game.board[i][j].fish_no == 0) && (Game.board[i][j].player_no == 0)) + { + SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE); + printf("~ "); // If there are no penguins and fish on the field we print the 'wave' + SetConsoleTextAttribute(hConsole, saved_attributes); + } + else if(Game.board[i][j].fish_no == 0) + { + SetConsoleTextAttribute(hConsole, FOREGROUND_RED); + printf("P%d ",Game.board[i][j].player_no); + SetConsoleTextAttribute(hConsole, saved_attributes); + } + else + { + SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN); + printf("F%d ",Game.board[i][j].fish_no); + SetConsoleTextAttribute(hConsole, saved_attributes); + } + } + printf("\n"); + } + printf("\n"); + ScoreBoard(Game); + return 0; +} diff --git a/EPFU/src/test_board.c b/EPFU/src/test_board.c new file mode 100644 index 00000000..27c69e63 --- /dev/null +++ b/EPFU/src/test_board.c @@ -0,0 +1,25 @@ +#include "printing.c" +#include "generate.c" +#include "game_struct.h" + +#include +#include +#include +#include +#include +#include + +int main() +{ + GameState Game; + Game.board_height = 10; + Game.board_width = 10; + + Game.board = allocateMemory(Game); + + Game.board = randomizeFields(Game); + Game.board[5][5].player_no = 0; + Game.board[5][5].fish_no = 0; + printBoard(Game); + return 0; +} diff --git a/EPFU/src/userinteraction.c b/EPFU/src/userinteraction.c new file mode 100644 index 00000000..fc1553c3 --- /dev/null +++ b/EPFU/src/userinteraction.c @@ -0,0 +1,37 @@ +#include "game_struct.h" +#include "windows.h" +void welcome() +{ + printf("Hey there! Lets play ''HEY THAT'S MY FISH'' \n"); +} + +/* When the movement phase will be finished (There no more moves)) +Check who won and who lost +Print out the score and winner +printf("Game over! %s you won, %s you suck", winner, loser) +END */ +// Each round the function will store the the starting score of the player +// The score will be the difference between the initial amount of fish before the start of the round and the current number after it ended +// So at the end of the round the current number of fish becomes the initial number +// At the same time the starting score becomes the sum of previous starting score and the score of the round + + +int ScoreBoard(GameState Game) +{ + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_SCREEN_BUFFER_INFO consoleInfo; + WORD saved_attributes; + + /* Save current attributes */ + GetConsoleScreenBufferInfo(hConsole, &consoleInfo); + saved_attributes = consoleInfo.wAttributes; + SetConsoleTextAttribute(hConsole, 5); + printf("Player number 1 score: %d\n", Game.player_data[0].score); + + SetConsoleTextAttribute(hConsole, saved_attributes); + SetConsoleTextAttribute(hConsole, 6); + printf("Player number 2 score: %d\n\n", Game.player_data[1].score); + SetConsoleTextAttribute(hConsole, saved_attributes); + + return 1; +}