Tải bản đầy đủ (.pdf) (978 trang)

data structure for game programers - ron penton

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (19.61 MB, 978 trang )

Team LRN
Team LRN
Data Structures FM 11/5/02 9:28 AM Page i
Data
Structures
for Game
Programmers
Team LRN
This page intentionally left blank
Team LRN
Data Structures FM 11/5/02 9:28 AM Page iii
Data
Structures
Ron Penton
TM
for Game
Programmers
Team LRN
Data Structures FM 11/5/02 9:28 AM Page iv
© 2003 by Premier Press, a division of Course Technology. All rights reserved. No part of this book
may be reproduced or transmitted in any form or by any means, electronic or mechanical, including
photocopying, recording, or by any information storage or retrieval system without written permission
from Premier Press, except for the inclusion of brief quotations in a review.
The Premier Press logo and related trade dress are trademarks of Premier Press and may not
be used without written permission.
TM
Publisher: Stacy L. Hiquet
Marketing Manager: Heather Hurley
Acquisitions Editor: Emi Smith
Project Editor: Karen A. Gill
Technical Reviewer: André LaMothe


Copyeditor: Stephanie Koutek
Interior Layout: LJ Graphics, Susan Honeywell
Cover Design: Mike Tanamachi
Indexer: Kelly Talbot
Proofreader: Jenny Davidson
Microsoft, Windows, and Visual C++ are trademarks of Microsoft Corporation.Wolfenstein, Doom, and
Quake are trademarks of Id Software, Inc. Warcraft and Starcraft are trademarks of Blizzard
Entertainment.
The artwork used in this book is copyrighted by its respective owners, and you may not use it in your
own commercial works.
All other trademarks are the property of their respective owners.
Important: Premier Press cannot provide software support. Please contact the appropriate software
manufacturer’s technical support line or Web site for assistance.
Premier Press and the author have attempted throughout this book to distinguish proprietary trade-
marks from descriptive terms by following the capitalization style used by the manufacturer.
Information contained in this book has been obtained by Premier Press from sources believed to be
reliable. However, because of the possibility of human or mechanical error by our sources, Premier
Press, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any
information and is not responsible for any errors or omissions or the results obtained from use of
such information. Readers should be particularly aware of the fact that the Internet is an ever-chang-
ing entity. Some facts may have changed since this book went to press.
ISBN: 1-931841-94-2
Library of Congress Catalog Card Number: 2002111226
Printed in the United States of America
03 04 05 06 07 BH 10 9 8 7 6 5 4 3 2 1
Premier Press, a division of Course Technology
2645 Erie Avenue, Suite 41
Cincinnati, Ohio 45208
Team LRN
Data Structures FM 11/5/02 9:28 AM Page v

To my family, for always being there for me.
Team LRN
Data Structures FM 11/5/02 9:28 AM Page vi
Acknowledgments
I
would first like to thank my family for putting up with me for the past nine
months. Yes, yes, I’ll start cleaning the house now.
I would like to thank all of my friends at school: Jim, James, Dan, Scott, Kevin, and
Kelvin, for helping me get through all of those boring classes without falling asleep.
I would like to thank everyone at work for supporting me through this endeavor.
I especially want to thank Ernest Pazera, André LaMothe, and everyone else at
Premier Press for giving me this tremendous opportunity and believing in me.
I would like to thank Bruno Sousa for opening the door to writing for me.
I want to thank the pioneers of Gamedev.net, Kevin Hawkins and Dave Astle, for
paving the road for me and making a book such as this possible.
I would like to thank all of you in the #gamedev crew, specifically (in no particular
order) Trent Polack, Evan Pipho, April Gould, Joseph Fernald, Andrew Vehlies,
Andrew Nguyen, John Hattan, Ken Kinnison, Seth Robinson, Denis Lukianov, Sean
Kent, Nicholas Cooper, Ian Overgard, Greg Rosenblatt, Yannick Loitière, Henrik
Stuart, Chris Hargrove, Richard Benson, Mat Noguchi, and everyone else!
I would like to thank my artists, Steven Seator and Ari Feldman, who made this
book’s demos look so much better than they would have been.
And finally, I would like to thank the Pepsi Corporation, for making that wonderful
“stay awake” juice known as Mountain Dew.
Team LRN
Data Structures FM 11/5/02 9:28 AM Page vii
About the Author
Ron Penton’s lifelong dream has always been to be a game programmer. From the
age of 11, when his parents bought him his first game programming book on how
to make adventure games, he has always striven to learn the most about how games

