Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. #include <map>
  2. #include <tuple>
  3.  
  4. /* This file specifies the (mandatory) interface. As usual, you can
  5.  * add additional classes and additional attributes and methods to
  6.  * existing classes. */
  7.  
  8. /* This class, ‹gttt› (short for generalized tic-tac-toe), provides
  9.  * the entire interface of the game. */
  10. class gttt {
  11.     int players_count;
  12.     int win_count;
  13.     std::map<std::tuple<int, int, int>, int> board;
  14.     int active_player;
  15.  
  16. public:
  17.     /* Start a new game with a given number of players. The
  18.      * ‹to_win› argument specifies how many consecutive symbols need
  19.      * to be placed by the same player in order to win the game. The
  20.      * number of players must be between 2 and 1000000 (inclusive).
  21.      * Same limits apply to the length of the winning sequence. */
  22.     gttt( int players, int to_win ) {
  23.     players_count = players;
  24.     win_count = to_win;
  25.     active_player = 1;
  26.     }
  27.  
  28.     /* Which player plays next. Players are numbered from 0 and play
  29.      * in ascending order (player 0 goes first). */
  30.     int next() const {
  31.         if(active_player == players_count) {
  32.             return 0;
  33.         }
  34.         return active_player;
  35.     }
  36.  
  37.  
  38.     void next_player() {
  39.         if (players_count >= active_player) {
  40.             active_player += 1;
  41.         }
  42.         active_player = 1;
  43.     }
  44.  
  45.     /* Play the active player's round by placing a mark on the cell
  46.      * given by (x, y, z). Returns true if the move was valid.
  47.      * Returns false if the cell was already occupied (and does not
  48.      * change the state in this case and the player's round is not
  49.      * completed).  */
  50.     bool play( int x, int y, int z ) {
  51.         if (get(x, y, z) == 0) {
  52.             board[std::tuple(x, y, z)] = active_player;
  53.             next_player();
  54.             return true;
  55.         }
  56.         return false;
  57.     }
  58.  
  59.     /* Return true if the specified cell is empty, false otherwise. */
  60.         bool empty( int x, int y, int z ) const {
  61.         return !board.count(std::tuple(x, y, z));
  62.         //return board.at(std::tuple(x, y, z)) == 0;
  63.     }
  64.  
  65.     int get(int x, int y, int z) const {
  66.         if (!empty(x, y, z)) { return 0; }
  67.         return board.at(std::tuple(x, y, z));
  68.     }
  69.     /* Return the identifier of the player who owns (placed their
  70.      * mark on) the given cell. The behaviour is unspecified if the
  71.      * cell is empty. */
  72.         int owner( int x, int y, int z ) const {
  73.         int own = board.at(std::tuple(x, y, z));
  74.         if (own != 0) {
  75.             return own;
  76.         }
  77.         return -1;
  78.         //TODO non-defiend
  79.     }
  80.  
  81.  
  82.     bool line_check(int c_x, int c_y, int c_z, int d_x, int d_y, int d_z, int player) const {
  83.         int count = 1;
  84.         bool left = true;
  85.         bool right = true;
  86.        
  87.         while (!left && !right) {
  88.             if (right) {
  89.                 if (get(c_x + d_x, c_y + d_y, c_z + d_z) == player) {
  90.                     ++count;
  91.                 } else {
  92.                     right = false;
  93.                 }
  94.             }
  95.             if (left) {
  96.                             if (get(c_x - d_x, c_y - d_y, c_z - d_z) == player) {
  97.                                     ++count;
  98.                             } else {
  99.                                     left = false;
  100.                             }
  101.                     }
  102.             if (count == win_count) {
  103.                 return true;
  104.             }
  105.         }
  106.         return false;  
  107.     };
  108.    
  109.     /* True iff there is a winning sequence on the grid. */
  110.         bool finished() const {
  111.         for (auto itr = board.begin(); itr != board.end(); ++itr) {
  112.             if (is_winnable_from(std::get<0>(itr->first), std::get<1>(itr->first),std::get<2>(itr->first))) {
  113.                 return true;
  114.             }
  115.         }
  116.         return false;
  117.     };
  118.  
  119.     /* The player who won. Undefined unless finished() is true. */
  120.         int winner() const {
  121.         if (finished() ) {
  122.             if(active_player != 1) {
  123.                 return active_player - 1;
  124.             }
  125.             return players_count;
  126.         }
  127.         return -1;
  128.         //TODO
  129.  
  130.     }
  131.  
  132.     /* Returns true if the next player can immediately win the game
  133.      * by placing their mark strategically. */
  134.         bool may_win() const {
  135.         for (auto itr = board.begin(); itr != board.end(); ++itr) {
  136.                         if (itr->second == active_player) {
  137.                                            
  138.                         }
  139.                 }
  140.                 return false;
  141.  
  142.     }
  143.  
  144.        
  145.     bool is_winnable_from(int c_x, int c_y, int c_z) const {
  146.         for(int i = -1; i <= 1; ++i) {
  147.                     for(int j = -1; j <= 1; ++j) {
  148.                             for(int k = -1; k <= 1; ++k) {
  149.                                     if (line_check(c_x, c_y, c_z, i, j, k, get(c_x, c_y, c_z))) { return true;}
  150.                             }
  151.                     }
  152.         }
  153.         return false;
  154.     }
  155.  
  156. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement