Artificial Intelligence in CS1
Brian C. Ladd
Department of Mathematics, Computer Science, and Statistics
St. Lawrence University
Guessing Game
- Context
- Overview
- Introducing the game
- Introducing the program
- Assignments
- Evaluation
- Summary
Context
- Institution
- St. Lawrence University
- CS140 (CS1) provides University distribution
- Required for 4+1 MBA program
- Course
- Java with objects-early rather than objects-first
- Combined lecture/lab
- Range of interest and ability
- Assignment
- Last third of course; previous assignments include
- Reading/writing text files
- Extending an existing object
Overview
- Introduction of game and program mechanics
- As introduced in the course
- Demonstrate the use of play and manipulatives
- A look at the assignment and student work
- An evaluation of the assignment
Introducing the game
- Almost everyone has played "Twenty Questions":
- One player thinks of an animal
- Other player asks yes/no questions in an effort to guess the animal
- How would a computer play the game?
- (Note that it can be played on the Web)
Introducing the program
- How does that program work?
Introducing the program
- How does that program work?
- Hard to extend (requires recompiling).
- Can we move the information out of the program itself?
- Many commercial computer games are data driven.
- The data is the set of questions, the set of guesses, and the
connections between them.
- How could the data for this program be stored inside the computer?
Introducing the program
- Imagine you are the computer.
- Given index cards and string, how could you represent the required
qantities?
- How would you represent a database?
- How would you play the game?
- How would you extend the data?
Assignments
- Given that introduction, what do the students do?
- (Group)Design classes to represent the index cards
- (Individual)Write non-learning GuessingGame
- play()
- yesOrNo()
- Build a database by hand
Student Solutions: Pseudocode
public void play() {
// set curr to beginning of the game
// while we haven't yet guessed
// show curr's text to user (prompt the user)
// get user input (need a variable)
// if its a question
// update curr
// else
// if answer is yes
// we win
// else
// we lose
} // play()
Student Solutions
public void play() {
GuessOrQuestion curr = root;
boolean done = false;
while (!done) {
// getUserInput
String userInput = yesOrNo(curr.getText());
// How do we know if curr is a Guess
if (curr.isQuestion())
if (userInput.equals("yes"))
curr = curr.getYes();
else
curr = curr.getNo();
else {
if (userInput.equals("yes"))
screen.putln("I guessed your animal!");
else
screen.putln("I was stumped!");
done = true;
}
}
} // play()
Student Work
- Handling inheritance (as a group), code was acceptable.
- Handling recursion was not as successful, even with pseudocode.
Student Work
/*readGuessOrQuestion
*purpose- reads a .txt file and uses it to play
*a guessing game.
*pre-there must be a valid text file open
*post-An object is returned is returned with
*the proper text.
*
*parameters- none
*return type- String, returns the text from the file.
*/
public GuessOrQuestion readGuessOrQuestion() {
// ...
}
Student Work
// ...
public GuessOrQuestion readGuessOrQuestion() {
String type = "", text = "";
type = myInputFile.getlnString();
if (type.equals("Question")) {
text = myInputFile.getlnString();
Question q = new Question(text);
q.setYes(readGuessOrQuestion());
q.setNo(readGuessOrQuestion());
root = q;
return root;
} else {
text = myInputFile.getlnString();
Guess g = new Guess(text);
root = g;
return root;
}
}
Assignments
- (Group)Extend the tree
- (Individual)Read the data
- (Individual)Save the data
Evaluation
- Engaging
- Ends justify the effort
- Accessible to all students
- Challenging only in respect to teaching material
- Material taught is at heart of problem
(From Feldman and Zelenski The Quest for Excellence in Designing
CS1/CS2 Assignments 1996)
Evaluation
- Engaging
- Ends justify the effort
- Accessible to all students
- Limited success: problem is easy to understand; explanation makes
data structure easy to understand; code is complex
- Challenging only in respect to teaching material
- Limited success: too much packed into one exercise
Evaluation
- Material taught is at heart of problem
- Inheritance - not a good example
- References - not strictly necessary
- Recursion - necessary to save tree as a tree
Alternative array representation can dispense with it
- Next time
- Eliminate inheritance (find new inheritance example)
- Move after array saving and loading
- Use array indices instead of references
- Eliminate recursion
Summary
- Guessing Game concepts can be understood by CS1 students
- Brings together file I/O, (and in new version) array loading,
traversing, and even extending
- Current assignment is a little too challenging
- Students enjoy making
a game that learns