Guest User

Untitled

a guest
Jan 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. /*
  2. *$Id: framework.h,v 1.2 2012/10/03 16:55:26 Ben Exp Ben $
  3. $Log: framework.h,v $
  4. Revision 1.2 2012/10/03 16:55:26 Ben
  5. Mostly Working, few bugs
  6.  
  7. Revision 1.1 2012/10/03 04:13:26 Ben
  8. Initial revision
  9.  
  10. */
  11.  
  12. #ifndef framework
  13. #define framework
  14. #include "player.h"
  15. #include <vector>
  16. #include <map>
  17. #include "takeawayClass.h"
  18. #include <iostream>
  19. #include "player.h"
  20. #include <map>
  21. using namespace std;
  22.  
  23. template <typename T>
  24. class Framework {
  25.  
  26. public:
  27.  
  28. Framework<T>(){};
  29.  
  30. void move(T game, player p){
  31. int move=0;
  32. game.printBoard();
  33. cout<<"Player "<<p.getPlayerNum()<<"'s turn!"<<endl;
  34. if(p.getType()=="HU"){
  35. game.move(game.getHumanMove());
  36. }//end human move
  37. else if(p.getType()=="CO"){
  38.  
  39. //move=findBestMove(tiles,*(new int(0)),*(new int(0)),1);
  40. move=findBestMove(game);
  41. cout<<"AI determined "<<move<<" was the best move"<<endl;
  42. game.move(move);
  43.  
  44. }
  45. };
  46. int findBestMove(T game){
  47. //vector<T> games(10);
  48. int max=-100;
  49. T maxGame(0);
  50. vector<int> scores(game.possibleMoves().size());
  51. for(int i=0;i<(game.possibleMoves().size());i++){
  52. // games[i]=game.possibleMoves().at(i);
  53. scores[i]=findBestMoveHelper(game.possibleMoves()[i],1);
  54. cout<<"Move: "<<game.penniesLeft()-game.possibleMoves()[i].penniesLeft()<<" Score: "<<scores[i]<<endl;
  55. if(scores[i]>max){
  56. T newMax(game.possibleMoves()[i].penniesLeft());
  57. maxGame=newMax;
  58. max=scores[i];
  59. }
  60. }
  61. cout<<"Move: "<<game.penniesLeft()-maxGame.penniesLeft()<<endl;
  62.  
  63. };
  64. int findBestMoveHelper(T game, int turn){
  65.  
  66. //else{
  67. //cout<<"Helper called on: "<<game.penniesLeft()<<endl;
  68. int score=0;
  69. if(turn==0){
  70. if(game.possibleMoves().size()==1){
  71. return -1;
  72. }
  73. else if(game.possibleMoves().size()==0){
  74. return 1;
  75. }
  76. else{
  77. for(int i =0; i < game.possibleMoves().size(); i++){
  78. // cout<<game.possibleMoves()[i].penniesLeft()<<endl;
  79. T newGame(game.possibleMoves()[i].penniesLeft());
  80. score+= findBestMoveHelper(newGame,1);
  81. }
  82. return score;
  83. }
  84. }
  85.  
  86. else if(turn==1){
  87. if(game.possibleMoves().size()==1){
  88. return 1;
  89. }
  90. else if(game.possibleMoves().size()==0){
  91. return -1;
  92. }
  93. else{
  94. for(int i =0; i < game.possibleMoves().size(); i++){
  95. // cout<<game.possibleMoves()[i].penniesLeft()<<endl;;
  96.  
  97. T newGame(game.possibleMoves()[i].penniesLeft());
  98. score+= findBestMoveHelper(newGame,0);
  99. }
  100.  
  101. return score;
  102. }
  103. }
  104. // }
  105.  
  106.  
  107.  
  108. };
  109.  
  110. T returnBestMove(player p){
  111. return findBestMove();
  112. }
  113.  
  114. bool contains( int name ) const{
  115. return memo.find(name)!=memo.end();
  116. };
  117. private:
  118. map<int, int> memo;
  119. };
  120.  
  121.  
  122. #endif
Add Comment
Please, Sign In to add comment