Intro to Computer Science


A Game!

Code AND WORKSHEET due by Friday, Feb 24th at 11:00 AM



Come on admit it.  Somewhere along the line as a kid (at least those of you who grew up in the US) you  played the card game called War. It's a simple two-player game. You divide a deck (or multiple decks) of cards into two, and each player gets half the cards. Next, each player turns over the top card of their decks. If player 1 has the higher card, player 1 wins that round and takes both cards. Both the card totals are added to player 1's score. If player 2 has the higher card, player 2 wins that round and takes both cards. Both the card totals are added to player 2's score. If there is a tie, different things can happen, depending on the rules.

The game is often seen as a game of chance. Whichever player wins is random, similar to flipping a coin or throwing a dice randomly.

Original Program Specifications

Before starting your code, read through the program specification below and fill in the worksheet for each task/subtask you will need to perform

Your program (saved in a file called  will allow a human user to play several rounds of War with the computer.  Each round of the game will have the following structure:

For example, one sample game might look like this:


For this version of this game, you must program the computer to pick cards randomly.  To do this you should put the following code at the top of your program:

import random

This is the code necessary to set up a random number generator.

Each time you need to pick a random number you use:


This will produce a value from 1 to 13 INCLUSIVE (notice this is different from the way range works).  1=Ace, 2=2, 3=3 ... 10=10, 11=Jack, 12=Queen, and 13=King.


Getting Started (Worksheet)

  1. Do all the standard startup things. Create a new file called Put your comments in at the top, save it.
  2. Now you need to break the problem down into parts. Read the description and identify the tasks/subtasks that need to be solved in the worksheet. For example, one task would be to prompt for valid user input.
  3. Now address one subtask, getting user input. Do this in stages as well. Can you:
    1. Prompt for and get a choice (a string) from the user?
    2. Once you can do that, can you check for “legal” character responses from the user, and print an error message when an illegal response is given?

Once you can do all that, be sure all the code you have works before moving on to the next subtask.

  1. Remember, save the file and run it all the time! It will make debugging the program easier.

BONUS PROBLEM worth 3 extra points- Let's declare a real war!

On tie, both players keep flipping cards until the tie is broken. In a more typical game, only the player that wins the tie break at the end will get points. In this version, all cards involved in the tie(s) and subsequent win should be added to the winning player's score, and no points should be added to the loosing players score. See this example run:

Name this version of the game Only work on this once you have the previous version of the game working and saved in a separate loation. If you finish, you only need to submit this copy of the game. It is very important that you name the program correctly to signal us to check for the extra credit.

Final Submission

To upload your homework for grading, log on to eLearning, select this class, and navigate to the "Assignment Submissions" area. Click on the "Programming Assignment 5" folder and upload the following files to the designated location: