Skip to content

Commit

Permalink
add Tool: controlled dice
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonly committed Apr 29, 2016
1 parent ab23455 commit b68e625
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
59 changes: 50 additions & 9 deletions Monopoly/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,13 @@ namespace monopoly {
case GS::normal:
{
if (strcmp(cmd, "t") == 0) {
gs.message += "请选择工具编号:";
gs.state = GS::tool;
}
else if (strcmp(cmd, "s") == 0) {
gs.message += "请输入步数(-9 ~ 9), 负号表示反向于前进方向: (输入 " + RED + "x" + NC + " 返回)";
gs.state = GS::step;
}
// else if (strcmp(cmd, "i") == 0) {
// Player &player = gs.currentPlayer();
// gs.message += "玩家 " + gc.posSymbolMap[player.name] + "信息:";
// gs.message += "\n现金: ¥" + to_string(player.cash);
// gs.message += "\n存款: ¥" + to_string(player.deposit);
// }
else if (strcmp(cmd, "r") == 0) {
gs.lastRoll = static_cast<int>(rand() % 6) + 1;
// gs.lastRoll = 2; // TODO: remove
Expand All @@ -62,11 +57,32 @@ namespace monopoly {
{
Player& player = gs.currentPlayer();
size_t count = player.tools.size();
if (strlen(cmd) == 1 && cmd[0] >= 48/*0*/ && cmd[0] < 48 + count) {
gs.message = string("你选择了道具: ") + cmd + player.tools[atoi(cmd)].name;
gs.state = GS::normal;
if (player.usingMagicDice) {
int num = atoi(cmd);
if (num > 0 && num < 7) {
gs.message += "前进 " + to_string(num) + "";
gs.state = GS::normal;
player.usingMagicDice = false;
movePlayerWithAnimation(num);
handleEvents();
return;
}
else {
gs.errMsg = RED + "无效的步数" + NC;
gs.message += "请输入前进步数 (1~6)";
break;
}
}
else if (strlen(cmd) == 1 && cmd[0] >= 48/*0*/ && cmd[0] < 48 + count) {
const Tool &tool = player.tools[atoi(cmd)];
gs.message = string("你选择了道具: ") + cmd + tool.name;
useTool(atoi(cmd));
return;
}
else if (strcmp(cmd, "x") == 0) {
gs.message += "取消操作";
gs.state = GS::normal;
}
else {
gs.errMsg = RED + "没有这个工具 :(" + NC;
break; // error
Expand Down Expand Up @@ -367,4 +383,29 @@ namespace monopoly {
gs.today.nextDay();
}
}
// toolMap[ToolType::MAGIC_DICE] = "遥控骰子";
// toolMap[ToolType::ROADBLOCK] = "路障";
// toolMap[ToolType::TURNING_CARD] = "转向卡";
// toolMap[ToolType::AVERAGE_CARD] = "均富卡";
// toolMap[ToolType::BUY_CARD] = "购地卡";
// toolMap[ToolType::REMOVE_CARD] = "拆迁卡";
// toolMap[ToolType::MONSTER_CARD] = "怪兽卡";

void Controller::useTool(int i) {
Player &player = gs.currentPlayer();
const Tool &tool = player.tools[i];
switch(tool.type) {
case ToolType::MAGIC_DICE:
{
vector<Tool>::iterator it = player.tools.begin();
for (int j = 0; j < i; j++, it++) ;
player.tools.erase(it);
gs.message += "\n请输入前进步数 (1~6)";
gs.currentPlayer().usingMagicDice = true;
}
break;
default:
break;
}
}
}
2 changes: 2 additions & 0 deletions Monopoly/Controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <iostream>
#include <unistd.h>
#include "Tool.hpp"
#include "GameContext.hpp"
#include "GameState.hpp"

Expand All @@ -35,6 +36,7 @@ namespace monopoly {
void displayPlayers();
void handleEvents();
void nextDay();
void useTool(int);
};
}

Expand Down
7 changes: 2 additions & 5 deletions Monopoly/GameContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace monopoly {
posSymbolMap["player2Land4"] = LBLUE+string("П4 ")+NC;
posSymbolMap["player2Land5"] = LBLUE+string("П5 ")+NC;
posSymbolMap["player2Land6"] = LBLUE+string("П6 ")+NC;
posSymbolMap["roadBlock"] = RED+string("")+NC;
posSymbolMap["void"] = " ";

toolMap[ToolType::MAGIC_DICE] = "遥控骰子";
Expand Down Expand Up @@ -87,13 +88,9 @@ namespace monopoly {
gs.players.push_back(Player("player1", toolMap));
gs.players.push_back(Player("player2", toolMap));

// gs.players[0].tools.push_back(Tool(ToolType::ROADBLOCK));
// gs.players[0].tools.push_back(Tool(ToolType::MAGIC_DICE));
gs.players[0].addTool(ToolType::ROADBLOCK);
gs.players[0].addTool(ToolType::MAGIC_DICE);

// gs.board[0][0].name = gs.currentPlayer().name;

init();
}

Expand Down Expand Up @@ -181,7 +178,7 @@ namespace monopoly {
}

cout << NC << "你可以做:" << endl
// << "t(tool) - 道具列表" << endl
<< "t(tool) - 道具列表" << endl
<< "s(step) - 查看前后若干步的具体信息" << endl
// << "i(info) - 查看玩家资产信息" << endl
<< "r(roll) - 掷骰子" << endl
Expand Down
3 changes: 2 additions & 1 deletion Monopoly/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "Player.hpp"

namespace monopoly {
Player::Player(string name, map<ToolType, string> &m): name(name), toolMap(m) {
Player::Player(string name, map<ToolType, string> m): name(name), toolMap(m) {
direction = true;
curPos = 0;
prePos = 0;
Expand All @@ -18,6 +18,7 @@ namespace monopoly {
cash = 1000;
deposit = 0;
done = false;
usingMagicDice = false;
}

void Player::addTool(ToolType type) {
Expand Down
3 changes: 2 additions & 1 deletion Monopoly/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ namespace monopoly {
int cash;
int deposit;
bool done; // 回合行动结束标志
bool usingMagicDice; // 正在使用遥控骰子的标识
map<ToolType, string> toolMap;

Player(string, map<ToolType, string> &);
Player(string, map<ToolType, string>);
void addTool(ToolType);
};
ostream& operator <<(ostream&, Player&);
Expand Down

0 comments on commit b68e625

Please sign in to comment.