Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // compile with "g++ -std=c++11 game.cpp"
- #include <utility>
- #include <vector>
- #include <string>
- #include <random>
- #include <map>
- #include <iostream>
- #include <functional>
- #include <cstdlib>
- class Player
- {
- public:
- Player(const std::string &name) : name(name), location(0), encounteredChutes(0), encounteredLadders(0) {}
- std::string getName() const
- {
- return name;
- }
- int getLocation() const
- {
- return location;
- }
- void setLocation(int newLocation)
- {
- location = newLocation;
- }
- int getEncounteredLadders() const
- {
- return encounteredLadders;
- }
- void incEncounteredLadders()
- {
- encounteredLadders++;
- }
- int getEncounteredChutes() const
- {
- return encounteredChutes;
- }
- void incEncounteredChutes()
- {
- encounteredChutes++;
- }
- private:
- std::string name;
- int location;
- int encounteredLadders;
- int encounteredChutes;
- };
- class Game
- {
- public:
- Game()
- {
- // 不知道要不要随机设置ladders 和 chutes?
- ladders = std::vector<std::pair<int, int>>{
- {1, 3},
- {8, 11},
- {20, 33},
- {33, 44},
- {55, 88},
- {70, 90},
- };
- chutes = std::vector<std::pair<int, int>>{
- {10, 4},
- {30, 21},
- {66, 53},
- {80, 71},
- {89, 67},
- };
- }
- // 原文是“requires a player name and location”, 但是我感觉得接收一个player才行。。不然没法记录ladder/chutes的历史(把历史信息包成tuple作为返回值返回的话有点坑)
- void turn(Player &player)
- {
- std::string playerName = player.getName();
- int curLocation = player.getLocation();
- int spinNum = spin();
- int newLocation = curLocation + spinNum;
- // If a player is on square 95 or higher, then a spin which takes them past 100 must be ignored
- // 直接按原文翻译是这样, 不知道要不要考虑95以上有chute的情况。。。
- if (newLocation > 100)
- {
- std::cout << playerName << " remains at " << curLocation << " after spin as " << spinNum << "\n";
- return;
- }
- std::cout << playerName << " moved:" << curLocation << "==>" << newLocation << " after spin as " << spinNum << "\n";
- newLocation = handleLadders(player, newLocation);
- newLocation = handleChutes(player, newLocation);
- player.setLocation(newLocation);
- }
- bool winner(Player &player)
- {
- int curLocation = player.getLocation();
- if (curLocation < 100)
- {
- return false;
- }
- std::cout << player.getName()
- << " is the winner! He/She encountered "
- << player.getEncounteredLadders()
- << " ladders and "
- << player.getEncounteredChutes()
- << " chutes \n";
- return true;
- }
- private:
- int spin()
- {
- auto gen = std::mt19937{std::random_device{}()};
- std::uniform_int_distribution<std::mt19937::result_type> dist{1, 6};
- return dist(gen);
- }
- int handleLadders(Player &player, int curLocation)
- {
- for (auto &ladder : ladders)
- {
- if (ladder.first == curLocation)
- {
- player.incEncounteredLadders();
- std::cout << player.getName() << " climbed up an ladder:" << ladder.first << "==>" << ladder.second << "\n";
- return handleLadders(player, ladder.second);
- }
- }
- return curLocation;
- }
- int handleChutes(Player &player, int curLocation)
- {
- for (auto &chute : chutes)
- {
- if (chute.first == curLocation)
- {
- player.incEncounteredChutes();
- std::cout << player.getName() << " fall through an chute:" << chute.first << "==>" << chute.second << "\n";
- return handleChutes(player, chute.second);
- }
- }
- return curLocation;
- }
- private:
- std::vector<std::pair<int, int>> ladders;
- std::vector<std::pair<int, int>> chutes;
- };
- int main(int argc, char **argv)
- {
- std::cout << "Please enter number of players [2-6]:";
- int numberOfPlayers;
- std::cin >> numberOfPlayers;
- if (numberOfPlayers < 2 || numberOfPlayers > 6)
- {
- std::cerr << "number of players must be within [2-6]! You entered:" << numberOfPlayers << "\n";
- return 1;
- }
- std::vector<Player> players;
- for (int i = 1; i != numberOfPlayers + 1; ++i)
- {
- std::cout << "Please enter name of player " << i << ":";
- std::string playerName;
- std::cin >> playerName;
- players.emplace_back(playerName);
- }
- auto gen = std::mt19937{std::random_device{}()};
- std::uniform_int_distribution<std::mt19937::result_type> dist{0, std::mt19937::result_type(players.size()) - 1};
- int nextPlayerIndex = dist(gen);
- Game game = Game{};
- bool gameEnded = false;
- while (!gameEnded)
- {
- auto &player = players[nextPlayerIndex];
- nextPlayerIndex = (nextPlayerIndex + 1) % players.size();
- game.turn(player);
- if ((gameEnded = game.winner(player)))
- {
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement