(E) Count the points of the game Mölkky

Goal: I will learn how to implement a small class in C++, and I will also study the main program to learn how to use a pointer when pointing to objects.

Instructions: Retrieve the code template: templates/03/molkky/ -> student/03/molkky/.

Mölkky 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 Mölkky 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:

../../_images/molkky_epasuora_osoitus_en.png

Implement the class Player which includes the information about a single player, and all the methods that are called in the main program. 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!

Tips for completing the assignment:

  • Please remember to create the new files in Qt Creator by choosing ”New File or Project.” In the next window, choose ”C++” and ”C++ Class”. Then Qt Creator will automatically take care of including all of the essential files in the compiling process correctly.
  • After you have inserted the declaration of the method in the file player.hh, go and test the property ”Refactor” -> ”Add Definition in player.cpp”. You can find it by right-clicking the method declaration.
  • The program will not compile in case the function main includes calls for methods you have not yet defined. If you want to be smart and implement your program in parts, you should first create a so-called stub of the class interface that only contains empty definitions of the methods with the return statements you might need. After doing this, you will naturally do a commit to the version control (the stub of the class interface is ready).

A+ presents the exercise submission form here.