Monthly Archives: August 2020

Arrays 2D: Minesweeper

Screen Shot 2014-12-07 at 12.13.44 PM

minesweeper

youtube “how-to”

Write a program Minesweeper_YI.java that takes 3 input arguments m, n, and p and produces an m-by-n boolean array where each entry is occupied with probability p. In the minesweeper game, occupied cells represent bombs and empty cells represent safe cells. Print the array using an asterisk for bombs and a period for safe cells. Then, replace each safe square with the number of neighboring bombs (above, below, left, right, or diagonal) and print the solution.

HS-TEACHER-17464:Array2D gracielaelia$ java Minesweeper 3 3 0.2
. . . 
. . . 
* . . 

0 0 0 
1 1 0 
* 1 0 


Try to write your code so that you have as few special cases as possible to deal with, by using an (m+2)-by-(n+2) boolean array.

HS-TEACHER-17464:Array2D gracielaelia$ java Minesweeper 3 3 0.3
. . * 
. . * 
. . * 

0 2 * 
0 3 * 
0 2 * 
HS-TEACHER-17464:Array2D gracielaelia$ java Minesweeper 3 3 0.4
. . . 
* . . 
. . . 

1 1 0 
* 1 0 
1 1 0 
HS-TEACHER-17464:Array2D gracielaelia$ java Minesweeper 3 3 0.5
* * * 
. * * 
* . * 

* * * 
4 * * 
* 4 * 
HS-TEACHER-17464:Array2D gracielaelia$ java Minesweeper 3 3 0.6
* * * 
. . * 
* * * 

* * * 
4 7 * 
* * * 
HS-TEACHER-17464:Array2D gracielaelia$ java Minesweeper 3 3 0.7
. * * 
* * * 
* . * 

3 * * 
* * * 
* 5 * 
HS-TEACHER-17464:Array2D gracielaelia$ java Minesweeper 3 3 0.8
* . * 
* * . 
* * * 

* 4 * 
* * 4 
* * * 
HS-TEACHER-17464:Array2D gracielaelia$ java Minesweeper 3 3 0.9
* * * 
* * . 
* * * 

* * * 
* * 5 
* * *

Required Submission:

  1. Copy and paste the program (with the output as a paragraph comment at the end of your program) to the Text entry tab.

  2. Submit the file with the ‘”java” extension.

3. Make sure you document code snippets that have a particular purpose!

4. Testing: Have multiple boards with different board sizes and different probabilities. The minimum should be 3 boards.


					

Arrays: Game of Life

Game Of Life:

If you have successfully completed the random block program and written the draft for the algorithm in pseudocode, you should be able to get started with the game of life assignment.

Start small. Hardcode the seed for the glider:

Requirements:
1. Take as input the size of the grid.
2. Show the live cells in color other than black.

Challenges:
1. Take as input the coordinates for the seed. You must include in your documentation the coordinates for the glider seed.
2. Change the color of the dead cells to a faded color for at least one generation after dying.
3. Take as input the option to speed or slow down the simulation.

Arrays: Connect Four

Game: Connect Four is a two-player board game in which the players alternately drop colored disks into a seven-column, six-row vertically suspended grid, as shown below.

The objective of the game is to connect four same-colored disks in a row, a column, or a diagonal before an opponent can do likewise. The program prompts a player to choose a color disk. Then the computer and the player will drop a red or yellow disk alternately. In the figure, the player with the red disk is the winner of the game.

This implementation is text based. Take a look a the simplicity of the format. This will enable you to concentrate on the algorithm and the strategy you need to implement to have the computer to win. Let’s develop the “computer to win” a bit further. You do not want to play against an infallible player (the computer) because you will not even want to play the game. So a better approach would be a good blend of random moves with smart moves. You decide how “smart” the computer is.

Whenever a disk is dropped, the program redisplays the board on the console and determines the status of the game (win, draw, or continue). Here is a sample run:

Let's assume the user is controlling the red disk,

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
———————————————

Drop a red disk at column (0–6): 0
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|R| | | | | | |
———————————————

Computer drops a yellow disk at column 4

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|R| | | |Y| | |
.
.
.
.

Computer drops a yellow disk at column 6 

| | | | | | | |
| | | | | | | |
| | | | |Y| | |
| | | |Y|Y|Y|R|
| | |Y|R|R|Y|Y|
|R|Y|R|Y|R|R|R|
———————————————
The computer won
Do you want to play again?
  1. Write a one-player program, Connect4TwoD_YI.java using 2D array. Display at a every move the board as shown in the example above. Include a prompt to continue playing.
  2. Write a one-player program, Connect4OneD_YI.java using only 1D array. Display at a every move the board as shown in the example above. Include a prompt to continue playing. This is a bit more complex but you will find a pattern to help you implement it.

Little help #1: Use paper and pencil to design, trace and test your algorithm before you start.
Little help #2: You can use static methods (functions) to make the code more readable and easier to manage. This is only optional.

Arrays: Connect 4 Algorithm – 2D Arrays

Let’s not just connect 4. Let’s connect what you have learned about arrays and everything that brought you to this point to develop your first AI game in java.

  1. What does “your” simplified flowchart for a game look like?
Invent With Python – https://inventwithpython.com/invent4thed/chapter7.html

2. Let’s talk about “your” algorithm.

Let’s play the game

