Exemple #1
0
/* Move choosing algorithm for the AI.
 *
 * Parameters:
 * piece: The game piece for the AI.
 *
 * Returns an integer in range 0-8 representing a place in a vector.
 */
int Game::botMove(char piece) {
    int ret = 0;
    int favoured[9] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
    std::vector<char> tempBoard = board;
    char opponent = (piece == cross) ? naught : cross;

    for (int i = 0; i < 9; i++) {
        if (isLegal(i)) {
            tempBoard[i] = piece;
            if (checkWin(tempBoard) == ((piece == naught) ? naughtWin : crossWin))
                return i;
            tempBoard[i] = clear;
        }
    }

    for (int i = 0; i < 9; i++) {
        if (isLegal(i)) {
            tempBoard[i] = opponent;
            if (checkWin(tempBoard) == ((opponent == naught) ? naughtWin : crossWin))
                return i;
            tempBoard[i] = clear;
        }
    }

    for (int i = 0; i < 9; i++) {
        if (isLegal(favoured[i]))
            return favoured[i];
    }
    return ret;
}
Board makeMove (Board b, int direction) {
    int position;
    int row;
    int column;
    position = findPosition(b, BLANK);
    row = position/b->size;
    column = position%b->size;
    if (isLegal(b,direction)) {
        if (direction == UP) {
            //Swap positions with position - size
            int temp;
            temp = b->board[row][column];
            b->board[row][column] = b->board[row - 1][column];
            b->board[row-1][column] = temp;
        } else if (direction == DOWN) {
            int temp;
            temp = b->board[row][column];
            b->board[row][column] = b->board[row + 1][column];
            b->board[row+1][column] = temp;
        } else if (direction == LEFT) {
            int temp;
            temp = b->board[row][column];
            b->board[row][column] = b->board[row][column - 1];
            b->board[row][column - 1] = temp;
        } else if (direction == RIGHT) {
            int temp;
            temp = b->board[row][column];
            b->board[row][column] = b->board[row][column + 1];
            b->board[row][column + 1] = temp;
        }
    }
    return b;
}
Exemple #3
0
Uint32 rotateAnti(ttsItem *t){
	t->id = getRotatedAntiShape(t->id);
	if (isLegal(t))
		return 1;
	t->id = getRotatedClockShape(t->id);
	return 0;
}
Exemple #4
0
const Position Game::move(const Position &pos, const ActionType &ac) const
    {
            //make sure its a legal action.
        if(!isLegal(ac, pos))
            return pos;
        int x = pos.x, y = pos.y;
        switch(ac)
        { //each case is the position in the surroundings array.
            case NW: x--; y--;
                break;
            case N: x--;
                break;
            case NE: x--; y++;
                break;
            case W: y--;
                break;
            case STAY:
                return pos; //don't change the position
            case E: y++;
                break;
            case SW: x++; y--;
                break;
            case S: x++;
                break;
            case SE: x++; y++;
                break;
        }
        return Position(x,y);
        
    }
Exemple #5
0
	const Position Game::move(const Position & pos, const ActionType & ac) const
	{
		if (isLegal(ac, pos))
		{
			int x, y;
			x = pos.x;
			y = pos.y;
			switch (ac)
			{
				case N: x--;
					break;
				case NE: y++; x--; 
					break;
				case E: y++;
					break;
				case SE: x++; y++;
					break;
				case S: x++;
					break;
				case SW: y--; x++;
					break;
				case W: y--;
					break;
				case NW: y--; x--;
					break;
				default:
					break;
			}
			Position newP((unsigned)x, (unsigned)y);
			return newP;
		}
		return pos;
	}
