Example #1
0
int sBook::AddLineToGuideBook(sPosition *p, char *ptr)
{
  char token[512];
  UNDO u[1];
  int move;
  int freq;
  int flagIsProblem = 0;
    
  SetPosition(p, START_POS);

  for (;;) {
    ptr = Parser.ParseToken(ptr, token);
	  
    if (*token == '\0') break;

	move = StrToMove(p, token);
      
	if (IsLegal(p, move)) {
		// apply move frequency modifiers
		freq = 1;
        if (strstr(token, "?")) freq = -100;
		if (strstr(token, "!")) freq = +100;
        if (strstr(token, "??")) freq = -4900;
		if (strstr(token, "!!")) freq = +900;

		AddMoveToGuideBook( GetBookHash(p), move, freq);
		Manipulator.DoMove(p, move, u);
	}
	else { flagIsProblem = 1; break; };

    if (p->reversibleMoves == 0)
        p->head = 0;
    }
    return flagIsProblem;
}
Example #2
0
int sBook::GetBookMove(sPosition *p, int canPrint, int *flagIsProblem) {
	int i;
	int floorVal = 0;
	int curVal   = 0;
	int bestVal  = 0;
	int choice   = 0;
	int maxFreq  = 0;
	int values[100];
	U64 localHash = GetBookHash(p);

	nOfChoices = 0;

	if (Data.isAnalyzing) return 0; // book moves aren't returned in analyse mode

	for (i = 0; i < nOfGuideRecords; i++ ) {
		if (guideBook[i].hash == localHash
		&& IsLegal(p, guideBook[i].move ) )
		{
			moves[nOfChoices]  = guideBook[i].move;
			if (guideBook[i].freq > 0 ) values[nOfChoices] = guideBook[i].freq + 20;
			else                        values[nOfChoices] = -1;
	       // we add 20, so that any move not marked as bad in the text-based
		   // opening book has a chance to be played
			nOfChoices++;
		}
	}

	if (nOfChoices) {

       srand(Timer.GetMS() );

	   if (canPrint) printf("info string ");
	   for (i = 0; i < nOfChoices; i++ ) {
		   
		   // display info about possible choices
		   if (canPrint) printf(" ");
		   MoveToStr(moves[i], testString);
		   printf( testString );
		   if (canPrint) { 
			   if (values[i] > 0 ) printf(" %d ", values[i] );
		       else                printf("? ");
		   }

           // pick move with the best random value based on frequency
		   if (values[i] > 0) curVal = 1 + rand() % values[i]; 
		   else curVal = -1;

		   if (curVal > bestVal) {
			   bestVal = curVal;
			   choice  = i;
		   }
	   }
	   if (canPrint) printf(" guide \n");

       return moves[choice];
	}

	return GetPolyglotMove(p, 1);
}
Example #3
0
File: uci.c Project: raimarHD/lcec
int make_move(char *input, ENGINE_STATE *stat)
{
  char *str_param = strtok(input, " \n\t");
  if (str_param) {
    MOVE curr_move = AlgebToMove(str_param);
    if (IsLegal(stat->board, &curr_move)) MakeMove(stat->board, &curr_move);
  }
  return 1;
}
	bool CanGang(int hand_cards[], int num, int card)
	{
		if (!IsLegal(card)) return false;

		int num_gang = 0;
		for (int i = 0; i < num; i ++) {

			if (card == hand_cards[i])
				num_gang++;
		}
		return (num_gang==4);
	}
	bool CanPeng(int hand_cards[], int num, int card)
	{
		if (!IsLegal(card)) return false;

		int num_chi = 0;
		for (int i = 0; i < num; i ++) {

			if (card == hand_cards[i])
				num_chi ++;
		}
		return (num_chi>=3);
	}
