Luis Albertorio преди 8 години
родител
ревизия
7d74997863
променени са 1 файла, в които са добавени 215 реда и са изтрити 0 реда
  1. 215
    0
      README-en.md

+ 215
- 0
README-en.md Целия файл

@@ -0,0 +1,215 @@
1
+#Repetition Structures - Counting Squares
2
+
3
+![main1.png](images/main1.png)
4
+![main2.png](images/main2.png)
5
+![main3.png](images/main3.png)
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 referred to as *repetition structures*. 
8
+
9
+Algorithms are one of the fundamental concepts in Computer Science. Given a small set of instructions and the basic programming structures, we can solve many problems. In this laboratory experience you will practice the creation of algorithms simulating a robot that must explore a space using a set of very limited instructions.
10
+
11
+##Objectives:
12
+
13
+1. Design algorithms using sequential, decision and repetition structures.
14
+2. Analyze the number of steps of the proposed algorithms and try to minimize them.
15
+3. Practice invoking the methods of an object.
16
+
17
+This laboratory experience is an adaptation of http://nifty.stanford.edu/2015/tychonievich-sherriff-layer-counting-squares/ .
18
+
19
+
20
+##Pre-Lab:
21
+
22
+Before arriving to the laboratory you should have:
23
+
24
+1. Reviewed the basic decision and repetition structures for C++.
25
+2. Reviewed the creation of objects and how to invoke their methods.
26
+3. Studied the concepts and instructions for the laboratory session.
27
+
28
+
29
+---
30
+
31
+---
32
+
33
+
34
+## Counting Squares
35
+
36
+In this laboratory experience we will be programming a robot that has been placed in a grid of square rooms. Each one of the four walls in each room can have a door. Only the walls of adjoining rooms have doors. The walls that make up the grid's exterior do not have doors.
37
+
38
+---
39
+
40
+![](images/cuarto.png)
41
+
42
+**Figure 1.** The robot is in the room on the upper left of the 36 room grid.
43
+
44
+---
45
+
46
+The only commands the robot understands are:
47
+
48
+1. Verify if there is a door on the wall to the north (N), south (S), east (E), or west (W) of the room where it currently is.
49
+2. Move to the room that is located towards the north (N), south (S), east (E), or west (W) of the current room.
50
+3. Create variables and assign them values.
51
+4. Carry out basic operations for sum, subtraction, multiplication and division.
52
+5. Use decision and repetition structures.
53
+6. Display the results on the screen.
54
+
55
+
56
+## Objects and methods
57
+
58
+The following is the `main` function of a basic program like the one you will be creating. During the laboratory experience you only need to program the `main` function (within the `main.cpp` file). The rest of the files contain functions that implement the functionality of the instructions the robot understands.
59
+
60
+---
61
+
62
+```cpp
63
+int main(int argc, char *argv[]) {
64
+    QApplication   a(argc, argv);
65
+
66
+    // Create the grid and the robot
67
+    MainGameWindow *w = new MainGameWindow(Mode::RECT_RANDOM);
68
+    w->show();
69
+
70
+    // Show the word "Start"
71
+    w->display("Start");
72
+
73
+    // Move the robot towards the west while possible,
74
+    // counting the number of steps the robot moves
75
+
76
+    int ctr = 0;
77
+    while (w->canMove('W')) {
78
+        w->moveRobot('W');
79
+        ctr = ctr + 1;
80
+    }
81
+
82
+    // Display the total steps taken by the robot
83
+    w->display("Total: " + QString::number(ctr));
84
+
85
+    return a.exec();
86
+}
87
+
88
+```
89
+
90
+**Figure 2** Example of the `main` function.
91
+
92
+---
93
+
94
+In the example we are creating a robot within the grid that only knows how to check if there is a door towards the west and (if there is one), walk towards that direction. Let us see the function, line by line.
95
+
96
+The first line creates the only object you should create, an object of the `MainGameWindow` class. The parameter `Mode::SQUARE_TOP_LEFT` specifies that the grid will be square and that the robot will start in the upper left corner. Other options for the parameter are `RECT_TOP_LEFT`, `RECT_RANDOM` and `PYRAMID_RANDOM`.
97
+
98
+```cpp
99
+MainGameWindow *w = new MainGameWindow(Mode::SQUARE_TOP_LEFT);
100
+```
101
+
102
+The following shows the object `w`.
103
+
104
+```cpp
105
+w->show();
106
+```
107
+
108
+The `void display(QString)` method displays short messages on the screen. For example:
109
+
110
+```cpp
111
+w->display("Start");
112
+```
113
+
114
+shows the word "Start" before the robot starts to move. The code that follows:
115
+
116
+```cpp
117
+int ctr = 0;
118
+while ( w->canMove('W') ) {
119
+    w->moveRobot('W');
120
+    ctr = ctr + 1;
121
+}
122
+```
123
+
124
+illustrates the use of the `bool canMove(char)` and `void moveRobot(char)` methods:
125
+
126
+* `bool canMove(char)` - accepts as a parameter one of the following letters: `'N'`, `'S'`, `'E'` or `'W'` and returns `true` if a door exists in that direction from the room where the robot is currently found.
127
+* `void moveRobot(char)` - accepts as a parameter one of the letters `'N'`, `'S'`, `'E'` or `'W'` and moves the robot to the next room that is found in that direction.
128
+
129
+In the example, the code is trying to move the robot as many times as it can towards the west (`W`), and counting the number of rooms in that direction.
130
+
131
+---
132
+
133
+---
134
+
135
+
136
+## Laboratory Session
137
+
138
+### Exercise 1 - Square grid
139
+
140
+Suppose that the robot is currently in the upper left room (northwest) of a **square** space of rooms, i.e. the space contains the same number of rows and columns of rooms (like the one in Figure 1). Design an algorithm that allows the robot to compute the number of rooms that there are in the grid.
141
+
142
+**Instructions**
143
+
144
+1. Download the folder `Repetitions-CountingSquares` from `Bitbucket` by using the terminal, moving to the directory `Documents/eip`, and writing the command `git clone http://bitbucket.org/eip-uprrp/repetitions-countingsquares`.
145
+
146
+2. Load the `CountingSquares` project to Qt creator by double clicking on the `CountingSquares.pro` file that can be found in the `Documents/eip/Repetitions-CountingSquares` folder of your computer.
147
+
148
+3. Configure the project. The project consists of various files. **You will only write code in the file** `main.cpp`. The rest of the files contain functions that implement the functionality of the instructions the robot can understand.
149
+
150
+4. When writing your algorithm, you should make sure that the `MainGameWindow` object is created using the argument `Mode::SQUARE_TOP_LEFT`. Remember, the robot does not know beforehand how many rooms there are. Test your algorithm with some examples.
151
+
152
+5. If the size of the grid is 3x3, how many rooms should the robot visit to complete your algorithm? How about 4x4? How about $$n \times n$$ rooms?
153
+
154
+6. Suppose we want to minimize the amount of energy used by the robot. Can you create an algorithm that uses less steps for the same grid size?
155
+
156
+7. Once you have finished your algorithm, and made it correct and efficient, hand it in using Delivererable 1 in Moodle. On the algorithm's header, write and explain the expression you found about the number of rooms the robot should visit to complete its task for a grid of size $$n \times n$$ (For example, "The robot takes 2x+5 steps, 5 to arrive at the middle and 2n to count the rest").
157
+
158
+
159
+### Exercise 2 -  Rectangular grid
160
+
161
+**Instructions**
162
+
163
+1. Suppose that now the robot is in the upper left room (northwest) of a **rectangular** space (not necessarily square) of rooms. Design an algorithm so the robot can compute the number of rooms in the grid.
164
+
165
+2. To test this part of the program you should make sure the `MainGameWindow` object is created using the argument `Mode::RECT_TOP_LEFT`.
166
+
167
+3. Once you have finished your algorithm, and made it correct and efficient, implement it in the `main` function. In the header of the program, write and explain the expression you found about the number of rooms the robot should visit to complete its task for a grid of size  $$m \times n$$. 
168
+
169
+4. Hand in the `main.cpp` file with the code to calculate the number of rooms in a rectangular grid using Deliverable 2 in Moodle.
170
+
171
+### Exercise 3 - Rectangular grid, random position
172
+
173
+**Instrucciones**
174
+
175
+1. Suppose that now the robot starts its task in any of the rooms in a **rectangular** grid (not necessarily square). Design an algorithm so the robot can compute the number of rooms there are in the grid.
176
+
177
+2. To test this part of the program you should make sure the `MainGameWindow` object is created using the argument `Mode::RECT_RANDOM`.
178
+
179
+3. Once you have finished your algorithm, and made it correct and efficient, implement it in the `main` function. In the header of the program, write and explain the expression you found about the number of rooms the robot should visit to complete its task for a grid of size  $$m \times n$$. In this case, the number of rooms to visit will depend on the robot's initial position, so express the worst case, i.e. according to your algorithm, how many rooms should the robot visit if it starts in the *worst* room.
180
+
181
+4. Hand in the `main.cpp` file with the code to calculate the number of rooms in a rectangular grid with the robot in a random position using Deliverable 3 in Moodle.
182
+
183
+
184
+
185
+### Exercise 4 - Pyramid grid, random position
186
+
187
+**Instructions**
188
+
189
+1. Suppose that now the robot starts its task in any of the rooms in a grid shaped like a pyramid. Design an algorithm so the robot can compute the number of rooms there are in the grid.
190
+
191
+2. To test this part of the program you should make sure the `MainGameWindow` object is created using the argument `Mode::PYRAMID_RANDOM`.
192
+
193
+3. Once you have finished your algorithm, and made it correct and efficient, implement it in the `main` function. In the header of the program, write and explain the expression you found about the number of rooms the robot should visit to complete its task for a grid of size  $$m \times n$$. In this case, the number of rooms to visit will depend on the robot's initial position, so express the worst case, i.e. according to your algorithm, how many rooms should the robot visit if it starts in the *worst* room.
194
+
195
+4. Hand in the `main.cpp` file with the code to calculate the number of rooms in the pyramid with the robot in a random position using Deliverable 4 in Moodle.
196
+
197
+
198
+
199
+---
200
+
201
+---
202
+
203
+## Deliverables
204
+
205
+Use the "Deliverable" links in Moodle to hand in the algorithm for Exercise 1 and the `main.cpp` files that contain the code you implemented in exercises 2, 3 and 4. Remember to use good programming techniques, to include the names of the programmers involved, and to document your program.
206
+
207
+
208
+
209
+---
210
+
211
+---
212
+
213
+## References
214
+
215
+[1] Luther A. Tychonievich, Mark S. Sherriff, and Ryan M. Layer, http://nifty.stanford.edu/2015/tychonievich-sherriff-layer-counting-squares/