Exemple #6
0
Uint32 moveRight(ttsItem *t){
	++t->y;
	if (isLegal(t))
		return 1;
	--t->y;
	return 0;
}
Exemple #7
0
Uint32 moveDown(ttsItem *t){
	++t->x;
	if (isLegal(t))
		return 1;
	--t->x;
	return 0;
}
Exemple #8
0
Uint32 moveLeft(ttsItem *t){
	--t->y;
	if (isLegal(t))
		return 1;
	++t->y;
	return 0;
}
Exemple #9
0
void Position::applyMoves (std::string str_moves) {									// set list of moves given in string
	if (str_moves == "") { return; }

	print();

	std::istringstream iss(str_moves);
	std::string single_move_str;

	bool first = true;
	while (iss >> single_move_str) {
		std::cerr << single_move_str << std::endl;
		Move m(single_move_str);
		if (first) {
			turn = bitb.color(m.from());
			if (turn == 2) {
				std::cerr << "this move list does not apply to this position, i fear. i better do nothing with this moves ... " << std::endl;
				return;
			}
			first = false;
		}
		if (!isLegal(m.index())) {
			std::cerr << "looks like " << m.toString() << " is not a legal move here. i better do nothing with this moves ... " << std::endl;
			return;
		}
		if (m.isOnPromotionSquare() && !tower[m.from()].isOfficer()) {	// move promotes
			m.setPromotion();
		}
		moveRecorded(m.index());
	 	print();
	}
}
Exemple #10
0
Uint32 moveUp(ttsItem *t){
	--t->x;
	if (isLegal(t))
		return 1;
	++t->x;
	return 0;
}
bool IsValidUsername(const QString& username, QString& error){
    if (username.length() ==0) {
        error = QObject::tr("Username can not be empty. ");
        return false;
    }

    for(int i = 0; i < username.length(); ++i) {
        if (!isLegal(username.at(i).toLatin1())) {
            error = QObject::tr("Username must comprise a~z, 0~9, - or _. ");
            return false;
        }
    }

    if (!isLowcase(username.at(0).toLatin1())) {
        error = QObject::tr("The first character must be in lower case.");
        return false;
    }

    size_t n = sizeof(s_ReserveUsername)/sizeof(s_ReserveUsername[0]);
    for (size_t i =0; i < n; ++i) {
        if(0 == username.compare(s_ReserveUsername[i])) {
            error = QString(QObject::tr("The username has been used by system."));
            return false;
        }
    }

    return true;
}
Exemple #12
0
int hasSolution(sudokuGrid game){
    int solved;
    cell candidateCell;
    value trialValue;

    if(isFull(game)){
        solved = TRUE;
    }else{
        candidateCell = getEmptyCell(game);
        trialValue = MIN_VALUE;
        solved = FALSE;
        while(!solved && (trialValue <= MAX_VALUE)){
            if(isLegal(game, candidateCell, trialValue)){
                setCell(game, candidateCell, trialValue);

                if(hasSolution(game)){
                    solved = TRUE;
                }else{
                    clearCell(game, candidateCell);
                }
            }
            trialValue++;
        }
    }
    return solved;
}
Exemple #13
0
/* Checks if a given move for a given piece is valid and if so, adds it to
 * the board.
 *
 * Parameters:
 * piece: The players game piece.
 * move: An integer representing the position in a vector.
 *
 * Returns whether adding the move to the board was successful.
 */
