summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--res/ur.asepritebin1918 -> 2246 bytes
-rw-r--r--res/ur.pngbin2258 -> 2673 bytes
-rw-r--r--src/helper.cpp22
-rw-r--r--src/helper.hpp9
-rw-r--r--src/log.hpp2
-rw-r--r--src/ur.cpp124
6 files changed, 114 insertions, 43 deletions
diff --git a/res/ur.aseprite b/res/ur.aseprite
index 88d106a..8bb9910 100644
--- a/res/ur.aseprite
+++ b/res/ur.aseprite
Binary files differ
diff --git a/res/ur.png b/res/ur.png
index 066cf67..6fc3855 100644
--- a/res/ur.png
+++ b/res/ur.png
Binary files differ
diff --git a/src/helper.cpp b/src/helper.cpp
index d0a349a..3d44bc6 100644
--- a/src/helper.cpp
+++ b/src/helper.cpp
@@ -240,6 +240,22 @@ createPassSprites(std::shared_ptr<std::vector<sf::Texture>> textures)
}
std::shared_ptr<std::vector<sf::Sprite>>
+createWinSprites(int player_id,
+ std::shared_ptr<std::vector<sf::Texture>> textures)
+{
+ auto sprites = std::make_shared<std::vector<sf::Sprite>>();
+ auto tile_ids = player_id == P1_ID ? P1_WIN_TILES : P2_WIN_TILES;
+ auto x = 4;
+ auto y = 4;
+ for (int i = 0; i < 5; i++) {
+ auto s = sf::Sprite((*textures)[tile_ids[i]]);
+ s.setPosition(pos(x + i, y));
+ sprites->push_back(s);
+ }
+ return sprites;
+}
+
+std::shared_ptr<std::vector<sf::Sprite>>
createStartSprites(std::shared_ptr<std::vector<sf::Texture>> textures)
{
auto sprites = std::make_shared<std::vector<sf::Sprite>>();
@@ -397,3 +413,9 @@ clearPiece(std::shared_ptr<std::vector<struct piece_t>> pieces,
}
}
}
+
+bool
+hasWon(std::shared_ptr<struct player_t> player)
+{
+ return player->pieces->size() == 0;
+}
diff --git a/src/helper.hpp b/src/helper.hpp
index f7d6b8d..e13ee5e 100644
--- a/src/helper.hpp
+++ b/src/helper.hpp
@@ -34,6 +34,8 @@ static const int ROLL_TILES[2] = { 20, 21 };
static const int PASS_TILES[3] = { 24, 25, 26 };
static const int START_TILES[3] = { 27, 28, 29 };
static const int REROLL_POS[3] = { 3, 7, 13 };
+static const int P1_WIN_TILES[5] = { 30, 31, 32, 33, 34 };
+static const int P2_WIN_TILES[5] = { 35, 36, 37, 38, 39 };
static const char* TITLE = "Royal Game of Ur";
static const sf::Color GLOBAL_MASK(255, 0, 255, 255);
@@ -101,6 +103,10 @@ std::shared_ptr<std::vector<sf::Sprite>>
createPassSprites(std::shared_ptr<std::vector<sf::Texture>> textures);
std::shared_ptr<std::vector<sf::Sprite>>
+createWinSprites(int player_id,
+ std::shared_ptr<std::vector<sf::Texture>> textures);
+
+std::shared_ptr<std::vector<sf::Sprite>>
createStartSprites(std::shared_ptr<std::vector<sf::Texture>> textures);
void
@@ -137,4 +143,7 @@ void
clearPiece(std::shared_ptr<std::vector<struct piece_t>> pieces,
struct piece_t* piece);
+bool
+hasWon(std::shared_ptr<struct player_t> player);
+
#endif
diff --git a/src/log.hpp b/src/log.hpp
index fcc4e75..5927682 100644
--- a/src/log.hpp
+++ b/src/log.hpp
@@ -2,8 +2,8 @@
#define UR_LOG_H
#include "helper.hpp"
-#include <iostream>
#include <ctime>
+#include <iostream>
#include <string>
/**
* A very simple stdio/stderr logger
diff --git a/src/ur.cpp b/src/ur.cpp
index db9fca6..afc72d3 100644
--- a/src/ur.cpp
+++ b/src/ur.cpp
@@ -24,6 +24,13 @@ sf::Vector2f grabbed_piece_origin;
int turn_roll = 0;
bool mouse_left_locked = false;
+// tracks the turn pids
+int turn_pid = P1_ID;
+int rolling_frame = 0;
+
+// which player won P1_ID or P2_ID
+int winner = -1;
+
inline void
change_state(GameState next)
{
@@ -33,16 +40,25 @@ change_state(GameState next)
}
inline void
+set_game_over()
+{
+ Log::debug("Game over!");
+ change_state(GameState::GAME_OVER);
+}
+
+inline bool
+is_game_over()
+{
+ return state == GameState::GAME_OVER;
+}
+
+inline void
change_color(std::shared_ptr<std::vector<sf::Sprite>> sprites, sf::Color color)
{
for (auto& s : *sprites)
s.setColor(color);
}
-// tracks the turn pids
-int turn_pid = P1_ID;
-int rolling_frame = 0;
-
inline void
next(int* i, int max)
{
@@ -215,6 +231,12 @@ main()
const std::shared_ptr<std::vector<sf::Sprite>> pass_sprites =
createPassSprites(textures);
+ const std::shared_ptr<std::vector<sf::Sprite>> p1_win_sprites =
+ createWinSprites(P1_ID, textures);
+
+ const std::shared_ptr<std::vector<sf::Sprite>> p2_win_sprites =
+ createWinSprites(P2_ID, textures);
+
const std::shared_ptr<std::vector<sf::Sprite>> start_sprites =
createStartSprites(textures);
@@ -351,7 +373,8 @@ main()
if (takenPieceId >= 0) {
for (auto& ep : (*enemyPieces)) {
if (ep.id == takenPieceId) {
- Log::debug("Captured piece " + Log::str(takenPieceId) + " returning to board side");
+ Log::debug("Captured piece " + Log::str(takenPieceId) +
+ " returning to board side");
ep.sprite.setPosition(ep.origin);
ep.position = -1;
}
@@ -360,7 +383,8 @@ main()
grabbed_piece->sprite.setPosition(s.getPosition());
if (bp.position == (grabbed_piece->position + turn_roll)) {
- Log::debug("Placed piece in position " + Log::str(bp.position));
+ Log::debug("Placed piece in position " +
+ Log::str(bp.position));
grabbed_piece->position = bp.position;
in_place = true;
}
@@ -375,11 +399,13 @@ main()
if (grabbed_piece->position == EXIT_SPACE) {
if (p1_turn()) {
makeNum(&p1Score, ++p1->score, textures);
- Log::debug("P1 scored. Score " + Log::str(p1->score) + ". Clearing piece " + Log::str(grabbed_piece->id));
+ Log::debug("P1 scored. Score " + Log::str(p1->score) +
+ ". Clearing piece " + Log::str(grabbed_piece->id));
clearPiece(p1->pieces, grabbed_piece);
} else {
makeNum(&p2Score, ++p2->score, textures);
- Log::debug("P2 scored. Score " + Log::str(p2->score) + ". Clearing piece " + Log::str(grabbed_piece->id));
+ Log::debug("P2 scored. Score " + Log::str(p2->score) +
+ ". Clearing piece " + Log::str(grabbed_piece->id));
clearPiece(p2->pieces, grabbed_piece);
}
} else {
@@ -409,49 +435,63 @@ main()
window.clear(BG_COLOR);
window.setView(view);
+ if (!is_game_over()) {
+ for (auto s : *(board)) {
+ window.draw(s.sprite);
+ }
- for (auto s : *(board)) {
- window.draw(s.sprite);
- }
+ auto mPos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
+ if (grabbed_piece != nullptr) {
+ float x = mPos.x - (grabbed_piece->sprite.getGlobalBounds().width / 2);
+ float y = mPos.y - (grabbed_piece->sprite.getGlobalBounds().height / 2);
+ grabbed_piece->sprite.setPosition(x, y);
+ }
- auto mPos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
- if (grabbed_piece != nullptr) {
- float x = mPos.x - (grabbed_piece->sprite.getGlobalBounds().width / 2);
- float y = mPos.y - (grabbed_piece->sprite.getGlobalBounds().height / 2);
- grabbed_piece->sprite.setPosition(x, y);
- }
+ // draw pieces (draw own pieces last to ensure "on top")
+ if (p1_turn()) {
+ for (auto& p : *(p2->pieces)) {
+ window.draw(p.sprite);
+ }
+ for (auto& p : *(p1->pieces)) {
+ window.draw(p.sprite);
+ }
+ } else {
+ for (auto& p : *(p1->pieces)) {
+ window.draw(p.sprite);
+ }
+ for (auto& p : *(p2->pieces)) {
+ window.draw(p.sprite);
+ }
+ }
- // draw pieces (draw own pieces last to ensure "on top")
- if (p1_turn()) {
- for (auto& p : *(p2->pieces)) {
- window.draw(p.sprite);
+ render_dice(&window,
+ p1_turn() ? p1 : p2,
+ p2_turn() ? p1 : p2,
+ dice,
+ roll_sprites,
+ pass_sprites,
+ textures,
+ &roll_result,
+ &rolling_animation_timer,
+ &rolling_animation_frame_pause_timer);
+ for (auto& s : (*roll_sprites)) {
+ window.draw(s);
}
- for (auto& p : *(p1->pieces)) {
- window.draw(p.sprite);
+
+ if (!is_game_over() && hasWon(p1)) {
+ set_game_over();
+ winner = P1_ID;
+ } else if (!is_game_over() && hasWon(p2)) {
+ set_game_over();
+ winner = P2_ID;
}
} else {
- for (auto& p : *(p1->pieces)) {
- window.draw(p.sprite);
- }
- for (auto& p : *(p2->pieces)) {
- window.draw(p.sprite);
+ auto win_sprites = winner == P1_ID ? p1_win_sprites : p2_win_sprites;
+ for (auto& s : (*win_sprites)) {
+ window.draw(s);
}
}
- render_dice(&window,
- p1_turn() ? p1 : p2,
- p2_turn() ? p1 : p2,
- dice,
- roll_sprites,
- pass_sprites,
- textures,
- &roll_result,
- &rolling_animation_timer,
- &rolling_animation_frame_pause_timer);
- for (auto& s : (*roll_sprites)) {
- window.draw(s);
- }
-
window.draw(p1Score);
window.draw(p2Score);
window.setView(window.getDefaultView());