예제 #1
0
int main(int argc, char* argv[]){
    //invite players
    Player *players [4];

    for(int i= 0; i<4; i++){
        cout << "Is player " << i+1 << " a human(h) or a computer(c)?" << endl << ">";
        string input;
        cin >> input;
        assert(input=="c" || input=="C" || input=="h" || input=="H");

        if(input=="c"||input=="C")
            players[i] = new Computer(i+1);
        else if(input=="h" || input=="H")
            players[i] = new Human(i+1);
    }

    createDeck();

    //seeding the random number 
    if(argc==2)
	srand48(atol(argv[1]));
    else
	srand48(0);

    //game loops while no one has more than 80 points
    while (players[0]->getTotalScore() < 80 && players[1]->getTotalScore() < 80 && players[2]->getTotalScore() < 80 && players[3]->getTotalScore() < 80)
    {
        shuffle();

        //reset tables
        tableC.clear();
        tableD.clear();
        tableH.clear();
        tableS.clear();
        int currentPlayer=0;
        //split the deck amongst the first four players
        for(int i = 0; i<CARD_COUNT; i++){
            players[i/13]->setHand(cards_[i]);
            if(cards_[i]->getSuit()==3 && cards_[i]->getRank()==6){
                currentPlayer=i/13;
            }
        }

        //gameplay starts
        cout << "A new round begins. It's player " << currentPlayer+1 << "'s turn to play." << endl;

        //loop for one round of the game
        for(int i = 0; i<52; i++)
        {
            players[currentPlayer]->turn(tableC, tableD, tableH, tableS);
            //human player turn
	    if(players[currentPlayer]->getptype() == "h")
            {
                Human * h = dynamic_cast<Human*>(players[currentPlayer]);
                Command c;
                bool acceptablePlayMade = false;
                while (!acceptablePlayMade)
                {
                    cout << ">";
                    cin >> c;
                    if(c.type == PLAY)
                    {
			//call play with respective table 
                        if(c.card.getSuit()==0)
                            acceptablePlayMade = h->play(c.card, tableC);
                        else if(c.card.getSuit()==1)
                            acceptablePlayMade = h->play(c.card, tableD);
                        else if(c.card.getSuit()==2)
                            acceptablePlayMade = h->play(c.card, tableH);
                        else if(c.card.getSuit()==3)
                            acceptablePlayMade = h->play(c.card, tableS);
                    }
                    else if(c.type == RAGEQUIT){
                        cout << "Player " << currentPlayer + 1 << " ragequits. A computer will now take over." << endl;
                        players[currentPlayer] = new Computer(h);
                        delete h;
                        acceptablePlayMade = true;
                        //decrement currentPlayer and i to restart this turn as computer
                        currentPlayer--;
                        i--;
                    }
                    else if(c.type == DECK){
                        for (int i = 0; i<CARD_COUNT; i++)
                        {
                            cout << *(cards_[i]);
                            if (i%13 == 12)
                                cout << endl;
                            else
                                cout << " ";
                        }
                    }
                    else if(c.type == DISCARD){
                        acceptablePlayMade = h->discard(c.card, tableC, tableD, tableH, tableS);
                    }
                    else if(c.type == QUIT){
                        return 0;
                    }
                }
            }
            currentPlayer = (++currentPlayer)%4;
        }
        //total scores
        for (int i = 0; i < 4; i++)
            players[i]->countRoundScore();
    }
예제 #2
0
파일: model.cpp 프로젝트: Hamyli/Straights
//plays the game, either play a legal play or discards
void Model::play(int pos)
{
    Human * h = dynamic_cast<Human*>(players[currentPlayer]);
    bool acceptablePlayMade = false;

    //check for ragequit
    if (pos == -1)
    {
        cout << "Player " << currentPlayer + 1 << " ragequits. A computer will now take over." << endl;
        players[currentPlayer] = new Computer(h);
        delete h;
        acceptablePlayMade = true;
        players[currentPlayer]->turn(tableC, tableD, tableH, tableS);
    }
    else
    {
        //check to see if a blank card is clicked
        if (pos >= h->hand.size())
            return;
        //play the card
        deque<Card*> playable = players[currentPlayer]->findLegalPlays(tableC, tableD, tableH, tableS);

        if(playable.size() != 0)
        {
            //call play with respective table
            if(h->hand[pos]->getSuit()==0)
                acceptablePlayMade = h->play(*(h->hand[pos]), tableC, false, tableS);
            else if(h->hand[pos]->getSuit()==1)
                acceptablePlayMade = h->play(*(h->hand[pos]), tableD, false, tableS);
            else if(h->hand[pos]->getSuit()==2)
                acceptablePlayMade = h->play(*(h->hand[pos]), tableH, false, tableS);
            else if(h->hand[pos]->getSuit()==3)
                acceptablePlayMade = h->play(*(h->hand[pos]), tableS, true, tableS);
        }
        else{
            acceptablePlayMade = h->discard(*(h->hand[pos]), tableC, tableD, tableH, tableS);
        }
    }

    //if an acceptable play is made, run computer turns until it's another human player's turn
    if (acceptablePlayMade)
    {
        currentPlayer = (++currentPlayer)%4;
        if (players[currentPlayer]->hand.size() == 0)
        {
            this->endRound();
            return;
        }
        players[currentPlayer]->turn(tableC, tableD, tableH, tableS);
        //loop until next human player
        while (players[currentPlayer]->getptype() != "h")
        {
            currentPlayer = (++currentPlayer)%4;
            if (players[currentPlayer]->hand.size() == 0)
            {
                this->endRound();
                return;
            }
            players[currentPlayer]->turn(tableC, tableD, tableH, tableS);
        }
    }
    notify();

}