Advertisement
Guest User

Untitled

a guest
May 11th, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1.  
  2. #include "player_q_learning.h"
  3.  
  4. player_q_learning::player_q_learning()
  5. {
  6.  
  7. }
  8.  
  9. void player_q_learning::calc_input(float input[9],int player_nr, int dice){
  10. int index;
  11. if(player_nr != -1 || player_nr != 99)
  12. index = player_nr + dice;
  13.  
  14. // Get out of Home 1 0 0 0 0 0 0 0 0
  15. if(dice == 6 && player_nr == -1){
  16. input[0] = 1;
  17. //cout << "Get out of Home" << endl;
  18. }
  19.  
  20. if(player_nr != -1){
  21.  
  22. // Get in goal 0 1 0 0 0 0 0 0 0
  23. if(index == 56){
  24. input[1] = 1;
  25. //cout << "Goal" << endl;
  26. }
  27. // Move to Globe 0 0 0 0 1 0 0 0 0
  28. // Globes pos: 8 13 21 26 34 39 47
  29. if(index == 8 || index == 13 || index == 21 || index == 26 || index == 34 || index == 39 || index == 47){
  30. input[3] = 1;
  31. //cout << "Globe" << endl;
  32. }
  33.  
  34. // Move to Star 0 0 0 0 1 0 0 0 0
  35. if(index == 5 || index == 18 || index == 31 || index == 44 || index == 11 || index == 24 || index == 37){
  36. input[4] = 1;
  37. //cout << "Star" << endl;
  38. }
  39.  
  40. // Get into safety with an other token
  41. if(index == pos_start_of_turn[0] || index == pos_start_of_turn[1] || index == pos_start_of_turn[2] || index == pos_start_of_turn[3]){
  42. input[5] = 1;
  43. //cout << "Safety" << endl;
  44. }
  45.  
  46. // Move to Goal via Star 0 1 0 0 1 0 0 0 0
  47. if(index == 50){
  48. input[1] = 1;
  49. input[4] = 1;
  50. //cout << "Goal via Star" << endl;
  51. }
  52.  
  53. // Get into the Winner Road
  54. if(index > 50 && index < 56){
  55. input[6] = 1;
  56. //cout << "Winner Road" << endl;
  57. }
  58.  
  59. // Kill an opponent
  60. for(int i = 4 ; i < 16 ; i++ )
  61. if(index == pos_start_of_turn[i]){
  62. // Kamikaze if the opponent is on a Globe
  63. if(index == 8 || index == 13 || index == 21 || index == 26 || index == 34 || index == 39 || index == 47){
  64. input[8] = 1;
  65. //cout << "Globe Kamikaze" << endl;
  66. }
  67. // Kill that Mo-Fo
  68. else{
  69. input[7] = 1;
  70. //cout << "Kill Opponent" << endl;
  71. }
  72. }
  73.  
  74. // Two opponent on the same square
  75. for(int j = 1 ; j < 4 ; j++){
  76. // Token 1 and 2
  77. if(pos_start_of_turn[j*4] == pos_start_of_turn[j*4+1]){
  78. if(index == pos_start_of_turn[j*4]){
  79. input[8] = 1;
  80. //cout << "Two opponent on the same square" << j*4 << " " << j*4+1 << endl;
  81. }
  82. }
  83. // Token 1 and 3
  84. else if(pos_start_of_turn[j*4] == pos_start_of_turn[j*4+2]){
  85. if(index == pos_start_of_turn[j*4]){
  86. input[8] = 1;
  87. //cout << "Two opponent on the same square" << j*4 << " " << j*4+2 << endl;
  88. }
  89. }
  90. // Token 1 and 4
  91. else if(pos_start_of_turn[j*4] == pos_start_of_turn[j*4+3]){
  92. if(index == pos_start_of_turn[j*4]){
  93. input[8] = 1;
  94. //cout << "Two opponent on the same square" << j*4 << " " << j*4+3 << endl;
  95. }
  96. }
  97. // Token 2 and 3
  98. else if(pos_start_of_turn[j*4+1] == pos_start_of_turn[j*4+2]){
  99. if(index == pos_start_of_turn[j*4]){
  100. input[8] = 1;
  101. //cout << "Two opponent on the same square" << j*4+1 << " " << j*4+2 << endl;
  102. }
  103. }
  104. // Token 2 and 4
  105. else if(pos_start_of_turn[j*4+1] == pos_start_of_turn[j*4+3]){
  106. if(index == pos_start_of_turn[j*4]){
  107. input[8] = 1;
  108. //cout << "Two opponent on the same square: " << j*4+1 << " " << j*4+3 << endl;
  109. }
  110. }
  111. // Token 3 and 4
  112. else if(pos_start_of_turn[j*4+2] == pos_start_of_turn[j*4+3]){
  113. if(index == pos_start_of_turn[j*4]){
  114. input[8] = 1;
  115. //cout << "Two opponent on the same square" << j*4+2 << " " << j*4+3 << endl;
  116. }
  117. }
  118. }
  119.  
  120. if(input[0] == 0 && input[1] == 0 && input[3] == 0 && input[4] == 0 && input[5] == 0 && input[6] == 0 && input[7] == 0 && input[8] == 0){
  121. if(player_nr != 99){
  122. input[2] = 1;
  123. //cout << "Free Space" << endl;
  124. }
  125. }
  126. }
  127. }
  128.  
  129.  
  130. int player_q_learning::make_decision(){
  131. if(dice_roll == 6){
  132. for(int i = 0; i < 4; ++i){
  133. if(pos_start_of_turn[i]<0){
  134. return i;
  135. }
  136. }
  137. for(int i = 0; i < 4; ++i){
  138. if(pos_start_of_turn[i]>=0 && pos_start_of_turn[i] != 99){
  139. return i;
  140. }
  141. }
  142. } else {
  143. for(int i = 0; i < 4; ++i){
  144. if(pos_start_of_turn[i]>=0 && pos_start_of_turn[i] != 99){
  145. return i;
  146. }
  147. }
  148. for(int i = 0; i < 4; ++i){ //maybe they are all locked in
  149. if(pos_start_of_turn[i]<0){
  150. return i;
  151. }
  152. }
  153. }
  154. return -1;
  155. }
  156.  
  157. void player_q_learning::start_turn(positions_and_dice relative){
  158. pos_start_of_turn = relative.pos;
  159. dice_roll = relative.dice;
  160. int decision = make_decision();
  161. emit select_piece(decision);
  162. }
  163.  
  164. void player_q_learning::post_game_analysis(std::vector<int> relative_pos){
  165. pos_end_of_turn = relative_pos;
  166. bool game_complete = true;
  167. for(int i = 0; i < 4; ++i){
  168. if(pos_end_of_turn[i] < 99){
  169. game_complete = false;
  170. }
  171. }
  172. emit turn_complete(game_complete);
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement