This commit is contained in:
lcw 2024-02-10 23:05:07 +08:00
parent 5ab2725947
commit e7f74d98a1

View File

@ -1,9 +1,8 @@
#include "GameBoard.h" #include "GameBoard.h"
#include <algorithm> #include <algorithm>
GameBoard::GameBoard(pos_t w, pos_t h, const InitInfo &info) GameBoard::GameBoard(pos_t w, pos_t h, const InitInfo& info)
: n(info.n), t(0), players(info.n + 1), w(w), h(h), board(w * h) : n(info.n), t(0), players(info.n + 1), w(w), h(h), board(w * h) {
{
for (std::uint8_t i = 1; i <= n; ++i) { for (std::uint8_t i = 1; i <= n; ++i) {
players[i].id = i; players[i].id = i;
players[i].team = info.player_team[i]; players[i].team = info.player_team[i];
@ -19,12 +18,12 @@ const Tile& GameBoard::at(pos_t x, pos_t y) const {
return board[x * w + y]; return board[x * w + y];
} }
bool GameBoard::attack(const PlayerMove &o) { bool GameBoard::attack(const PlayerMove& o) {
if (!o.isValid(w, h)) if (!o.isValid(w, h))
return false; return false;
auto &sc_tile = at(o.x, o.y); auto& sc_tile = at(o.x, o.y);
auto &tt_tile = at(o.tx(), o.ty()); auto& tt_tile = at(o.tx(), o.ty());
if (sc_tile.owner != o.player) if (sc_tile.owner != o.player)
return false; return false;
@ -34,7 +33,8 @@ bool GameBoard::attack(const PlayerMove &o) {
auto moving_unit = o.half ? sc_tile.unit : sc_tile.unit / 2; auto moving_unit = o.half ? sc_tile.unit : sc_tile.unit / 2;
sc_tile.unit -= moving_unit; sc_tile.unit -= moving_unit;
auto isfriend = isTeammate(o.player, tt_tile.owner); auto isfriend = isTeammate(o.player, tt_tile.owner);
auto delta = isfriend ? moving_unit + tt_tile.unit : moving_unit - tt_tile.unit; auto delta
= isfriend ? moving_unit + tt_tile.unit : moving_unit - tt_tile.unit;
updatedPosition(o.x, o.y); updatedPosition(o.x, o.y);
updatedPosition(o.ty(), o.ty()); updatedPosition(o.ty(), o.ty());
@ -51,7 +51,7 @@ bool GameBoard::attack(const PlayerMove &o) {
void GameBoard::capitalCaptured(Player tt, Player sc) { void GameBoard::capitalCaptured(Player tt, Player sc) {
for (pos_t x = 0; x < h; ++x) { for (pos_t x = 0; x < h; ++x) {
for (pos_t y = 0; y < w; ++y) { for (pos_t y = 0; y < w; ++y) {
auto &tile = at(x, y); auto& tile = at(x, y);
if (tile.owner != tt || tile.type == TileType::Capital) if (tile.owner != tt || tile.type == TileType::Capital)
continue; continue;
@ -69,11 +69,12 @@ bool GameBoard::isTeammate(Player x, Player y) const {
void GameBoard::turnUpdate() { void GameBoard::turnUpdate() {
for (pos_t x = 0; x < h; ++x) { for (pos_t x = 0; x < h; ++x) {
for (pos_t y = 0; y < w; ++y) { for (pos_t y = 0; y < w; ++y) {
auto &tile = at(x, y); auto& tile = at(x, y);
if (tile.owner == neutral_player) if (tile.owner == neutral_player)
continue; continue;
if (tile.type == TileType::Stronghold || tile.type == TileType::Capital) if (tile.type == TileType::Stronghold
|| tile.type == TileType::Capital)
tile.unit += 1; tile.unit += 1;
if (tile.type == TileType::Swamp) { if (tile.type == TileType::Swamp) {
@ -88,7 +89,7 @@ void GameBoard::turnUpdate() {
void GameBoard::roundUpdate() { void GameBoard::roundUpdate() {
for (pos_t x = 0; x < h; ++x) { for (pos_t x = 0; x < h; ++x) {
for (pos_t y = 0; y < w; ++y) { for (pos_t y = 0; y < w; ++y) {
auto &tile = at(x, y); auto& tile = at(x, y);
if (tile.owner == neutral_player) if (tile.owner == neutral_player)
continue; continue;
tile.unit += 1; tile.unit += 1;
@ -115,8 +116,11 @@ std::uint8_t GameBoard::numTeams() const {
std::vector<TeamRanking> GameBoard::leaderboard() const { std::vector<TeamRanking> GameBoard::leaderboard() const {
std::vector<TeamRanking> res(t); std::vector<TeamRanking> res(t);
std::vector<PlayerRanking> prank(n); std::vector<PlayerRanking> prank(n);
for (Player i = 1; i <= n; ++i) for (Player i = 1; i <= n; ++i)
prank[i - 1].player = i; prank[i - 1].player = i,
prank[i - 1].is_defeated
= defeated_players.find(i) != defeated_players.end();
for (Team i = 1; i <= t; ++i) for (Team i = 1; i <= t; ++i)
res[i - 1].team = i; res[i - 1].team = i;
@ -131,12 +135,28 @@ std::vector<TeamRanking> GameBoard::leaderboard() const {
} }
} }
// for (auto &p : prank) { for (const auto& p : prank)
// res[teamOf(p.player) - 1] res[teamOf(p.player) - 1].players.push_back(p);
// TODO for (auto& team : res) {
for (const auto& p : team.players) {
team.land += p.land;
team.unit += p.unit;
}
sort(team.players.begin(),
team.players.end(),
[](const PlayerRanking& a, const PlayerRanking& b) {
return a.unit != b.unit ? a.unit > b.unit
: a.player < b.player;
});
}
sort(res.begin(),
res.end(),
[](const TeamRanking& a, const TeamRanking& b) {
return a.unit != b.unit ? a.unit > b.unit : a.team < b.team;
});
return res;
} }
PlayerState::PlayerState() { PlayerState::PlayerState() {
is_defeated = false; is_defeated = false;
} }