work and how to create them.
Ron is currently finishing up his bachelor’s degree in computer science at the State
University of New York at Buffalo. He hopes to have a long career in game develop
-
ment.
Team LRN
Data Structures FM 11/5/02 9:28 AM Page viii
Contents at a Glance
Introduction . . . . . . . . . . . . . . . . xxxii
Part One
Concepts
. . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 1 Basic Algorithm Analysis . . . . . . . . . 3
Chapter 2 Templates . . . . . . . . . . . . . . . . . . . 13
Part Two
The Basics
. . . . . . . . . . . . . . . . . . . . . . 37
Chapter 3 Arrays. . . . . . . . . . . . . . . . . . . . . 39
Chapter 4 Bitvectors. . . . . . . . . . . . . . . . . . . 83
Chapter 5 Multi-Dimensional Arrays . . . . . . . . 107
Chapter 6 Linked Lists . . . . . . . . . . . . . . . . . 147
Chapter 7 Stacks and Queues . . . . . . . . . . . . 189
Chapter 8 Hash Tables . . . . . . . . . . . . . . . . . 217
Chapter 9 Tying It Together: The Basics. . . . . . 241
Part Three
Recursion and Trees
. . . . . . . . . . . . . . 315
Chapter 10 Recursion . . . . . . . . . . . . . . . . . . 317
Chapter 11 Trees . . . . . . . . . . . . . . . . . . . . . 329
Chapter 12 Binary Trees . . . . . . . . . . . . . . . . 359

Chapter 13 Binary Search Trees. . . . . . . . . . . 389
Chapter 14 Priority Queues and Heaps . . . . . . . 407
Team LRN
Data Structures FM 11/5/02 9:28 AM Page ix
ix
Contents at a Glance
Chapter 15 Game Trees and Minimax Trees . . . . . 431
Chapter 16 Tying It Together: Trees . . . . . . . . 463
Part Four
Graphs . . . . . . . . . . . . . . . . . . . . . . 477
Chapter 17 Graphs . . . . . . . . . . . . . . . . . . . . 479
Chapter 18 Using Graphs for AI: Finite State
Machines
. . . . . . . . . . . . . . . . . . . 529
Chapter 19 Tying It Together: Graphs
. . . . . . . 563
Part Five
Algorithms
. . . . . . . . . . . . . . . . . . . . 597
Chapter 20 Sorting Data . . . . . . . . . . . . . . . . 599
Chapter 21 Data Compression
. . . . . . . . . . . . . 645
Chapter 22 Random Numbers
. . . . . . . . . . . . . 697
Chapter 23 Pathfinding
. . . . . . . . . . . . . . . . . . 715
Chapter 24 Tying It Together: Algorithms
. . . . . 769
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . 793
Part Six

