Computer science students code Solitaire card game for in-class lab
November 25, 2022
Advanced Placement Computer Science with Data Structures classes taught by upper school computer science teacher Anu Datar programmed the card game Solitaire to practice using the stack data structure from Oct. 21 to Nov. 18.
Students started off the lab by familiarizing themselves with Solitaire by playing the game with real cards. After understanding the rules better, the students coded the lab with pre-existing files and an instruction sheet with a step-by-step procedure. First, students set up the tableau and stock piles for the cards and created the deck. Then, they coded functions to select the cards and move them between piles. Students completed the basic structure of the game by implementing the foundation piles.
Usually, when players make moves in Solitaire, only the topmost card in a pile is considered, so students practiced using a stack. The stack, a last-in first-out data structure, efficiently maintains its topmost element. Like a stack of plates, the cards can only be added, removed and accessed from the top.
“We were [studying] stacks and queues, and it [allowed] us [to] practice with those pretty well,” AP Computer Science student Akul Goyal (10) said.
In addition to the basic structure, students implemented extra features, such as moving a card from the foundation to a pile. They could also choose to code more features into their game such as a timer or a scoring system. When students finished their projects, they ran a tester program provided by Datar to check the accuracy of their methods. In addition, students had to play through their game to fully test the functionality of their code. After finishing the writing and testing, students completed the lab by showing their code to Datar or a teaching assistant for review.
“It was a pretty fun lab because of the whole interface where you’re creating a game,” Akul said. “That makes it a lot more fun than previous labs.”