Computer Chess Club Archives


Search

Terms

Messages

Subject: CWinboard.cpp

Author: Mathieu Pagé

Date: 06:33:16 03/16/04

Go up one level in this thread


#include "CWinboard.h"

#include <string>
#include <vector>
#include <iostream>

using namespace std;

namespace WinboardWrapper{

// This function is not a method of CWinboard but is required by the parser
vector <string> Tokenize(string s, char cDelimiteur)
{
   string strTmp;
   vector <string> tokens;

   unsigned int i;
   for(i = 0; i < s.length(); i++)
   {
      if (s[i] != cDelimiteur)
      {
         strTmp += s[i];
      }
      else
      {
         tokens.push_back(strTmp);
         strTmp = "";
      }
   }

   if (strTmp != "")
   {
      tokens.push_back(strTmp);
   }

   return tokens;
}

void CWinboard::RunWinboardMode()
{
   int iCmp;               // Compteur bon à tout faire
   string strEntre;        // Chaîne que l'utilisateur tappe
   vector<string> tokens;  // Liste des paramêtres incluant la commande

   const int iNbCommandes = 42;
   const string strCommandes[] = {"protover",
                                  "accepted",
                                  "rejected",
                                  "new",
                                  "variant",
                                  "random",
                                  "force",
                                  "go",
                                  "playother",
                                  "white",
                                  "black",
                                  "level",
                                  "st",
                                  "sd",
                                  "time",
                                  "otime",
                                  "usermove",
                                  "movenow",
                                  "ping",
                                  "draw",
                                  "result",
                                  "setboard",
                                  "edit",
                                  "hint",
                                  "bk",
                                  "undo",
                                  "remove",
                                  "hard",
                                  "easy",
                                  "post",
                                  "noPost",
                                  "analyze",
                                  "name",
                                  "rating",
                                  "ics",
                                  "computer",
                                  "pause",
                                  "resume",
                                  "partner",
                                  "ptell",
                                  "holding",
                                  "exit"};

   void (CWinboard::*commandes[])(vector<string>) = {ParserProtover,
                                                     ParserAccepted,
                                                     ParserRejected,
                                                     ParserNew,
                                                     ParserVariant,
                                                     ParserRandom,
                                                     ParserForce,
                                                     ParserGo,
                                                     ParserPlayOther,
                                                     ParserWhite,
                                                     ParserBlack,
                                                     ParserLevel,
                                                     ParserSt,
                                                     ParserSd,
                                                     ParserTime,
                                                     ParserOtime,
                                                     ParserUserMove,
                                                     ParserMoveNow,
                                                     ParserPing,
                                                     ParserDraw,
                                                     ParserResult,
                                                     ParserSetBoard,
                                                     ParserEdit,
                                                     ParserHint,
                                                     ParserBk,
                                                     ParserUndo,
                                                     ParserRemove,
                                                     ParserHard,
                                                     ParserEasy,
                                                     ParserPost,
                                                     ParserNoPost,
                                                     ParserAnalyze,
                                                     ParserName,
                                                     ParserRating,
                                                     ParserIcs,
                                                     ParserComputer,
                                                     ParserPause,
                                                     ParserResume,
                                                     ParserPartner,
                                                     ParserPTell,
                                                     ParserHolding,
                                                     ParserExit};

   // turn off buffering on output
   cout.setf(ios::unitbuf);

   do
   {
      // On lit une entrée
      getline(cin, strEntre);

      // on sépare la ligne de commande en jetons
      tokens = Tokenize(strEntre, ' ');

      if (tokens.size() > 0 && tokens[0] != "quit")
      {
         iCmp = 0;
         while(tokens[0] != strCommandes[iCmp] && iCmp < iNbCommandes)
         {
            iCmp++;
         }

         // Si on a trouvé la commande
         if (iCmp < iNbCommandes)
         {
            (this->*commandes[iCmp])(tokens);
         }
         else
         {
            // si on a pas trouvé la commande
            Error("unknown command", tokens[0]);
         }
      }
   } while (tokens.size() == 0 || tokens[0] != "quit");
}

void CWinboard::ParserProtover(std::vector<std::string> vecParams)
{
   protover(vecParams[1]);
}

void CWinboard::ParserAccepted(std::vector<std::string> vecParams)
{
   accepted(vecParams[1]);
}

void CWinboard::ParserRejected(std::vector<std::string> vecParams)
{
   rejected(vecParams[1]);
}

void CWinboard::ParserNew(std::vector<std::string> vecParams)
{
   _new();
}

void CWinboard::ParserVariant(std::vector<std::string> vecParams)
{
   static const ciNbVariants = 13;
   string strVariants[] = {"wildcastle", "nocastle", "fisherandom", "bughouse",
                           "crazyhouse", "losers", "suicide", "giveaway",
                           "twokings", "kriegspiel", "atomic", "3check",
                           "unknown"};

   int iCmp = 0;
   while (iCmp < ciNbVariants && strVariants[iCmp] != vecParams[1])
   {
      iCmp++;
   }

   if (iCmp < ciNbVariants)
   {
      variant((EVariant)iCmp);
   }
   else
   {
      Error("Unknown variant", "variant");
   }
}

void CWinboard::ParserRandom(std::vector<std::string> vecParams)
{
   random();
}

void CWinboard::ParserForce(std::vector<std::string> vecParams)
{
   force();
}

void CWinboard::ParserGo(std::vector<std::string> vecParams)
{
   go();
}

void CWinboard::ParserPlayOther(std::vector<std::string> vecParams)
{
   playother();
}

void CWinboard::ParserWhite(std::vector<std::string> vecParams)
{
   white();
}

void CWinboard::ParserBlack(std::vector<std::string> vecParams)
{
   black();
}

void CWinboard::ParserLevel(std::vector<std::string> vecParams)
{
   level(atoi(vecParams[1].c_str()), atoi(vecParams[2].c_str()) * 60,
atoi(vecParams[3].c_str()));
}

void CWinboard::ParserSt(std::vector<std::string> vecParams)
{
   st(atoi(vecParams[1].c_str()));
}

void CWinboard::ParserSd(std::vector<std::string> vecParams)
{
   sd(atoi(vecParams[1].c_str()));
}

void CWinboard::ParserTime(std::vector<std::string> vecParams)
{
   time(atoi(vecParams[1].c_str()));
}

void CWinboard::ParserOtime(std::vector<std::string> vecParams)
{
   otime(atoi(vecParams[1].c_str()));
}

void CWinboard::ParserUserMove(std::vector<std::string> vecParams)
{
   usermove(vecParams[1]);
}

void CWinboard::ParserMoveNow(std::vector<std::string> vecParams)
{
   movenow();
}

void CWinboard::ParserPing(std::vector<std::string> vecParams)
{
   ping(atoi(vecParams[1].c_str()));
}

void CWinboard::ParserDraw(std::vector<std::string> vecParams)
{
   draw();
}

void CWinboard::ParserResult(std::vector<std::string> vecParams)
{
   if (vecParams[1] == "1-0")
   {
      result(cWhitesWin, vecParams[2]);
   }
   else if (vecParams[1] == "0-1")
   {
      result(cBlackWin, vecParams[2]);
   }
   else
   {
      result(cDraw, vecParams[2]);
   }
}

void CWinboard::ParserSetBoard(std::vector<std::string> vecParams)
{
   setboard(vecParams[1] + " " + vecParams[2] + " " + vecParams[3] + " " +
vecParams[4] + " " + vecParams[5] + " " + vecParams[6]);
}

void CWinboard::ParserEdit(std::vector<std::string> vecParams)
{
   edit();
}

void CWinboard::ParserHint(std::vector<std::string> vecParams)
{
   hint();
}

void CWinboard::ParserBk(std::vector<std::string> vecParams)
{
   bk();
}

void CWinboard::ParserUndo(std::vector<std::string> vecParams)
{
   undo();
}

void CWinboard::ParserRemove(std::vector<std::string> vecParams)
{
   remove();
}

void CWinboard::ParserHard(std::vector<std::string> vecParams)
{
   hard();
}

void CWinboard::ParserEasy(std::vector<std::string> vecParams)
{
   easy();
}

void CWinboard::ParserPost(std::vector<std::string> vecParams)
{
   post();
}

void CWinboard::ParserNoPost(std::vector<std::string> vecParams)
{
   nopost();
}

void CWinboard::ParserAnalyze(std::vector<std::string> vecParams)
{
   analyze();
}

void CWinboard::ParserName(std::vector<std::string> vecParams)
{
   string str;
   unsigned int i;

   for(i = 1; i < vecParams.size(); i++)
   {
      str = str + vecParams[i] + " ";
   }

   name(str);
}

void CWinboard::ParserRating(std::vector<std::string> vecParams)
{
   rating(atoi(vecParams[1].c_str()), atoi(vecParams[2].c_str()));
}

void CWinboard::ParserIcs(std::vector<std::string> vecParams)
{
   ics(vecParams[1]);
}

void CWinboard::ParserComputer(std::vector<std::string> vecParams)
{
   computer();
}

void CWinboard::ParserPause(std::vector<std::string> vecParams)
{
   pause();
}

void CWinboard::ParserResume(std::vector<std::string> vecParams)
{
   resume();
}

void CWinboard::ParserPartner(std::vector<std::string> vecParams)
{
   if (vecParams.size() == 1)
   {
      NoPartner();
   }
   else
   {
      partner(vecParams[1]);
   }
}

void CWinboard::ParserPTell(std::vector<std::string> vecParams)
{
   string str;
   unsigned int i;

   for(i = 1; i < vecParams.size(); i++)
   {
      str = str + vecParams[i] + " ";
   }

   ptell(str);
}

void CWinboard::ParserHolding(std::vector<std::string> vecParams)
{
   if (vecParams.size() == 3)
   {
      holding(vecParams[1], vecParams[2]);
   }
   else
   {
      holding(vecParams[1], vecParams[2], vecParams[3][0], vecParams[3][1]);
   }
}

void CWinboard::ParserExit(std::vector<std::string> vecParams)
{
   exit();
}

void CWinboard::feature(bool ping,             bool playother,
                        bool san,              bool time,
                        bool draw,             bool sigint,
                        bool sigterm,          bool reuse,
                        bool analyze,          std::string myname,
                        std::string variants,  bool colors,
                        bool ics,              bool name,
                        bool pause)
{
   cout <<"feature"
        <<" ping=" <<(ping ? 1 : 0)
        <<" setboard=1"
        <<" playother=" <<(playother ? 1 : 0)
        <<" san=" <<(san ? 1 : 0)
        <<" usermove=1"
        <<" time=" <<(time ? 1 : 0)
        <<" draw=" <<(draw ? 1 : 0)
        <<" sigint=" <<(sigint ? 1 : 0)
        <<" analyze=" <<(analyze ? 1 : 0)
        <<" myname=\"" <<myname <<"\""
        <<" variants=\"" <<variants <<"\""
        <<" colors=" <<(colors ? 1 : 0)
        <<" ics=" <<(ics ? 1 : 0)
        <<" name=" <<(name ? 1 : 0)
        <<" pause=" <<(pause ? 1 : 0) <<endl;

   cout <<"feature done=1" <<endl;
}

void CWinboard::IllegalMove(std::string strMove, std::string strReason)
{
   cout <<"Illegal move";

   if (strReason != "")
   {
      cout <<" (" <<strReason <<")";
   }

   cout <<": " <<strMove <<endl;
}

void CWinboard::Error(std::string strErrorType, std::string strCommand)
{
   cout <<"Error (" <<strErrorType <<"): " <<strCommand <<endl;
}

void CWinboard::Move(std::string strMove)
{
   cout <<"move " <<strMove <<endl;
}

void CWinboard::Result(EResult ersResultat, std::string strComment)
{
   static const string strResults[] = {"1-0", "0-1", "1/2-1/2"};

   cout <<strResults[ersResultat] <<"{" <<strComment <<"}" <<endl;
}

void CWinboard::Resign()
{
   cout <<"resign" <<endl;
}

void CWinboard::OfferDraw()
{
   cout <<"offer draw" <<endl;
}

void CWinboard::TellOpponent(std::string strMessage)
{
   cout <<"tellopponent " <<strMessage <<endl;
}

void CWinboard::TellOthers(std::string strMessage)
{
   cout <<"tellothers " <<strMessage <<endl;
}

void CWinboard::TellAll(std::string strMessage)
{
   cout <<"tellall " <<strMessage <<endl;
}

void CWinboard::TellUser(std::string strMessage)
{
   cout <<"telluser " <<strMessage <<endl;
}

void CWinboard::TellUserError(std::string strMessage)
{
   cout <<"tellusererror " <<strMessage <<endl;
}

void CWinboard::AskUser(std::string strRepTag, std::string strMessage)
{
   cout <<"askuser " <<strRepTag <<' ' <<strMessage <<endl;
}

void CWinboard::TellICS(std::string strMessage)
{
   cout <<"tellics " <<strMessage <<endl;
}

void CWinboard::TellICSNoAlias(std::string strMessage)
{
   cout <<"tellicsnoalias " <<strMessage <<endl;
}

void CWinboard::ThinkingOutput(int iPly, int iScore, int iTime, int iNodes,
std::string strPV)
{
   cout <<iPly <<' ' <<iScore <<' ' <<iTime <<' ' <<iNodes <<' ' <<strPV <<endl;
}

void CWinboard::Pong(int iPingNumber)
{
   cout <<"pong " <<iPingNumber <<endl;
}

};



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.