Appendixes
. . . . . . . . . . . . . . . . . . . . 799
Appendix A A C++ Primer . . . . . . . . . . . . . . . . 801
Appendix B The Memory Layout of a Computer
Program . . . . . . . . . . . . . . . . . . . 835
Appendix C Introduction to SDL. . . . . . . . . . . . 847
Appendix D Introduction to the Standard Template
Library. . . . . . . . . . . . . . . . . . . . 879
Index. . . . . . . . . . . . . . . . . . . . . . 901
Team LRN
Data Structures FM 11/5/02 9:28 AM Page x
Contents
Letter from the Series Editor . . . . . . . . xxx
Introduction. . . . . . . . . . . . . . . . . . . . xxxii
Part One
Concepts
. . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 1
Basic Algorithm Analysis . . . . . . . 3
A Quick Lesson on Algorithm Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Big-O Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Comparing the Various Complexities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Graphical Demonstration:Algorithm Complexity . . . . . . . . . . . . . . . . . . . . 10
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Chapter 2
Templates . . . . . . . . . . . . . . . . . 13
What Are Templates?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Template Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Doing It the Old Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Doing It with Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Template Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Multiple Parameterized Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Using Values as Template Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Using Values of a Specific Datatype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Using Values of Other Parameterized Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Team LRN
Data Structures FM 11/5/02 9:28 AM Page xi
xi
Contents
Problems with Templates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Visual C++ and Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Under the Hood. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Part Two
The Basics
. . . . . . . . . . . . . . . . . . . . . . 37
Chapter 3
Arrays . . . . . . . . . . . . . . . . . . 39
What Is an Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Graphical Demonstration:Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Increasing or Decreasing Array Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Inserting or Removing an Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Native C Arrays and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Static Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Dynamic Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
An Array Class and Useful Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
The Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
The Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
The Destructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
The Resize Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

The Access Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
The Conversion Operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Inserting an Item Between Two Existing Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Removing an Item from the Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
A Faster Removal Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Retrieving the Size of an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Example 3-3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Storing/Loading Arrays on Disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Writing an Array to Disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Reading an Array from Disk. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Considerations for Writing and Reading Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Team LRN
Data Structures FM 11/5/02 9:28 AM Page xii
xii
Contents
Application: Using Arrays to Store Game Data . . . . . . . . . . . . . . . . . . . . . . 71
The Monster Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Declaring a Monster Array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Adding a Monster to the Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Making a Better Insertion Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Removing a Monster from the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Checking for Monster Removal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Playing the Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Analysis of Arrays in Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Cache Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Resizing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Inserting/Removing Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Chapter 4
Bitvectors . . . . . . . . . . . . . . . . 83

What Is a Bitvector? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Graphical Demonstration: Bitvectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
The Main Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Using the Buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Creating a Bitvector Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
The Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
The Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
The Destructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
The Resize Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
The Access Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
The Set Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
The ClearAll Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The SetAll Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The WriteFile Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
The ReadFile Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Example 4-1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Team LRN
Data Structures FM 11/5/02 9:28 AM Page xiii
xiii
Contents
Application:The Quicksave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Creating a Player Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Storing the Players in the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Initializing the Data Structures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Modifying Player Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Saving the Player Array to Disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Playing the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Bitfields. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Declaring a Bitfield . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Using a Bitfield. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

Analysis of Bitvectors and Bitfields in Games . . . . . . . . . . . . . . . . . . . . . . . 105
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Chapter 5
Multi-Dimensional Arrays . . . . . . 107
What Is a Multi-Dimensional Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Graphical Demonstration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Native Multi-Dimensional Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Declaring a Multi-Dimensional Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Accessing a Multi-Dimensional Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Inside a Multi-Dimensional Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Dynamic Multi-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
The Array2D Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
The Array3D Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Application: Using 2D Arrays as Tilemaps . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Storing the Tilemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Generating the Tilemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Drawing the Tilemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Playing the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Application: Layered Tilemaps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Redefining the Tilemap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Reinitializing the Tilemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Modifying the Rendering Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Team LRN
Data Structures FM 11/5/02 9:28 AM Page xiv
xiv
Contents
Playing the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Comparing Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Comparing Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Analysis of Multi-Dimensional Arrays in Games. . . . . . . . . . . . . . . . . . . . . . . 144

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Chapter 6
Linked Lists . . . . . . . . . . . . . . . 147
What Is a Linked List? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Singly Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Graphical Demonstration: Singly Linked Lists. . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Example 6-4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Final Thoughts on Singly Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Doubly Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Graphical Demonstration: Doubly Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Creating a Doubly Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Doubly Linked List Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Reading and Writing Lists to Disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Writing a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Reading a Linked List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Application: Game Inventories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
The Player Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
The Item Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Adding an Item to the Inventory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Removing an Item from the Inventory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Playing the Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Application: Layered Tilemaps Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Declaring the Tilemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Creating the Tilemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Drawing the Tilemap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Analysis and Comparison of Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Algorithm Comparisons. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Team LRN
Data Structures FM 11/5/02 9:28 AM Page xv

