#include "player.h"

int copy2bot ()
{
    /* do whatever would have beat the opponent two turns ago */
    if (opp_history[0]==1) 
       return( random() % 3);
    else
       return( (opp_history[opp_history[0]-1] + 1) % 3);
}

int copyeitherbot () {
    /* do whatever would have beat the opponent one or two turns ago */
    if ((random() % 2) || (opp_history[0]==1))
       return( (opp_history[opp_history[0]] + 1) % 3);
    else
       return( (opp_history[opp_history[0]-1] + 1) % 3);
}

static void makestats(int *rcount, int *pcount, int *scount) {
    int i;
    *rcount = 0;  *pcount = 0;  *scount = 0;
    for (i = 1; i <= opp_history[0]; i++) {
        if (opp_history[i] == rock)            { ++*rcount; }
        else if (opp_history[i] == paper)      { ++*pcount; }
        else /* opp_history[i] == scissors */  { ++*scount; }
    }
}

int tfreqbot () {
    /* play whatever will tie the opponent's most tfrequent choice */
    int rcount, pcount, scount; makestats(&rcount,&pcount,&scount);
    if ( (rcount > pcount) && (rcount > scount) ) { return(rock); }
    else if ( pcount > scount ) { return(paper); }
    else { return(scissors); }
}

int mlfreqbot () {
    /* play whatever will lose the opponent's middle choice */
    int rcount, pcount, scount; makestats(&rcount,&pcount,&scount);
    if ( (rcount > pcount) && (rcount < scount) ) { return(scissors); }
    else if ( (rcount < pcount) && (rcount > scount) ) { return(scissors); }
    else if ( (rcount < pcount) && (scount > pcount) ) { return(rock); }
    else if ( (rcount > pcount) && (scount < pcount) ) { return(rock); }
    else { return(paper); }
}

int ifreqbot () {
    /* play whatever will beat the opponent's most infrequent choice */
    int rcount, pcount, scount; makestats(&rcount,&pcount,&scount);
    if ( (rcount < pcount) && (rcount < scount) ) { return(paper); }
    else if ( pcount < scount ) { return(scissors); }
    else { return(rock); }
}
