You are previewing Realm of Racket.
1. Dedication
2. Acknowledgments
3. Preface (Hello World)
4. Introduction (Open Paren)
5. 1. (Getting Started)
6. 2. (A First Racket Program)
1. 2.1 The Guess My Number Game
2. 2.2 Defining Variables
3. 2.3 Basic Racket Etiquette
4. 2.4 Defining Functions in Racket
5. Resume&#8212;Chapter Checkpoint—Chapter Checkpoint
7. 3. (Basics of Racket)
1. 3.1 Syntax and Semantics
2. 3.2 The Building Blocks of Racket Syntax
3. 3.3 The Building Blocks of Racket Semantics
4. 3.4 Lists in Racket
5. 3.5 Structures in Racket
6. Interrupt&#8212;Chapter Checkpoint—Chapter Checkpoint
8. 4. (Conditions and Decisions)
2. 4.2 The Conditionals: IF and Beyond
3. 4.3 Cool Tricks with Conditionals
4. 4.4 Equality Predicates, Once More
5. 4.5 Comparing and Testing
6. Call-with-current-continuation&#8212;Chapter Checkpoint—Chapter Checkpoint
9. 4½. (define define ’define)
1. 4½.1 Module-Level Definitions
2. 4½.2 Local Definitions
3. Abort&#8212;Chapter Checkpoint—Chapter Checkpoint
10. 5. (big-bang)
1. 5.1 Graphical User Interface
2. 5.2 Landing a UFO
3. 5.3 Using big-bang: Syntax and Semantics
4. 5.4 Guessing Gooey
5. Exit&#8212;Chapter Checkpoint—Chapter Checkpoint
6. Chapter Challenges
11. 6. (Recursion Is Easy)
1. 6.1 Robot Snake
2. 6.2 A Data Representation for the Snake Game
3. 6.3 The Main Function
4. 6.4 Clock Ticks
5. 6.5 Key-Events
6. 6.6 Rendering
7. 6.7 End Game
8. 6.8 Auxiliary Functions
9. Return&#8212;Chapter Checkpoint—Chapter Checkpoint
10. Chapter Challenges
12. 7. (Land of Lambda)
13. 8. (Mutant Structs)
14. 9. (The Values of Loops)
15. 10. (Dice of Doom)
1. 10.1 The Game Tree
2. 10.2 Dice of Doom, The Game
3. 10.3 Designing Dice of Doom: Take One
4. 10.4 How Game Trees Work
5. 10.5 Game States and Game Trees for Dice of Doom
6. 10.6 Roll the Dice
7. 10.7 Rendering the Dice World
8. 10.8 Input Handling
9. 10.9 Creating a Game Tree
10. 10.10 The End Game
11. Kill&#8212;Chapter Checkpoint—Chapter Checkpoint
12. Chapter Challenges
16. 11. (Power to the Lazy)
17. 12. (Artificial Intelligence)
18. 13. (The World Is Not Enough)
1. 13.1 What Is a Distributed Game?
2. 13.2 The Data
3. 13.3 The Network Postal Service
5. 13.5 Distributed Guess
6. Error&#8212;Chapter Checkpoint—Chapter Checkpoint
7. Chapter Challenges
19. 14. (Hungry Henry)
1. 14.1 King Henry the Hungry
2. 14.2 Hungry Henry, the Game
3. 14.3 Two United States
4. 14.4 Henry’s Universe
5. 14.5 State of the Union
6. 14.6 Main, Take Client
7. 14.7 Main, Take Server
8. 14.8 See Henry Run
9. On-disconnect&#8212;Chapter Checkpoint—Chapter Checkpoint
10. Chapter Challenges
20. Conclusion. Good-Bye (Close Paren)
21. Index

## Chapter 2. (A First Racket Program)

#|

You’ve installed DrRacket, learned that programs go into the definitions panel, and experimented with Racket in the interactions panel. You are now ready to write your first real program, a simple game for guessing numbers.

|#

## 2.1 The Guess My Number Game

The first game we will write is one of the simplest and oldest games around. It’s the classic Guess My Number game. In this game, the player thinks of a number between `1` and `100`. Our program will then figure out that number by repeatedly making guesses and asking the player if her number is bigger or smaller than the current guess.

The following piece shows what a game might look like in the interactions panel if the player chose `18`:

`> (guess) 50 > (smaller) 25 ...`