Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,16 @@
"control_flow_loops"
]
}
],
"concept": [
{
"slug": "pacman-rules",
"name": "Pacman Rules",
"uuid": "9f14f611-e783-49a5-904e-6f8f1c4d1f51",
"concepts": [],
"prerequisites": [],
"status": "wip"
}
]
},
"concepts": [
Expand Down
27 changes: 27 additions & 0 deletions exercises/concept/pacman-rules/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Hints

## General

- Don't worry about how the arguments are derived, just focus on combining the arguments to return the intended result.

## 1. Define if pac-man can eat a ghost

- The function must return a boolean value.
- You can use the logical operator and ([`&&`][logical operators]) to combine the arguments for a result.

## 2. Define if pac-man scores

- The function must return a boolean value.
- You can use the logical operator or ([`||`][logical operators]) to combine the arguments for a result.

## 3. Define if pac-man loses

- The function must return a boolean value.
- You can use the boolean operators [`&&`][logical operators] and [`!`][logical operators] to combine the arguments for a result.

## 4. Define if pac-man wins

- The function must return a boolean value.
- You can use the boolean operators [`&&`][logical operators] and [`!`][logical operators] to combine the arguments and results of one of the previously implemented functions.

[logical operators]: https://en.cppreference.com/w/c/language/operator_logical.html
43 changes: 43 additions & 0 deletions exercises/concept/pacman-rules/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Instructions

In this exercise, you need to translate some rules from the classic game Pac-Man into C functions.

You have four rules to translate, all related to the game states.

> Don't worry about how the arguments are derived, just focus on combining the arguments to return the intended result.

## 1. Define if Pac-Man eats a ghost

Define the `can_eat_ghost` function that takes two arguments (_if Pac-Man has a power pellet active_ and _if Pac-Man is touching a ghost_) and returns a boolean value if Pac-Man is able to eat the ghost. The function should return true only if Pac-Man has a power pellet active and is touching a ghost.

```c
can_eat_ghost(false, true);
// => false
```

## 2. Define if Pac-Man scores

Define the `scored` function that takes two arguments (_if Pac-Man is touching a power pellet_ and _if Pac-Man is touching a dot_) and returns a boolean value if Pac-Man scored. The function should return true if Pac-Man is touching a power pellet or a dot.

```c
scored(true, true);
// => true
```

## 3. Define if Pac-Man loses

Define the `lost` function that takes two arguments (_if Pac-Man has a power pellet active_ and _if Pac-Man is touching a ghost_) and returns a boolean value if Pac-Man loses. The function should return true if Pac-Man is touching a ghost and does not have a power pellet active.

```c
lost(false, true);
// => true
```

## 4. Define if Pac-Man wins

Define the `won` function that takes three arguments (_if Pac-Man has eaten all of the dots_, _if Pac-Man has a power pellet active_, and _if Pac-Man is touching a ghost_) and returns a boolean value if Pac-Man wins. The function should return true if Pac-Man has eaten all of the dots and has not lost based on the arguments defined in part 3.

```c
won(false, true, false);
// => false
```
38 changes: 38 additions & 0 deletions exercises/concept/pacman-rules/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Introduction

A Boolean expression is a question asked of one or more values which can be answered with `true` or `false`.
A true expression returns `1`; a false expression returns `0`.
Also, numbers have true/false values. Zero is `false`; non-zero is `true`.

Old C code will either use the `int` type directly for Boolean values, or the programmer may have defined constants such as `TRUE` and `FALSE` to map `1` and `0` to a Boolean representation.

With version C99 the `_Bool` type was added.
The `stdbool.h` header file defines three new Boolean symbols: `bool`; `true` and `false`.
`true` maps to `1`; `false` maps to `0`; and `bool` maps to `_Bool`.

So now, instead of directly using `int`s or rolling their own Boolean mappings, programmers can include `stdbool.h` and automatically get Boolean symbols in their code.

## Boolean Operators

C supports three boolean operators: `||` (Or), `&&` (And), and `!` (Not).

`!` is used with one operand.
It reverses the Boolean value of the operand.
For instance, `0` is `false`, but `!0` is `true`.

`||` and `&&` are used with two operands.
The operand to the right is only evaluated when the operand to the left does not already determine the result of the expression.

That is, `||` only evaluates its operand to the right when its operand to the left evaluates to `false`.
`&&` only evaluates its operand to the right when its operand to the left evaluates to `true`.

## Precedence

The three boolean operators each have different _operator precedence_.
As a consequence, they are evaluated in this order: `!` first, `&&` second, and finally `||`.
If you want to force a different ordering, you can enclose a boolean expression in parentheses (ie. `()`), as the parentheses have even higher operator precedence.

```c
!true && false // => false
!(true && false) // => true
```
22 changes: 22 additions & 0 deletions exercises/concept/pacman-rules/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"colinleach"
],
"files": {
"solution": [
"pacman_rules.c",
"pacman_rules.h"
],
"test": [
"test_pacman_rules.c"
],
"exemplar": [
".meta/exemplar.c"
]
},
"language_versions": "c99",
"forked_from": [
"cpp/pacman_rules"
],
"blurb": "In C, the boolean type enables working with logical values, allowing for boolean operations such as AND, OR, and NOT to evaluate and manipulate conditions."
}
19 changes: 19 additions & 0 deletions exercises/concept/pacman-rules/.meta/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Design

