Montana Tech of The University of Montana
Computer Science & Software Engineering

CSCI 135
Fundamentals of Computer Science I
Fall 2011



ASSIGNMENT #1

The goal of this assignment is introduce you to programming with loops (such as the for-loop or while-loop) and conditions (such as the if-then-else). You will also learn how to get keyboard input from users.


Part 1: Gambler
You are currently $X in debt to your bookie but currently have only $Y to your name. Create a program Gambler.java to calculate the minimum number of times you would have to go double-or-nothing at the roulette table to pay your debt. Your program should take two command line arguments X and Y. X and Y are floating point numbers representing the amounts in dollars and cents (e.g. 2345.32). Assume you go "all-in" on every bet and the casino allows bets of any value. Assume X > 0 and Y > 0. Here some example runs:
% java Gambler 1000 100
After 1 bet(s), total: 200.0
After 2 bet(s), total: 400.0
After 3 bet(s), total: 800.0
After 4 bet(s), total: 1600.0
Number of bets: 4
Final amount: 1600.0

% java Gambler 12345.67 13.00
After 1 bet(s), total: 26.0
After 2 bet(s), total: 52.0
After 3 bet(s), total: 104.0
After 4 bet(s), total: 208.0
After 5 bet(s), total: 416.0
After 6 bet(s), total: 832.0
After 7 bet(s), total: 1664.0
After 8 bet(s), total: 3328.0
After 9 bet(s), total: 6656.0
After 10 bet(s), total: 13312.0
Number of bets: 10
Final amount: 13312.0

% java Gambler 0.99 0.25
After 1 bet(s), total: 0.5
After 2 bet(s), total: 1.0
Number of bets: 2
Final amount: 1.0

% java Gambler 100 1000
Number of bets: 0
Final amount: 1000.0

% java Gambler 1000.00 1000.00
Number of bets: 0
Final amount: 1000.0
Do I really need to worry about strange cases like the last three? Yes. While these cases may seem silly in our imaginary problem, you should always pay attention to such things when developing software. Failure to handle such tricky cases can result in all sorts of bugs such as your program crashing or going into an infinite loop. You might find this entertaining/terrifying: History's Worst Software Bugs.


Part 2: Sawtooth Pattern
Create a program Sawtooth.java that prints a sawtooth pattern of astricks. The program takes two command line arguments, the first is the width of the teeth in characters. The second argument is the number of teeth to output. The pattern should go from left-to-right and then back again. The teeth should be "sharp" consisting of a single asterick at the rightmost or leftmost point.
% java Sawtooth 4 2
*
 *
  *
   *
  *
 *
*
 *
  *
   *
  *
 *
*

% java Sawtooth 1 4
*
*
*
*

% java Sawtooth 6 0

% java Sawtooth 0 5


Part 3: Number Hunt
Create a game NumberHunt.java in which users try and guess a random number between 1 and 100. After each guess, the computer prints out a hint according to how close the guess was to the target value: If the user inputs a number that isn't between 1 and 100, you should print "Invalid input!". After correctly guessing the number, the game should print the total number of guesses (including invalid guesses). You can assume users will only enter integers and not floating-point numbers or text.

Your program should optionally take a single integer command line argument. If a command line argument is given, use this number instead of drawing a random number. This will help you test your program since you can start with a known number. You can assume the command line argument (if given) will always be between 1 and 100. Here are some example runs:
% java NumberHunt
Guess a number between 1-100? 50
Ice cold.
Guess a number between 1-100? 20
Getting warmer.
Guess a number between 1-100? 10
Hot.
Guess a number between 1-100? 5
Getting warmer.
Guess a number between 1-100? 15
Hot.
Guess a number between 1-100? 12
You nailed it!
It took you 6 guesses.

% java NumberHunt 42
Guess a number between 1-100? 10
Ice cold.
Guess a number between 1-100? 101
Invalid input!
Guess a number between 1-100? 30
Cold.
Guess a number between 1-100? 35
Getting warmer.
Guess a number between 1-100? 40
Hot.
Guess a number between 1-100? 41
On fire.
Guess a number between 1-100? 43
On fire.
Guess a number between 1-100? 42
You nailed it!
It took you 8 guesses.
How do I read in a number from the user? You need to use download the StdIn.java class and place it in the same directory as your NumberHunt.java program. When you want your program to read in an int from the user, call the method StdIn.readInt(). For an example program, see OrderProduct.java.

How do I check if a command line argument was given? You can use args.length to find out how many command line arguments were sent in. If args.length is zero, you know you need to draw a random number. If args.length is greater than zero, you can obtain the target value from args[0].

How do I draw a random number? You want to use the Math.random() method. Check out the TwoDice.java example.


Extra credit: Zombie Apocalypse
Create a text-based zombie game called Zombie.java. The game is played starting on a 10x10 grid. You start in the upper-left corner. Your goal is to reach the safety of the mall in the lower-right corner without getting eaten.

         

Luckily it is early in the zombie apocalypse so you only have one zombie to worry about. Every turn you move north, south, east or west by typing 'n' 's' 'e' or 'w' respectively. If you type an invalid character or try and go off the grid, you lose your turn. If you are dumb enough to walk into the current position of the zombie, kiss your brain goodbye.

The zombie also moves north, south, east or west and must remain on the grid. The zombie cannot enter the mall. The zombie always chooses a valid move but chooses the direction at random (it hasn't learned to smell brains yet). If the zombie's move and your move result in the same location, say goodbye to your grey matter.

A level is completed when you reach the mall. There are nine levels and in each level the size of the grid is reduced by one (the final level is 2x2). You win if you complete all levels. Empty grid cells appear as '.', the zombie is '*', the mall is '#', and you are '!'.

Extra-extra credit. There is plenty of scope for improvements such as: a smarter zombie, multiple zombies, obstacles on the grid, etc. The zombie-infected world is your oyster!

Submission. Submit all three programs Gambler.java, Sawtooth.java, NumberHunt.java and optionally Zombie.java via Moodle. Be sure each submitted source file has the required header with your name, username, and a description of the program.

Copyright © 2011 by Keith Vertanen.

Page last updated: August 16, 2012