Example #6
0
/*This works because the replacement scheme ensures shallow PV is not overwritten,
and may fail if the hash table is full.*/
static int RetrievePV(BOARD *board, MOVE *PV, unsigned int depth)
{
    unsigned int PVlen = 0;
    MOVE mov = GetHashMove(&hash_table, board->zobrist_key);
    while(mov && PVlen <= depth && IsLegal(board, &mov)){
        PV[PVlen++] = mov;
        MakeMove(board, &mov);
        mov = GetHashMove(&hash_table, board->zobrist_key);
    }
    for(int i = PVlen; i > 0; i--){
        Takeback(board, PV[i-1]);
    }
    return PVlen;
}
Example #7
0
const GoMove* GoBoard::Play(int p, int color)
{
  if(!IsLegal(p, color))
    {
      std::stringstream ss;
      ss<<"Illegal move at "<<ReadablePosition(p)<<std::endl;//throw "Illegal move at ";
      throw Exception(ss.str());
    }
  AddStone(p,color);
  State.toPlay = color==S_WHITE ? S_BLACK : S_WHITE; //Inverse color.

  moves[movePointer]->Color = color;
  moves[movePointer++]->Point = ReversePos(p,color);
  return 0;
}
	bool CanHu(int hand_cards[], int num, int card)
	{
		if (!IsLegal(card)) return false;

		int *p_arr = new int [num];
		int i = 0;
		for (i = 0; i < num; i ++)
			p_arr[i] = hand_cards[i];
		p_arr[i] = card;

		int hu_cards[4][10];
		for (int i = 0; i < 4; i ++)
			for (int j = 0; j < 10; j ++)
				hu_cards[i][j] = 0;

		return IsBasicHu(hu_cards);
	}