xv
Contents
Size Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Real-World Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Chapter 7
Stacks and Queues . . . . . . . . . . 189
Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
What Is a Stack? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Graphical Demonstration: Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
The Stack Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Implementing a Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Application: Game Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Graphical Demonstration: Queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
The Queue Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Implementing a Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Application: Command Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Chapter 8
Hash Tables . . . . . . . . . . . . . . . 217
What Is Sparse Data? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
The Basic Hash Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Collisions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Hashing Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Enhancing the Hash Table Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Linear Overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Quadratic Overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Linked Overflow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Graphical Demonstration: Hash Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

Implementing a Hash Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Team LRN
Data Structures FM 11/5/02 9:28 AM Page xvi
xvi
Contents
The HashEntry Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
The HashTable Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Example 8-1: Using the Hash Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Application: Using Hash Tables to Store Resources . . . . . . . . . . . . . . . . . . 235
The String Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Using the Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
How the Demo Loads Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Playing the Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Chapter 9
Tying It Together: The Basics . . . 241
Why Classes Are Good . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Storing Data in a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Hiding Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Using the Classes in a Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Making a Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Adventure:Version One . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Game 2—The Map Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Part Three
Recursion and Trees
. . . . . . . . . . . . . . 315
Chapter 10
Recursion . . . . . . . . . . . . . . . . 317

What Is Recursion? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
A Simple Example: Powers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
The Towers of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
The Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Solving the Puzzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Team LRN
Data Structures FM 11/5/02 9:28 AM Page xvii
xvii
Contents
Solving the Puzzle with a Computer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323
Terminating Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Example 10-1: Coding the Algorithm for Real . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Graphical Demonstration:Towers of Hanoi. . . . . . . . . . . . . . . . . . . . . . . . . 327
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Chapter 11
Trees . . . . . . . . . . . . . . . . . . . 329
What Is a Tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
The Recursive Nature of Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Common Structure of Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Graphical Demonstration:Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Building the Tree Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
The Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
The Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
The Destructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
The Destroy Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
The Count Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
The Tree Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
The Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
The Basic Iterator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343

The Vertical Iterator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
The Horizontal Iterator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .346
The Other Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Building a Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Top Down . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Bottom Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Traversing a Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
The Preorder Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
The Postorder Traversal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Graphical Demonstration:Tree Traversals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Team LRN
Data Structures FM 11/5/02 9:28 AM Page xviii
xviii
Contents
Game Demo 11-1: Plotlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352
Using Trees to Store Plotlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Playing the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Chapter 12
Binary Trees . . . . . . . . . . . . . . 359
What Is a Binary Tree?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Fullness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Denseness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Balance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Structure of Binary Trees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Linked Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Arrayed Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Graphical Demonstration: Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Coding a Binary Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
The Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368

The Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
The Destructor and the Destroy Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
The Count Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Using the BinaryTree Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Traversing the Binary Tree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
The Preorder Traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
The Postorder Traversal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
The Inorder Traversal. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Graphical Demonstration: Binary Tree Traversals . . . . . . . . . . . . . . . . . . . . . . . . . 373
Application: Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
Arithmetic Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Parsing an Arithmetic Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Recursive Descent Parsing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Playing the Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388
Team LRN
Data Structures FM 11/5/02 9:28 AM Page xix
xix
Contents
Chapter 13
Binary Search Trees . . . . . . . . 389
What Is a BST? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Inserting Data into a BST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Finding Data in a BST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Removing Data from a BST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
The BST Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Sub-Optimal Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Graphical Demonstration: BSTs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Coding a BST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
The Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397

