------------------------------------------------------------------------
Lab 8 -- Introduction to 2-D arrays.
Study the code in Table to see how a two dimensional array
is declared and filled. It is important to note that an array of objects
(in this case of Cells) is empty until each location (row,col) has an object
instantiated there. Study the code in Cell.
Notice in particular the static variable "theNumberOfCells"
and how it is used. This is a class
variable and there is only one copy of it when a program is executing, no
matter how many cells there are.
For lab 8, Create a new class Lab8 and copy and revise TableTest by doing
the following:
Create a loop to test five inputs
prompt the user for the number of
a cell
change the cell number to 0 (will
require a new method in Table)
//be sure that the program is robust
(do not allow indices to go out of range)
print the new table
When testing your program, choose numbers of cells first, last, first in another
row, last in another row, interior and out of range. Have your class catch the out of range cell
number and print an appropriate message.
************************************************************************************
Lab 9 -- Create a new class, Game, to play the following game
==> USE THE METHOD YOU ADDED to set the cell value in Lab 8 <==
Prompt the user for the size of the playing board
Randomly fill 10% of the cells with snakes
Randomly fill 5% of the cells or 1, whichever is greater, with treasure
To play the game, the user chooses a cell
If the cell is empty, mark the cell, display the table and the user may play
again
If the cell has a snake, the game is over and the player losses (snake
cell = -1)
If the cell has the treasure, the game is over and the player wins (treasure
cell = -2)
Print appropriate messages throughout. At the end of the game, show the cells
that have snakes and treasures. You
may change the class Cell to better play the
game, adding another instance variable. It
would be handy, for example, if a
cell held both numbers and a char for the snake or treasure or to denote empty.
Plan out your game class, using meaningful method names. Stub it out with
documentation and call me over to check it before you proceed.
Create a class, Lab9, to test your Game class.
After declaring your game,
call a play method.
------------------------------------------------------------------------