added basic janken module

This commit is contained in:
Lendemor 2015-10-25 20:12:14 +01:00
parent f281f57296
commit 1d8d5650c4
7 changed files with 207 additions and 2 deletions

View File

@ -16,7 +16,9 @@ SOURCES = main.cpp \
poilaumodule.cpp \
fourasmodule.cpp \
riddles.cpp \
todomodule.cpp
todomodule.cpp \
jankenmodule.cpp \
janken.cpp
HEADERS += \
regismodule.h \
@ -25,4 +27,6 @@ HEADERS += \
poilaumodule.h \
fourasmodule.h \
riddles.h \
todomodule.h
todomodule.h \
jankenmodule.h \
janken.h

81
app/janken.cpp Normal file
View File

@ -0,0 +1,81 @@
#include "janken.h"
Janken::Janken()
{
nb_played = NB_COUPS;
memset(played,1,sizeof(int)*NB_COUPS);
std::srand(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;
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;
}

29
app/janken.h Normal file
View File

@ -0,0 +1,29 @@
#ifndef JANKEN_H
#define JANKEN_H
#include "module.h"
enum Move{
PIERRE,FEUILLE,CISEAUX,NB_COUPS
};
class Janken
{
private:
int bot_move;
int last_move;
int nb_played;
int played[Move::NB_COUPS];
public:
Janken();
bool hasPlayed(QString msg);
void updateKnowledge();
int pickMove();
bool hasUniqueSolution();
int getSolution();
QString getWinner();
};
#endif // JANKEN_H

53
app/jankenmodule.cpp Normal file
View File

@ -0,0 +1,53 @@
#include "jankenmodule.h"
#include "message.h"
#include "janken.h"
JankenModule::JankenModule()
{
playing = false;
jk = new Janken();
}
bool JankenModule::messageHandler(Message msg)
{
if(msg.command.compare(QString("PRIVMSG"), Qt::CaseInsensitive) != 0 || !msg.target.contains(getChan()))
return false;
if(msg.args.compare("!janken") == 0)
{
answer = say(" Pon !");
playing = true;
return true;
}
if(msg.args.compare("!stopjanken") == 0)
{
playing = false;
return false;
}
if(playing && jk->hasPlayed(msg.args)){
int move = jk->pickMove();
switch(move){
case PIERRE:
answer=say(QString("Pierre\n")) + say(jk->getWinner()) ;
break;
case FEUILLE:
answer=say(QString("Feuille\n"))+ say(jk->getWinner());
break;
case CISEAUX:
answer=say(QString("Ciseaux\n"))+ say(jk->getWinner());
break;
};
jk->updateKnowledge();
playing = false;
return true;
}
return false;
}
QString JankenModule::getName()
{
return "janken";
}

21
app/jankenmodule.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef JANKENMODULE_H
#define JANKENMODULE_H
#include "module.h"
class Janken;
class JankenModule : public Module
{
private:
bool playing;
Janken* jk;
public:
JankenModule();
virtual bool messageHandler(Message msg);
virtual QString getName();
};
#endif // JANKENMODULE_H

View File

@ -1,3 +1,5 @@
#include <iostream>
#include "botapp.h"
#include "module.h"
#include "message.h"
@ -7,6 +9,9 @@
#include "poilaumodule.h"
#include "fourasmodule.h"
#include "todomodule.h"
#include "jankenmodule.h"
//
#include "janken.h"
class HelloWorldModule : public Module
{
@ -37,6 +42,17 @@ int main(int argc, char *argv[])
app.addModule(new SparrowModule());
app.addModule(new PoilAuModule());
app.addModule(new FourasModule());
app.addModule(new JankenModule());
//app.addModule(new TodoModule());
return app.exec();
/* Janken jk = Janken();
QString str;
jk.hasPlayed("Pierre");
int a = jk.pickMove();
switch(a){
case PIERRE:
std::cerr << QString("Pierre").toStdString() << std::endl;
break;
}*/
}

View File

@ -8,6 +8,7 @@ enum{VOY_A, VOY_E, VOY_I, VOY_O, VOY_U, VOY_ET, VOY_AI, VOY_OU, VOY_IN, VOY_ON,
bool PoilAuModule::messageHandler(Message msg)
{
if (msg.args.startsWith("!")) return false;
if(!isReady)
return false;
if(msg.command.compare(QString("PRIVMSG"), Qt::CaseInsensitive) != 0)