Corrected a bug with moveOnBoard, Added special rule for 2 players and a drawer for pieces (at this time, we can move player 0's pieces around)

merge-requests/1/merge
marouault 4 years ago
parent 1801098574
commit 4e5f963eeb

@ -5,10 +5,9 @@
#include "model/Game.h"
#include "engine/TextureHandler.h"
#include "view/BoardDrawer.h"
#include "engine/Button.h"
bool drawGame(SDL_Renderer* renderer, SDL_Rect* windowSize, SDL_Rect* boardRect, Game* game,TextureHandler* textureHandler);
bool drawGame(SDL_Renderer* renderer, const SDL_Rect* windowSize, const SDL_Rect* boardRect, const Game* game,TextureHandler* textureHandler);
#endif

@ -0,0 +1,10 @@
#ifndef PIECES_DRAWER_INCLUDED
#define PIECES_DRAWER_INCLUDED
#include <SDL2/SDL.h>
#include <stdbool.h>
#include "model/Piece.h"
void drawPieces(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Piece arrPieces[], const size_t nbPieces, SDL_Texture* piece);
#endif //PIECES_DRAWER_INCLUDED

@ -2,6 +2,36 @@
#include "model/IslandOrBridge.h"
#include <assert.h>
// Not defined in header to not pollute inferface
void applySpecificRulesFor2PlayersGame(Game* g) {
g->currentPlayerID = 0;
g->phase = MOVE_PIECE;
size_t iJ1 = 0;
size_t iJ2 = 0;
const Island startPosJ1[] = {
newIsland(1,0),
newIsland(2,1),
newIsland(3,0)
};
const Island startPosJ2[] = {
newIsland(1,4),
newIsland(2,3),
newIsland(3,4)
};
for (size_t i = 0; i < g->board.nbPieces; ++i) {
if (g->board.arrPieces[i].idJ==0) {
g->board.arrPieces[i].island = startPosJ1[iJ1++]; // Post increment return the value "before" incrementing
}
else {
g->board.arrPieces[i].island = startPosJ2[iJ2++];
}
}
}
Game newGame(const size_t nbPlayers, const char* pseudos[]) {
Game g = {
// In Placement phase, the last player initialized is the 1st to play
@ -10,11 +40,6 @@ Game newGame(const size_t nbPlayers, const char* pseudos[]) {
.phase = PLACEMENT,
.board = newBoard(nbPlayers)
};
if (nbPlayers == 2) {
g.currentPlayerID = 0;
g.phase = MOVE_PIECE;
}
// red, green, blue, yellow
// TODO meilleures couleurs (?)
@ -29,9 +54,14 @@ Game newGame(const size_t nbPlayers, const char* pseudos[]) {
g.arrPlayers[player_i] = newPlayer(pseudos[player_i] ,colors[player_i]);
}
if (nbPlayers == 2) {
applySpecificRulesFor2PlayersGame(&g);
}
return g;
}
bool placePiece(Piece* p, const Island island, const Board* b) {
if (isIslandEmpty(island, b->arrPieces, b->nbPieces)) {
p->island = island;
@ -43,13 +73,13 @@ bool placePiece(Piece* p, const Island island, const Board* b) {
bool movePiece(Piece* p, const Island i, const Board* b)
{
if (isIslandEmpty(i, b->arrPieces, b->nbPieces)
&& isPieceAdjacentToIsland(*p, i)
&& checkBridge(p->island, i, b)) {
if (isIslandEmpty(i, b->arrPieces, b->nbPieces) && isPieceAdjacentToIsland(*p, i) && checkBridge(p->island, i, b)) {
p->island = i;
return true;
} else return false;
}
else {
return false;
}
}
bool isIslandEmpty(const Island island, const Piece arrPieces[], const size_t nbPieces) {
@ -178,7 +208,6 @@ bool clickOnBoard(const Coord coord, Game* game) {
return true;
}
break;
default:
break;
@ -207,17 +236,19 @@ bool moveOnBoard(const Coord start, const Coord end, Game* game) {
case MOVE_PIECE:
if(islandOrBridgeStart.type==ISLAND && islandOrBridgeEnd.type==ISLAND)
{
Piece *piece;
size_t idCurrentPlayer = game->currentPlayerID;
Island islandStart = islandOrBridgeStart.data.island;
Island islandEnd = islandOrBridgeEnd.data.island;
piece=getPieceFromIsland(game->board.arrPieces, game->board.nbPieces,islandStart);
if(idCurrentPlayer==piece->idJ) //Check if the current player id is the same than the piece selected by the player
const size_t idCurrentPlayer = game->currentPlayerID;
const Island islandStart = islandOrBridgeStart.data.island;
const Island islandEnd = islandOrBridgeEnd.data.island;
Piece* piece = getPieceFromIsland(game->board.arrPieces, game->board.nbPieces, islandStart);
if(piece != NULL && idCurrentPlayer==piece->idJ) { //Check if the current player id is the same than the piece selected by the player
return movePiece(piece, islandEnd, &game->board);
return false;
}
}
break;
default:
break;
}
return false;
}

@ -1,5 +1,5 @@
#include "view/BoardDrawer.h"
#include "model/Game.h"
bool drawBoard(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Board* board, SDL_Texture* island, SDL_Texture* bridge, SDL_Texture* water)
{
@ -8,7 +8,7 @@ bool drawBoard(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Board* b
SDL_RenderCopy(renderer, water, NULL, boardRect);
//Islands
//Islands
for (int y=0; y<9; y+=2) {
for (int x=0; x<9; x+=2) {
const SDL_Rect destRect = {
@ -36,7 +36,7 @@ bool drawBoard(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Board* b
}
}
//HBridge
//VBridge
for (int y=0; y<4; ++y) {
for (int x=0; x<5; ++x) {
if (board->vBridges[y][x]) {

@ -1,6 +1,8 @@
#include "view/GameDrawer.h"
#include "view/BoardDrawer.h"
#include "view/PiecesDrawer.h"
bool drawGame(SDL_Renderer* renderer, SDL_Rect* windowSize, SDL_Rect* boardRect, Game* game, TextureHandler* textureHandler)
bool drawGame(SDL_Renderer* renderer, const SDL_Rect* windowSize, const SDL_Rect* boardRect, const Game* game,TextureHandler* textureHandler)
{
SDL_Texture* menuTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 50, 70);
@ -15,4 +17,7 @@ bool drawGame(SDL_Renderer* renderer, SDL_Rect* windowSize, SDL_Rect* boardRect,
drawBoard(renderer, boardRect, &(game->board), textureHandler->textures[TEXTURE_Island], textureHandler->textures[TEXTURE_Bridge], textureHandler->textures[TEXTURE_Water]);
drawPieces(renderer, boardRect, game->board.arrPieces, game->board.nbPieces, textureHandler->textures[TEXTURE_Piece]);
return true;
}

@ -0,0 +1,26 @@
#include "view/PiecesDrawer.h"
#include "model/Island.h"
//Don't put this in model
SDL_Rect islandToRect(const SDL_Rect* boardRect, const Island island) {
const int w = boardRect->w/9;
const int h = boardRect->h/9;
SDL_Rect r = {
.x = boardRect->x + w*(island.x*2),
.y = boardRect->y + h*(island.y*2),
.w = w,
.h = h
};
return r;
}
void drawPieces(SDL_Renderer* renderer, const SDL_Rect* boardRect, const Piece arrPieces[], const size_t nbPieces, SDL_Texture* piece) {
for (size_t i = 0; i < nbPieces; ++i)
{
const SDL_Rect rDest = islandToRect(boardRect, arrPieces[i].island);
SDL_RenderCopy(renderer, piece, NULL, &rDest);
}
}
Loading…
Cancel
Save