CS100: Project 3

Revision Date: January 8, 2017

Printable Version


Preamble

Before you submit this project you must ensure that it runs without crashing on a Linux distribution.

Snake

"You have to learn the rules of the game. And then you have to play better than anyone else." - Albert Einstein

Your task is to create the game Snake! If you are not familiar with the game, here are the rules:

1. There is a rectangular board of size 20 rows, 30 columns. The snake and you both live on the board. The snake is represented by the letter "X" on the board, and you are represented by the letter "i" on the board.

2. The object of the game is to move your player so that you avoid touching the snake. If you touch any part of the snake, you die and the game is over. Also, if the snake traps itself or no longer is able to make any valid moves, you win.

3. The snake begins with a length of 3, meaning that it takes up 3 coordinates on the board. You take up one coordinate on the board. Every 5 moves, the snake grows its tail by 1 coordinate (for example, on the sixth move the snake should be of length 4). So, after the first five moves, the snake has a length of 4.

4. The snake moves (head first) towards you. It chooses the coordinate closest to you out of the set of valid, legal coordinates. The snake can move up, down, right, and left and diagonally in all directions. It cannot go past the edges of the board.

5. You can move your player by pressing the appropriate keys: j for left, k for right, m for down, and i for up. You cannot move diagonally. You also cannot go back the edges of the board.

In addition to the above rules, a score is kept for how long the player manages to stay alive without being bit. The initial score for the player is 0, with each turn the score for the player increases by 1 (reflecting the number of turns a player lives).

You should first display an initial board with your snake of length 3 and your player in predefined positions (for example, you might choose the coordinates (0,1), (0,2), and (0,3) for the snake and the coordinate (0,0) for your player.) The user should be prompted to enter one of the four letters as described above. Once the user has entered a letter and pressed Enter, the board should be updated, be displayed again, and the user should once again be prompted for input. This loop should continue until your player touches the snake.

The function that displays the board should print out something similar to "_ " for each coordinate on the board.

You should print an appropriate message whenever the player tries to move beyond the board or tries to enter an incorrect key (correct keys being i, j, k, and m).

At the end of the game, you should print the end score along with an appropriate message for whether the player won or lost.

The name of your program will be snake.py.

Program Organization

Your snake.py program should have a main function, of course, plus the following helper functions:

For more information on creating these functions, you can refer to the chapter on matrices in the textbook, The Art and Craft of Programming, Chapter 14.

Example output

i X X X _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
_ _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_ _ _ _ _ _ _ _ _ _ _ _ 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
Enter j to move left, k to move right, i to move up, and m to move down.

Stepwise refinement

Write a version of the program that:

Challenge

Create a strategy for your snake to win. For example, when your snake grows long enough, have it stretch across the board to trap the player.

Add dollar signs to random points on the board. Your player should collect the dollar signs to gain points while still avoiding the snake.

Compliance Instructions

You should be able to run your program, like this:

    python3 snake.py 

The board should be displayed and the user should be prompted to press a key.

Submission Instructions

Make sure you have the following files in your directory before submitting:

Change to the directory containing your assignment. To submit the project, run the command:

    submit cs100 YYY project3

Replace YYY with the name of your instructor. If your instructor teaches more than one section, add the hour of your class after your instructor's name.

eawilliams2@crimson.ua.edu