• Home
• Contact Me
•
Aeries Portal
• Royal High School
• Simi Valley USD

AP Statistics

• Course Calendar
• Chapter Notes
• Practice Tests

AP Comp-Sci
Course Content
• Programs
• Practice Tests

CP Statistics
• Course Calendar
• Chapter Notes
• Practice Tests

Algebra
Geometry

Math Power

AP Computer Science Unit 4 Programs
Data Structures: Arrays, Strings and ArrayLists

 Working with 2D Arrays Jump to: Text Files 1D Arrays Strings ArrayLists

(1) Write a program to generate a random playing field for the game Minesweeper. Begin the program by asking what the dimensions of the field should be (max: 10 x 10). Then ask for the number of mines to be placed (error-check to make sure that there are enough spaces for the specified number of mines). Next, randomly place the mines on the field. Then display the field to the screen showing:

• X's for squares containing mines
• A number corresponding the number of adjacent mines for each box that is adjacent to at least one mine
• 0’s for squares that are not adjacent to mines

Here is an example for a 5x5 field with 6 mines:

 0 1 1 2 1 1 2 X 3 X 1 X 2 3 X 2 3 3 2 1 1 X X 1 0

(2)  Write a program to simulate the random movement of an ant walking on a coordinate grid with dimensions 9x9. Position the ant in cell (5,5) to begin. Then allow the ant to move randomly up, down, left or right. The ant stops when it falls off of the grid (goes to 0 or 10 in either direction). In the example below, the ant falls off the grid to the South, noted by the X. As the ant is moving, you need to keep track of the following information:

• The number of moves before falling off
• The number of times the ant visited each cell (displayed in the matrix)
• Which direction the ant fell off the grid (North, South, East or West)

Note: You must update and redisplay the matrix after each movement

 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 2 1 0 0 0 0 0 0 0 1 2 1 0 0 0 0 0 1 2 1 2 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 2 2 0 0 0 0 0 0 0 1 1 0 0 0 0 0 X

(3) In the carnival game Penny Pitch, a two-dimensional board of numbers is laid out at follows:

 1 1 1 1 1 1 2 2 2 1 1 2 3 2 1 1 2 2 2 1 1 1 1 1 1

A player tosses three pennies on the board, aiming for the number with the highest value. At the end of the game, the sum total of the three tosses is returned. Write a program that plays this game. The program should perform the following steps for each new game:

• Generate two random numbers for the row and column of the toss
• Add the number at this position to a running total
• Display the board, replacing the numbers with P’s where the penny lands

Note: Each penny must land in a separate space. This requires a separate, Boolean matrix to ensure no duplicates are used.

(4)  The “Game of Life” is a genetics simulation, which is popular with mathematicians and computer scientists. It is called a game because of the various outcomes over generations of times. The game was invented by John Conway, a noted mathematician, to model the life cycles of simple organisms.

In the Game of Life, a given organism occupies a single space in a two-dimensional array of n rows and m columns. The current population is considered a generation for a small game:

 % % % % % % %

In one version of the game, the species live and die according to the following rules:

1. Each empty space with three adjacent occupied spaces will be occupied in the next generation
2. Any occupied space with only one or with no neighbors will die of loneliness and be empty in the next generation.
3. Any occupied space with four or more neighbors will die of overcrowding and be empty in the next generation.
4. Any occupied space with two or three neighbors will live into the next generation.

Write a program to play the game of life over 10 generations, showing one generation at a time. Use a field 20x20 and start with 50 randomly selected occupied cells. The screen should clear and show the next generation when the user presses enter (or any other key if you wish).

See these links to get a better understanding of this "game":
http://www.math.com/students/wonders/life/life.html
http://www.ibiblio.org/lifepatterns/

(5)

(6)