Fundamentals 3 min read

Building a Simple “Take Stones” Game in Python

This tutorial walks you through creating a basic “take stones” game in Python, explaining the game rules, required functions, use of random numbers and the retrying library, and the main loop that lets a user play against a computer opponent.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Building a Simple “Take Stones” Game in Python

The article introduces a simple "take stones" game where two players alternately remove 1‑3 stones from a pile, and the player who takes the last stone loses.

It shows the final animated result and then guides you to implement the game in Python.

Key functions are defined, including a random number generator ( random.randint ) to let the computer choose its move, and a retry mechanism using the retrying library ( @retry ) to repeatedly prompt for valid user input.

The main workflow starts by calling get_start_num to set the initial stone count, enters an infinite while 1: loop, and within each iteration calls cpu_number for the computer's move, judge to evaluate the game state, and user_number to obtain and validate the player's choice.

Implementation details include using random.randint to generate moves, handling edge cases when fewer than three stones remain, and raising exceptions to trigger retries for invalid inputs.

The tutorial highlights two main take‑aways: using random.randint for random moves and leveraging the retrying module to simplify input validation.

It notes that the current computer opponent selects moves randomly and encourages readers to improve the AI for smarter play.

PythonGame DevelopmentTutorialFundamentalsRandomretrying
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.