From 471dab296b2cb726e4140d5a136f8c2de35d9aa3 Mon Sep 17 00:00:00 2001 From: thmaillarb Date: Mon, 29 Nov 2021 11:20:34 +0100 Subject: [PATCH] Added struct Board and function newPiece() --- Pontu/include/model/Board.h | 32 ++++++++++++++++++++++++++++++++ Pontu/include/model/Piece.h | 8 +++++++- Pontu/src/model/Board.c | 26 ++++++++++++++++++++++++++ Pontu/src/model/Piece.c | 9 +++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 Pontu/include/model/Board.h create mode 100644 Pontu/src/model/Board.c diff --git a/Pontu/include/model/Board.h b/Pontu/include/model/Board.h new file mode 100644 index 0000000..c1af9fe --- /dev/null +++ b/Pontu/include/model/Board.h @@ -0,0 +1,32 @@ +/** + * \file Board.h + * \brief Board management + * \author Théotime Maillarbaux + * \date 29/11/221 + */ + +#ifndef BOARD_H +#define BOARD_H + +#include +#include "model/Piece.h" +#include // SDL_Color + +/** + * \struct Board + * \brief Represents a board for a Game. + * + * The bridges are represented by 2D-arrays of SDL_Bools. + * For a given set of coordinates, the value is TRUE if the bridge is there, + * else false. + */ +typedef struct { + // y x + 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. +} Board; + +Board newBoard(int nbPlayers); + +#endif // BOARD_H diff --git a/Pontu/include/model/Piece.h b/Pontu/include/model/Piece.h index aca450d..6ceb54a 100644 --- a/Pontu/include/model/Piece.h +++ b/Pontu/include/model/Piece.h @@ -9,6 +9,7 @@ #define PIECE_H #include "Island.h" //#include "Player.h" //If we use a pointer for the player instead of an int +#include /** *\Struct Piece @@ -20,7 +21,12 @@ typedef struct int idJ; ///< Player's id (owner of the piece) bool stuck; ///< Evaluate to true if the piece is isolated Island island; ///< Piece's localisation on the game board - }Piece; +/** + * \brief Creates a new Piece + * \param[in] idJ The ID (index in array) of the Player owning the Piece + */ +Piece newPiece(int idJ); + #endif // PIECE_H diff --git a/Pontu/src/model/Board.c b/Pontu/src/model/Board.c new file mode 100644 index 0000000..4b753f9 --- /dev/null +++ b/Pontu/src/model/Board.c @@ -0,0 +1,26 @@ +#include "model/Board.h" + +Board newBoard(int nbPlayers) { + Board board; + int nbPiecesPerPlayer = (nbPlayer == 4) ? 2 : 3; + int pieceIndex = 0; + + // Init pieces + for (int player_i = 0; i < nbPlayers; player_i++) { + for (int piece_i = 0; i < nbPiecesPerPlayer; piece_i++) { + board.arrPieces[pieceIndex] = newPiece(player_i); + pieceIndex++; + } + } + + // Init bridges + // max of vy (vertical y) is equal to max of hx (horizontal x), + // so vy will be used as hx as well. + for (int vy = 0; vy < 4; vy++) { + for (int vx = 0; vx < 5; vx++) { + board.vBridges[vy][vx] = true; + board.hBridges[vx][vy] = false; + } + } + return board; +} diff --git a/Pontu/src/model/Piece.c b/Pontu/src/model/Piece.c index e69de29..9a8657e 100644 --- a/Pontu/src/model/Piece.c +++ b/Pontu/src/model/Piece.c @@ -0,0 +1,9 @@ +#include "model/Piece.h" + +Piece newPiece(int idJ) { + Piece piece; + piece.idJ = idJ; + piece.stuck = false; + return piece; +} +