---
title: "console-snake"
year: "2019"
motto: "Console Snake Game"
description: "Windows console snake video-game."
stack: ["cpp"]
website: "https://github.com/RolandoAndrade/console-snake"
url: "https://rolandoandrade.me/projects/console-snake/"
author: "Rolando Andrade"
---

# console-snake

> Console Snake Game

Windows console snake video-game.

Year: 2019. Stack: cpp. Website: https://github.com/RolandoAndrade/console-snake

Repositories:

- [github](https://github.com/RolandoAndrade/console-snake)
- [gitlab](https://gitlab.com/RolandoAndrade/console-snake)

In 2019, I was studying linked lists in my computer science courses. I read the theory, understood it, and knew I would forget it a week later. Textbook exercises gave me nothing I could see or use.

So I skipped the exercises and built something with the data structure instead. I made a Snake game that runs in the Windows console. It uses no graphics libraries and no game engine. It is only C++, the Windows API and a linked list that holds the snake's body.

## Snake made of asterisks

The game is classic Snake in a text screen. You steer a snake around a 50x50 grid. Eating food makes the snake longer. Hitting a wall or your own tail ends the game.

The snake is drawn with asterisks. Food appears at random positions. The arrow keys change direction. The game keeps a steady pace with Windows sleep functions.

It is simple on purpose. The goal was never a polished game. It was to prove that I understood how linked lists work.

## The body is a list

A linked list is a chain of nodes where each node points to the next one. The snake's body is a doubly linked list, so each node also points back to the one before it. Each node stores an x coordinate, a y coordinate and a direction. The head of the list is the snake's head. The last node is the tail.

This makes growth easy. When the snake eats, I create a new node and attach it to the tail. Its position comes from the current tail's direction. There is no array to resize and no elements to shift. It is one pointer update.

Movement passes down the chain. The head moves first, based on the player's input. Each segment then takes the direction of the segment in front of it. That chain of updates creates the snake's typical motion.

## Drawing only what changes

The Windows console API lets a program move the cursor. I move the cursor to exact coordinates and print characters there. Erasing the old tail and drawing the new head makes the snake look like it moves.

The game never redraws the whole screen. Only the changed positions update each frame. That keeps drawing fast enough for smooth play.

Random numbers place the food. The game seeds the generator with the current time, then picks coordinates inside the play area.

## How the snake dies

The snake dies when its head hits a wall or any part of its body. The wall check tests if the coordinates go past the edges. The body check searches the linked list to see if the head's position matches any node.

That search is recursive. It walks the list one node at a time. If it finds a match, the game ends. A simple search like this is fast enough for a snake of normal length.

## No turning back

The game checks for arrow key presses with the Windows asynchronous key state function. A small guard stops the snake from turning back into itself. You cannot go left while you are moving right.

## Lists outside the textbook

This game taught me that data structures are useful far beyond interview questions. A linked list is more than theory. It is a tool that models real problems cleanly.

Working without a game framework also made me learn what happens at a lower level: cursor positions, timing loops and keyboard polling. Those basics still matter.

I learned more about linked lists from one snake than from the textbook.

[Book a call with Rolando](https://rolandoandrade.me/pricing/)
