![]() |
Project_2020
1.0
project for IUT student (2020--2021)
|
Gyges game engine functions. More...
#include <stdbool.h>
Go to the source code of this file.
Macros | |
#define | DIMENSION 6 |
Game dimension. More... | |
#define | NB_SIZE 3 |
number of sizes for robustness if the enumeration is modified. | |
#define | NB_PLAYERS 2 |
number of players in the game. | |
#define | NB_INITIAL_PIECES 2 |
number of pieces of each size on each player's line at the beginning. Usually, this value is 2. | |
Typedefs | |
typedef struct board_s * | board |
Pointer to the structure that holds the game. More... | |
typedef enum sizes_e | size |
typedef enum players_e | player |
typedef enum direction_e | direction |
typedef enum return_code_e | return_code |
Enumerations | |
enum | sizes_e { NONE, ONE, TWO, THREE } |
the different sizes of pieces. More... | |
enum | players_e { NO_PLAYER, SOUTH_P, NORTH_P } |
the different players for further reference. NO_PLAYER is used when informing that a square is empty. SOUTH starts on the side of the board with line number 0, NORTH on the side with line number DIMENSION + 1 | |
enum | direction_e { GOAL, SOUTH, NORTH, EAST, WEST } |
the different directions in the game. GOAL is legal only on the northmost line for south player, southmost line for north player. other directions are natural. | |
enum | return_code_e { OK, EMPTY, FORBIDDEN, PARAM } |
error codes returned by the function More... | |
Functions | |
player | next_player (player current_player) |
return the next player More... | |
Creation/deletion functionalities. | |
board | new_game () |
Defines a new empty board for starting a game of gobblet-gobblers. | |
board | copy_game (board original_game) |
Makes a deep copy of the game. More... | |
void | destroy_game (board game) |
Delete the game and frees all required memory. More... | |
Accessing game data functionalities. | |
size | get_piece_size (board game, int line, int column) |
returns the size of the piece at the precised position on the board, if any. More... | |
player | get_winner (board game) |
Tells if the game has a winner. More... | |
int | southmost_occupied_line (board game) |
Returns the most southern line number which contains a piece. More... | |
int | northmost_occupied_line (board game) |
Returns the northmost line number which contains a piece. More... | |
player | picked_piece_owner (board game) |
returns the player whose piece is currently moving More... | |
size | picked_piece_size (board game) |
returns the size of the piece currently moving (if any) More... | |
int | picked_piece_line (board game) |
returns the line number of the piece currently moving (if any). More... | |
int | picked_piece_column (board game) |
returns the column number of the piece currently moving (if any) More... | |
int | movement_left (board game) |
returns the number of movement units left to the current moving piece More... | |
Game setting up | |
Functionalities for the initial phase, initial placing of the pieces. | |
int | nb_pieces_available (board game, size piece, player player) |
Indicates whether the size of piece is still to be placed by the suggested player. More... | |
return_code | place_piece (board game, size piece, player player, int column) |
places a piece on the board, during the initial setting up of the game. More... | |
Playing functionalities | |
return_code | pick_piece (board game, player current_player, int line, int column) |
Select a piece to start with. More... | |
bool | is_move_possible (board game, direction direction) |
Indicates whether a move is possible. More... | |
return_code | move_piece (board game, direction direction) |
Moves the current picked piece, if possible. More... | |
return_code | swap_piece (board game, int target_line, int target_column) |
swap the current piece with the piece below it, and place the piece below at the given position. More... | |
return_code | cancel_movement (board game) |
cancels the current movement of the player, putting the piece back where it started. More... | |
return_code | cancel_step (board game) |
cancels the last step of the current move. More... | |
Gyges game engine functions.
In this file, all the functions for having a game of gyges run are defined.
#define DIMENSION 6 |
Game dimension.
For being general in terms of dimensions, they are defined in a general parameter, though the parameter is not supposed to change. In the following, all indices are given from 0 to DIMENSION - 1. Small line numbers correspond to south.
Pointer to the structure that holds the game.
Details of the content are not necessary for its use, so the structure is not included here.
enum return_code_e |
enum sizes_e |
return_code cancel_movement | ( | board | game | ) |
cancels the current movement of the player, putting the piece back where it started.
This is necessary since a player could start a movement with some piece which have no possible issue. Using this function, the moving piece is placed back where it started, and no piece is consiedered picked anymore. The function returns OK when the operation succeeds, otherwise a return_code corresponding the the problem met:
game | the game where to cancel the movement. |
return_code cancel_step | ( | board | game | ) |
cancels the last step of the current move.
This function cancels only the last step of the current move. If the last step was just picking the piece, this is equivalent to cancelling the movement.
The function returns OK when the operation succeeds, otherwise a return_code corresponding the the problem met:
game | the game where to cancel the movement. |
Makes a deep copy of the game.
original_game | the game to copy. |
void destroy_game | ( | board | game | ) |
Delete the game and frees all required memory.
game | the game to destroy. |
size get_piece_size | ( | board | game, |
int | line, | ||
int | column | ||
) |
returns the size of the piece at the precised position on the board, if any.
Return the size of a piece on the board, NONE if there is no piece. If there is a piece being moved on that place, it is not taken into account by this function. If the coordinates do not correspond to a valid place on the board, returns NONE.
game | the game from which to collect information. |
line | the line number, South has small values. |
column | the column number. |
player get_winner | ( | board | game | ) |
Tells if the game has a winner.
Recall that winning condition is that one player reached the goal, placing one piece there.
game | the game to test. |
bool is_move_possible | ( | board | game, |
direction | direction | ||
) |
Indicates whether a move is possible.
Returns true if the move is possible for the current piece. The direction GOAL is possible if the piece is on the northmost line and south is playing, or reversely. Other moves are possible if the place indicated by the direction, or if it contains a piece and the piece has only one movement unit left. Additionnally, if the piece has already made a move along this line, the movement should no longer be possible.
game | the game where to move a piece. |
direction | the direction where the movement is aimed |
return_code move_piece | ( | board | game, |
direction | direction | ||
) |
Moves the current picked piece, if possible.
Moves the piece according to the given direction. If the piece arrived over another piece at the end of its previous movement, calling this function set that the piece bounced and continues its movement. The direction may be GOAL if the piece is on the closest line to the goal. If moving the piece is not possible, returns a return_code encoding the identified problem. EMPTY: there is no piece currently in the players hand. PARAM: the target position is off the board FORBIDDEN: moving toward that position is not allowed (e.g. occupied while the piece movement is not finished or target is GOAL but not reachable) Otherwise, returns OK for success.
game | the game where to move a piece. |
direction | the direction where the movement is aimed. |
int movement_left | ( | board | game | ) |
returns the number of movement units left to the current moving piece
If a piece is currently being moved, this function returns the number of movement unit left to that piece (ignoring future bouncing) This function returns 0 if and only if the piece just finished its movement over another piece (i.e. the player must choose whether to bounce or to substitute the piece with the one under.) If there is no piece under movement, this function returns -1.
game | the game to consider. |
int nb_pieces_available | ( | board | game, |
size | piece, | ||
player | player | ||
) |
Indicates whether the size of piece is still to be placed by the suggested player.
returns the number of pieces of the given size the players still need to place, -1 if the parameters are invalid. The behaviour of this function simply assumes the game is in initial phase, no verification is made.
game | the game to be considered |
piece | the piece size requested |
player | the player whose piece is to check |
player next_player | ( | player | current_player | ) |
return the next player
This function simply return the player following current_player in the game turn. It does not use any information from the board game it is merely a turn among the players. It also serve as an example for the unit tests.
player | the player to change |
int northmost_occupied_line | ( | board | game | ) |
Returns the northmost line number which contains a piece.
This can be used for deciding whether a north player move is legal. This is the largest line number to carry a piece that can move for player North. If there is no piece that can move on any line, return -1 (the board is empty)
game | the game to consider. |
return_code pick_piece | ( | board | game, |
player | current_player, | ||
int | line, | ||
int | column | ||
) |
Select a piece to start with.
The piece must be taken from the player's line. Whether the player is allowed to play the piece is tested. The piece is then ready to be moved with the other functions,
If picking the piece is not possible, the return_code indicates why, in this order:
game | the game where to pick a piece. |
current_player | the player who is supposed to be playing. |
line | the line number of where to pick the piece (from 0 to DIMENSION - 1). |
column | the column number of where to pick the piece (from 0 to DIMENSION - 1). |
int picked_piece_column | ( | board | game | ) |
returns the column number of the piece currently moving (if any)
If a player is currently moving, return the column number of the moving piece. Returns -1 if no piece is being moved.
game | the game to consider. |
int picked_piece_line | ( | board | game | ) |
returns the line number of the piece currently moving (if any).
If a player is currently moving, return the line number of the moving piece. returns -1 if no piece is being moved.
game | the game to consider. |
player picked_piece_owner | ( | board | game | ) |
returns the player whose piece is currently moving
If a player is currently in a move, returns that player's side. Otherwise, returns NO_PLAYER.
game | the game to consider. |
size picked_piece_size | ( | board | game | ) |
returns the size of the piece currently moving (if any)
If a player is currently in a move, returns the piece size, returns NONE if there is no piece currently in hand.
game | the game to consider. |
return_code place_piece | ( | board | game, |
size | piece, | ||
player | player, | ||
int | column | ||
) |
places a piece on the board, during the initial setting up of the game.
The piece is placed on the given player's side, which determines the line number. If placing the piece is not possible, the function returns in this order:
game | the game to consider |
piece | the piece size requested |
player | the player whose piece to place |
column | the column where to place the piece (from 0 to DIMENSION - 1) |
int southmost_occupied_line | ( | board | game | ) |
Returns the most southern line number which contains a piece.
This can be used for deciding whether a south player move is legal This is the smallest line number to carry a piece that can move for player South. If there is no piece that can move on any line, return -1 (the board is empty).
game | the game to consider. |
return_code swap_piece | ( | board | game, |
int | target_line, | ||
int | target_column | ||
) |
swap the current piece with the piece below it, and place the piece below at the given position.
If the piece moving just finished its movement over another piece, this function allows the player to swap the two pieces, and place the piece below anywhere. A successful swap finishes the movement of the player (no picked piece, no moves left, etc.) The function returns OK when the operation succeeds, otherwise a return_code corresponding the the problem met:
game | the game where to move a piece. |
target_line | the line number of where to move the swapped piece. |
target_column | the column number of where to move the swapped piece. |