bool Game::addMove(char piece, int move) {
    if (isLegal(move)) {
        board[move] = piece;
        turn++;
        return true;
    }
    return false;
}
bool IsValidUsernameChar(const QString& username, QString& error) {
    for(int i = 0; i < username.length(); ++i) {
        if (!isLegal(username.at(i).toLatin1())) {
            error = QObject::tr("Username must comprise a~z, 0~9, - or _. ");
            return false;
        }
    }
    return true;
}
Exemple #15
0
void OneInteraction::show() {
  ask();
  getResponse();
  while (!isLegal()) {
    reask();
    getResponse();
  }
  process();
}
Exemple #16
0
ttsItem getLegalItem(){
	static Uint32 cnt;
	ttsItem t;
	cnt = 0;
	do{
		t = getItem();
	}while (!isLegal(&t) && ++cnt < 20);
	t.x = -1;
	return t;
}
Exemple #17
0
vector<vector<int> > Farmer::getNeighbors(vector<int> config ){

  vector<vector<int> > moves; //all possible configs
  vector<vector<int> > retv; //all legal configs to return

  vector<int> temp;
  for (int i=0; i<3; i++) {
    if (config[i] < 3) {
      temp = config;
      temp[i]++;
      moves.push_back(temp);
    }
  }

  for (int i=0; i<3; i++) {
    if( config[i] > 1 ) {
      temp = config;
      temp[i]--;
      moves.push_back(temp);
    }
  }

  for (int i=0; i<2; i++) {
    for (int j=i+1; j<3; j++) {
      temp = config;
      if (config[i] == 2) {
        if (config[j] == 1 || config[j] == 3) {
          temp[i] = config[j];
          temp[j] = config[i];
          moves.push_back(temp);
        }
      }
      if (config[j] == 2) {
        if (config[i] == 1 || config[i] == 3) {
          temp[i] = config[j];
          temp[j] = config[i];
          moves.push_back(temp);
        }
      }
     
    }
  }

  for( int i = 0; i < moves.size(); i++ ) { 
    if(isSol(moves[i]) ) {
      retv.push_back( moves[i] );
    } else if(isLegal(moves[i]) ) {
      retv.push_back(moves[i]);
    }
  }
       

  return retv;

}
Exemple #18
0
int main ()
{
	int n;
	char op[6];
	int x, y, l;
	scanf ("%*s");
	while (~scanf ("%s %d%d%d", op, &x, &y, &l)){
		int i, j;
		if (op[0] == 'B'){
			for (i = x; i <= x+l-1 && i <= 100; i ++){
				for (j = y; j <= y+l-1 && j <= 100; j ++){
					if (isLegal(i, j))
						map[i][j] = true;
				}
			}
		}
		else if (op[0] == 'W'){
			for (i = x; i <= x+l-1 && i <= 100; i ++){
				for (j = y; j <= y+l-1 && j <= 100; j ++){
					if (isLegal(i, j))
						map[i][j] = false;
				}
			}
		}
		else if (op[0] == 'T'){
			int ans = 0;
			for (i = x; i <= x+l-1 && i <= 100; i ++){
				for (j = y; j <= y+l-1 && j <= 100; j ++){
					if (isLegal(i, j))
						if (map[i][j])ans ++;
				}
			}
			printf ("%d\n", ans);
		}
	}
	return 0;
}
Exemple #19
0
int main(int argc, char** argv) {
    
    partie Partie;
    string choice, answer;
    bool is_Legal = false;
    createPartie(Partie);
    displayPartie(Partie);
    for (int i=0; i<TOTALMOVE; i++){
        int numTurn=Partie.moveNum[i][0];
        
        Display:
        cout<<"Turn "<<i+1<<": "<<Partie.gamers[numTurn].name<<endl;
        cout<<"Enter your move:";
        cin>>choice;
        
        for (int i=0; i<100; i++){
            if(choice.compare(Partie.game.pos[i])==0)
                Partie.coup[8] = i;
        }
        Partie.coup[9] = numTurn;
        //check if move is legal
        is_Legal = isLegal(Partie.coup, Partie.game);
        if(!is_Legal){
            cout<<"Position illegal"<<endl;
            goto Display;
        }else{   
            validated(Partie.coup, Partie.game);
            displayPartie(Partie);
            cout<<"Do you validate your turn? (y/n)";
            cin>>answer;
            if(answer.compare("n")==0){
                isCancelled(Partie.coup, Partie.game);
                displayPartie(Partie);
                goto Display;
            }
            Partie.moveNum[i][1] = Partie.coup[8];
        }    

    }

    string winner;
    (Partie.game.pawn[BLACK]<Partie.game.pawn[WHITE]) ? winner= Partie.gamers[WHITE].name:Partie.gamers[BLACK].name;
    cout<<"Winner is: "<<winner<<" *** CONGRATULATION!!! ***" <<endl;
    return 0;
}
Exemple #20
0
 const Position Game::move(const Position &pos, const ActionType &ac) const
 {
     if(!isLegal(ac, pos))
         return pos;
     int x = pos.x, y = pos.y;
     switch(ac)
     {
         case N: x--; break;
         case NW: x--; y--; break;
         case W: y--; break;
         case NE: x--; y++; break;
         case S: x++; break;
         case SW: x++; y--; break;
         case E: y++; break;
         case SE: x++; y++; break;
         case STAY: break;
     }
     return Position(x,y);
 }
    //we assume legal and use isLegal
    const Position Game::move(const Position &pos, const ActionType &ac) const {
        Position shino(pos.x, pos.y);
        if(isLegal(ac,pos)){
            switch(ac){
                //when it goes left its ++ and right its -- for x
                //y goes down when its ++ and goes up when --
                case N:
                    shino.x--;      //y goes up one
                    break;
                case NE:
                    shino.x--;      //y goes up one and right one
                    shino.y++;
                    break;
                case NW:
                    shino.y--;       //y goes up one and left one
                    shino.x--;
                    break;
                case W:
                    shino.y--;      //x gors lrft onr
                    break;
                case E:
                    shino.y++;      //x goes right one
                    break;
                case S:
                    shino.x++;      //y goes down one
                    break;
                case SE:
                    shino.y++;      //y goes down one and x goes righ one
                    shino.x++;
                    break;
                case SW:
                    shino.x++;      //y goes down and x goes left one
                    shino.y--;
                    break;
                case STAY:          //we leave STAY blank since the piece does not move ta all

                    break;

            }
        }
        return  shino; // dont forget to down throw
    }
