[Solved] CSCI235 Lab12

$25

File Name: CSCI235_Lab12.zip
File Size: 122.46 KB

SKU: [Solved] CSCI235 Lab12 Category: Tag:
5/5 - (1 vote)

You will use a lab account for a VNC display. The lab account already started a VNC session, so you dont need to do Step 3 or 4 of Lab 7s Task #1. Two steps that you need to do are:

  1. Create a tunnel from your machine to a VNC display (Step 5 of Lab 7s Task #1).
    1. A cslab machine number and a display number will be provided to you.
    2. For example, if the given machine is cslab10, and the display number is 12, the command for creating a tunnel to the display will be:
      • Mac Terminal: ssh L 5901:localhost:5912 your_loginID@cslab10.wheaton.edu
      • Windows PuTTY:

Host Name: cslab10.wheaton.edu

For Connection, SSH, Tunnels: 5901 for the Source port and localhost:5912 for the Destination.

  1. The CS system will prompt for your (ID and) password as usual.
  1. Start a VNC viewer on your machine (Step 6 of Lab 7s Task #1).
    1. The given displays VNC password will be provided. You need to use this password to connect to the display.

Tips:

  • If you dont see the menu bar at the top of the CS desktop window, right-click the desktop screen to get Applications menu.
  • When you are done with the desktop, close the desktop and connection by doing: o For TightVNC, use the X button at the top corner of the window.

o For Mac, in Screen Sharing menu at the top, select Connection, and select Close.

===

Documentation and styling of your code will be part of your grade.

Create a new directory and cd to it. Then copy starting files from the course directory to your current directory.

$ cp /cslab/class/csci235/labs/lab12/* .

You should have two files, FifteenPuzzle.java and PuzzlePiece.java.

Fifteen Puzzle

A Fifteen Puzzle consists in a grid of square, same-sized tiles, with all but one of the grid positions filled. The object of the game is to rearrange the tiles by sliding the tiles one at a time until a certain configuration is reached. You will write a program that simulates a fifteen puzzle.

Open FifteenPuzzle.java, and inspect the code. Before moving to the next paragraph, answer to the following questions (Your answers will not be collected, but DO NOT skip the questions!):

  1. How many buttons will be generated from the first nested for loop?
  2. What is the action listener class? What is the type of pieces?
  3. What arguments are passed to the action listeners constructor? How does the variable counter work?
  4. How many listener objects are generated?
  5. What class has the method addNeighbor? What is the argument to the method? What is its type?
  6. See the second nested for loop, and answer to the following questions:
    1. When i is 0, and j is 0, what pieces are linked to pieces[0][0]?
    2. When i is 1, and j is 0, what pieces are linked to pieces[1][0]?
    3. When i is 2, and j is 2, what pieces are linked to pieces[2][2]?
    4. What is the maximum number of neighbors? What is the minimum number of neighbors? Why does this difference exist on the grid?
    5. See the below figure, which is the window generated by the complete FifteenPuzzle program.
      1. What does this second nested loop do?
      2. The provided starting file needs to be edited for this complete window.

The code basically adds sixteen buttons to the window, numbered from 1 through 15 and then 0. Compile and run FifteenPuzzle, and see that the buttons are put right on top of each other: thus, you see only one of them. Update the code so that you can see all of the 16 buttons arranged in a square as seen in the figure.

  • Hint: Resize the window, and use a layout manager.
  • You will not see the empty button when running the current version of FifteenPuzzle. Instead, you will see a button with 0. The work for presenting the empty button should be done by the action listener class.

Each button stands for a tile or block in the puzzle. Fifteen of them will have numbers, and the 16th (numbered 0) will be blank. We wont actually move the buttons around; instead, we will change the numbers on the buttons to give the appearance of moving tiles. When a button is clicked, the following should happen:

  • If it is blank, nothing should happen.
  • If it is not blank and none of its neighbors is blank (neighbors meaning immediately above, below, left, or right; diagonals do not count), nothing should happen.
  • If one of its neighbors is the blank one, then the blank neighbor should change to have the clicked buttons number, and the clicked button should turn blank.

Task #1: Each button needs to have an action listener to perform one of the above behaviors. We are using an action listener class called PuzzlePiece, and your task is to complete this class.

  • Open the file, and see the four instance variables. Why does each PuzzlePiece object need these four instance variables? Document each variable based on your understanding on the code.

Task #2: Complete the following methods next:

  • PuzzlePiece(JButton, int): This constructor tells the action listener what button it is attached to and the number in the order the buttons were made (1 to 15, with the last one numbered 0). It also takes care of setting the text on the corresponding button. Youll need to fix its body so that the button for 0 shows as blank. In addition, create or initialize the instance variables.
  • After completing the constructor, run the program. You should see the empty button instead of the button with 0.
  • addNeighbor(PuzzlePiece): When a button is clicked, it affects its neighbors. The action listener is not going to have direct access to the neighboring buttons, but it should have references to the action listeners attached to those buttons (You can perform some actions on the neighboring buttons through those references). How can we store this information for later reuse? Write the method body.
    • Hint: Find the code of FifteenPuzzle that calls addNeighbor. Whenever this method is called, the reference delivered through the formal parameter, neighbor should be stored in the array, neighbors. In addition, the instance variable, numOfNeighbors should be updated accordingly.
  • actionPerformed(ActionEvent): This method should implement the actions described above. o Hint: Determine if one of the neighbors is blank, and then make any necessary changes. Recall that the information about the neighbors has been stored in the array. Each element of the array (a neighbor) is in the type of PuzzePiece, so your code can directly access to the neighbors instance variables (the four variables).
    • Important: When a PuzzlePieces instance variable, number is updated, the corresponding text on the button should be also updated, and vice versa.
    • If you want, you can add a few extra helper methods that can be used by this method.

Run the FifteenPuzzle program, and see if it works. Make sure to understand how all the programming components are related and work together in the program.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CSCI235 Lab12
$25