Red is the player – Blue is the computer
Answer the following questions
1. How would your program select the next move?
2. When would your program choose to make a random move?
3. When would your program make an intelligent move?
4. If the red ball numbered 1 was the first move, can you trace the following steps up to this point?
5. Can you make an educated guess as to how many different ways?

3. Use your “Toolkit” from previous lessons:

2D Arrays Intro

  1. You learned how to add rows:

    int total = 0;
    for (int i = 0; i < COUNTRIES; i++)
    {
      total = 0;
      for (int j = 0; j < MEDALS; j++) 
      {
        total = total + counts[i][j];
      }
    }
    

  2. You learned how to add columns:
    int total = 0;
    for (int j = 0; j < MEDALS; j++)
    {
      total = 0;
      for (int i = 0; i < COUNTRIES; i++) 
      {
        total = total + counts[i][j]; 
      }
    }
    

And accessing the neighboring elements of a 2D array

and from Magic Square

You learned how to add diagonals:

sum = 0;
for (int i = 0; i < square.length; i++) {
            sum += square[i][i];
        }

sum = 0;
        for (int i = 0; i < square.length; i++) {
            sum += square[i][square.length-i];
        }

4. Add details to your simplified flowchart. Let's develop the "computer to win" a bit further. You do not want to play against an infallible player (the computer) because you will not even want to play the game. So a better approach would be a good blend of random moves with smart moves. You decide how "smart" the computer is.

5. Levels of difficulty. Choose the "Medium" level of difficulty and start the same as in the first game. Do the same for "Hard" level and compare to easy and medium. How different are the behaviors of the "computer" moves? Can you find a pattern?

Arrays – Connect Four

Classwork/Homework:
(Game: connect four) Connect four is a two-player board game in which the players alternately drop colored disks into a seven-column, six-row vertically suspended grid, as shown below.

connectfour

The objective of the game is to connect four same-colored disks in a row, a column, or a diagonal before your opponent can do likewise. The program prompts two players to drop a red or yellow disk alternately. In the preceding figure, the red disk is shown in a dark color and the yellow in a light color. Whenever a disk is dropped, the program redisplays the board on the console and determines the status of the game (win, draw, or continue). Here is a sample run:

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
———————————————

Drop a red disk at column (0–6): 0
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|R| | | | | | |
———————————————

Drop a yellow disk at column (0–6): 4

| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
|R| | | |Y| | |
.
.
.
.

Drop a yellow disk at column (0–6): 6 

| | | | | | | |
| | | | | | | |
| | | | |Y| | |
| | | |Y|Y|Y|R|
| | |Y|R|R|Y|Y|
|R|Y|R|Y|R|R|R|
———————————————
The yellow player won
Do you want to play again?

Write the two-player program, YI_Connect4.java. Display at a every move the board as shown in the example above. Include a prompt to continue playing.

Your program should include:
1. Good documentation
2. An introduction message
3. Functions (Optional)
4. An end of game message
5. Input/output

What is good documentation?
Answer:

  1. Header with author’s name, assignment description, date and python version.
  2. Comments in each function. A short sentence will be sufficient.
  3. Comments for code snippets written for specific handling. An example might be checking if there are 3 in a row, or 4 in a row.
  4. Comments to indicate unusual or rare situations that don’t follow a pattern or repetition.

Blackboard: Nothing like pseudocode

Nothing like good pseudocode!!
javapseudocodeconn4

One more
lastpeudocode

Arrays: Students Show and Tell

Welcome back!!
new-year-gif
Class activity:
Discussions and students’ presentations about the following assignments:
StdIn/StdOut – Hwk 12/3 -YI_Duplicates.java
StdDraw – Hwk 12/4 – YI_TriangleTess.java
StdDraw – Clwk 12/4 – YI_MyGraphPaper.java
StdDraw – Hwk 12/7 – YI_SongLyrics.java
StdDraw – Clwk 12/8 – YI_Histogram.java

Homework:
1 Question on edmodo.com
Work on missing assignments.

Arrays: Preparing for assessments

Preparing for assessments

Your notes should include:

  • All loops syntax: while, do while, and for.
  • The Scanner class: how to create an object of it and use it’s methods.
  • The Math.random() function.
  • Typical array-processing code highlights:
    • 1D and 2D arrays: how to declare and initialize them.
    • Code snippets to print all the elements, to add columns, rows.
    • Programs designed to create frequency arrays.
    • The code needed to swap elements of an array, to find the max and the min.
    • A short program to simulate rolling dice.
  • StdIn, StdOut and StdDraw short API’s with few examples.
  • The rules to print formatted output, printf.
  • Simple drawings programs.

Arrays: Test Review Arrays

Screen Shot 2014-12-07 at 12.13.44 PM

Arrays Test Review

  • One Dimensional arrays:
    1. Create arrays and initialize them with a loop.
    2. Initialize an array when created.
    3. Find the number of elements in an array.
    4. Check an array’s bounds.
    5. Swapping elements of an array – permutations (sampling without replacement)
    6. Given an array of N elements, write the code to shuffle them using the random feature.
    7. Find the max – min of the elements.
    8. Reverse the elements.
    9. Find the average of the elements.
    10. Print each element.
    11. Sieve of Erasthostenes http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
    12. Arrays as frequency or counters.
    13. What types must be used as indices for arrays?