bool Snake::collision(Score& _hScore) { // Snake eats the bait if(Pixels[0]->getFrontDot() == food.getFrontDot()) { _hScore.addScore(); _hScore.levelControl(); addPixel(); feedRefresh(); } // If the snake hits the tail for(int i=1; i<numberOfPixel; i++) { if( Pixels[0]->getFrontDot() == Pixels[i]->getFrontDot() ) return true; } // If the snake hits the wall if( Pixels[0]->getFrontDot().getX() >= 0 && Pixels[0]->getFrontDot().getX() < 800 && Pixels[0]->getFrontDot().getY() >= 0 && Pixels[0]->getFrontDot().getY() < 540 ) { return false; } else return true; }
Score *Player::serveAPoint() { if( playersIdx != MAX_PLAYERS ) { std::cerr << "You need two players to play this game!" << std::endl; exit( 1 ); } int rand = getARandomNumber(1, 100); Score *score = new PointScore( players[0], players[1] ); score->addScore( rand <= getProbabilityOfWinningAServe() ? this : Player::otherPlayer( this ) ); return score; }
Score *Game::play( Player *p ) { Score *score = new GameScore( player1(), player2() ); while( !score->haveAWinner() ) { PointScore *pointScore = reinterpret_cast<PointScore *>( p->serveAPoint() ); score->addScore( pointScore->getWinner() ); delete pointScore; } return score; }
Score *Player::serveAPoint() { //This function simulates a players server if( playersIdx != MAX_PLAYERS ) { //if there are less than two players std::cerr << "You need two players to play this game!" << std::endl; exit( 1 ); } int rand = getARandomNumber(1, 100); Score *score = new PointScore( players[0], players[1] ); //create a new score object //if the random number is less than the probability of the player serving, return that player score->addScore( rand <= getProbabilityOfWinningAServe() ? this : Player::otherPlayer( this ) ); return score; //returns the result of what and how a player won the serve }
Score *Game::play( Player *p ) { /**** Derived function of play from base Competition class. Driver for a match between the two players. ****/ //creates new instance of score (0-0 game) Score *score = new GameScore( player1(), player2() ); int i = 0; while( !score->haveAWinner() ) { /**** Checks to see if either of the players has won yet. If not, it creates a new PointScore object and has a player server, which will result in a point. ****/ PointScore *pointScore; pointScore = reinterpret_cast<PointScore *>( p->serveAPoint() ); score->addScore( pointScore->getWinner() ); delete pointScore; //delete the player point } //returns the score of a game between two players return score; }