Esempio n. 1
0
void RenJS::parseGame()
{
	mFilePath = mGameFile.GetFilePath();

	CString strLine;

	if (!mGameFile.ReadString(strLine))
	{
		throw Exception(READ_FILE_ERROR);
	}

	Utils::trim(strLine);

	int left  = strLine.Find("[");
	int right = strLine.Find("]", left);

	while (left < right)
	{
		CString strMove(strLine.Mid(left + 1, right - left - 1));

		parseMove(strMove);

		left  = strLine.Find("[", right);
		right = strLine.Find("]", left);
	}
}
Esempio n. 2
0
int parseMoveFromTerminal(void) {
	char line[MAX_MOVE_LEN];
	if(fgets(line, MAX_MOVE_LEN, stdin)) {
		ERROR_PRINT("Failed to read move from command line");
		exit(1);
	}

	return parseMove(line);
}
Esempio n. 3
0
int parseMoveFromFile(char *fileName) {
	FILE *fp = fopen(fileName, "r");

	if (fp == NULL) {
		ERROR_PRINT("Couldn't find file: %s", fileName);
		exit(1);
	}

	char line[MAX_MOVE_LEN];

	if (fgets(line, MAX_MOVE_LEN, fp) == NULL) {
		ERROR_PRINT("Move is missing");
		return MOVE_INVALID;
	}

	fclose(fp);

	return parseMove(line);
}
Esempio n. 4
0
void *doInput(void *details)
{
	prodcons *toProg;
	Action action;
	const size_t BUFF_INC = 1000;
	char inputBuffer[BUFF_INC], *wholeBuffer, *newline, *stripped, *theinput,
		*firstspace, *command;
	char *rest;
	int temp, xboardmode = 0, done = 0;
	size_t commandLength, currentMaxLength, currentBufferLength;


#ifdef DEBUG	
	FILE *debugFile;
	debugFile = fopen("iolog.log","w");
	setlinebuf(debugFile);
#endif
	newline = NULL;
	toProg = ((threadParameter *) details)->input;
	wholeBuffer = (char *) xmalloc(BUFF_INC);
	currentMaxLength = BUFF_INC;
	while (done == 0) {
		currentBufferLength = 0;
		memset((void *) wholeBuffer, '\0', currentMaxLength);
		do {
			if (fgets(inputBuffer, BUFF_INC, stdin) == NULL) {
				done = -1;
				break;
			}
			currentBufferLength += BUFF_INC;
			if (currentBufferLength > currentMaxLength) {
				wholeBuffer = (char *) xrealloc((void *) wholeBuffer, currentBufferLength);
				currentMaxLength = currentBufferLength;
			}
			wholeBuffer = strcat(wholeBuffer, inputBuffer);
			newline = strchr(wholeBuffer, '\n');
		} while (newline == NULL);

		if (newline == NULL) {
			fprintf(stderr, "Error reading from input\n");
			continue;
		}
		*newline = '\0';
#ifdef DEBUG		
		fprintf(debugFile,"%s\n",wholeBuffer);
#endif
		stripped = strip(wholeBuffer);
		action.command = xstrdup(stripped);
		theinput = uppercase(stripped);
		free(stripped);
		if ((theinput != NULL) && (strlen(theinput) > 0)) {
			firstspace = strchr(theinput, ' ');
			if (firstspace == NULL) {
				commandLength = strlen(theinput);
				rest = NULL;
			} else {
				commandLength = firstspace - theinput;
				rest = xstrdup(firstspace + 1);
			}
			/* if (rest != NULL) {
			 * printf("rest: %s\n",rest);
			 * } */
			command = (char *) xmalloc(commandLength + 1);
			memcpy(command, theinput, commandLength);
			command[commandLength] = '\0';

			if (strcmp(command, "USERMOVE") == 0) {
				action.theType = USERMOVE;
				if (parseMove(rest, &action.data.move) < 0) {
					action.data.move.from = -1;
				}
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}

			if ((strcmp(command, "SP_SANMOVE") == 0) || 
			((!xboardmode) && (strcmp(command, "SANMOVE") == 0))) {
				action.theType = SP_SANMOVE;
				action.data.message = (char *) xstrdup(action.command + commandLength + 1);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}

			if (strcmp(command, "XBOARD") == 0) {
				xboardmode = 1;
				action.theType = XBOARD;
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}
			if (strcmp(command, "PROTOVER") == 0) {
				action.theType = PROTOVER;
				action.data.message = rest;
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "ACCEPTED") == 0) {
				action.theType = ACCEPTED;
				action.data.feature = stringToFeature(rest);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}

				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "REJECTED") == 0) {
				action.theType = REJECTED;
				action.data.feature = stringToFeature(rest);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}

				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "NEW") == 0) {
				action.theType = NEW;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}

				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "VARIANT") == 0) {
				action.theType = VARIANT;
				action.data.message = rest;
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "QUIT") == 0) {
				action.theType = QUIT;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}

				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "RANDOM") == 0) {
				action.theType = RANDOM;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "FORCE") == 0) {
				action.theType = FORCE;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}

				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "GO") == 0) {
				action.theType = GO;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "PLAYOTHER") == 0) {
				action.theType = PLAYOTHER;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "WHITE") == 0) {
				action.theType = WHITE;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "BLACK") == 0) {
				action.theType = BLACK;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "LEVEL") == 0) {
				action.theType = LEVEL;
				if (parseLevel(rest, &action.data.timecontrol) == 0) {
					putAction(toProg, &action);
				}
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}
			if (strcmp(command, "SD") == 0) {
				action.theType = SD;
				action.data.depth = parseInteger(rest);
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}
			if (strcmp(command, "ST") == 0) {
				action.theType = ST;
				action.data.time = parseInteger(rest) * 100;
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}
			if (strcmp(command, "TIME") == 0) {
				action.theType = TIME;
				action.data.time = parseInteger(rest);
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}
			if (strcmp(command, "OTIM") == 0) {
				action.theType = OTIM;
				action.data.time = parseInteger(rest);
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}
			if (strcmp(command, "?") == 0) {
				action.theType = MOVENOW;
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}
			if (strcmp(command, "PING") == 0) {
				action.theType = PING;
				action.data.time = parseInteger(rest);
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}
			if (strcmp(command, "DRAW") == 0) {
				action.theType = DRAW;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "RESULT") == 0) {
				action.theType = RESULT;
				if (parseResult(rest, &action.data.result) == 0) {
					putAction(toProg, &action);
				}
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}
			if (strcmp(command, "SETBOARD") == 0) {
				action.theType = SETBOARD;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				action.data.message =
					(char *) xstrdup(action.command + commandLength + 1);
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "EDIT") == 0) {
				action.theType = EDIT;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "HINT") == 0) {
				action.theType = HINT;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "BK") == 0) {
				action.theType = BK;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "UNDO") == 0) {
				action.theType = UNDO;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "REMOVE") == 0) {
				action.theType = REMOVE;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "HARD") == 0) {
				action.theType = HARD;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "EASY") == 0) {
				action.theType = EASY;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "POST") == 0) {
				action.theType = POST;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "NOPOST") == 0) {
				action.theType = NOPOST;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "EXIT") == 0) {
				action.theType = EXIT;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}

			if (strcmp(command, "ANALYZE") == 0) {
				action.theType = ANALYZE;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}

			if (strcmp(command, ".") == 0) {
				action.theType = UPDATESTATUS;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}

			if (strcmp(command, "NAME") == 0) {
				action.theType = NAME;
				action.data.message = rest;
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "RATING") == 0) {
				action.theType = RATING;
				temp = parseRatings(rest, &action.data.ratings);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				if (temp == 0) {
					putAction(toProg, &action);
				} else {
					free(action.command);
				}
				goto ENDPARSE;
			}
			if (strcmp(command, "ICS") == 0) {
				action.theType = ICS;
				action.data.message = rest;
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if (strcmp(command, "COMPUTER") == 0) {
				action.theType = COMPUTER;
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}
			if ((strcmp(command, "SP_DEBUG") == 0) || 
			((!xboardmode) && (strcmp(command, "DEBUG") == 0))){
				action.theType = DEBUG;
				action.data.message = rest;
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			if ((strcmp(command, "SP_MOVETOSAN") == 0) || 
			((!xboardmode) && (strcmp(command, "MOVETOSAN") == 0))){
				action.theType = SP_MOVETOSAN;
				if (parseMove(rest, &action.data.move) < 0) {
					action.data.move.from = -1;
				}
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}

			if ((strcmp(command, "SP_BENCHMOVEUNMOVE") == 0) || 
			((!xboardmode) && (strcmp(command, "BENCHMOVEUNMOVE") == 0)))  {
				action.theType = SP_BENCHMOVEUNMOVE;
				if (parseMove(rest, &action.data.move) < 0) {
					action.data.move.from = -1;
				}
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}

			if ((strcmp(command, "SP_BENCHMOVEGEN") == 0) || 
			((!xboardmode) && (strcmp(command, "BENCHMOVEGEN") == 0))){
				action.theType = SP_BENCHMOVEGEN;
				action.data.time = parseInteger(rest);				
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}

			if ((strcmp(command, "SP_BENCHMOVECYCLE") == 0) || 
			((!xboardmode) && (strcmp(command, "BENCHMOVECYCLE") == 0))) {
				action.theType = SP_BENCHMOVECYCLE;
				action.data.time = parseInteger(rest);				
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}

			if ((strcmp(command, "SP_BENCHEVAL") == 0) || 
			((!xboardmode) && (strcmp(command, "BENCHEVAL") == 0))) {
				action.theType = SP_BENCHEVAL;
				action.data.time = parseInteger(rest);				
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}



			if ((strcmp(command, "SP_PERFT") == 0) || 
			((!xboardmode) && (strcmp(command, "PERFT") == 0))) {
				action.theType = SP_PERFT;
				action.data.depth = parseInteger(rest);				
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}

			if ((strcmp(command, "SP_EPDSUITE") == 0) || 
			((!xboardmode) && (strcmp(command, "EPDSUITE") == 0))) {
				action.theType = SP_EPDSUITE;
				action.data.message = (char *) xstrdup(action.command  + commandLength + 1);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}				
				putAction(toProg, &action);
				goto ENDPARSE;
			}

			if ((strcmp(command, "SP_EPDLINE") == 0) || 
			((!xboardmode) && (strcmp(command, "EPDLINE") == 0))) {
				action.theType = SP_EPDLINE;
				action.data.message = (char *) xstrdup(action.command  + commandLength + 1);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}				
				putAction(toProg, &action);
				goto ENDPARSE;
			}

			if ((strcmp(command, "SP_MODIFYBOOK") == 0) || 
			((!xboardmode) && (strcmp(command, "MODIFYBOOK") == 0))) {
				action.theType = SP_MODIFYBOOK;
				action.data.message = (char *) xstrdup(action.command  + commandLength + 1);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}				
				putAction(toProg, &action);
				goto ENDPARSE;
			}
			
			if ((strcmp(command, "SP_CLOSEBOOK") == 0) || 
			((!xboardmode) && (strcmp(command, "CLOSEBOOK") == 0))) {
				action.theType = SP_CLOSEBOOK;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}

			if ((strcmp(command, "SP_SHOWMOVES") == 0) || 
			((!xboardmode) && (strcmp(command, "SHOWMOVES") == 0))) {
				action.theType = SP_SHOWMOVES;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}

			if ((strcmp(command, "SP_EVALUATE") == 0) || 
			((!xboardmode) && (strcmp(command, "EVALUATE") == 0))) {
				action.theType = SP_EVALUATE;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}

			if (strcmp(command, "HELP") == 0) {
				action.theType = HELP;
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				putAction(toProg, &action);
				goto ENDPARSE;
			}

			if ((strcmp(command, "SP_EPDMINDEPTH") == 0) || 
			((!xboardmode) && (strcmp(command, "EPDMINDEPTH") == 0))) {
				action.theType = SP_EPDMINDEPTH;
				action.data.depth = parseInteger(rest);				
				putAction(toProg, &action);
				if (rest != NULL) {
					free(rest);
					rest = NULL;
				}
				goto ENDPARSE;
			}


			action.theType = UNKNOWN_COMMAND;
			if (rest != NULL) {
				free(rest);
				rest = NULL;
			}
			putAction(toProg, &action);
			

		 ENDPARSE:;
		}
		free(theinput);
		theinput = NULL;
	}
	free(wholeBuffer);
