|
|
|
|
1
|
-#Repetition Structures - Game of sticks
|
|
|
|
|
1
|
+# Repetition Structures - Game of Sticks
|
2
|
|
2
|
|
3
|
![main1.png](images/main1.png)
|
3
|
![main1.png](images/main1.png)
|
4
|
![main2.png](images/main2.png)
|
4
|
![main2.png](images/main2.png)
|
5
|
![main3.png](images/main3.png)
|
5
|
![main3.png](images/main3.png)
|
6
|
|
6
|
|
7
|
-One of the advantages of using computer programs is that we can easily implement repetitive tasks. Structures such as the `for`, `while`, and `do-while` allow us to repeat a block of instructions as many times as needed. These structures are also known as *repetition structures*. In today’s laboratory experience you will practice the use of loops to implement a game of sticks in C++.
|
|
|
8
|
|
7
|
|
9
|
-##Objectives:
|
|
|
|
|
8
|
+[verano2016 - Coralys]
|
10
|
|
9
|
|
11
|
-Design algorithms to realize a task.
|
|
|
12
|
-Translate an algorithm into pseudocode.
|
|
|
13
|
-Implement a complete program.
|
|
|
14
|
-Practice the use of repetition structures.
|
|
|
|
|
10
|
+One of the advantages of using computer programs is that we can easily implement repetitive tasks. Structures such as the `for`, `while`, and `do-while` allow us to repeat a block of instructions as many times as needed. These structures are also known as *repetition structures*. In today’s laboratory experience, you will practice the use of loops to implement a game of sticks in C++.
|
15
|
|
11
|
|
16
|
-This laboratory experience is an adaptation of the exercise found in:
|
|
|
17
|
-http://nifty.stanford.edu/2014/laaksonen-vihavainen-game-of-sticks/handout.html.
|
|
|
|
|
12
|
+## Objectives:
|
18
|
|
13
|
|
19
|
-##Pre-Lab
|
|
|
|
|
14
|
+1. Design algorithms to realize a task.
|
|
|
15
|
+2. Translate an algorithm into pseudocode.
|
|
|
16
|
+3. Implement a complete program.
|
|
|
17
|
+4. Practice the use of repetition structures.
|
|
|
18
|
+
|
|
|
19
|
+This laboratory experience is an adaptation of the exercise found in: http://nifty.stanford.edu/2014/laaksonen-vihavainen-game-of-sticks/handout.html.
|
|
|
20
|
+
|
|
|
21
|
+## Pre-Lab:
|
20
|
|
22
|
|
21
|
Before coming to the laboratory session you should have:
|
23
|
Before coming to the laboratory session you should have:
|
22
|
|
24
|
|
23
|
-Reviewed the basic concepts related to repetition structures.
|
|
|
|
|
25
|
+1. Reviewed the basic concepts related to repetition structures.
|
24
|
|
26
|
|
25
|
2. Studied the concepts and instructions for the laboratory session.
|
27
|
2. Studied the concepts and instructions for the laboratory session.
|
26
|
|
28
|
|
|
|
|
|
36
|
|
38
|
|
37
|
For example, a game of sticks could go like this:
|
39
|
For example, a game of sticks could go like this:
|
38
|
|
40
|
|
39
|
-* The game starts with 20 sticks on the table.
|
|
|
|
|
41
|
+* The game starts with 20 sticks on the table.
|
40
|
* Aurora takes 3 sticks, there are 17 left.
|
42
|
* Aurora takes 3 sticks, there are 17 left.
|
41
|
* Bruno takes 2 sticks, there are 15 left.
|
43
|
* Bruno takes 2 sticks, there are 15 left.
|
42
|
* Aurora takes 1 stick, there are 14 left.
|
44
|
* Aurora takes 1 stick, there are 14 left.
|
|
|
|
|
53
|
---
|
55
|
---
|
54
|
|
56
|
|
55
|
|
57
|
|
56
|
-## Laboratory Session
|
|
|
|
|
58
|
+## Laboratory Session:
|
57
|
|
59
|
|
58
|
In today’s laboratory experience you will practice the use of loops to implement a game of sticks.
|
60
|
In today’s laboratory experience you will practice the use of loops to implement a game of sticks.
|
59
|
|
61
|
|
60
|
-### Exercise 1: Design the algorithm
|
|
|
|
|
62
|
+### Exercise 1 - Design the Algorithm
|
61
|
|
63
|
|
62
|
Design an algorithm so that two people can play the game of sticks on the computer. The algorithm should have these restrictions:
|
64
|
Design an algorithm so that two people can play the game of sticks on the computer. The algorithm should have these restrictions:
|
63
|
|
65
|
|
64
|
-At the beginning, it should ask the number of sticks there will be on the table. The range of accepted numbers is [10, 100]. If the user enters a number outside of this range, the program will continue asking.
|
|
|
|
|
66
|
+1. At the beginning, it should ask the number of sticks that will be on the table. The range of accepted numbers is [10, 100]. If the user enters a number outside of this range, the program will continue asking.
|
65
|
|
67
|
|
66
|
-In each turn, the player will be allowed to take from 1 to 3 sticks (without exceeding the number of available sticks on the table). If the user enters a number outside of this range, the program will continue asking.
|
|
|
|
|
68
|
+2. In each turn, the player will be allowed to take from 1 to 3 sticks (without exceeding the number of available sticks on the table). If the user enters a number outside of this range, the program will continue asking.
|
67
|
|
69
|
|
68
|
-When the game ends, the program should say who the winner is.
|
|
|
|
|
70
|
+3. When the game ends, the program should say who the winner is.
|
69
|
|
71
|
|
70
|
-**Example**
|
|
|
|
|
72
|
+**Example:**
|
71
|
|
73
|
|
72
|
```
|
74
|
```
|
73
|
Welcome to the game of sticks!
|
75
|
Welcome to the game of sticks!
|
|
|
|
|
94
|
You lose, Player 1 :-(
|
96
|
You lose, Player 1 :-(
|
95
|
```
|
97
|
```
|
96
|
|
98
|
|
97
|
-###Exercise 2: Write the pseudocode
|
|
|
|
|
99
|
+### Exercise 2 - Write the Pseudocode
|
98
|
|
100
|
|
99
|
-#### Instructions:
|
|
|
|
|
101
|
+#### Instructions
|
100
|
|
102
|
|
101
|
1. Close the computer.
|
103
|
1. Close the computer.
|
102
|
|
104
|
|
|
|
|
|
105
|
3. Execute the pseudocode by hand to capture logical errors. Try it with various *extreme* cases and write the results by hand. [Here](https://docs.google.com/a/upr.edu/presentation/d/1m-lXCUTKhCZC8vTnmtAXwfxQjIKkSVhTUrcywiZsdbQ/edit?usp=sharing) you can see an example of how to hand check an algorithm.
|
107
|
3. Execute the pseudocode by hand to capture logical errors. Try it with various *extreme* cases and write the results by hand. [Here](https://docs.google.com/a/upr.edu/presentation/d/1m-lXCUTKhCZC8vTnmtAXwfxQjIKkSVhTUrcywiZsdbQ/edit?usp=sharing) you can see an example of how to hand check an algorithm.
|
106
|
|
108
|
|
107
|
|
109
|
|
108
|
-###Exercise 3: Implement the code
|
|
|
|
|
110
|
+### Exercise 3 - Implement the Code
|
109
|
|
111
|
|
110
|
-#### Instructions:
|
|
|
|
|
112
|
+#### Instructions
|
111
|
|
113
|
|
112
|
1. Open the computer.
|
114
|
1. Open the computer.
|
113
|
|
115
|
|
114
|
2. Implement the algorithm in a C++ program.
|
116
|
2. Implement the algorithm in a C++ program.
|
115
|
|
117
|
|
116
|
-**Help**
|
|
|
|
|
118
|
+**Help:**
|
117
|
|
119
|
|
118
|
-1) Use the `int` type variable to keep track of the player. At the end of each turn, change the player like this:
|
|
|
|
|
120
|
+1) Use an `int` type variable to keep track of the player. At the end of each turn, change the player like this:
|
119
|
|
121
|
|
120
|
```cpp
|
122
|
```cpp
|
121
|
player = player == 1 ? 2 : 1;
|
123
|
player = player == 1 ? 2 : 1;
|
|
|
|
|
136
|
|
138
|
|
137
|
c. How is the loop control variable modified during each iteration?
|
139
|
c. How is the loop control variable modified during each iteration?
|
138
|
|
140
|
|
139
|
-###Deliverables:
|
|
|
|
|
141
|
+### Deliverables
|
140
|
|
142
|
|
141
|
-Use “Deliverables 1” in Moodle to hand in the pseudocode for the game of sticks.
|
|
|
|
|
143
|
+1. Use “Deliverables 1” in Moodle to turn in the pseudocode for the game of sticks.
|
142
|
|
144
|
|
143
|
-2. Use “Deliverables 2” in Moodle to hand in the paper where you and your partner executed the pseudocode by hand.
|
|
|
|
|
145
|
+2. Use “Deliverables 2” in Moodle to turn in the paper where you and your partner executed the pseudocode by hand.
|
144
|
|
146
|
|
145
|
-3. Use “Deliverables 3” in Moodle to hand in a `.cpp` file with the C++ program to play the game of sticks. The program should not have compiler errors, should be documented and function as intended. Remember to use good programming techniques, include the name of the programmers and document your program.
|
|
|
|
|
147
|
+3. Use “Deliverables 3” in Moodle to turn in a `.cpp` file with the C++ program to play the game of sticks. The program should not have compiler errors, should be documented and function as intended. Remember to use good programming techniques, include the name of the programmers, and document your program.
|
146
|
|
148
|
|
147
|
|
149
|
|
148
|
|
150
|
|