Example #9
0
File: main.cpp Project: zcfelix/TW
bool GetItemProperties(const std::string& line, std::string& item_name, unsigned int& item_count, unsigned int& item_price, bool& is_imported)
{
    if (!IsLegal(line)) return false;
    
    std::string delim = " at ";
    auto pos1 = line.find(delim);
    
    
    std::string price_str = line.substr(pos1 + delim.length());
    item_price = PriceStringToCent(price_str);
    
    //double price_origin = std::stod(line.substr(pos1 + delim.length()));
    //item_price = static_cast<unsigned int>(std::stod(line.substr(pos1 + delim.length())) * 100.0);
    
    
    auto pos2 = line.find(" ");
    item_count = std::stoi(line.substr(0, pos2));
    item_name = line.substr(pos2 + 1, pos1 - 2);
    if (line.find("imported") != std::string::npos) is_imported = true;
    return true;
}
Example #10
0
File: uci.c Project: raimarHD/lcec
int position(char *input, ENGINE_STATE *stat)
{
  strtok(input, " \n\t");
  char *str_param = strtok(NULL, " \n\t");
  if (!strcmp(str_param, "startpos")) ReadFEN(STARTPOS, stat->board);
  else if (!strcmp(str_param, "fen")) {
    /*Don't split chain at spaces; cut at 'm' for 'moves' or at '\n'.*/
    str_param = strtok(NULL, "m\n\t"); 
    if (str_param) ReadFEN(str_param, stat->board);
  } else return 0;
  
  str_param = strtok(NULL, " \n\t");  /*e.g. str_param == "moves"*/
  str_param = strtok(NULL, " \n\t");  /*e.g. str_param == "e2e4"*/
  while (str_param) {
    MOVE curr_move = AlgebToMove(str_param);
    if (IsLegal(stat->board, &curr_move)) MakeMove(stat->board, &curr_move);
    else return 0;
    str_param = strtok(NULL, " \n\t");
  }
  return 1;
}
Example #11
0
UINT CGuiControlBar::OnNcHitTest(CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	CRect rcWindow;
	//no se convierte las coordenadas de pantalla porque el punto
	//entregado por esta funciĆ³n esta dado en el mismo sentido.
	GetWindowRect(rcWindow);
	int it=0;
//	if (IsFloating())
//        return CControlBar::OnNcHitTest(point);
	CRect rcT=m_rcCloseBtn;
	ClientToScreen(rcT);
	CPoint pt=point;
	pt.y+=23;
	pt.x+=5;
	//pt.Offset(-rcT.left,-rcT.top);
	if (rcT.PtInRect(pt))
		return HTCLOSE;
	CRect rcTemp;
	for (int i=0; i< 4; i++)
	{
		rcTemp=rcWindow;
		if (i== 0)		//left
		{
			it=	rcTemp.left;
			it+=4;
			rcTemp.right=it;
			m_rcBorder=rcTemp;
			if (rcTemp.PtInRect(point))
				if(IsLegal(HTLEFT)) return m_SideMove=HTLEFT;
		}
		if (i==1)  //top
		{
			it=	rcTemp.top;
			it+=4;
			rcTemp.bottom=it;
			m_rcBorder=rcTemp;
			if (rcTemp.PtInRect(point))
				 if(IsLegal(HTTOP)) return m_SideMove=HTTOP ;			
		}
		if (i==2)  //right
		{
			it=	rcTemp.right;
			it-=4;
			rcTemp.left=it;
			m_rcBorder=rcTemp;
			if (rcTemp.PtInRect(point))
				if (IsLegal(HTRIGHT)) return m_SideMove=HTRIGHT;			
		}
		if (i==3)  //bottom
		{
			it=	rcTemp.bottom;
			it-=4;
			rcTemp.top=it;
			m_rcBorder=rcTemp;
			if (rcTemp.PtInRect(point))
				if (IsLegal(HTBOTTOM))return m_SideMove=HTBOTTOM;			
		}

	}
	it=0;
	rcTemp=rcWindow;
	it=	rcTemp.top+nGapGripper;
	rcTemp.bottom=it;
	if (rcTemp.PtInRect(point))
		return m_SideMove=HTCAPTION;
	
	return CControlBar::OnNcHitTest(point);
}
Example #12
0
int main(int argc, char *argv[])
{
    char text[1028],temptext[1028], str[1028];
    int tlen,mlen,move[12],numlegal,done;
    if(argc>=3)
    {
       if(!strncasecmp("-MaxDepth",argv[argc-2], strlen("-MaxDepth")))
       {
          MaxDepth = atoi(argv[argc-1]);
          argc-=2;
       }
       else MaxDepth = -1;
    }
    else MaxDepth = -1;

#ifndef GRAPHICS
    printf("No graphics\n");
    if(argc != 4) Usage(argv[0]);
    strcpy(player1,argv[1]);
    strcpy(player2,argv[2]);
    SecPerMove = atof(argv[3]);
#endif

#ifdef GRAPHICS
    printf("Graphics\n");
    InitGraphics(argc,argv);
#else
      NewGame();
      {
     int x,y;
     /* I'll wait a bit to make sure both oponents are ready to go */
     printf("waiting\n");
     sleep(1);
     for(x=0;x<1000;x++)
     for(y=0;y<10000;y++);
      }
#endif
    ResetBoard();
    for(;;) {
        pthread_t thread;
        int rc, dummy;
        HandleEvents();
        if(playing) {
            sprintf(str,"Waiting for player %d",turn+1);
            Message(str);
            HumanMoved = done = 0;
            //start = times(&bff);
            rc = pthread_create(&thread, NULL, timer, (void*)&done);
            pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &dummy);
            do {
                HandleEvents();
                /* Give humans all the time they want to move */
                if(player[turn] == HUMAN) done = HumanMoved;
                else if(player[turn] == COMPUTER) 
                {
                    char *ptr;
                    memset(temptext,0,sizeof(temptext));
                    tlen = read(readfd[turn],temptext,1028);
                    if(tlen > 0) 
                    {
                        ptr = temptext;
                        while(*ptr == 10 && *ptr !=0) ptr++;
                        strcpy(text,ptr);
                        if(strlen(text)) done=1;
                    }
                }
            } while(playing && !done);
            pthread_cancel(thread);
            if(!playing) continue;
            if(player[turn] == COMPUTER && tlen <= 0) {
                sprintf(str,"Player %d has lost the game (time limit reached).",turn+1);
                Message(str);
                StopGame();
            }    
            else {
                if(player[turn] == COMPUTER) {
                    text[tlen] = '\0';
                    memset(move,0,12*sizeof(int));
                    mlen = TextToMove(text,move);
                }
                else if(player[turn] == HUMAN) {
                    mlen = hlen;
                    memcpy(move,hmove,12*sizeof(int));
                    hlen = 0;
                    memset(hmove,0,12*sizeof(int));
                    MoveToText(move,text);
                }

                if(!mlen) { /* Illegal move check 1 */
                    /*char temp[1000];
                    char *ptr1, *ptr2;
                    ptr1=text;
                    temp[0] = 0;
                    ptr2=temp;
                    while(*ptr1) 
                    {
                        sprintf(ptr2,"%i, ", *ptr1);
                        ptr1++;
                        ptr2 = &(ptr2[strlen(ptr2)]);
                    }*/
                    //sprintf(str,"Player %d has lost the game (illegal move %s %s submitted).",turn+1,text, temp);
                    sprintf(str,"Player %d has lost the game (illegal move %s submitted).",turn+1,text);
                    Message(str);
                    StopGame();
                }
                else {
                    if(!IsLegal(move,mlen)) { /* Illegal move check 2 */
                       /*char temp[1000];
                       char *ptr1, *ptr2;
                       ptr1=text;
                       temp[0] = 0;
                       ptr2=temp;
                       while(*ptr1) 
                       {
                           sprintf(ptr2,"%i, ", *ptr1);
                           ptr1++;
                           ptr2 = &(ptr2[strlen(ptr2)]);
                       }
                        sprintf(str,"Player %d has lost the game (illegal move %s %s submitted).",turn+1,text, temp);*/
                        sprintf(str,"Player %d has lost the game (illegal move %s submitted).",turn+1,text);
                        Message(str);
                        StopGame();
                    }
                    else {  /* Legal Move */
                        PerformMove(move,mlen);
#ifdef GRAPHICS
                        UpdateBoard();
#else
                        printf("Move: %s\n",text);
                        PrintBoard();
#endif
                        if(turn) turn=0; else turn=1;
    
                        /* Check to see if other player has now lost */
                        numlegal = FindLegalMoves(turn+1);
                        if(!numlegal) {
                            sprintf(str,"Player %d has lost the game.",turn+1);
                            Message(str);
                            StopGame();
                        }
                        else if(player[turn] == COMPUTER) {
                            write(writefd[turn],text,strlen(text));
                        }
                    }
                }
            }
        }            
    }
}
Example #13
0
bool GoBoard::IsLegal(const GoPoint& p, int color)
{
  return IsLegal(Pos(p),color);
}
	int FindLowValueCard(int arr[], int num, int caishen) 
	{
		int temp = 255;
		int bfind = false;
		for (int i = 0; i < num; i ++) {

			temp = arr[i];
			int kezi[3];
			int shunzi1[3];
			int shunzi2[3];
			int shunzi3[3];
			int duizi[2];
			kezi[0] = temp; kezi[1] = temp; kezi[2] = temp;
			shunzi1[0] = temp; shunzi1[1] = temp+1; shunzi1[2] = temp+2;
			shunzi2[0] = temp-1; shunzi2[1] = temp; shunzi2[2] = temp+1;
			shunzi3[0] = temp-2; shunzi3[1] = temp-1; shunzi3[2] = temp;
			duizi[0] = temp; duizi[1] = temp;

			if (temp == caishen) {

				continue;
			}

			if (IsLegal(kezi[0]) && IsSubArrExist(arr, num, kezi, 3)) {

				continue;
			}

			if (IsThreeCardsExist(shunzi1[0], shunzi1[1], shunzi1[2]) && 
				IsSubArrExist(arr, num, shunzi1, 3) && IsArrInSameGrap(shunzi1, 3)) {

					continue;
			}

			if (IsThreeCardsExist(shunzi2[0], shunzi2[1], shunzi2[2]) && 
				IsSubArrExist(arr, num, shunzi2, 3) && IsArrInSameGrap(shunzi2, 3)) {

					continue;
			}

			if (IsThreeCardsExist(shunzi3[0], shunzi3[1], shunzi3[2]) && 
				IsSubArrExist(arr, num, shunzi3, 3) && IsArrInSameGrap(shunzi3, 3)) {

					continue;
			}

			if (IsLegal(duizi[0]) && IsSubArrExist(arr, num, duizi, 2)) {

				continue;
			}

			if (temp>=31 && temp<=37)
				return temp;
			bfind = true;
		}

		if (!bfind) {

			for (int i = 0; i < num; i ++) {

				if (arr[i] != caishen) {

					temp = arr[i];
					break;
				}
			}
		}

		return temp;
	}
	bool IsThreeCardsExist(int card1, int card2, int card3)
	{
		return (IsLegal(card1) && IsLegal(card2) && IsLegal(card3));
	}
