Merge branch of gitlab.iut-clermont.uca.fr:/maribemont/projet-tut

merge-requests/1/merge
Mathis RIBEMONT 4 years ago
commit 4dfbfb7bc4

1
.gitattributes vendored

@ -0,0 +1 @@
*.h linguist-language=C

@ -5,7 +5,9 @@ SET(PROJECT_NAME Pontu)
project(${PROJECT_NAME})
file(GLOB_RECURSE MY_SOURCES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c)
add_executable(${PROJECT_NAME} ${MY_SOURCES})
add_executable(${PROJECT_NAME} ${MY_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/entryPoints/main.c)
add_executable(Test ${MY_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/test/test.c)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
@ -17,14 +19,19 @@ find_package(SDL2 REQUIRED)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${SDL2_INCLUDE_DIR})
target_include_directories(Test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${SDL2_INCLUDE_DIR})
SET(OptionGCC -Wall -Wextra -pedantic -Wconversion -std=c17)
if (MINGW)
target_link_options(${PROJECT_NAME} PRIVATE -lmingw32 -lSDL2main -lSDL2 -mwindows)
target_compile_options(${PROJECT_NAME} PRIVATE ${OptionGCC} -Dmain=SDL_main -fdiagnostics-color=always)
target_link_options(${PROJECT_NAME} PRIVATE -lmingw32 -lSDL2main -lSDL2 -mwindows)
target_compile_options(${PROJECT_NAME} PRIVATE ${OptionGCC} -Dmain=SDL_main -fdiagnostics-color=always)
target_link_options(Test PRIVATE -lmingw32 -lSDL2main -lSDL2 -mwindows)
target_compile_options(Test PRIVATE ${OptionGCC} -Dmain=SDL_main -fdiagnostics-color=always)
elseif(UNIX)
target_compile_options(${PROJECT_NAME} PRIVATE ${OptionGCC} -D_REENTRANT)
target_compile_options(Test PRIVATE ${OptionGCC} -D_REENTRANT)
endif()
target_link_libraries(${PROJECT_NAME} SDL2::Main)
target_link_libraries(Test SDL2::Main)

@ -1,5 +1,8 @@
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
#include "engine/InputProcessor.h"
#include "engine/InputElement.h"
int main(int argc, char* argv[])
{
@ -27,12 +30,44 @@ int main(int argc, char* argv[])
goto Quit;
}
SDL_bool quit = SDL_FALSE;
InputProcessor inputProcessor = {.selectedCase = {.x=-1, .y=-1}};
SDL_Rect rectBoard = {.x=20, .y=20, .w=99, .h=99};
bool quit = false;
while(!quit)
{
// Event handling
InputElement inputElement;
while (InputType_None != (inputElement = proccessInput(&inputProcessor, &rectBoard)).type) {
switch (inputElement.type)
{
case InputType_ActivateUI:
switch (inputElement.data.uiAction)
{
case UIAction_Quit:
quit = true;
break;
case UIAction_Validate:
break;
case UIAction_Cancel:
break;
default:
break;
}
break;
case InputType_MoveGame:
break;
case InputType_ClickGame:
break;
case InputType_None:
default:
break;
}
}
// Drawing

@ -17,7 +17,7 @@
*/
typedef struct
{
ArrayButton tabButton;
//ArrayButton tabButton;
Coord selectedCase; ///< Board , (-1;-1) si inexistant
} InputProcessor;

@ -25,6 +25,7 @@ typedef struct {
bool vBridges[4][5]; ///< 2D-array of vertical bridges.
bool hBridges[5][4]; ///< 2D-array of horizontal bridges.
Piece arrPieces[9]; ///< Array of pieces on the Board.
const size_t nbPieces;
} Board;
Board newBoard(const int nbPlayers);

@ -10,6 +10,8 @@
#include "model/Player.h"
#include "model/Board.h"
#include "model/Island.h"
#include "model/Coord.h"
#include <SDL2/SDL_pixels.h>
#include <stdbool.h>
@ -42,39 +44,61 @@ typedef struct {
*/
Game newGame(const int nbPlayers, const char* pseudos[]);
/**
* \brief Place a piece into the board
*
* \param [in, out] p The piece to place
* \param [in] island The island where the piece is placed
* \param [in] b The board in which the piece is placed
* \return true If the pieces is placed
*/
bool placePiece(Piece* p, const Island island, const Board* b);
/**
* \brief Move a piece to an island
* \param[in] p the piece to move
* \param[in] i island target
* \param[in,out] p the piece to move
* \param[in] i island target
* \param[in] b The Board for this Game
* \return True if the piece can be move otherwise false
*/
bool movePiece(Piece* p, const Island i, const Board* b);
bool movePiece(Piece p, Island i);
/**
* \brief Check if an island is empty
*
* \param [in] island The island to check
* \param [in] arrPieces the array of piece from the board
* \param [in] nbPieces number of pieces
* \return true if none of the pieces is on th island, false otherwise
*/
bool isIslandEmpty(const Island island, const Piece arrPieces[], const size_t nbPieces);
/**
* \brief Check if the the island is attainable from the piece's position (no player on the island)
* \param[in] p the piece to move
* \brief Check if the the island is adjacent to the piece
* \param[in] p the piece
* \param[in] i island target
* \return True if the island is attainable (no player on the island) otherwise false
* \return True if the island is adjacent to the piece otherwise false
*/
bool checkIsland(Piece p, Island i);
bool isPieceAdjacentToIsland(const Piece p, const Island i);
/**
* \biref Check if there is a bridge at (coord->x; coord->y)
* \param[in] coords Coords to test
* \param[in] board Actual game board
* \return True if there is a bridge. Else return false.
* \bref Check if there is a bridge between two Island.
* \param[in] start On island were the bridge ends
* \param[in] target The other island were the bridge ends
* \param[in] b The Board for this Game.
* \return true if there is a bridge, false otherwise.
*/
bool checkBridge(Coord* coords, Board* board);
bool checkBridge(const Island start, const Island target, const Board* b);
/**
* \brief Remove bridge from board at (coord->x; coord->y)
* \param[in] coords Bridge's coords to remove
* \param[in] board Actual game board
* \return True on succsess. Else return false.
* \return True on success. Else return false.
*/
bool rmBridge(Coord* coords, Board* board);
bool rmBridge(Coord coords, Board* board);
#endif //PARTIE_H

@ -8,6 +8,8 @@
#ifndef ISLAND_H
#define ISLAND_H
#include <stdbool.h>
/**
*\struct Island
*\brief Island representation
@ -17,6 +19,24 @@ typedef struct
{
int x;///< Coordinate : absciss
int y;///< Coordinate : ordinate
// bool hasPiece; ///< Indicates if there is a Piece on this Island
} Island;
#endif
/**
* \brief Check if two islands are equal
*
* \param a An island
* \param b An island
* \return true if a and b are equal
*/
bool islandEqual(const Island a, const Island b);
/**
* \brief Check if an island is in board bounds
*
* \param island the island to check
* \return true is the island is in board bounds
*/
bool islandValid(const Island island);
#endif

@ -52,11 +52,11 @@ InputElement proccessInput(InputProcessor *inputProcessor, const SDL_Rect* board
}
else
{
for (size_t i = 0; i<inputProcessor->tabButton.size; ++i) {
/*for (size_t i = 0; i<inputProcessor->tabButton.size; ++i) {
if (SDL_PointInRect(&mousePoint, &inputProcessor->tabButton.buttons[i].rect)) {
// ...
}
}
}*/
return createInputElementNone();
}
break;

@ -1,8 +1,8 @@
#include "model/Board.h"
Board newBoard(const int nbPlayers) {
Board board;
int nbPiecesPerPlayer = (nbPlayers == 4) ? 2 : 3;
const int nbPiecesPerPlayer = (nbPlayers == 4) ? 2 : 3;
Board board = {.nbPieces = nbPiecesPerPlayer*nbPlayers};
int pieceIndex = 0;
// Init pieces for each player

@ -1,12 +1,15 @@
#include "model/Game.h"
#include "model/IslandOrBridge.h"
#include <assert.h>
Game newGame(const int nbPlayers, const char* pseudos[]) {
Game g;
// In Placement phase, the last player initialized is the 1st to play
g.currentPlayerID = nbPlayers - 1;
g.nb_rounds = 0;
g.phase = PLACEMENT;
g.board = newBoard(nbPlayers);
Game g = {
// In Placement phase, the last player initialized is the 1st to play
.currentPlayerID = nbPlayers - 1,
.nb_rounds = 0,
.phase = PLACEMENT,
.board = newBoard(nbPlayers)
};
// red, green, blue, yellow
// TODO meilleures couleurs (?)
@ -24,44 +27,104 @@ Game newGame(const int nbPlayers, const char* pseudos[]) {
return g;
}
bool placePiece(Piece* p, const Island island, const Board* b) {
if (isIslandEmpty(island, b->arrPieces, b->nbPieces)) {
p->island = island;
return true;
}
return false;
}
bool movePiece(Piece p, Island i)
bool movePiece(Piece* p, const Island i, const Board* b)
{
return checkIsland(p,i) && checkBridge(p,i); //Otherwise call one function before one other
if (isIslandEmpty(i, b->arrPieces, b->nbPieces)
&& isPieceAdjacentToIsland(*p, i)
&& checkBridge(p->island, i, b)) {
p->island = i;
return true;
} else return false;
}
bool isIslandEmpty(const Island island, const Piece arrPieces[], const size_t nbPieces) {
assert(islandValid(island) && "Pass invalid island to isIslandEmpty");
for (size_t i = 0; i < nbPieces; ++i)
{
if (islandEqual(island, arrPieces[i].island)) {
return false;
}
}
return true;
}
//Not yet over
bool checkIsland(Piece p, Island i)
bool isPieceAdjacentToIsland(const Piece p, const Island i)
{
bool check;
int diff;
if(p.island.x==i.x) //piece and island are on the same x axe
//Maybe turn this into an if (with message on stderr)
assert(islandValid(i) && "Send invalid island to isPieceAdjacentToIsland");
assert(islandValid(p.island) && "Send invalid piece island to isPieceAdjacentToIsland");
if(p.island.x==i.x) //piece and island are on the same x axis
{
diff=p.island.y-i.y;
return -1<diff && diff<1; //Island is atteinable if the difference is between -1 and 1 y on the y axe
const int diff=p.island.y-i.y;
return -1==diff || diff==1; //Island is adjacent if the difference is equal -1 or 1 on the y axis
}
else if (p.island.y==i.y) //piece and island are on the same y axe
{
diff=p.island.x-i.y;
return -1<diff && diff<1; //Island is atteinable if the difference is between -1 and 1 x on the x axe
const int diff=p.island.x-i.x;
return -1==diff || diff==1; //Island is adjacent if the difference is equal to -1 or 1 on the x axis
}
return false;
}
bool checkBridge(Coord* coords, Board* board)
bool checkBridge(const Island start, const Island target, const Board* board)
{
if((coords->x%2 == 1) && (coords->y%2 == 0))
{
return board->hBridge[coord->y][coord->x];
//Maybe turn this into an if (with message on stderr)
assert(islandValid(start) && islandValid(target) && "Send invalid island to checkBridge");
// Horizontal difference between start and target.
// If xdiff is negative, then target is on the left of start.
// If xdiff is positive, then target is on the right of start.
// If xdiff is 0, then target and start are aligned horizontally.
const int xdiff = target.x - start.x;
// Vertical difference between start and target.
// Works similarly to xdiff, except negative and positive values
// indicate that target is above start and bellow start respectively.
const int ydiff = target.y - start.y;
// Vertical bridge
if (xdiff == 0 && abs(ydiff) == 1) {
return board->vBridges[start.y+ydiff][start.x];
}
if((coords->x%2 == 0) && (coords->y%2 == 1))
{
return board->vBridge[coord->y][coord->x];
// Horizontal bridge
else if (abs(xdiff) == 1 && ydiff == 0) {
return board->hBridges[start.y][start.x+xdiff];
}
// Not a bridge
else {
return false;
}
return false;
}
bool rmBridge(Coord coord, Board* board) {
IslandOrBridge bridge = coordToEntity(coord);
if (bridge.type == HBRIDGE) {
if (board->hBridges[bridge.y][bridge.x]) {
board->hBridges[bridge.y][bridge.x] = false;
return true;
}
}
else if (bridge.type == VBRIDGE) {
if (board->vBridges[bridge.y][bridge.x]) {
board->vBridges[bridge.y][bridge.x] = false;
return true;
}
}
return false;
}

@ -0,0 +1,11 @@
#include "model/Island.h"
bool islandEqual(const Island a, const Island b) {
return a.x == b.x && a.y == b.y;
}
bool islandValid(const Island island) {
return island.x>=0 && island.x<=4
&& island.y>=0 && island.y<=4;
}

@ -1,6 +1,6 @@
#include "model/IslandOrBridge.h"
IslandOrBridge(Coord c) {
IslandOrBridge coordToEntity(Coord c) {
IslandOrBridge res;
if (c.x % 2 == 0) {

@ -3,8 +3,9 @@
Piece newPiece(const int idJ) {
Piece piece = {
.idJ = idJ,
.stuck = false
} ;
.stuck = false,
.island = {.x = -1, .y=-1}
};
return piece;
}

@ -0,0 +1,68 @@
#include "model/Game.h"
#include <assert.h>
void testIsIslandEmpty() {
Piece arrPieces[4] = {
newPiece(0),
newPiece(0),
newPiece(1),
newPiece(1),
};
Island i = {.x = 2, .y = 4};
assert(isIslandEmpty(i, arrPieces, 4));
arrPieces[1].island = i;
assert(!isIslandEmpty(i, arrPieces, 4));
arrPieces[1].island.x-=1;
assert(isIslandEmpty(i, arrPieces, 4));
arrPieces[3].island = i;
assert(!isIslandEmpty(i, arrPieces, 4));
}
void testIsPieceAdjacentToIsland() {
Piece piece = newPiece(0);
Island i = {.x = 2, .y = 4};
piece.island.x=0;
piece.island.y=0;
assert(!isPieceAdjacentToIsland(piece, i));
piece.island = i;
assert(!isPieceAdjacentToIsland(piece, i));
piece.island.x-=1;
assert(isPieceAdjacentToIsland(piece, i));
piece.island.y-=1;
assert(!isPieceAdjacentToIsland(piece, i));
piece.island.x+=1;
assert(isPieceAdjacentToIsland(piece, i));
}
void testPlacePiece() {
Board board = newBoard(2);
Island island = {.x = 0, .y = 0};
assert(placePiece(&board.arrPieces[0], island, &board));
assert(islandEqual(board.arrPieces[0].island, island));
assert(!placePiece(&board.arrPieces[1], island, &board));
assert(!islandEqual(board.arrPieces[1].island, island));
}
void testCheckBridge() {
Board board = newBoard(2);
}
int main(int argc, char * argv[]) {
testIsIslandEmpty();
testIsPieceAdjacentToIsland();
testPlacePiece();
testCheckBridge();
return 0;
}

@ -24,24 +24,28 @@ Nous développerons successivement les versions suivantes :
### On Linux:
* git clone [urlToThisRepo]
* cd [thisRepo]
* git submodule init
* git submodule update
* mkdir bin
* cd bin
* cmake ..
* make
* ./Pontu
```BASH
git clone [urlToThisRepo]
cd [thisRepo]
git submodule init
git submodule update
mkdir bin
cd bin
cmake ..
make
./Pontu
```
### On Windows:
* git clone [urlToThisRepo]
* cd [thisRepo]
* git submodule init
* git submodule update
* mkdir bin
* cd bin
* cmake ..
* mingw32-make
* ./Pontu
```BASH
git clone [urlToThisRepo]
cd [thisRepo]
git submodule init
git submodule update
mkdir bin
cd bin
cmake ..
mingw32-make
./Pontu
```

Loading…
Cancel
Save