#include "janken.h" #include Janken::Janken() { nb_played = NB_COUPS; memset(played,1,sizeof(int)*NB_COUPS); std::srand(std::time(NULL)); } bool Janken::hasPlayed(QString msg){ if(msg.compare(QString("Pierre")) == 0){ last_move = PIERRE; return true; }else if(msg.compare(QString("Feuille")) == 0){ last_move = FEUILLE; return true; }else if(msg.compare(QString("Ciseaux")) == 0){ last_move = CISEAUX; return true; } return false; } void Janken::updateKnowledge(){ played[last_move] += 1; nb_played += 1; } int Janken::pickMove() { if (hasUniqueSolution()){ bot_move = getSolution(); return bot_move; }else{ bot_move = rand()%Move::NB_COUPS; return bot_move; } } bool Janken::hasUniqueSolution(){ int nb_solution = 0; float ratio, solution = 0.f; for(int i = 0;i < NB_COUPS; i++){ ratio = played[i]/nb_played; if (ratio > solution){ solution = ratio; nb_solution = 1; }else if(ratio == solution){ nb_solution++; } } if (nb_solution == 1) return true; else return false; } int Janken::getSolution(){ int solution = 0; float ratio, best_ratio = 0; for(int i = 0;i < NB_COUPS; i++){ if ((ratio = played[i]/nb_played) > best_ratio){ best_ratio = ratio; solution = i; } } return (solution + 1) % NB_COUPS; } QString Janken::getWinner() { QString winner; if(bot_move == (last_move+1)%NB_COUPS) winner = QString("You lose!"); else if (bot_move == last_move) winner = QString("Tch.. that's a draw."); else winner= QString("... ... You win ... ..."); return winner; }