Exemplo n.º 1
0
	//Compute the minimum edge length on any of my edges
	double calculateFaceLenSq(int faceNo,double minLenSq) const
	{
		for (int i=0;i<nPer;i++) {
			int l=findValid(faceNo,i);
			int r=findValid(faceNo,(l+1)%nPer);
			double curLenSq=getNodeLoc(getNode(faceNo,l)).distSqr(getNodeLoc(getNode(faceNo,r)));
			if (curLenSq<minLenSq) minLenSq=curLenSq;
		}
		return minLenSq;
	}
Exemplo n.º 2
0
int main(int argc, char **argv){
    user = 0;
    online = 0;
    couple = 0;
    k = 0;
    
    if((listenfd = socket(AF_INET,SOCK_STREAM,0)) < 0){
        perror("Problem in creating the socket\n");
        exit(1);
    }
    
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT);
    
    bind (listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
    listen(listenfd, LISTENQ);
    printf("Server running ... Waiting for connections.\n");
    /*
    int ret = pthread_create(&heartthread,NULL,(void*)heartBeatThread,NULL);
    if (ret != 0){
        printf("Create thread error!\r\n");
        exit(1);
    }*/
    
    while(1){
        clilen = sizeof(cliaddr);
        k = findValid();//处理并发,k!=-1,有空线程可用
        childthread[k].connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen);
        printf("Received request ...\n");
        if(k != -1){
            childthread[k].used = 1;
            int ret = pthread_create(&(childthread[k].handlethread),NULL,(void*)handleThread,(void *)k);
            if (ret != 0){
                printf("Create thread error!\r\n");
                exit(1);
            }
            else{
                printf("Succeed to create a thread!\r\n");
            }
        }
        else{
            printf("Sorry, the server is busy!!!\n");
            exit(1);
        }
    }
    //pthread_join(heartthread,NULL);
    return 0;
}
Exemplo n.º 3
0
/*
 * Compute the next move given the opponent's last move. Your AI is
 * expected to keep track of the board on its own. If this is the first move,
 * or if the opponent passed on the last move, then opponentsMove will be NULL.
 *
 * msLeft represents the time your AI has left for the total game, in
 * milliseconds. doMove() must take no longer than msLeft, or your AI will
 * be disqualified! An msLeft value of -1 indicates no time limit.
 *
 * The move returned must be legal; if there are no valid moves for your side,
 * return NULL.
 */
Move *Player::doMove(Move *opponentsMove, int msLeft) {
    /* 
     * TODO: Implement how moves your AI should play here. You should first
     * process the opponent's opponents move before calculating your own move
     */ 
    std::clock_t start;
    double duration;
    start = std::clock();

    Move *myMove = NULL;
    if(testingMinimax || minimaxPlaying){
        // Process opponent's move (doMove handles NULL moves)
        board->doMove(opponentsMove, opp_side); 

        // Call minimax
        int max_depth = 4;
        
        std::pair<int, Move*> best_choice = 
            minimax(board, 0, max_depth, true);

        myMove = best_choice.second;

        // set board to the best board
        board->doMove(myMove, my_side);

    }
    else{
        // Process opponent's move (doMove handles NULL moves)
        board->doMove(opponentsMove, opp_side);

        // Calculate my valid moves
        std::vector<Move*> valid_moves = findValid(my_side, board);

        if(!valid_moves.empty() && dumbPlaying){ // Randomly choose a valid move to play
            int index = rand() % valid_moves.size();
            myMove = valid_moves[index];
        }

        if(!valid_moves.empty() && !dumbPlaying){ // choose valid move using heuristic
            //find the best move out of all the valid moves
            std::vector<Move*>::iterator i;
            int best_score = std::numeric_limits<int>::min(); 
            Move *curr;
            int curr_score;
            for(i = valid_moves.begin(); i != valid_moves.end(); i++){
                curr = *i;
                curr_score = heuristicScore(curr, my_side);
                if(curr_score > best_score){
                    best_score = curr_score;
                    myMove = curr;
                }
            }
        }
        // Process my own move
        board->doMove(myMove, my_side);
        fprintf(stderr,"Using %s, Iago (%s) chose Move: (%d, %d)\n",
            (dumbPlaying) ? "random choice": "heuristic",
            (my_side == BLACK) ? "Black": "White", 
            myMove->getX(), myMove->getY());
    }// closes else

    duration = (std::clock() - start) / (double)CLOCKS_PER_SEC;
    fprintf(stderr, "Time to make move: %f seconds\n", duration);
    return myMove;
}
Exemplo n.º 4
0
/* Given the current state of the board, the depth being searched, and
   the maximum depth to be search, minimax returns the chosen move */
std::pair<int, Move*> Player::minimax(Board *b, int depth,
                    int max_depth, bool maximizingPlayer){
    std::pair<int, Move*> best_choice;
    int best_score;
    Move *best_move = NULL;

    if (depth == max_depth){
        int score = boardScore(my_side, b);
        best_choice = std::make_pair(score, best_move);
        return best_choice;
    }
    else{
        std::vector<Move*> valid_moves;
        if(maximizingPlayer){
            valid_moves = findValid(my_side, b);
        }
        else{
            valid_moves = findValid(opp_side, b);
        }
        if(valid_moves.empty()){
            int score = boardScore(my_side, b);
            best_choice = std::make_pair(score, best_move);
            return best_choice;
        }
        else{
            if(maximizingPlayer){
                std::vector<Move*>::iterator i; 
                best_score = std::numeric_limits<int>::min(); 
                for(i = valid_moves.begin(); i != valid_moves.end(); i++){
                    Board *b2 = b->copy();
                    b2->doMove(*i, my_side);
                    std::pair<int, Move*> choice = 
                        minimax(b2, depth + 1, max_depth, false);
                    if(choice.first > best_score){  
                        best_score = choice.first;
                        best_move = *i;
                    } 
                    delete b2;
                }
                best_choice = std::make_pair(best_score, best_move);
                return best_choice; 
            }
            else{
                std::vector<Move*>::iterator j; 
                best_score = std::numeric_limits<int>::max(); 
                for(j = valid_moves.begin(); j != valid_moves.end(); j++){
                    Board *b2 = b->copy();
                    b2->doMove(*j, opp_side);
                    std::pair<int, Move*> choice = 
                        minimax(b2, depth + 1, max_depth, true);
                    if(choice.first < best_score){
                        best_score = choice.first;
                        best_move = *j;
                    } 
                    delete b2;
                }
                best_choice = std::make_pair(best_score, best_move);
                return best_choice;
            }
        } 
    }
}