#ifdef DEBUG		
	fclose(debugFile);  
#endif
	return NULL;
}
Esempio n. 5
0
static bool parseSexp(Game& game, sexp_t* expression)
{
  sexp_t* sub, *subsub;
  if( !expression ) return false;
  expression = expression->list;
  if( !expression ) return false;
  if(expression->val != NULL && strcmp(expression->val, "status") == 0)
  {
    GameState gs;
    while(expression->next != NULL)
    {
      expression = expression->next;
      sub = expression->list;
      if ( !sub ) return false;
      if(string(sub->val) == "game")
      {
          sub = sub->next;
          if ( !sub ) return false;
          gs.mapWidth = atoi(sub->val);
          sub = sub->next;
          if ( !sub ) return false;
          gs.mapHeight = atoi(sub->val);
          sub = sub->next;
          if ( !sub ) return false;
          gs.waterDamage = atoi(sub->val);
          sub = sub->next;
          if ( !sub ) return false;
          gs.turnNumber = atoi(sub->val);
          sub = sub->next;
          if ( !sub ) return false;
          gs.maxUnits = atoi(sub->val);
          sub = sub->next;
          if ( !sub ) return false;
          gs.playerID = atoi(sub->val);
          sub = sub->next;
          if ( !sub ) return false;
          gs.gameNumber = atoi(sub->val);
          sub = sub->next;
          if ( !sub ) return false;
          gs.maxSiege = atoi(sub->val);
          sub = sub->next;
          if ( !sub ) return false;
          gs.oxygenRate = atof(sub->val);
          sub = sub->next;
          if ( !sub ) return false;
          gs.depositionRate = atoi(sub->val);
          sub = sub->next;
      }
      else if(string(sub->val) == "Player")
      {
        sub = sub->next;
        bool flag = true;
        while(sub && flag)
        {
          Player object;
          flag = parsePlayer(object, sub);
          gs.players[object.id] = object;
          sub = sub->next;
        }
        if ( !flag ) return false;
      }
      else if(string(sub->val) == "Mappable")
      {
        sub = sub->next;
        bool flag = true;
        while(sub && flag)
        {
          Mappable object;
          flag = parseMappable(object, sub);
          gs.mappables[object.id] = object;
          sub = sub->next;
        }
        if ( !flag ) return false;
      }
      else if(string(sub->val) == "PumpStation")
      {
        sub = sub->next;
        bool flag = true;
        while(sub && flag)
        {
          PumpStation object;
          flag = parsePumpStation(object, sub);
          gs.pumpStations[object.id] = object;
          sub = sub->next;
        }
        if ( !flag ) return false;
      }
      else if(string(sub->val) == "Unit")
      {
        sub = sub->next;
        bool flag = true;
        while(sub && flag)
        {
          Unit object;
          flag = parseUnit(object, sub);
          gs.units[object.id] = object;
          sub = sub->next;
        }
        if ( !flag ) return false;
      }
      else if(string(sub->val) == "Tile")
      {
        sub = sub->next;
        bool flag = true;
        while(sub && flag)
        {
          Tile object;
          flag = parseTile(object, sub);
          gs.tiles[object.id] = object;
          sub = sub->next;
        }
        if ( !flag ) return false;
      }
      else if(string(sub->val) == "UnitType")
      {
        sub = sub->next;
        bool flag = true;
        while(sub && flag)
        {
          UnitType object;
          flag = parseUnitType(object, sub);
          gs.unitTypes[object.id] = object;
          sub = sub->next;
        }
        if ( !flag ) return false;
      }
    }
    game.states.push_back(gs);
  }
  else if(string(expression->val) == "animations")
  {
    std::map< int, std::vector< SmartPointer< Animation > > > animations;
    while(expression->next)
    {
      expression = expression->next;
      sub = expression->list;
      if ( !sub ) return false;
      if(string(ToLower( sub->val ) ) == "dig")
      {
        SmartPointer<dig> animation = new dig;
        if ( !parseDig(*animation, expression) )
          return false;

        animations[ ((AnimOwner*)&*animation)->owner ].push_back( animation );
      }
      if(string(ToLower( sub->val ) ) == "attack")
      {
        SmartPointer<attack> animation = new attack;
        if ( !parseAttack(*animation, expression) )
          return false;

        animations[ ((AnimOwner*)&*animation)->owner ].push_back( animation );
      }
      if(string(ToLower( sub->val ) ) == "spawn")
      {
        SmartPointer<spawn> animation = new spawn;
        if ( !parseSpawn(*animation, expression) )
          return false;

        animations[ ((AnimOwner*)&*animation)->owner ].push_back( animation );
      }
      if(string(ToLower( sub->val ) ) == "death")
      {
        SmartPointer<death> animation = new death;
        if ( !parseDeath(*animation, expression) )
          return false;

        animations[ ((AnimOwner*)&*animation)->owner ].push_back( animation );
      }
      if(string(ToLower( sub->val ) ) == "move")
      {
        SmartPointer<move> animation = new move;
        if ( !parseMove(*animation, expression) )
          return false;

        animations[ ((AnimOwner*)&*animation)->owner ].push_back( animation );
      }
      if(string(ToLower( sub->val ) ) == "flow")
      {
        SmartPointer<flow> animation = new flow;
        if ( !parseFlow(*animation, expression) )
          return false;

        animations[ ((AnimOwner*)&*animation)->owner ].push_back( animation );
      }
      if(string(ToLower( sub->val ) ) == "fill")
      {
        SmartPointer<fill> animation = new fill;
        if ( !parseFill(*animation, expression) )
          return false;

        animations[ ((AnimOwner*)&*animation)->owner ].push_back( animation );
      }
    }
    game.states[game.states.size()-1].animations = animations;
  }
  else if(string(expression->val) == "ident")
  {
    expression = expression->next;
    if ( !expression ) return false;
    sub = expression->list;
    while(sub)
    {
      subsub = sub->list;
      if ( !subsub ) return false;
      int number = atoi(subsub->val);
      if(number >= 0)
      {
        subsub = subsub->next;
        if ( !subsub ) return false;
        subsub = subsub->next;
        if ( !subsub ) return false;
        game.players[number] = subsub->val;
      }
      sub = sub->next;
    }
  }
  else if(string(expression->val) == "game-winner")
  {
    expression = expression->next;
    if ( !expression ) return false;
    expression = expression->next;
    if ( !expression ) return false;
    expression = expression->next;
    if ( !expression ) return false;
    game.winner = atoi(expression->val);
		expression = expression->next;
		if( !expression ) return false;
		game.winReason = expression->val;
  }

  return true;
}
Esempio n. 6
0
void IO::consoleLoop(Board* board, SEARCHINFO* info) {
    printf("Welcome to TidesTicTactics in Console Mode!\n");
    info->POST_THINKING = true;
    setbuf(stdin, NULL);
    setbuf(stdin, NULL);
    int depth = 81;
    int movetime = 500; // 3 sec
    Coordinate move;
    char inBuf[80], command[80], modifier[80];
    Color engineSide = COLOR_NONE;
    Engine engine;
    Movelist movecheck;
    bool init = false;
    bool initX = true;
    Movelist movelist;

    while (true) {
        fflush(stdout);
        if ((board->toMove == engineSide || engineSide == COLOR_BOTH) && board->winner == COLOR_NONE) {
            info->starttime = GetTimeMs();
            info->depth = depth;
            if (movetime != 0) {
                info->timeset = true;
                info->stoptime = info->starttime + movetime;
            }
            engine.searchPosition(board, info, true);
            engineSide = COLOR_NONE;
        }
        // print prompt
        printf("");
        fflush(stdout);
        memset(&inBuf[0], 0, sizeof(inBuf));
        fflush(stdout);
        if(!fgets(inBuf, 80, stdin))
            continue;
        std::transform(inBuf, inBuf + 80 - 1, inBuf, [](unsigned char c) { return std::tolower(c); });
        sscanf(inBuf, "%s", command);

        if(!strcmp(command, "help")) {
            printf("\nCommands:\n");
            printf("quit - quit game\n");
            printf("force - will not move\n");
            printf("print - show board\n");
            printf("post - show thinking\n");
            printf("nopost - do not show thinking\n");
            printf("new - start new game\n");
            printf("go - set computer thinking\n");
            printf("depth x - set depth to x\n");
            printf("time x - set thinking time to x seconds (depth still applies if set)\n");
            printf("view - show current depth and movetime settings\n");
            printf("moves - show valid moves\n");
            printf("captures - show moves winning a board");
            printf("test x - load first x moves of demo");
            printf("test2 x - load first x moves of demo");
            printf("** note ** - to reset time and depth, set to 0\n");
            printf("enter moves using B1..9F1..9 notation\n\n\n");
            continue;
        }

        if(!strcmp(command, "moves")) {
            board->getMoves(&movecheck);
            if (movecheck.count == 0) {
                printf("No moves found");
            }
            for(int i = 0; i < movecheck.count; i++) {
                printf("%s ", PRMOVE(movecheck.moves[i].move).c_str());
            }
            printf("\n");
            continue;
        }

        if(!strcmp(command, "player")) {
            sscanf(inBuf, "player %s init", modifier);
            if(!strcmp(modifier, "one")) {
                init = true;
                initX = true;
            } else if(!strcmp(modifier, "two")) {
                init = true;
                initX = false;
            } else {
                printf("unknown player: %s\n", modifier);
            }
            continue;
        }

        if(!strcmp(command, "start")) {
            sscanf(inBuf, "start %s", modifier);
            if(!strcmp(modifier, "turns")) {
                init = false;
                board->toMove = COLOR_X;
                board->next = SQUARE_NONE;
            } else if(!strcmp(modifier, "game")) {
                init = true;
            } else {
                printf("unknown start modifier: %s\n", modifier);
            }
            continue;
        }

        if(!strcmp(command, "captures")) {
            board->getCaptureMoves(&movecheck);
            if (movecheck.count == 0) {
                printf("no captures found");
            }
            for(int i = 0; i < movecheck.count; i++) {
                printf("%s ", PRMOVE(movecheck.moves[i].move).c_str());
            }
            printf("\n");
            continue;
        }

        if(!strcmp(command, "eval")) {
            printf(board->printBoard().c_str());
            printf("\neval:%d",board->getScore());
            continue;
        }
        if(!strcmp(command, "quit")) {
            info->quit = true;
            break;
        }

        if(!strcmp(command, "post")) {
            info->POST_THINKING = true;
            continue;
        }

        if(!strcmp(command, "print")) {
            printf(board->printBoard().c_str());
            continue;
        }

        if(!strcmp(command, "nopost")) {
            info->POST_THINKING = false;
            continue;
        }

        if(!strcmp(command, "force")) {
            engineSide = COLOR_NONE;
            continue;
        }

        if(!strcmp(command, "demo")) {
            Color temp = COLOR_NONE;
            while (temp != board->toMove && board->winner == COLOR_NONE) {
                temp = board->toMove;
                info->starttime = GetTimeMs();
                info->depth = std::min(depth, MAXMOVES - board->movecount);
                if (movetime != 0) {
                    info->timeset = true;
                    info->stoptime = info->starttime + movetime;
                }
                engine.searchPosition(board, info, true);
                printf(board->printBoard().c_str());
                // printf("\nNONE\n");
                // printf(board->printBoard(COLOR_NONE).c_str());

            }
            continue;
        }

        if(!strcmp(command, "view")) {
            if(depth == 81) printf("depth not set ");
            else printf("depth %d",depth);

            if(movetime != 0) printf(" movetime %ds\n",movetime/1000);
            else printf(" movetime not set\n");

            continue;
        }

        if(!strcmp(command, "depth")) {
            sscanf(inBuf, "depth %d", &depth);
            if(depth==0) depth = 81;
            continue;
        }

        if(!strcmp(command, "test")) {
            engineSide = COLOR_NONE;
            while (board->movecount > 0)
                board->undo();
            int target;
            sscanf(inBuf, "test %d", &target);
            demo1(board, target);
            continue;
        }

        if(!strcmp(command, "test2")) {
            engineSide = COLOR_NONE;
            while (board->movecount > 0)
                board->undo();
            int target;
            sscanf(inBuf, "test2 %d", &target);
            demo2(board, target);
            continue;
        }

        if(!strcmp(command, "time")) {
            sscanf(inBuf, "time %d", &movetime);
            movetime *= 1000;
            continue;
        }

        if(!strcmp(command, "new")) {
            engineSide = COLOR_O;
            while (board->movecount > 0)
                board->undo();
            continue;
        }

        if(!strcmp(command, "go")) {
            engineSide = board->toMove;
            info->stopped = false;
            continue;
        }

        move = parseMove(inBuf);
        if(move == NOMOVE) {
            printf("command unknown:%s\n",inBuf);
            continue;
        }
        if(init) {
            if(initX) {
                board->toMove = COLOR_X;
                board->setupmove(move);
            } else {
                board->toMove = COLOR_O;
                board->setupmove(move);
            }
        } else {
            board->getMoves(&movelist);
            bool found = false;
            for (int i = 0; i < movelist.count; i++) {
                if (move == movelist.moves[i].move) {
                    found = true;
                    break;
                }
            }
            if (found)
                board->move(move);
            else {
                printf("INVALID MOVE: %s\n", PRMOVE(move).c_str());
                printf("%s", board->printBoard().c_str());
                break;
            }
        }
    }
    printf("Engine shutting down...\n");
}
Esempio n. 7
0
void consoleLoop(board& b, searchInfo* search) {

	printf("Welcome to PENIQLIOTUV In Console Mode!\n");
	printf("Type help for commands\n\n");

	search->gameMode = CONSOLEMODE;
	search->postThinking = true;
	setbuf(stdin, NULL);
  setbuf(stdout, NULL);

	int depth = MAXDEPTH, moveTime = 3000;
	int engineSide = BOTH;
	int move = NOMOVE;
	char inBuf[80], command[80];

	engineSide = BLACK;
	parseFen(START_FEN, b);

	while(true) {
		fflush(stdout);
		if(b.side == engineSide && checkResult(b) == false) {
			search->startTime = getTime();
			search->depth = depth;

			if(moveTime != 0) {
				search->timeSet = true;
				search->stopTime = search->startTime + moveTime;
			}
			searchPosition(b, search);
		}
		std::cout << std::endl << "PENIQLIOTUV > ";

		memset(&inBuf[0], 0, sizeof(inBuf));
		fflush(stdout);
		if (!fgets(inBuf, 80, stdin))
		continue;

		sscanf(inBuf, "%s", command);

		if(!strcmp(command, "help")) {
			printf("Commands:\n");
			printf("quit - quit game\n");
			printf("force - computer will not think\n");
			printf("print - show board\n");
			printf("post - show thinking\n");
			printf("nopost - do not show thinking\n");
			printf("new - start new game\n");
			printf("go - set computer thinking\n");
			printf("depth x - set depth to x\n");
			printf("time x - set thinking time to x seconds (depth still applies if set)\n");
			printf("view - show current depth and movetime settings\n");
			printf("** note ** - to reset time and depth, set to 0\n");
			printf("enter moves using b7b8q notation\n\n\n");
			continue;
		}

		if(!strcmp(command, "quit")) {
			search->quit = true;
			break;
		}

		if(!strcmp(command, "post")) {
			search->postThinking = true;
			continue;
		}

		if(!strcmp(command, "print")) {
			printBoard(b);
			continue;
		}

		if(!strcmp(command, "nopost")) {
			search->postThinking = false;
			continue;
		}

		if(!strcmp(command, "force")) {
			engineSide = BOTH;
			continue;
		}

		if(!strcmp(command, "view")) {
			if(depth == MAXDEPTH) printf("depth not set ");
			else printf("depth %d",depth);

			if(moveTime != 0) printf(" movetime %ds\n",moveTime/1000);
			else printf(" movetime not set\n");

			continue;
		}

		if(!strcmp(command, "depth")) {
			sscanf(inBuf, "depth %d", &depth);
		  if(depth==0) {
				depth = MAXDEPTH;
			}
			continue;
		}

		if(!strcmp(command, "time")) {
			sscanf(inBuf, "time %d", &moveTime);
			moveTime *= 1000;
			continue;
		}

		if(!strcmp(command, "new")) {
			engineSide = BLACK;
			parseFen(START_FEN, b);
			continue;
		}

		if(!strcmp(command, "go")) {
			engineSide = b.side;
			continue;
		}

		move = parseMove(inBuf, b);
		if(move == NOMOVE) {
			printf("Command unknown:%s\n",inBuf);
			continue;
		}
		makeMove(b, move);
		b.ply=0;
    }
}
Esempio n. 8
0
static bool parseSexp(Game& game, sexp_t* expression)
{
  sexp_t* sub, *subsub;
  if( !expression ) return false;
  expression = expression->list;
  if( !expression ) return false;
  if(expression->val != NULL && strcmp(expression->val, "status") == 0)
  {
    GameState gs;
    while(expression->next != NULL)
    {
      expression = expression->next;
      sub = expression->list;
      if ( !sub ) return false;
      if(string(sub->val) == "game")
      {
          sub = sub->next;
          if ( !sub ) return false;
          gs.turnNumber = atoi(sub->val);
          sub = sub->next;
          if ( !sub ) return false;
          gs.playerID = atoi(sub->val);
          sub = sub->next;
          if ( !sub ) return false;
          gs.gameNumber = atoi(sub->val);
          sub = sub->next;
          if ( !sub ) return false;
          gs.TurnsToStalemate = atoi(sub->val);
          sub = sub->next;
      }
      else if(string(sub->val) == "Move")
      {
        sub = sub->next;
        bool flag = true;
        while(sub && flag)
        {
          Move object;
          flag = parseMove(object, sub);
          gs.moves[object.id] = object;
          sub = sub->next;
        }
        if ( !flag ) return false;
      }
      else if(string(sub->val) == "Piece")
      {
        sub = sub->next;
        bool flag = true;
        while(sub && flag)
        {
          Piece object;
          flag = parsePiece(object, sub);
          gs.pieces[object.id] = object;
          sub = sub->next;
        }
        if ( !flag ) return false;
      }
      else if(string(sub->val) == "Player")
      {
        sub = sub->next;
        bool flag = true;
        while(sub && flag)
        {
          Player object;
          flag = parsePlayer(object, sub);
          gs.players[object.id] = object;
          sub = sub->next;
        }
        if ( !flag ) return false;
      }
    }
    game.states.push_back(gs);
  }
  else if(string(expression->val) == "animations")
  {
    std::map< int, std::vector< SmartPointer< Animation > > > animations;
    while(expression->next)
    {
      expression = expression->next;
      sub = expression->list;
      if ( !sub ) return false;
      if(string(ToLower( sub->val ) ) == "move")
      {
        SmartPointer<move> animation = new move;
        if ( !parseMove(*animation, expression) )
          return false;

        animations[ ((AnimOwner*)&*animation)->owner ].push_back( animation );
      }
    }
    game.states[game.states.size()-1].animations = animations;
  }
  else if(string(expression->val) == "ident")
  {
    expression = expression->next;
    if ( !expression ) return false;
    sub = expression->list;
    while(sub)
    {
      subsub = sub->list;
      if ( !subsub ) return false;
      int number = atoi(subsub->val);
      if(number >= 0)
      {
        subsub = subsub->next;
        if ( !subsub ) return false;
        subsub = subsub->next;
        if ( !subsub ) return false;
        game.players[number] = subsub->val;
      }
      sub = sub->next;
    }
  }
  else if(string(expression->val) == "game-winner")
  {
    expression = expression->next;
    if ( !expression ) return false;
    expression = expression->next;
    if ( !expression ) return false;
    expression = expression->next;
    if ( !expression ) return false;
    game.winner = atoi(expression->val);
		expression = expression->next;
		if( !expression ) return false;
		game.winReason = expression->val;
  }

  return true;
}