Wednesday 12 September 2012

sample question of c++ 4



                                                                                    Total Marks: 10
Course: Programming Fundamentals
Objectives: Functions, random number generation, algorithm building
                                                                          
Problem:
We want to make a program which implements a simple variation of LUDO game.


Following are the rules of the game:

1.     There are only two players playing the game.

2.     Each player is playing with a single piece and two pieces start in the same color start area.

3.     The players are using a single die. In order to simulate rolling a die, use rand() function to get a randomly generated number in the range 1 - 6.

4.     At the start of the game, each player's piece is placed in the start area. i.e., worksum of each piece is 0.

5.     Players take turns to throw a die. A player must first throw a 6 to be able to move a piece from the starting area onto the starting square. In each subsequent turn the player moves a piece forward 1 to 6 squares as indicated by the die.

When a player throws a 6, he/she is granted another turn as a bonus. If the player gets a 6 on the bonus turn as well, he gets another bonus turn and this could continue until the player gets a number other than 6. e.g., the player rolls the die and gets 6, then he gets an extra bonus turn. If he again gets a 6, he gets another bonus turn. Let’s say he gets a 3 this time. Now, his turn will be over and he moves his piece by 6+6+3 = 15 squares.

We will simulate moving the pieces by maintaining the worksums for each piece. Only after a player gets a 6, we start incrementing the worksum of his piece by the number on the die when he rolls the die on his turn. e,g, a player’s piece is in the start area, therefore its worksum is 0. On his turn, he rolls the die and gets a 5, the worksum of his piece remains 0. Next time, when his turn comes, he gets a 6 and 3. The worksum of his piece becomes 3. Now each time, his turn comes, his worksum will be incremented by the number he gets when the die is rolled.

6.     Once a piece has completed the circuit of the board it reaches HOME. The player whose piece gets HOME first is the one who wins the game.

We will simulate reaching HOME for a piece by checking if the worksum  of the piece is 56 or not. The player whose worksum becomes 56 first is the winner.
7.     The player must throw the exact number to advance to the HOME area.

This could be simulated by not contributing the number on the die to the worksum of the piece if the worksum exceeds 56.  e.g., if the current worksum of a piece is 53 and the player rolls the die and gets 4, then 4 will not contribute to worksum since it requires exactly 3 to move to HOME area. The worksum in this case will remain 53.
Similarly, if the current worksum is 49 and the player rolls the die and gets a 6 and then a 3 due to extra turn, then 6+3 = 9 will not contribute to worksum since it exceeds 7 needed to move to HOME area. The worksum in this case will remain 53.

8.     If a player's piece lands on a square containing the opponent's piece, the opponent's piece is captured and returns to the starting area. It will again require a 6 to move to the starting square.

The could be simulated by resetting the worksum of the opponent’s piece to 0 and setting the start flag to false, if the number on the die makes the worksum equal to that of the opponent e.g., if the worksums of player 1 and player 2 are 10 and 15 and it is player 1’s turn now. Player 1 rolls the die and gets 5, then the worksum of player 1 will become 15 and that of player 2 will be reset to 0.



Please see the sample output on next page.



Submission Guidelines:
-          Follow the submission instructions from TA.
-          No Late Submissions will be entertained.
-          Copied Assignments will be marked Zero.

Sample Output


Player1:
Please roll the die: 3
worksum = 0

Player2:
Please roll the die: 5
worksum = 0
______________________________________________
Player1:
Please roll the die: 5
worksum = 0

Player2:
Please roll the die: 6
Please roll the die again: 3
worksum = 3
___________________________________________________
Player1:
Please roll the die: 6
Please roll the die again: 5
worksum = 5

Player2:
Please roll the die: 4
worksum = 7
___________________________________________________
Player1:
Please roll the die: 5
worksum = 10

Player2:
Please roll the die: 4
worksum = 11
___________________________________________________
Player1:
Please roll the die: 1
worksum = 11

Player2:
Please roll the die: 2
worksum = 0
___________________________________________________
Player1:
Please roll the die: 6
Please roll the die again: 3
worksum = 20

Player2:
Please roll the die: 4
worksum = 0
___________________________________________________
Player1:
Please roll the die: 1
worksum = 21

Player2:
Please roll the die: 6
Please roll the die again: 4
worksum = 4
___________________________________________________
Player1:
Please roll the die: 3
worksum = 24

Player2:
Please roll the die: 3
worksum = 7
___________________________________________________
Player1:
Please roll the die: 6
Please roll the die again: 2
worksum = 32

Player2:
Please roll the die: 1
worksum = 13
___________________________________________________
Player1:
Please roll the die: 5
worksum = 37

Player2:
Please roll the die: 6
Please roll the die again: 6
Please roll the die again: 2
worksum = 27
___________________________________________________
Player1:
Please roll the die: 3
worksum = 40

Player2:
Please roll the die: 1
worksum = 28
___________________________________________________
Player1:
Please roll the die: 4
worksum = 44

Player2:
Please roll the die: 6
Please roll the die again: 3
worksum = 37
___________________________________________________
Player1:
Please roll the die: 5
worksum = 49

Player2:
Please roll the die: 3
worksum = 40
___________________________________________________
Player1:
Please roll the die: 6
Please roll the die again: 2
worksum = 49

Player2:
Please roll the die: 4
worksum = 44
___________________________________________________
Player1:
Please roll the die: 4
worksum = 53

Player2:
Please roll the die: 4
worksum = 48
___________________________________________________
Player1:
Please roll the die: 6
Please roll the die again: 3
worksum = 53

Player2:
Please roll the die: 6
Please roll the die again: 1
worksum = 55
___________________________________________________
Player1:
Please roll the die: 5
worksum = 53

Player2:
Please roll the die: 3
worksum = 55
___________________________________________________
Player1:
Please roll the die: 3
worksum = 56

*************
Player1 wins.
*************

No comments:

Post a Comment