Computer science students code Solitaire card game for in-class lab


Victor Gong

Aarav Borthakur (10) and Tanay Sharma (12) review the Solitaire lab from the AP Computer Science with Data Structures classes. The students completed the lab to better understand the implementation of the stack data structure.

by Andrew Liang, Assistant STEM Editor

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.

Varun Thvar (10) contemplates his next move in a game of Solitaire during upper school computer science teacher Anu Datar’s afterschool office hours on November 11th. In the past weeks, students taking AP Computer Science with Data Structures and Honors Data Structures have been working on a lab that involves recreating the card game with Java. (Victor Gong)

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.”