Skip to content

Commit

Permalink
add tool and player addTool method
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonly committed Apr 29, 2016
1 parent 0dd5a00 commit ab23455
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Monopoly/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ 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)].type;
gs.message = string("你选择了道具: ") + cmd + player.tools[atoi(cmd)].name;
gs.state = GS::normal;
return;
}
Expand Down Expand Up @@ -219,7 +219,7 @@ namespace monopoly {
Player& player = gs.currentPlayer();
cout << "当前拥有道具:" << endl;
for (int i = 0; i < player.tools.size(); i++) {
cout << i << ". " << player.tools[i].type << " ";
cout << i << ". " << player.tools[i].name << " ";
}
}

Expand Down
1 change: 1 addition & 0 deletions Monopoly/Controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace monopoly {

class GameContext;
class GameState;

class Controller {
private:
Expand Down
24 changes: 21 additions & 3 deletions Monopoly/GameContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ namespace monopoly {
posSymbolMap["player2Land6"] = LBLUE+string("П6 ")+NC;
posSymbolMap["void"] = " ";

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] = "怪兽卡";

int i, j, k;
for (k = 0; k < gs.road.size(); k++) {
// fix land type of road
Expand All @@ -72,10 +80,20 @@ namespace monopoly {
gs.board[i][j].landType = gs.road[k].landType;
gs.board[i][j].street = gs.road[k].street;
}
gs.board[0][0].name = gs.currentPlayer().name;

controller = new Controller(*this);

// finish GameState initialization
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 @@ -153,10 +171,10 @@ namespace monopoly {
vector<Tool>::iterator it = player.tools.begin();
int i = 0;
for (; !player.tools.empty() && it != player.tools.end()-1; it++, i++) {
cout << i << "." << it->type << ", ";
cout << i << "." << it->name << ", ";
}
if (!player.tools.empty()) {
cout << i << "." << it->type << endl;
cout << i << "." << it->name << endl;
}
else {
cout << "" << endl;
Expand Down
20 changes: 1 addition & 19 deletions Monopoly/GameContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,6 @@ using std::map;

namespace monopoly {

enum class ToolType {
TURNING_CARD, // turn around immediately
STAY_CARD, // stay for one turn, trigger event in this position
TURTLE_CARD, // make another player can only go 1 step in 3 turns
MAGIC_DICE, // make a roll number as you want
ROADBLOCK, // can be placed in 8 steps, make anyone passing by stop
BUY_CARD, // buy the other player's land in this position
TAX_CARD, // make the other player lose 30% deposit
AVERAGE_CARD, // make all players' cash averaged
LAND_CARD, // in 5 turns, make land your own in your position
REMOVE_CARD, // remove all house in this street, owners get 150% of current price
MONSTER_CARD, // make all house 1 level in this street
MONEY_CARD, // get 10,000 cash immediately, won't pay on others' land in 8 turns
FORTUNE_CARD, // get a random card immediately, won't pay on others' land in 8 turns
LOTTERY_CARD, // manipulate result of lottery this month
RED_CARD, // make stock rise 10% for 2 days
BLACK_CARD // make stock fall 10% for 2 days
};

class Controller;

class GameContext {
Expand All @@ -52,6 +33,7 @@ namespace monopoly {
map<string, LandType> landMap;
map<LandType, string> IlandMap;
map<string, string> posSymbolMap;
map<ToolType, string> toolMap;

GameContext();
~GameContext();
Expand Down
6 changes: 0 additions & 6 deletions Monopoly/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ namespace monopoly {
errMsg = "";
playerIndex = 0;
lastRoll = -1;

players.push_back(Player("player1"));
players.push_back(Player("player2"));

players[0].tools.push_back(Tool("红卡"));
players[0].tools.push_back(Tool("福神卡"));
}

Player& GameState::currentPlayer() {
Expand Down
7 changes: 5 additions & 2 deletions 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): name(name) {
Player::Player(string name, map<ToolType, string> &m): name(name), toolMap(m) {
direction = true;
curPos = 0;
prePos = 0;
Expand All @@ -20,9 +20,12 @@ namespace monopoly {
done = false;
}

void Player::addTool(ToolType type) {
tools.emplace_back(Tool(type, toolMap[type]));
}

ostream& operator <<(ostream& os, Player& player) {
os << GREEN << player.name << NC;
return os;
}

}
6 changes: 5 additions & 1 deletion Monopoly/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

#include <iostream>
#include <vector>
#include <map>
#include "Color.hpp"
#include "Tool.hpp"

using std::string;
using std::ostream;
using std::map;
using std::vector;

namespace monopoly {
Expand All @@ -31,8 +33,10 @@ namespace monopoly {
int cash;
int deposit;
bool done; // 回合行动结束标志
map<ToolType, string> toolMap;

Player(string);
Player(string, map<ToolType, string> &);
void addTool(ToolType);
};
ostream& operator <<(ostream&, Player&);
}
Expand Down
2 changes: 1 addition & 1 deletion Monopoly/Tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "Tool.hpp"

namespace monopoly {
Tool::Tool(string type): type(type) {
Tool::Tool(ToolType type, string name): type(type), name(name) {
}

ostream& operator <<(ostream& os, Tool& tool) {
Expand Down
25 changes: 23 additions & 2 deletions Monopoly/Tool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,32 @@ using std::string;
using std::ostream;

namespace monopoly {

enum class ToolType {
TURNING_CARD, // turn around immediately
STAY_CARD, // stay for one turn, trigger event in this position
TURTLE_CARD, // make another player can only go 1 step in 3 turns
MAGIC_DICE, // make a roll number as you want
ROADBLOCK, // can be placed in 8 steps, make anyone passing by stop
BUY_CARD, // buy the other player's land in this position
TAX_CARD, // make the other player lose 30% deposit
AVERAGE_CARD, // make all players' cash averaged
LAND_CARD, // in 5 turns, make land your own in your position
REMOVE_CARD, // remove all house in this street, owners get 150% of current price
MONSTER_CARD, // make all house 1 level in this street
MONEY_CARD, // get 10,000 cash immediately, won't pay on others' land in 8 turns
FORTUNE_CARD, // ge't a random card immediately, won't pay on others' land in 8 turns
LOTTERY_CARD, // manipulate result of lottery this month
RED_CARD, // make stock rise 10% for 2 days
BLACK_CARD // make stock fall 10% for 2 days
};

class Tool {
public:
string type;
ToolType type;
string name;

Tool(string);
Tool(ToolType, string);
};
ostream& operator <<(ostream&, Tool&);
}
Expand Down

0 comments on commit ab23455

Please sign in to comment.