## Learning objectives

- Know of the existence of the `bool` type and its two values.
- Know about boolean operators and how to build logical expressions with them.
- Know of the boolean operator precedence rules.

## Out of scope

- Comparison operators such as `>` (will be covered in `conditionals`).

## Concepts

- `booleans`

## Prerequisites

- `basics`:
33 changes: 33 additions & 0 deletions exercises/concept/pacman-rules/.meta/exemplar.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "pacman_rules.h"

// eat_ghost returns a boolean value if Pac-Man is able to eat the ghost.
// The function should return true only if Pac-Man has a power pellet active
// and is touching a ghost.
bool can_eat_ghost(bool power_pellet_active, bool touching_ghost)
{
return power_pellet_active && touching_ghost;
}

// score returns a boolean value if Pac-Man scored.
// The function should return true if Pac-Man is touching a power pellet or a
// dot.
bool scored(bool touching_power_pellet, bool touching_dot)
{
return touching_power_pellet || touching_dot;
}

// lost returns a boolean value if Pac-Man loses.
// The function should return true if Pac-Man is touching a ghost and
// does not have a power pellet active.
bool lost(bool power_pellet_active, bool touching_ghost)
{
return touching_ghost && !power_pellet_active;
}

// won returns a boolean value if Pac-Man wins.
// The function should return true if Pac-Man
// has eaten all of the dots and has not lost
bool won(bool has_eaten_all_dots, bool power_pellet_active, bool touching_ghost)
{
return has_eaten_all_dots && !lost(power_pellet_active, touching_ghost);
}
37 changes: 37 additions & 0 deletions exercises/concept/pacman-rules/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
### If you wish to use extra libraries (math.h for instance),
### add their flags here (-lm in our case) in the "LIBS" variable.

LIBS = -lm

###
CFLAGS = -std=c99
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -pedantic
CFLAGS += -Werror
CFLAGS += -Wmissing-declarations
CFLAGS += -DUNITY_SUPPORT_64 -DUNITY_OUTPUT_COLOR

ASANFLAGS = -fsanitize=address
ASANFLAGS += -fno-common
ASANFLAGS += -fno-omit-frame-pointer

.PHONY: test
test: tests.out
@./tests.out

.PHONY: memcheck
memcheck: ./*.c ./*.h
@echo Compiling $@
@$(CC) $(ASANFLAGS) $(CFLAGS) test-framework/unity.c ./*.c -o memcheck.out $(LIBS)
@./memcheck.out
@echo "Memory check passed"

.PHONY: clean
clean:
rm -rf *.o *.out *.out.dSYM

tests.out: ./*.c ./*.h
@echo Compiling $@
@$(CC) $(CFLAGS) test-framework/unity.c ./*.c -o tests.out $(LIBS)
37 changes: 37 additions & 0 deletions exercises/concept/pacman-rules/pacman_rules.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "pacman_rules.h"

// eat_ghost returns a boolean value if Pac-Man is able to eat the ghost.
// The function should return true only if Pac-Man has a power pellet active
// and is touching a ghost.
bool can_eat_ghost(bool power_pellet_active, bool touching_ghost)
{
// TODO: Please implement the can_eat_ghost function
return false;
}

// score returns a boolean value if Pac-Man scored.
// The function should return true if Pac-Man is touching a power pellet or a
// dot.
bool scored(bool touching_power_pellet, bool touching_dot)
{
// TODO: Please implement the scored function
return false;
}

// lost returns a boolean value if Pac-Man loses.
// The function should return true if Pac-Man is touching a ghost and
// does not have a power pellet active.
bool lost(bool power_pellet_active, bool touching_ghost)
{
// TODO: Please implement the lost function
return false;
}

// won returns a boolean value if Pac-Man wins.
// The function should return true if Pac-Man
// has eaten all of the dots and has not lost
bool won(bool has_eaten_all_dots, bool power_pellet_active, bool touching_ghost)
{
// TODO: Please implement the won function
return false;
}
15 changes: 15 additions & 0 deletions exercises/concept/pacman-rules/pacman_rules.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef PACMAN_RULES_H
#define PACMAN_RULES_H

#include <stdbool.h>

bool can_eat_ghost(bool power_pellet_active, bool touching_ghost);

bool scored(bool touching_power_pellet, bool touching_dot);

bool lost(bool power_pellet_active, bool touching_ghost);

bool won(bool has_eaten_all_dots, bool power_pellet_active,
bool touching_ghost);

#endif
Loading
Loading