Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "player_q_learning.h"
- player_q_learning::player_q_learning()
- {
- }
- void player_q_learning::calc_input(float input[9],int player_nr, int dice){
- int index;
- if(player_nr != -1 || player_nr != 99)
- index = player_nr + dice;
- // Get out of Home 1 0 0 0 0 0 0 0 0
- if(dice == 6 && player_nr == -1){
- input[0] = 1;
- //cout << "Get out of Home" << endl;
- }
- if(player_nr != -1){
- // Get in goal 0 1 0 0 0 0 0 0 0
- if(index == 56){
- input[1] = 1;
- //cout << "Goal" << endl;
- }
- // Move to Globe 0 0 0 0 1 0 0 0 0
- // Globes pos: 8 13 21 26 34 39 47
- if(index == 8 || index == 13 || index == 21 || index == 26 || index == 34 || index == 39 || index == 47){
- input[3] = 1;
- //cout << "Globe" << endl;
- }
- // Move to Star 0 0 0 0 1 0 0 0 0
- if(index == 5 || index == 18 || index == 31 || index == 44 || index == 11 || index == 24 || index == 37){
- input[4] = 1;
- //cout << "Star" << endl;
- }
- // Get into safety with an other token
- 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]){
- input[5] = 1;
- //cout << "Safety" << endl;
- }
- // Move to Goal via Star 0 1 0 0 1 0 0 0 0
- if(index == 50){
- input[1] = 1;
- input[4] = 1;
- //cout << "Goal via Star" << endl;
- }
- // Get into the Winner Road
- if(index > 50 && index < 56){
- input[6] = 1;
- //cout << "Winner Road" << endl;
- }
- // Kill an opponent
- for(int i = 4 ; i < 16 ; i++ )
- if(index == pos_start_of_turn[i]){
- // Kamikaze if the opponent is on a Globe
- if(index == 8 || index == 13 || index == 21 || index == 26 || index == 34 || index == 39 || index == 47){
- input[8] = 1;
- //cout << "Globe Kamikaze" << endl;
- }
- // Kill that Mo-Fo
- else{
- input[7] = 1;
- //cout << "Kill Opponent" << endl;
- }
- }
- // Two opponent on the same square
- for(int j = 1 ; j < 4 ; j++){
- // Token 1 and 2
- if(pos_start_of_turn[j*4] == pos_start_of_turn[j*4+1]){
- if(index == pos_start_of_turn[j*4]){
- input[8] = 1;
- //cout << "Two opponent on the same square" << j*4 << " " << j*4+1 << endl;
- }
- }
- // Token 1 and 3
- else if(pos_start_of_turn[j*4] == pos_start_of_turn[j*4+2]){
- if(index == pos_start_of_turn[j*4]){
- input[8] = 1;
- //cout << "Two opponent on the same square" << j*4 << " " << j*4+2 << endl;
- }
- }
- // Token 1 and 4
- else if(pos_start_of_turn[j*4] == pos_start_of_turn[j*4+3]){
- if(index == pos_start_of_turn[j*4]){
- input[8] = 1;
- //cout << "Two opponent on the same square" << j*4 << " " << j*4+3 << endl;
- }
- }
- // Token 2 and 3
- else if(pos_start_of_turn[j*4+1] == pos_start_of_turn[j*4+2]){
- if(index == pos_start_of_turn[j*4]){
- input[8] = 1;
- //cout << "Two opponent on the same square" << j*4+1 << " " << j*4+2 << endl;
- }
- }
- // Token 2 and 4
- else if(pos_start_of_turn[j*4+1] == pos_start_of_turn[j*4+3]){
- if(index == pos_start_of_turn[j*4]){
- input[8] = 1;
- //cout << "Two opponent on the same square: " << j*4+1 << " " << j*4+3 << endl;
- }
- }
- // Token 3 and 4
- else if(pos_start_of_turn[j*4+2] == pos_start_of_turn[j*4+3]){
- if(index == pos_start_of_turn[j*4]){
- input[8] = 1;
- //cout << "Two opponent on the same square" << j*4+2 << " " << j*4+3 << endl;
- }
- }
- }
- 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){
- if(player_nr != 99){
- input[2] = 1;
- //cout << "Free Space" << endl;
- }
- }
- }
- }
- int player_q_learning::make_decision(){
- if(dice_roll == 6){
- for(int i = 0; i < 4; ++i){
- if(pos_start_of_turn[i]<0){
- return i;
- }
- }
- for(int i = 0; i < 4; ++i){
- if(pos_start_of_turn[i]>=0 && pos_start_of_turn[i] != 99){
- return i;
- }
- }
- } else {
- for(int i = 0; i < 4; ++i){
- if(pos_start_of_turn[i]>=0 && pos_start_of_turn[i] != 99){
- return i;
- }
- }
- for(int i = 0; i < 4; ++i){ //maybe they are all locked in
- if(pos_start_of_turn[i]<0){
- return i;
- }
- }
- }
- return -1;
- }
- void player_q_learning::start_turn(positions_and_dice relative){
- pos_start_of_turn = relative.pos;
- dice_roll = relative.dice;
- int decision = make_decision();
- emit select_piece(decision);
- }
- void player_q_learning::post_game_analysis(std::vector<int> relative_pos){
- pos_end_of_turn = relative_pos;
- bool game_complete = true;
- for(int i = 0; i < 4; ++i){
- if(pos_end_of_turn[i] < 99){
- game_complete = false;
- }
- }
- emit turn_complete(game_complete);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement