TournamentPair* RoundRobinTournament::nextPair(int gameNumber)
{
	if (gameNumber >= finalGameCount())
		return 0;
	if (gameNumber % gamesPerEncounter() != 0)
		return currentPair();

	if (m_pairNumber >= m_topHalf.size())
	{
		m_pairNumber = 0;
		setCurrentRound(currentRound() + 1);
		m_topHalf.insert(1, m_bottomHalf.takeFirst());
		m_bottomHalf.append(m_topHalf.takeLast());
	}

	int white = m_topHalf.at(m_pairNumber);
	int black = m_bottomHalf.at(m_pairNumber);

	m_pairNumber++;

	// If 'white' or 'black' equals 'playerCount()' it means
	// that it's a "bye" player, that is an empty player that
	// makes the pairings easier to organize. In that case
	// no game is played and we skip to the next pair.
	if (white < playerCount() && black < playerCount())
		return pair(white, black);
	else
		return nextPair(gameNumber);
}
Beispiel #2
0
//Function execute par le thread_Joueur
void* jouer(void* arg) {

    pthread_cleanup_push(cleanup_handler, NULL);
    MessageCJ* currentMessage;
    std::map<std::pair<int,int>,std::list<int> > alreadyTry;
    std::list<int>::iterator findIter;
    int randomNumber;
    int numberChoice;

    srand(time(NULL));

    while(1) {


        //lecture dans la file 1
        pthread_mutex_lock(&file1_lock);

//
        while(file1.size()==0) {

            pthread_cond_wait(&nonEmpty,&file1_lock);

//
        }

        //sem_wait(&file1_sem);
        //pthread_mutex_lock(&file1_lock);
        // prend le message et verifie si les valeurs sont deja tester

        currentMessage=file1.front();
        std::pair<int,int> currentPair(currentMessage->ligne,currentMessage->colonne);

//   std::cout<<"je suis "<< currentPair.first<<" "<<currentPair.second<< "  et je suis le thread numero "<<*(int*)arg<<std::endl;

        for(std::list<int>::iterator it = alreadyTry[currentPair].begin(); it!=alreadyTry[currentPair].end(); it++)

        {
            currentMessage->choiceList.remove(*it);




        }



        // select choice of response
        if(!currentMessage->choiceList.empty())
        {

            randomNumber = rand()%currentMessage->choiceList.size();

            std::list<int>::iterator ite = currentMessage->choiceList.begin();

            std::advance(ite,randomNumber);

            alreadyTry[currentPair].push_back(*ite);
            numberChoice=*ite;

            for(std::list<int>::iterator it1 = currentMessage->choiceList.begin(); it1!=currentMessage->choiceList.end(); it1++)
            {

                //std::cout<<(*it1)<<"  ";


            }


        }
        else
        {
            // std::cout<<"je suis vide"<<std::endl;
            numberChoice=0;



        }

//     std::cout<<std::endl;
        for(std::list<int>::iterator it1 = alreadyTry[currentPair].begin(); it1!=alreadyTry[currentPair].end(); it1++)
        {

            //   std::cout<<(*it1)<<"  ";


        }
        // std::cout<<std::endl;
        // std::cout<<std::endl;
        file1.pop();





        // file1.pop();
//     std::cout<<"je rend le lock "<<*(int*)arg<<std::endl;
        pthread_mutex_unlock(&file1_lock);

        sleep(2);



        // sleep(2);

//     ecriture du resultat sur la file 2
        pthread_mutex_lock(&file2_lock);
//     std::cout<<"je prend le lock de la file2"<<*(int*)arg<<std::endl;



        MessageJC* messageRetour= new MessageJC();
        messageRetour->ligne=currentPair.first;
        messageRetour->colonne=currentPair.second;
        messageRetour->tid=*(int*)arg;
        messageRetour->choice=numberChoice;
        file2.push(messageRetour);
//     std::cout<<"je rend le lock de la file2"<<*(int*)arg<<std::endl;
        pthread_mutex_unlock(&file2_lock);





    }


    pthread_cleanup_pop(0);
}