Exemple #22
0
    const Position Game::move(const Position &pos, const ActionType &ac) const { // note: assumes legal, use with isLegal()
        if (isLegal(ac, pos)) {
            int x = (pos.x);
            int y = (pos.y);

            switch (ac) {
                case NW:
                    y--;
                    x--;
                    break;
                case N:
                    x--;
                    break;
                case NE:
                    y++;
                    x--;
                    break;
                case W:
                    y--;
                    break;
                case E:
                    y++;
                    break;
                case SW:
                    y--;
                    x++;
                    break;
                case S:
                    x++;
                    break;
                case SE:
                    x++;
                    y++;
                    break;
                default :
                    break;
            }
            Position p(x, y);
            return p;
        }
    }
Exemple #23
0
 const Position Game::move(const Position &pos, const ActionType &ac) const
 {
     if (isLegal(ac, pos))
     {
         Position p(pos);
         switch (ac)
         {
             case N:
                 p.x -= 1;
                 break;
             case NE:
                 p.x -= 1;
                 p.y += 1;
                 break;
             case NW:
                 p.x -= 1;
                 p.y -= 1;
                 break;
             case E:
                 p.y += 1;
                 break;
             case W:
                 p.y -= 1;
                 break;
             case SE:
                 p.x += 1;
                 p.y += 1;
                 break;
             case SW:
                 p.x += 1;
                 p.y -= 1;
                 break;
             case S:
                 p.x += 1;
                 break;
         }
         return p;
     }
 }
