Advertisement
Guest User

Chutes & Ladders

a guest
Jan 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.39 KB | None | 0 0
  1. #include <utility>
  2. #include <vector>
  3. #include <string>
  4. #include <random>
  5. #include <map>
  6. #include <iostream>
  7. #include <functional>
  8. #include <cstdlib>
  9.  
  10. class Player
  11. {
  12.   public:
  13.     Player(const std::string &name) : name(name), location(0), encounteredChutes(0), encounteredLadders(0) {}
  14.  
  15.     std::string getName() const
  16.     {
  17.         return name;
  18.     }
  19.  
  20.     int getLocation() const
  21.     {
  22.         return location;
  23.     }
  24.  
  25.     void setLocation(int newLocation)
  26.     {
  27.         location = newLocation;
  28.     }
  29.  
  30.     int getEncounteredLadders() const
  31.     {
  32.         return encounteredLadders;
  33.     }
  34.  
  35.     void incEncounteredLadders()
  36.     {
  37.         encounteredLadders++;
  38.     }
  39.  
  40.     int getEncounteredChutes() const
  41.     {
  42.         return encounteredChutes;
  43.     }
  44.  
  45.     void incEncounteredChutes()
  46.     {
  47.         encounteredChutes++;
  48.     }
  49.  
  50.   private:
  51.     std::string name;
  52.     int location;
  53.     int encounteredLadders;
  54.     int encounteredChutes;
  55. };
  56.  
  57. class Game
  58. {
  59.   public:
  60.     Game()
  61.     {
  62.         // 不知道要不要随机设置ladders 和 chutes?
  63.         ladders = std::vector<std::pair<int, int>>{
  64.             {1, 3},
  65.             {8, 11},
  66.             {20, 33},
  67.             {33, 44},
  68.             {55, 88},
  69.             {70, 90},
  70.         };
  71.         chutes = std::vector<std::pair<int, int>>{
  72.             {10, 4},
  73.             {30, 21},
  74.             {66, 53},
  75.             {80, 71},
  76.             {89, 67},
  77.         };
  78.     }
  79.  
  80.     // 原文是“requires a player name and location”, 但是我感觉得接收一个player才行。。不然没法记录ladder/chutes的历史(把历史信息包成tuple作为返回值返回的话有点坑)
  81.     void turn(Player &player)
  82.     {
  83.         std::string playerName = player.getName();
  84.         int curLocation = player.getLocation();
  85.  
  86.         int spinNum = spin();
  87.         int newLocation = curLocation + spinNum;
  88.  
  89.         // If a player is on square 95 or higher, then a spin which takes them past 100 must be ignored
  90.         // 直接按原文翻译是这样, 不知道要不要考虑95以上有chute的情况。。。
  91.         if (newLocation > 100)
  92.         {
  93.             std::cout << playerName << " remains at " << curLocation << " after spin as " << spinNum << "\n";
  94.             return;
  95.         }
  96.  
  97.         std::cout << playerName << " moved:" << curLocation << "==>" << newLocation << " after spin as " << spinNum << "\n";
  98.         newLocation = handleLadders(player, newLocation);
  99.         newLocation = handleChutes(player, newLocation);
  100.         player.setLocation(newLocation);
  101.     }
  102.  
  103.     bool winner(Player &player)
  104.     {
  105.         int curLocation = player.getLocation();
  106.         if (curLocation < 100)
  107.         {
  108.             return false;
  109.         }
  110.  
  111.         std::cout << player.getName()
  112.                   << " is the winner! He/She encountered "
  113.                   << player.getEncounteredLadders()
  114.                   << " ladders and "
  115.                   << player.getEncounteredChutes()
  116.                   << " chutes \n";
  117.         return true;
  118.     }
  119.  
  120.   private:
  121.     int spin()
  122.     {
  123.         auto gen = std::mt19937{std::random_device{}()};
  124.         std::uniform_int_distribution<std::mt19937::result_type> dist{1, 6};
  125.         return dist(gen);
  126.     }
  127.  
  128.     int handleLadders(Player &player, int curLocation)
  129.     {
  130.         for (auto &ladder : ladders)
  131.         {
  132.             if (ladder.first == curLocation)
  133.             {
  134.                 player.incEncounteredLadders();
  135.                 std::cout << player.getName() << " climbed up an ladder:" << ladder.first << "==>" << ladder.second << "\n";
  136.                 return handleLadders(player, ladder.second);
  137.             }
  138.         }
  139.         return curLocation;
  140.     }
  141.  
  142.     int handleChutes(Player &player, int curLocation)
  143.     {
  144.         for (auto &chute : chutes)
  145.         {
  146.             if (chute.first == curLocation)
  147.             {
  148.                 player.incEncounteredChutes();
  149.                 std::cout << player.getName() << " fall through an chute:" << chute.first << "==>" << chute.second << "\n";
  150.                 return handleChutes(player, chute.second);
  151.             }
  152.         }
  153.         return curLocation;
  154.     }
  155.  
  156.   private:
  157.     std::vector<std::pair<int, int>> ladders;
  158.     std::vector<std::pair<int, int>> chutes;
  159. };
  160.  
  161. int main(int argc, char **argv)
  162. {
  163.     std::cout << "Please enter number of players [2-6]:";
  164.     int numberOfPlayers;
  165.     std::cin >> numberOfPlayers;
  166.     if (numberOfPlayers < 2 || numberOfPlayers > 6)
  167.     {
  168.         std::cerr << "number of players must be within [2-6]! You entered:" << numberOfPlayers << "\n";
  169.         return 1;
  170.     }
  171.  
  172.     std::vector<Player> players;
  173.     for (int i = 1; i != numberOfPlayers + 1; ++i)
  174.     {
  175.         std::cout << "Please enter name of player " << i << ":";
  176.         std::string playerName;
  177.         std::cin >> playerName;
  178.         players.emplace_back(playerName);
  179.     }
  180.  
  181.     auto gen = std::mt19937{std::random_device{}()};
  182.     std::uniform_int_distribution<std::mt19937::result_type> dist{0, std::mt19937::result_type(players.size()) - 1};
  183.     int nextPlayerIndex = dist(gen);
  184.  
  185.     Game game = Game{};
  186.     bool gameEnded = false;
  187.  
  188.     while (!gameEnded)
  189.     {
  190.         auto &player = players[nextPlayerIndex];
  191.         nextPlayerIndex = (nextPlayerIndex + 1) % players.size();
  192.  
  193.         game.turn(player);
  194.         if ((gameEnded = game.winner(player)))
  195.         {
  196.             break;
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement