Chapter 6: Compound Data and Data Abstraction
Chapter 6: Compound Data and Data Abstraction
Recall What This Course is About
What is Abstraction?
Procedural Abstraction
Data Abstraction
Compound Data
Operations on Compound Data
Design Goal for Constructor and Selectors:
Abstract Data Types
Enforcing the Separation of Data Use and Representation
Data Abstraction Example: Programming a Game
An ADT for Nim
Data Abstraction in Nim
A Procedure to Play Nim
Quoted Expressions
play-with-turns: Flow of Control
play-with-turns: First Attempt
Overall Structure of Nim Program
Error Handling
play-with-turns: Flow of Control (Revisited 1)
Error Output
More Simple I/O
Procedure display-game-state
play-with-turns: Flow of Control (Revisited 2)
play-with-turns: Final Version
Revised Structure of Nim Program
Game Playing Strategies for the Computer
Removing Coins from a Pile
Revised Structure of Nim Program
Getting the Human's Move
Reading Values from the Keyboard
Validating Input
Ending the Game
Revised Structure of Nim Program
Data Abstraction Revisited
Representing the Game State ADT
Game State Representation 1
Constructor/Selector for Representation 1
Drawback of Representation 1
Game State Representation 2
Constructor/Selector for Representation 2
Drawbacks of Representation 2
Game State Representation 3
Comparing Representations 1-3
Data Abstraction Revisited Again
Game State Representation 4
cons, car, and cdr
Constructor/Selector for Representation 4
A Complete Nim Program
The Chocolate Bar Game
A Greedy Strategy for the Chocolate Bar Game
A Winning Strategy for the Chocolate Bar Game
The Chocolate Bar Game and Two-Pile Nim Are Equivalent
Three-Pile Nim
Visualizing Pairs
The Printed Representation of Conses
Constructor/Selector for Three-pile Nim
Other Changes Required for Three-pile Nim
Adding Strategies to the Nim Program