Advertisement
Guest User

testmain

a guest
May 7th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.02 KB | None | 0 0
  1. testmain.cc************************************************************************************************************************
  2.  
  3. /*
  4. This is a test main for the mancala class.
  5. If you are reading this, please close this
  6. text file, as I probably am doing dumb stuff
  7. in here.
  8. */
  9.  
  10. #include <string>
  11. #include <queue>
  12. #include <iomanip>
  13. #include <iostream>
  14. #include "game.h"
  15. #include "mancala.h"
  16. using namespace main_savitch_14;
  17.  
  18. int main ()
  19. {
  20.     mancala m1;
  21.     m1.display_status();
  22.  
  23.     return 0;
  24.     std::cout<<"\n\n\n you shouldn't be reading this.";
  25. }
  26.  
  27.  
  28. mancala.cc************************************************************************************************************************
  29.  
  30. This is the mancala class container for
  31. creating the main game implementation
  32. */
  33.  
  34. #include <iostream>
  35. #include <queue>
  36. #include <string>
  37. #include <iomanip>
  38. #include <algorithm>
  39. #include "game.h"
  40. #include "colors.h"
  41. #include "mancala.h"
  42. using namespace main_savitch_14;
  43.  
  44. void mancala::display_status()const
  45. {
  46.     //Top Border
  47.     for(int j = 0; j<5; j++)
  48.     {
  49.         for(int i = 0; i<40; i++)
  50.         {
  51.             cout<<B_YELLOW<<"     ";
  52.         }
  53.         // cout<<RESET<<endl;
  54.     }
  55.     //West Border
  56.     /*for(int j = 0; j<40; j++)
  57.     {
  58.         for(int i = 0; i<5; i++)
  59.         {
  60.             if(i % 5 || 6 == 0)
  61.             {
  62.                 cout<<setw(4)<<B_BLACK<<"     ";
  63.             }
  64.             else
  65.             cout << B_YELLOW<<"     ";
  66.         //Middle Screen
  67.         }
  68.         for (int h = 0; h < 10; h++)
  69.         {
  70.             if (h % 5 || 6 == 0)
  71.             {
  72.                 cout << setw(4) << B_BLACK << "     ";
  73.             }
  74.                 cout<<setw(4)<<B_YELLOW<<"     ";
  75.         }
  76.         for(int i = 0; i<5; i++)
  77.         {
  78.             if(!(i % 10)&& i % 5 || 6 == 0)
  79.             {
  80.                 cout<<setw(4)<<B_BLACK<<"     ";
  81.             }
  82.             else
  83.             cout << B_YELLOW<<"     ";
  84.         }
  85.     }
  86.     cout<<RESET<<endl;*/
  87. }
  88.  
  89. mancala::mancala()
  90. {
  91.     holes = 14;
  92.     for(int i = 0; i < holes; i++)
  93.     {
  94.         if(i==0 || 7)
  95.         {
  96.             data [i] = 0;
  97.         }
  98.         else
  99.             data [i] = 4;
  100.     }
  101. }
  102.  
  103. void mancala::mid_screen(int m)const
  104. {
  105.     for(int i = 0; i<10; i++)
  106.     {
  107.         if(m % 10 != 0 && i % 5 || 6 == 0)
  108.         {
  109.             cout<<setw(4)<<B_BLACK<<WHITE<<"  "<<data[i]<<"   ";
  110.             cout<<setw(4)<<B_BLACK<<"     ";
  111.         }
  112.         else
  113.         cout << B_YELLOW<<"     ";
  114.         cout << B_YELLOW<<"     ";
  115.         cout << B_YELLOW<<"     ";
  116.     }
  117. }
  118.  
  119. void mancala::display_message(const std::string& message)const
  120. {
  121.  
  122. }
  123. std::string mancala::get_user_move()const
  124. {
  125.  
  126. }
  127. game::who mancala::last_mover()const
  128. {
  129.  
  130. }
  131. int mancala::moves_completed()const
  132. {
  133.  
  134. }
  135. game::who mancala::next_mover()const
  136. {
  137.  
  138. }
  139. game::who mancala::opposite(game::who player)const
  140. {
  141.  
  142. }
  143. game::who mancala::winning()const
  144. {
  145.  
  146. }
  147. void mancala::make_move(const std::string& move)
  148. {
  149.  
  150. }
  151. void mancala::restart()
  152. {
  153.  
  154. }
  155. game* mancala::clone()const
  156. {
  157.  
  158. }
  159. void mancala::compute_moves(std::queue<std::string>& moves)const
  160. {
  161.  
  162. }
  163.  
  164. int mancala::evaluate()const
  165. {
  166.  
  167. }
  168. bool mancala::is_game_over()const
  169. {
  170.  
  171. }
  172. bool mancala::is_legal(const std::string& move)const
  173. {
  174.  
  175. }
  176.  
  177. mancala.h************************************************************************************************************************
  178.  
  179. This is the header file for all of the main mancala
  180. game functions.
  181. */
  182. #include <string>
  183. #include <queue>
  184. #include <iomanip>
  185. #include "game.h"
  186. using namespace main_savitch_14;
  187.  
  188. class mancala:public game
  189. {
  190.         int holes;
  191.         int data[];
  192.  
  193.   public:
  194.     mancala();
  195.         void display_status()const;
  196.         void mid_screen(int m)const;
  197.     void display_message(const std::string& message)const;
  198.     std::string get_user_move()const;
  199.     who last_mover()const;
  200.     int moves_completed()const;
  201.     who next_mover()const;
  202.     who opposite(who player)const;
  203.     who winning()const;
  204.     void make_move(const std::string& move);
  205.     void restart();
  206.     game* clone()const;
  207.     void compute_moves(std::queue<std::string>& moves)const;
  208.     int evaluate()const;
  209.     bool is_game_over()const;
  210.     bool is_legal(const std::string& move)const;
  211.  
  212. };
  213.  
  214. game.h************************************************************************************************************************
  215.  
  216. // File: game.h (part of the namespace main_savitch_14)
  217.  
  218.  
  219. #ifndef MAIN_SAVITCH_GAME
  220. #define MAIN_SAVITCH_GAME
  221. #include <queue>   // Provides queue<string>
  222. #include <string>  // Provides string
  223.  
  224. namespace main_savitch_14
  225. {
  226.     class game
  227.     {
  228.     public:
  229.         // ENUM TYPE
  230.     enum who { HUMAN, NEUTRAL, COMPUTER }; // Possible game outcomes
  231.    
  232.     // CONSTRUCTOR and DESTRUCTOR
  233.     game( ) { move_number = 0; }
  234.     virtual ~game( ) { }
  235.    
  236.     // PUBLIC MEMBER FUNCTIONS
  237.     // The play function should not be overridden. It plays one game,
  238.     // with the human player moving first and the computer second.
  239.     // The computer uses an alpha-beta look ahead algorithm to select its
  240.     // moves. The return value is the winner of the game (or NEUTRAL for
  241.     // a tie).
  242.     who play( );
  243.  
  244.     protected:
  245.     // *******************************************************************
  246.     // OPTIONAL VIRTUAL FUNCTIONS (overriding these is optional)
  247.     // *******************************************************************
  248.     virtual void display_message(const std::string& message) const;
  249.         virtual std::string get_user_move( ) const;
  250.     virtual who last_mover( ) const
  251.         { return (move_number % 2 == 1 ? HUMAN : COMPUTER); }
  252.     virtual int moves_completed( ) const { return move_number; }
  253.     virtual who next_mover( ) const
  254.         { return (move_number % 2 == 0 ? HUMAN : COMPUTER); }
  255.     virtual who opposite(who player) const
  256.         { return (player == HUMAN) ? COMPUTER : HUMAN; }
  257.         virtual who winning( ) const;
  258.  
  259.     // *******************************************************************
  260.     // VIRTUAL FUNCTIONS THAT MUST BE OVERRIDDEND:
  261.     // The overriding function should call the original when it finishes.
  262.     // *******************************************************************
  263.     // Have the next player make a specified move:
  264.         virtual void make_move(const std::string& move) { ++move_number; }
  265.         // Restart the game from the beginning:
  266.         virtual void restart( ) { move_number = 0; }
  267.  
  268.     // *******************************************************************
  269.         // PURE VIRTUAL FUNCTIONS
  270.     // *******************************************************************
  271.     // (these must be provided for each derived class)
  272.         // Return a pointer to a copy of myself:
  273.         virtual game* clone( ) const = 0;
  274.         // Compute all the moves that the next player can make:
  275.         virtual void compute_moves(std::queue<std::string>& moves) const = 0;
  276.         // Display the status of the current game:
  277.         virtual void display_status( ) const = 0;
  278.         // Evaluate a board position:
  279.     // NOTE: positive values are good for the computer.
  280.         virtual int evaluate( ) const = 0;
  281.         // Return true if the current game is finished:
  282.         virtual bool is_game_over( ) const = 0;
  283.         // Return true if the given move is legal for the next player:
  284.         virtual bool is_legal(const std::string& move) const = 0;
  285.  
  286.     private:
  287.         // MEMBER VARIABLES
  288.     int move_number;                     // Number of moves made so far
  289.  
  290.     // STATIC MEMBER CONSTANT
  291.     static const int SEARCH_LEVELS = 4;  // Levels for look-ahead evaluation
  292.    
  293.         // PRIVATE FUNCTIONS (these are the same for every game)
  294.     int eval_with_lookahead(int look_ahead, int beat_this);
  295.     void make_computer_move( );
  296.     void make_human_move( );
  297.     };
  298. }
  299.  
  300. #endif
  301.  
  302. game.cc********************************************************************************************************************************
  303.  
  304. // File: game.cxx
  305.  
  306. #include <cassert>    // Provides assert
  307. #include <climits>    // Provides INT_MAX and INT_MIN
  308. #include <iostream>   // Provides cin, cout
  309. #include <queue>      // Provides queue<string>
  310. #include <string>     // Provides string
  311. #include "game.h"     // Provides definition of game class
  312. using namespace std;
  313.  
  314. namespace main_savitch_14
  315. {
  316.     //*************************************************************************
  317.     // STATIC MEMBER CONSTANTS
  318.     // const int game::SEARCH_LEVELS;
  319.    
  320.     //*************************************************************************
  321.     // PUBLIC MEMBER FUNCTIONS
  322.  
  323.     game::who game::play( )
  324.     // The play function should not be overridden. It plays one round of the
  325.     // game, with the human player moving first and the computer second.
  326.     // The return value is the winner of the game (or NEUTRAL for a tie).
  327.     {
  328.     restart( );
  329. // Note that as you develop the game you will be gradually un-commenting
  330. // this function.  
  331.     //while (!is_game_over( )) // un-comment this
  332.     //{
  333.         display_status( );
  334.     //    if (last_mover( ) == COMPUTER)
  335.         make_human_move( );
  336.     //    else
  337.     //  make_computer_move( );
  338.     //}
  339.     display_status( );
  340.     return HUMAN;
  341.     }
  342.  
  343.  
  344.    
  345.     //*************************************************************************
  346.     // OPTIONAL VIRTUAL FUNCTIONS (overriding these functions is optional)
  347.  
  348.     void game::display_message(const string& message) const
  349.     {
  350.             cout << message;
  351.     }
  352.  
  353.     string game::get_user_move( ) const
  354.     {
  355.             string answer;
  356.    
  357.         display_message("Your move, please: ");
  358.         getline(cin, answer);
  359.             return answer;
  360.     }
  361.  
  362.     game::who game::winning( ) const
  363.     {
  364.             int value = evaluate( ); // Evaluate based on move that was just made.
  365.  
  366.             if (value > 0)
  367.           return last_mover( );
  368.             else if (value < 0)
  369.           return next_mover( );
  370.             else
  371.           return NEUTRAL;
  372.     }
  373.  
  374.  
  375.  
  376.     //*************************************************************************
  377.     // PRIVATE FUNCTIONS (these are the same for every game)
  378.  
  379.     int game::eval_with_lookahead(int look_ahead, int beat_this)
  380.     // Evaluate a board position with lookahead.
  381.     // --int look_aheads:  How deep the lookahead should go to evaluate the move.
  382.     // --int beat_this: Value of another move that we?re considering. If the
  383.     // current board position can't beat this, then cut it short.
  384.     // The return value is large if the position is good for the player who just
  385.     // moved.
  386.     {
  387.         queue<string> moves;   // All possible opponent moves
  388.             int value;             // Value of a board position after opponent moves
  389.         int best_value;        // Evaluation of best opponent move
  390.         game* future;          // Pointer to a future version of this game
  391.    
  392.         // Base case:
  393.     if (look_ahead == 0 || is_game_over( ))
  394.     {
  395.         if (last_mover( ) == COMPUTER)
  396.                 return evaluate( );
  397.         else
  398.         return -evaluate( );
  399.     }
  400.  
  401.         // Recursive case:
  402.         // The level is above 0, so try all possible opponent moves. Keep the
  403.     // value of the best of these moves from the opponent's perspective.
  404.         compute_moves(moves);
  405.     // assert(!moves.empty( ));
  406.         best_value = INT_MIN;
  407.         while (!moves.empty( ))
  408.         {
  409.         future = clone( );
  410.         future->make_move(moves.front( ));
  411.         value = future->eval_with_lookahead(look_ahead-1, best_value);
  412.         delete future;
  413.         if (value > best_value)
  414.         {
  415.         best_value = value;
  416.         }
  417.         moves.pop( );
  418.         }
  419.  
  420.         // The value was calculated from the opponent's perspective.
  421.         // The answer we return should be from player's perspective, so multiply times -1:
  422.         return -best_value;
  423.     }
  424.  
  425.     void game::make_computer_move( )
  426.     {
  427.     queue<string> moves;
  428.     int value;
  429.     int best_value;
  430.     string best_move;
  431.     game* future;
  432.    
  433.     // Compute all legal moves that the computer could make.
  434.     compute_moves(moves);
  435.     //assert(!moves.empty( ));
  436.    
  437.     // Evaluate each possible legal move, saving the index of the best
  438.     // in best_index and saving its value in best_value.
  439.     best_value = INT_MIN;
  440.     while (!moves.empty( ))
  441.     {
  442.         future = clone( );
  443.         future->make_move(moves.front( ));
  444.         value = future->eval_with_lookahead(SEARCH_LEVELS, best_value);
  445.         delete future;
  446.         if (value >= best_value)
  447.         {
  448.         best_value = value;
  449.         best_move = moves.front( );
  450.         }
  451.         moves.pop( );
  452.     }
  453.        
  454.     // Make the best move.
  455.     make_move(best_move);
  456.     }
  457.  
  458.     void game::make_human_move( )
  459.     {
  460.         string move;
  461.  
  462.     move = get_user_move( );
  463.     while (!is_legal(move))
  464.     {
  465.         display_message("Illegal move.\n");
  466.         move = get_user_move( );
  467.         }
  468.     make_move(move);
  469.     }
  470.  
  471. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement