Question
C++ coding! main.ccp: #include player.hh #include #include #include int main() { Player player1 = Player(Matti); Player player2 = Player(Teppo); Player* in_turn = 0; int turn
C++ coding! main.ccp:
#include "player.hh" #include
int main() { Player player1 = Player("Matti"); Player player2 = Player("Teppo"); Player* in_turn = 0;
int turn = 1; while (true) { if (turn % 2 != 0) { in_turn = &player1; } else { in_turn = &player2; }
std::cout << "Enter the score of player " << in_turn->get_name() << " of turn " << turn << ": "; int pts = 0; std::cin >> pts;
in_turn->add_points(pts); if (in_turn->has_won()) { std::cout << "Game over! The winner is " << in_turn->get_name() << "!" << std::endl; return EXIT_SUCCESS; }
std::cout << std::endl; std::cout << "Scoreboard after turn " << turn << ":" << std::endl; std::cout << player1.get_name() << ": " << player1.get_points() << "p" << std::endl; std::cout << player2.get_name() << ": " << player2.get_points() << "p" << std::endl; std::cout << std::endl;
turn += 1; }
return EXIT_SUCCESS; } Instructions:
Mlkky is a block throwing game where your aim is to get exactly 50 points. If a player gets more than 50 points, their score is reduced to 25 points.
In this exercise, you will implement a program to simplify point counting and following a two-player Mlkky game.
The code template defines two objects of type Player and calls the methods get_name, get_points, has_won, and add_points for the objects. (Method get_name returns a string, while method get_points returns an int.) The variables player1 and player2 in the main function are instances of the Player class. The type of the variable in_turn is Player*, which means that the pointer will take turns pointing to one of the objects Player, depending on whose turn it is to throw. The next picture will give you an example:
Implement the class Player which includes the information about a single player, and all the methods that are called in the main program. So you need to make the player.hh header and player.cpp source files. Do not make any changes to the file called main.cpp.
After you have implemented the class, the program will work in the following way (in order to make the example shorter, all of the scores used are larger than in reality):
Enter the score of player Matti of turn 1: 24 Scoreboard after turn 1: Matti: 24p Teppo: 0p Enter the score of player Teppo of turn 2: 30 Scoreboard after turn 2: Matti: 24p Teppo: 30p Enter the score of player Matti of turn 3: 24 Scoreboard after turn 3: Matti: 48p Teppo: 30p Enter the score of player Teppo of turn 4: 21 Teppo gets penalty points! Scoreboard after turn 4: Matti: 48p Teppo: 25p Enter the score of player Matti of turn 5: 2 Game over! The winner is Matti!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started