Guest User

GameDev

a guest
May 7th, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.42 KB | None | 0 0
  1.  
  2. FSM     GameState;
  3.  
  4. int LARGE_INT = 1000;
  5. int LARGE_NEGATIVE_INT = -1000;
  6.  
  7. DecisionTree::DecisionTree(){
  8.  
  9.  
  10. }
  11.  
  12. //Check for win/loss/draw
  13. int DecisionTree::EvaluateBoard(Grid GridTemp){
  14.  
  15.     //Vertical Win State
  16.     if(GridTemp.Locations[0][0].state == GridTemp.Locations[0][1].state &&  GridTemp.Locations[0][1].state == GridTemp.Locations[0][2].state && GridTemp.Locations[0][2].state != 0 ){
  17.         if(GridTemp.Locations[0][0].state == 2){
  18.             return LARGE_INT;
  19.         }
  20.         else if (GridTemp.Locations[0][0].state == 1){
  21.             return LARGE_NEGATIVE_INT;
  22.         }  
  23.     }
  24.    
  25.     if(GridTemp.Locations[1][0].state == GridTemp.Locations[1][1].state &&  GridTemp.Locations[1][1].state ==  GridTemp.Locations[1][2].state && GridTemp.Locations[1][2].state != 0){
  26.         if(GridTemp.Locations[1][0].state == 2){
  27.             return LARGE_INT;
  28.         }
  29.         else if (GridTemp.Locations[1][0].state == 1){
  30.             return LARGE_NEGATIVE_INT;
  31.         }
  32.     }
  33.  
  34.     if(GridTemp.Locations[2][0].state == GridTemp.Locations[2][1].state &&  GridTemp.Locations[2][1].state ==  GridTemp.Locations[2][2].state && GridTemp.Locations[2][2].state != 0){
  35.         if(GridTemp.Locations[2][0].state == 2){
  36.             return LARGE_INT;
  37.         }
  38.         else if (GridTemp.Locations[2][0].state == 1){
  39.             return LARGE_NEGATIVE_INT;
  40.         }
  41.     }
  42.  
  43.     //Vertical Win State REVERSE
  44.     if(GridTemp.Locations[0][2].state == GridTemp.Locations[0][1].state &&  GridTemp.Locations[0][1].state == GridTemp.Locations[0][0].state && GridTemp.Locations[0][1].state != 0 ){
  45.         if(GridTemp.Locations[0][2].state == 2){
  46.             return LARGE_INT;
  47.         }
  48.         else if (GridTemp.Locations[0][2].state == 1){
  49.             return LARGE_NEGATIVE_INT;
  50.         }
  51.     }
  52.    
  53.     if(GridTemp.Locations[1][2].state == GridTemp.Locations[1][1].state &&  GridTemp.Locations[1][1].state ==  GridTemp.Locations[1][0].state && GridTemp.Locations[1][0].state != 0){
  54.         if(GridTemp.Locations[1][2].state == 2){
  55.             return LARGE_INT;
  56.         }
  57.         else if (GridTemp.Locations[1][2].state == 1){
  58.             return LARGE_NEGATIVE_INT;
  59.         }
  60.     }
  61.  
  62.     if(GridTemp.Locations[2][2].state == GridTemp.Locations[2][1].state &&  GridTemp.Locations[2][1].state ==  GridTemp.Locations[2][0].state && GridTemp.Locations[2][0].state != 0){
  63.         if(GridTemp.Locations[2][2].state == 2){
  64.             return LARGE_INT;
  65.         }
  66.         else if (GridTemp.Locations[2][2].state == 1){
  67.             return LARGE_NEGATIVE_INT;
  68.         }
  69.     }
  70.  
  71.     //Horizontal Win State
  72.     if(GridTemp.Locations[0][0].state == GridTemp.Locations[1][0].state &&  GridTemp.Locations[1][0].state ==  GridTemp.Locations[2][0].state && GridTemp.Locations[2][0].state != 0){
  73.         if(GridTemp.Locations[0][0].state == 2){
  74.             return LARGE_INT;
  75.         }
  76.         else if (GridTemp.Locations[0][0].state == 1){
  77.             return LARGE_NEGATIVE_INT;
  78.         }
  79.     }
  80.    
  81.     if(GridTemp.Locations[0][1].state == GridTemp.Locations[1][1].state &&  GridTemp.Locations[1][1].state ==  GridTemp.Locations[2][1].state && GridTemp.Locations[2][1].state != 0){
  82.         if(GridTemp.Locations[0][1].state == 2){
  83.             return LARGE_INT;
  84.         }
  85.         else if (GridTemp.Locations[0][1].state == 1){
  86.             return LARGE_NEGATIVE_INT;
  87.         }
  88.     }
  89.    
  90.     if(GridTemp.Locations[0][2].state == GridTemp.Locations[1][2].state &&  GridTemp.Locations[1][2].state ==  GridTemp.Locations[2][2].state && GridTemp.Locations[2][2].state != 0){
  91.         if(GridTemp.Locations[0][2].state == 2){
  92.             return LARGE_INT;
  93.         }
  94.         else if (GridTemp.Locations[0][2].state == 1){
  95.             return LARGE_NEGATIVE_INT;
  96.         }
  97.     }
  98.  
  99.     //Horizontal Win State
  100.     if(GridTemp.Locations[2][0].state == GridTemp.Locations[1][0].state &&  GridTemp.Locations[1][0].state ==  GridTemp.Locations[0][0].state && GridTemp.Locations[0][0].state != 0){
  101.         if(GridTemp.Locations[2][0].state == 2){
  102.             return LARGE_INT;
  103.         }
  104.         else if (GridTemp.Locations[2][0].state == 1){
  105.             return LARGE_NEGATIVE_INT;
  106.         }
  107.     }
  108.    
  109.     if(GridTemp.Locations[2][1].state == GridTemp.Locations[1][1].state &&  GridTemp.Locations[1][1].state ==  GridTemp.Locations[0][1].state && GridTemp.Locations[0][1].state != 0){
  110.         if(GridTemp.Locations[2][1].state == 2){
  111.             return LARGE_INT;
  112.         }
  113.         else if (GridTemp.Locations[2][1].state == 1){
  114.             return LARGE_NEGATIVE_INT;
  115.         }
  116.     }
  117.    
  118.     if(GridTemp.Locations[2][2].state == GridTemp.Locations[1][2].state &&  GridTemp.Locations[1][2].state ==  GridTemp.Locations[0][2].state && GridTemp.Locations[0][2].state != 0){
  119.         if(GridTemp.Locations[2][2].state == 2){
  120.             return LARGE_INT;
  121.         }
  122.         else if (GridTemp.Locations[2][2].state == 1){
  123.             return LARGE_NEGATIVE_INT;
  124.         }
  125.     }
  126.  
  127.  
  128.     //Diagonal Win State
  129.     if(GridTemp.Locations[0][0].state == GridTemp.Locations[1][1].state &&  GridTemp.Locations[1][1].state ==  GridTemp.Locations[2][2].state && GridTemp.Locations[2][2].state != 0){
  130.         if(GridTemp.Locations[0][0].state == 2){
  131.             return LARGE_INT;
  132.         }
  133.         else if (GridTemp.Locations[0][0].state == 1){
  134.             return LARGE_NEGATIVE_INT;
  135.         }
  136.     }
  137.    
  138.     //Diagonal Win State reverse
  139.     if(GridTemp.Locations[0][2].state == GridTemp.Locations[1][1].state &&  GridTemp.Locations[2][0].state ==  GridTemp.Locations[1][1].state && GridTemp.Locations[0][2].state != 0){
  140.         if(GridTemp.Locations[0][2].state == 2){
  141.             return LARGE_INT;
  142.         }
  143.         else if (GridTemp.Locations[0][2].state == 1){
  144.             return LARGE_NEGATIVE_INT;
  145.         }
  146.     }
  147.  
  148.     if(GridTemp.Locations [0][0].state != 0 &&
  149.         GridTemp.Locations[0][1].state != 0 &&
  150.         GridTemp.Locations[0][2].state != 0 &&
  151.         GridTemp.Locations[1][0].state != 0 &&
  152.         GridTemp.Locations[2][0].state != 0 &&
  153.         GridTemp.Locations[1][1].state != 0 &&
  154.         GridTemp.Locations[2][2].state != 0 &&
  155.         GridTemp.Locations[1][2].state != 0 &&
  156.         GridTemp.Locations[2][1].state != 0 ){
  157.             return 0;
  158.     }
  159.  
  160.     return -1;
  161.  
  162. }
  163.  
  164. //Returns container of all valid moves
  165. vector<int> DecisionTree::GetValidMoves(Grid Temp){
  166.  
  167.     //Container of valid move
  168.     vector<int> ValidMoves;
  169.  
  170.     //Check for Empty Locations
  171.     for(int i = 0; i != 3; i++){
  172.         for(int j = 0; j != 3; j++){
  173.             if(Temp.Locations[i][j].state == 0){
  174.                 ValidMoves.push_back(i);                //x Position
  175.                 ValidMoves.push_back(j);                //Y Position
  176.             }
  177.         }}
  178.  
  179.     return ValidMoves;
  180. }
  181.  
  182. //Calls Min/max Functions
  183. vector<int> DecisionTree::MiniMax(Grid Temp){
  184.    
  185.     int nBestScore = LARGE_NEGATIVE_INT;
  186.     vector<int> nBestMoves;
  187.  
  188.     //--Get Valid Moves--//
  189.     vector<int> nValidMoves;
  190.     nValidMoves = GetValidMoves(Temp);
  191.  
  192.     //While ValidMoves list is not empty
  193.     while(nValidMoves.size() > 0){
  194.  
  195.         //Set First valid move to Computers
  196.         Temp.Locations[nValidMoves[0]][nValidMoves[1]].state = 2;
  197.    
  198.         //Call min (Which calls max) - Recursive calls
  199.         int nScore = Min(Temp);
  200.        
  201.         //Add Best Moves
  202.         if(nScore > nBestScore){
  203.             nBestScore = nScore;
  204.             nBestMoves.clear();
  205.             nBestMoves.push_back(nValidMoves[0]);           //x Position
  206.             nBestMoves.push_back(nValidMoves[1]);           //Y Position
  207.         }
  208.  
  209.         //Add Best Moves
  210.         else if( nScore == nBestScore){
  211.             nBestMoves.push_back(nValidMoves[0]);
  212.             nBestMoves.push_back(nValidMoves[1]);
  213.         }
  214.  
  215.         //Reset Grid to previous state
  216.         Temp.Locations[nValidMoves[0]][nValidMoves[1]].state = 0;
  217.         nValidMoves.erase(nValidMoves.begin());
  218.         nValidMoves.erase(nValidMoves.begin());
  219.        
  220.     }
  221.  
  222.     //return random move from BestMoves Container
  223.     return nBestMoves;
  224. }
  225.  
  226. //Checks Player One moves
  227. int DecisionTree::Min(Grid Temp){
  228.  
  229.     //Check for win/loss/draw/continue
  230.     int nMoveScore = EvaluateBoard(Temp);
  231.     if(nMoveScore !=-1) {return nMoveScore;}        //Game is over
  232.  
  233.     //Winning Score
  234.     int nBestScore = LARGE_INT;
  235.    
  236.     //Create list of valid moves
  237.     vector<int> nValidMoves;
  238.     nValidMoves = GetValidMoves(Temp);
  239.  
  240.     //While there are valid moves:
  241.     while(nValidMoves.size() > 0){
  242.        
  243.         //Set First Valid move to Player, Call 'MAX'
  244.         Temp.Locations[nValidMoves[0]][nValidMoves[1]].state = 1;
  245.         int nScore = Max(Temp);
  246.  
  247.         if(nScore < nBestScore){
  248.             nBestScore = nScore;
  249.         }
  250.  
  251.         Temp.Locations[nValidMoves[0]][nValidMoves[1]].state = 0;
  252.         nValidMoves.erase(nValidMoves.begin() + 0);
  253.         nValidMoves.erase(nValidMoves.begin() + 0);
  254.        
  255.     }
  256.  
  257.     //return best result
  258.     return nBestScore;
  259. }
  260.  
  261. //Checks Player Two Moves
  262. int DecisionTree::Max(Grid Temp){
  263.  
  264.     int nMoveScore = EvaluateBoard(Temp);
  265.     if(nMoveScore !=-1) {return nMoveScore;}        //Game is over
  266.  
  267.     int nBestScore = LARGE_NEGATIVE_INT;
  268.    
  269.     vector<int> nValidMoves;
  270.     nValidMoves = GetValidMoves(Temp);
  271.  
  272.     while(nValidMoves.size() > 0){
  273.         Temp.Locations[nValidMoves[0]][nValidMoves[1]].state = 2;
  274.         int nScore = Min(Temp);
  275.  
  276.         if(nScore > nBestScore){
  277.             nBestScore = nScore;
  278.         }
  279.        
  280.         Temp.Locations[nValidMoves[0]][nValidMoves[1]].state = 0;
  281.         nValidMoves.erase(nValidMoves.begin() + 0);
  282.         nValidMoves.erase(nValidMoves.begin() + 0);
  283.        
  284.     }
  285.  
  286.     return nBestScore;
  287. }
Advertisement
Add Comment
Please, Sign In to add comment