Skip to content

Commit d9542a4

Browse files
committed
Merge branch 'main' of https://github.com/splashkit/the-programmers-field-guide into raspberry_gpio
2 parents ccaacd0 + 375f061 commit d9542a4

File tree

26 files changed

+560
-224
lines changed

26 files changed

+560
-224
lines changed

resources/code-examples/part-2/6-deep-dive-memory/linked-list.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <stdlib.h>
22
#include <stdio.h>
3+
#include <new>
34

45
/**
56
* A node is a struct that contains a pointer to the next node,
@@ -39,8 +40,8 @@ template <typename T>
3940
linked_list<T> *new_linked_list()
4041
{
4142
linked_list<T> *list = (linked_list<T> *) malloc(sizeof(linked_list<T>));
42-
list->first = NULL;
43-
list->last = NULL;
43+
list->first = nullptr;
44+
list->last = nullptr;
4445
return list;
4546
}
4647

@@ -54,17 +55,18 @@ template <typename T>
5455
void delete_linked_list(linked_list<T> *list)
5556
{
5657
node<T> *current = list->first;
57-
while (current != NULL)
58+
while (current != nullptr)
5859
{
5960
node<T> *next = current->next;
6061
// Clear data from node
61-
current->next = NULL;
62+
current->next = nullptr;
63+
current->data.~T(); // Call destructor
6264
free(current);
6365
current = next;
6466
}
6567
// Clear old data from list
66-
list->first = NULL;
67-
list->last = NULL;
68+
list->first = nullptr;
69+
list->last = nullptr;
6870
free(list);
6971
}
7072

@@ -79,9 +81,10 @@ template <typename T>
7981
void add_node(linked_list<T> *list, T data)
8082
{
8183
node<T> *new_node = (node<T> *) malloc(sizeof(node<T>));
84+
new(&new_node->data) T; // Placement new to construct the data in the node
8285
new_node->data = data;
83-
new_node->next = NULL;
84-
if (list->first == NULL)
86+
new_node->next = nullptr;
87+
if (list->first == nullptr)
8588
{
8689
list->first = new_node;
8790
list->last = new_node;
@@ -103,7 +106,7 @@ void add_node(linked_list<T> *list, T data)
103106
template <typename T>
104107
void remove_node(linked_list<T> *list, node<T> *node_to_remove)
105108
{
106-
node<T> *previous = NULL;
109+
node<T> *previous = nullptr;
107110

108111
// Removing the first node
109112
if (list->first == node_to_remove
@@ -118,14 +121,14 @@ void remove_node(linked_list<T> *list, node<T> *node_to_remove)
118121
node<T> *current = list->first;
119122

120123
// Iterate through the list until we find the node to remove
121-
while (current != node_to_remove && current != NULL)
124+
while (current != node_to_remove && current != nullptr)
122125
{
123126
previous = current;
124127
current = current->next;
125128
}
126129

127130
// Check if the node to remove was found
128-
if (current == NULL)
131+
if (current == nullptr)
129132
{
130133
return;
131134
}
@@ -141,7 +144,8 @@ void remove_node(linked_list<T> *list, node<T> *node_to_remove)
141144
}
142145

143146
// Clear data from node
144-
node_to_remove->next = NULL;
147+
node_to_remove->next = nullptr;
148+
node_to_remove->data.~T(); // Call destructor
145149
free(node_to_remove);
146150
}
147151

@@ -154,7 +158,7 @@ int main()
154158
add_node(list, 4);
155159

156160
node<int> *current = list->first;
157-
while (current != NULL)
161+
while (current != nullptr)
158162
{
159163
printf("%d->", current->data);
160164
current = current->next;
@@ -164,7 +168,7 @@ int main()
164168
remove_node(list, list->first->next->next);
165169

166170
current = list->first;
167-
while (current != NULL)
171+
while (current != nullptr)
168172
{
169173
printf("%d->", current->data);
170174
current = current->next;
23.1 KB
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: Installing Tools
3+
---
4+
5+
Learning to program will require you to put into practice the things you are reading. To do this, you need to install a few things.
6+
7+
- **Code Editor** - in which you will write your code.
8+
- **Compiler** - used to convert your code into machine code.
9+
- **Libraries** - packages of existing code you can build on top of.
10+
11+
Jump over to the [installation guides](/book/appendix/0-installation/0-overview) to install the tools you need. These guides will step you through installing these tools on your computer. You will get to use these in the next chapter.

src/content/docs/book/part-0-getting-started/2-computer-use/3-explore/3-explore.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
title: Activities
33
---
44

5+
## Install software development tools
6+
7+
The most important first step in getting ready is to install the compilers, libraries, and code editor we will use throughout the next chapters. Jump over to the [installation guides](/book/appendix/0-installation/0-overview) and follow the steps to install the tools you will need.
8+
9+
## Organise your files
10+
511
You will build a number of programs and small projects as you progress through the journey in this book. To help yourself, we recommend organising your file system in a way that will make it easy to find these projects from the terminal.
612

713
Think about how you will organise your projects, and where on your computer's file system you will store them.

src/content/docs/book/part-0-getting-started/3-building-programs/3-explore.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
title: Go Exploring
33
---
44

5+
:::tip
6+
Use the [installation guides](/book/appendix/0-installation/0-overview) to install the tools you need.
7+
:::
8+
59
Here is a fun hello world program to wrap up this section. You can also look through the guide and fine other sample code you can download and run now. This is a great way to explore the ideas you are learning.
610

711
The code for this part will be using SplashKit functionality to draw images, play sound effects and draw text with a specific font.

src/content/docs/book/part-2-organised-code/2-organising-code/3-explore/3-0-test-your-knowledge.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ Having explore how we can organise code to help make the code in your programs e
66

77
- [AFL game data](/book/part-2-organised-code/2-organising-code/3-explore/3-1-afl-score) - create functions and procedures to calculate AFL scores and game details.
88
- [Click game](/book/part-2-organised-code/2-organising-code/3-explore/3-2-targets) - create functions and procedures for a small target hitting game.
9+
10+
:::tip
11+
12+
Remember to focus on organising your code in appropriate functions and procedures, using parameters to share data as needed. Make sure your code isn't just all done in main.
13+
14+
:::

src/content/docs/book/part-2-organised-code/2-organising-code/3-explore/3-1-afl-score.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A game in the Australian Football League consists of two teams aiming to achieve
88

99
Build a program that allows the user to enter details of an AFL game, output scores and winners, and provide a menu for the user to update game details and output scores.
1010

11-
An example of this running is shown below.
11+
An example of this running is shown below:
1212

1313
```txt
1414
Welcome to the AFL score calculator!
@@ -80,13 +80,13 @@ Are you sure you want to quit? [Y/n]: y
8080
Bye!
8181
```
8282

83-
:::caution
83+
:::tip[Hints:]
8484

85-
Make sure you are using this to demonstrate how to build this using functions and procedures. Here are some functions and procedures you may want to consider adding:
85+
Here are some functions and procedures you may want to consider adding:
8686

87-
- Output details (taking in the two team names and scores)
88-
- Functions to read and validate data from the user (strings, numbers, boolean)
89-
- Print menu
87+
- Functions to [read and validate data from the user](/book/part-2-organised-code/2-organising-code/1-tour/00-2-explore-functions) (strings, numbers, boolean).
88+
- A procedure to output calculated details of each team (taking in the names and score details of the two teams).
89+
- A procedure to print the menu (taking in the names of the two teams).
9090
- A function to calculate a score from a number of goals and behinds.
9191
- A function to calculate and return the name of the winner, from the two team names and scores.
9292

src/content/docs/book/part-2-organised-code/2-organising-code/3-explore/3-2-targets.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ sidebar:
44
label: " - Click Game"
55
---
66

7-
For this activity, create a program that lets the user enters a number of targets to hit. Then, show a window that displays the number of targets left to hit, using a health-bar style graphic (a partially filled bar, based on proportion of initial target remaining). Draw a circle on the screen, and test if the user has clicked the circle. When they do click it, subtract one from the targets to be hit, ending when this is 0.
7+
For this activity, create a program that lets the user enter a number of targets to hit. Then, show a window that displays the number of targets left to hit, using a health-bar style graphic (a partially filled bar, based on proportion of initial target remaining). Draw a circle on the screen, and test if the user has clicked the circle. When they do click it, subtract one from the targets to be hit, ending when this is 0.
88

99
```txt
1010
Welcome to target click.
@@ -22,20 +22,20 @@ Click 10 targets to end the game.
2222
// end when there are no targets left to hit
2323
```
2424

25-
:::caution
25+
:::tip[Hints:]
2626

27-
Make sure you are using this to demonstrate how to build this using functions and procedures. Here are some functions and procedures you may want to consider adding:
27+
Here are some functions and procedures you may want to consider adding:
2828

29-
- Functions to read and validate data from the user (numbers)
30-
- A procedure to play the game
29+
- Functions to [read and validate data from the user](/book/part-2-organised-code/2-organising-code/1-tour/00-2-explore-functions) (numbers)
30+
- A procedure to play the game *(optional)*
3131
- A procedure to draw the game, that uses a procedure to draw the target bar
3232
- A procedure to draw the target bar - using parameters for position and proportion of bar to be filled. This can be drawn with two rectangles - using the proportion to be filled for one dimension of the second rectangle.
33-
- Function (target hit) to check if the user has clicked, and the mouse is inside the target circle.
33+
- A function (target hit) to check if the user has clicked, and the mouse is inside the target circle.
3434

3535
:::
3636

37-
:::tip
37+
:::note[Useful SplashKit functions:]
3838

39-
You can use the [point in circle](https://splashkit.io/api/geometry/#point-in-circle-with-values) function to test if the mouse is in the circle. See [mouse x](https://splashkit.io/api/input/#mouse-x) and [mouse y](https://splashkit.io/api/input/#mouse-y) to get the location of the mouse.
39+
You can use the [Point In Circle](https://splashkit.io/api/geometry/#point-in-circle-with-values) function to test if the mouse is in the circle. See [Mouse X](https://splashkit.io/api/input/#mouse-x) and [Mouse Y](https://splashkit.io/api/input/#mouse-y) to get the location of the mouse.
4040

4141
:::

src/content/docs/book/part-2-organised-code/3-structuring-data/3-explore/3-0-test-knowledge.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ At this point in your journey, you have the tools to build more interesting prog
77
- [Entity manager](/book/part-2-organised-code/3-structuring-data/3-explore/3-1-entity) - create a simple data entry system that allows you to see and edit details of a single record.
88
- [Explore game states](/book/part-2-organised-code/3-structuring-data/3-explore/3-2-game) - explore creating a game menu and other states.
99
- [Potentiometer->Servo Control](/book/part-2-organised-code/3-structuring-data/3-explore/3-3-servo-potentiometer/) - explore creating an application to control a servo state using a potentiometer and map the values.
10+
11+
:::tip
12+
13+
For this you want to show how to create and use structs and enums, while also continuing to show how to organise your code in functions and procedures. You want to see these new data types working in conjuncture with the other programming features you have already mastered.
14+
15+
:::

src/content/docs/book/part-2-organised-code/3-structuring-data/3-explore/3-1-entity.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ sidebar:
66

77
Create a program that lets the user enter and display details associated with a book. This should include your own struct to manage the book data, with a function to read in the book, and a procedure to output the details of the book. Also show a simple menu, with an associated enumeration. Use the enumeration in a read menu option function.
88

9-
An example of this running is shown below.
9+
An example of this running is shown below:
1010

1111
```txt
12-
Book entry system.
12+
Book entry system:
1313
1414
Enter the name of the book: Programmers Guide
1515
Enter book location: https://programmers.guide
@@ -33,10 +33,10 @@ Menu:
3333
Option: 3
3434
```
3535

36-
:::caution
36+
:::tip[Hints:]
3737

38-
Make sure you have a struct for your book data. Use this in a function that returns a book, and a procedure that accepts a book as a parameter.
38+
Make sure you have a [struct](/book/part-2-organised-code/3-structuring-data/2-trailside/03-01-struct) for your book data. Use this in a function that returns a book, and a procedure that accepts a book as a parameter.
3939

40-
Also have an enumeration for the menu options, and a function that returns one of the enum values based on what the user chose (read menu option).
40+
You should also have an [enumeration](/book/part-2-organised-code/3-structuring-data/2-trailside/03-02-enum) for the menu options, and a function for [reading data from the user](/book/part-2-organised-code/3-structuring-data/1-tour/0-1-explore-enums/#reading-data-from-the-user) that returns one of the enum values based on what the user chose (read menu option).
4141

4242
:::

0 commit comments

Comments
 (0)