Example #16
0
void Gogame::AddStone(unsigned int color, unsigned int x, unsigned int y,
                      bool ismove, int nodeid)
{
  // Add a stone of a color at (x, y)
  // may either be play or setup
  // if x or y is out-of-bounds, this is regarded as 'pass' move
  //
  // ismove indicates whether this is a move or setup
  // if setup, the movenumber does not increase

  // ismove flag indicates if this play is a move or setup

  // Rcpp::Rcout << "add stone: " <<
  //   "(" << color << "," << x << "," <<
  //   y << "," << ismove << "," << nodeid << ") ";

  // check validity of color
  if (color != BL && color != WH && color != EM) {
    Rcpp::Rcout << "at nodeid " << nodeid << " ";
    Rcpp::stop("invalid color");
  }
  if (ismove && color == EM) {
    Rcpp::Rcout << "at nodeid " << nodeid << " ";
    Rcpp::stop("empty stone is invalid for moves");
  }
  // check if the stone can be put there
  if (!IsLegal(x, y, color, ismove)) {
    Summary();
    Rcpp::Rcout << "at nodeid " << nodeid <<
      ", tries to play: (x, y, color, ismove) = (" <<
      x << ", " << y << ", " << color << ", " << (int)ismove << ")\n";
    Rcpp::stop("illegal move");
  }


  // update current node member field
  currentnode = nodeid;
  // case for setup move
  if (!ismove) {
    // if this is a setup move, then add stone at the point and
    // no need to check the liberty.
    // but do nothing if the x or y is out of bounds because
    // you cannot add stone there!
    if (x >= 1 && y >= 1 && (int)x <= boardsize && (int)y <= boardsize) {
      // if a stone is already there, then you need to first remove the stone
      if (board[y][x] == BL || board[y][x] == WH) {
        transitions.push_back(Transition(movenumber, x, y,
                                         -board[y][x], currentnode, false));
        board[y][x] = EM;
      }
      // if this new stone is colored, add that stone
      if (color == BL || color == WH) {
        board[y][x] = color;
        transitions.push_back(Transition(movenumber, x, y,
                                         color, currentnode, false));
      }
    }
    return;
  }

  // reaching here means it is a move
  movenumber++;

  // coordinates out of bounds are regarded as pass,
  // append transition, but do nothing afterwards
  // x = y = 0 means this move is a pass
  if (x < 1 || y < 1 || (int)x > boardsize || (int)y > boardsize) {
    transitions.push_back(Transition(movenumber, 0, 0, color, currentnode, true));
    return;
  }

  // put the stone temporarily
  board[y][x] = color;

  // and record it in the transitions field
  transitions.push_back(Transition(movenumber, x, y, color, currentnode, true));


  // enter four adjacent points to the checklist
  // if they are opponent color
  unsigned int opponent_color;
  if (color == BL) {
    opponent_color = WH;
  } else {
    opponent_color = BL;
  }


  // check if any opponent stone becomes captured due to this play

  // loop over the four adjacent point
  unsigned int xx;
  unsigned int yy;
  int increment;
  for (int k = 0; k < 4; k++)
  {
    increment = 2*(k % 2) - 1;
    if (k < 2) {
      xx = x + increment;
      yy = y;
    } else {
      xx = x;
      yy = y + increment;
    }

    // if this is a opponent stone,
    // check the liberty of this point,
    // and if it does not have a liberty,
    // remove all stones connected to it
    if (board[yy][xx] == opponent_color)
      CheckAndRemove(xx, yy);
  }

}