Comparison Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
The Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
The Destructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
The Insert Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
The Find Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Example 13-1: Using the BST Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Application: Storing Resources, Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . 402
The Resource Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
The Comparison Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Inserting Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Finding Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Playing the Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Chapter 14
Priority Queues and Heaps. . . . . 407
What Is a Priority Queue? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
What Is a Heap?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Why Can a Heap Be a Priority Queue? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Graphical Demonstration: Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Coding a Heap Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Team LRN
Data Structures FM 11/5/02 9:28 AM Page xx
xx
Contents
The Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
The Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
The Enqueue Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
The WalkUp Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
The Dequeue Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
The WalkDown Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422

Application: Building Queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
The Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Creating a Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
The Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Enqueuing a Unit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Starting Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Completing Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Playing the Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Chapter 15
Game Trees and Minimax Trees. . . 431
What Is a Game Tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
What Is a Minimax Tree? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Graphical Demonstration: Minimax Trees . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Game States. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
More Complex Games. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Application: Rock Piles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
The Game State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
The Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Generating the Game Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Simulating Play . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Playing the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
More Complex Games. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Never-Ending Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Huge Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Team LRN
Data Structures FM 11/5/02 9:28 AM Page xxi
xxi
Contents
Limited Depth Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Chapter 16
Tying It Together: Trees . . . . . . 463
Expanding the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Altering the Map Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Game Demo 16-1: Altering the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
The Map Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Further Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Part Four
Graphs
. . . . . . . . . . . . . . . . . . . . . . 477
Chapter 17
Graphs . . . . . . . . . . . . . . . . . . 479
What Is a Graph? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Linked Lists and Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Parts of a Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Types of Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
Bi-Directional Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Uni-Directional Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Weighted Graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Tilemaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Implementing a Graph. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Adjacency Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Direction Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
General-Purpose Linked Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Graphical Demonstration: Graphs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Graph Traversals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Team LRN

Data Structures FM 11/5/02 9:28 AM Page xxii
xxii
Contents
The Depth-First Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
The Breadth-First Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
A Final Word on Graph Traversals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499
Graphical Demonstration: Graph Traversals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
The Graph Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
The GraphArc Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501
The GraphNode Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
The Graph Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Application: Making a Direction-Table Dungeon . . . . . . . . . . . . . . . . . . . . . 512
The Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Creating the Map. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
Drawing the Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
Moving Around the Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
Playing the Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517
Application: Portal Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
Sectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Determining Sector Visibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
Coding the Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
Playing the Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528
Chapter 18
Using Graphs for AI: Finite State
Machines
. . . . . . . . . . . . . . . . 529
What Is a Finite State Machine? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 530
Complex Finite State Machines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 533
Implementing a Finite State Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 535

Graphical Demonstration: Finite State Machines . . . . . . . . . . . . . . . . . . . . 537
Even More Complex Finite State Machines . . . . . . . . . . . . . . . . . . . . . . . . 538
Multiplying States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
Conditional Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
Representing Conditional Event Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
Team LRN
Data Structures FM 11/5/02 9:28 AM Page xxiii
xxiii
Contents
Graphical Demonstration: Conditional Events . . . . . . . . . . . . . . . . . . . . . . 546
Game Demo 18-1: Intruder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
Playing the Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560
Chapter 19
Tying It Together: Graphs . . . . . 563
The New Map Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 564
The New Room Entry Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 565
The File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Game Demonstration 19-1:Adding the New Map Format . . . . . . . . . . . . . 567
The DirectionMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 568
Changes to the Game Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
Playing the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
Converting Old Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
The Directionmap Map Editor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
The Initial Map. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
Setting and Clearing Tiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
Loading a Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588
Saving a Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Using the Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 593

Upgrading the Tilemap Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
The Save Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
The Load Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596
Team LRN

×