QString ToDeepinUsername(const QString& username) {
    string stand = username.toLower().toStdString();
    string newstand = "";
    string::iterator itor = stand.begin();
    for (; itor!= stand.end(); ++itor) {
        if (isLegal(*itor)) {
            newstand += *itor;
        }
    }
    //first letter
    while (newstand.begin() != newstand.end()) {
        if (isLowcase(*newstand.begin())) {
            break;
        }
        newstand.erase(newstand.begin());
    }

    if (newstand.length()> 0 ) {
        return QString().fromStdString(newstand);
    }
    return "deepin";
}
Exemple #25
0
int UTF8Encoding::convert(int ch, unsigned char* bytes, int length) const
{
#ifdef _DEBUG
	unsigned char* lb = bytes;
#endif

	if (ch <= 0x7F)
	{
		if (bytes && length >= 1)
			*bytes = (unsigned char) ch;
		return 1;
	}
	else if (ch <= 0x7FF)
	{
		if (bytes && length >= 2)
		{
			*bytes++ = (unsigned char) (((ch >> 6) & 0x1F) | 0xC0);
			*bytes   = (unsigned char) ((ch & 0x3F) | 0x80);
		}
		poco_assert_dbg (isLegal(lb, 2));
		return 2;
	}
Exemple #26
0
void Server::computeNextTurn(){
    qDebug() << "\nServer::computeNextTurn()";
    QList<Move> jointMove;
    QList<Move> randomJointMove = proverSM.getRandomLegalJointMove(currentState);


    for(int i = 0; i<moves.size(); ++i){
        QString potentialMove = moves[i];
        if(potentialMove.isEmpty() || !(isLegal(i, potentialMove))){
            jointMove.append(randomJointMove[i]);
            qDebug() << "Move received is wrong : " << potentialMove;
            qDebug() << "Generated random move for player " << i << " : " << randomJointMove[i].toString();
        }
        else{
            jointMove.append(proverSM.getMoveFromString(potentialMove));
        }
    }

    currentState = proverSM.getNextState(currentState, jointMove);

    qDebug() << "Current state is now : " << currentState.toString();
}
Exemple #27
0
 const Position Game::move(const Position &pos, const ActionType &ac) const { // note: assumes legal, use with isLegal()
     if (isLegal(ac, pos)) {
         int x, y;
         x = pos.x;
         y = pos.y;
         switch (ac) {
             case E: y++; break;
             case NE: y++; x--; break;
             case N: x--; break;
             case NW: y--; x--; break;
             case W: y--; break;
             case SW: y--; x++; break;
             case S: x++; break;
             case SE: x++; y++; break;
             default: break;
         }
         Position p((unsigned )x, (unsigned)y);
         return p;
         //__grid[(unsigned)y + ((unsigned)x * __height)] = __grid[pos.y + (pos.x * __width)];
         //__grid[pos.y + (pos.x * __width)] = nullptr;
     }
     return pos;
 }
Exemple #28
0
void Move::make() {
    if (!isLegal()) {
        JWLogError << "Illegal move" << endLog;
        return;
    }
    if (target_->piece != nullptr) {
        captured_ = target_->piece;
        captured_->capture();
        // FIXME: save for unmake
        game_->resetFiftyMoves();
    }

    original_ =
        game_->getBoard().getSquare(piece_->getRow(), piece_->getColumn());

    oldHalfMoveNumber_ = piece_->getLastMoved();
    piece_->move(target_->row, target_->column, halfMoveNumber_);

    if (piece_->getType() == ChessPiece::pawn) {
        // FIXME: save for unmake
        game_->resetFiftyMoves();
    }
}
    //bool
    int myAtoi(string str) {
        isAcceptSpace_ = true;
        isAcceptSign_ = true;
        signPosi_ = true;
        int ii = 0;
        int size = str.size();
        long long ret = 0;
        while (1)
        {
            if (ii == size)
                break;
            if ( ! isLegal(str[ii]))
            {
                return (signPosi_ ? ret : -ret);
            }
            else if ( ! isNum(str[ii]))
            {
                ++ii;
                continue;
            }
            else
            {
                int tmp = str[ii] - '0';
                ret = ret*10 + tmp;
                if (ret > INT_MAX && signPosi_)
                    return INT_MAX;
                if (-ret < INT_MIN && !signPosi_)
                    return INT_MIN;
                ++ii;
                continue;
            }


        }
        return  (signPosi_ ? ret : -ret);
    }
QList<TrueFormula*> TrueFormula::RandGenerate(int number, int numberFormula)
{	
	QList<TrueFormula*> formule_list;

	for (size_t i = 0; i < numberFormula; i++)
	{
		while (true)
		{
			auto ele = TrueFormula::RandGenerate(number);
			if (ele->isLegal())
			{
				formule_list.push_back(ele);
				break;
			}
			else
			{
				SAFE_DELETE(ele);
			}
		}

	}

	return formule_list;
}