Ask Computer Engineering Expert

(1) Working On the Game:

Begin with your working code from assignment #5. Complete the game so that the following happens:

1. Add a private score integer attribute to the ShinyButtons model class. Create a getScore() method that returns the score.

2. When the user presses the New Game button, the score resets to zero and the game pieces are reset to a different set of random colors again.

3. When the user presses the Quit button, the application stops and the window closes.

4. When there is currently no selected game piece and then the user clicks on a game piece, that game piece's JButton becomes the "selectedButton". Each JButton can have two ImageIcons assigned to it: (1) the regular icon, and (2) the selected icon:

1048_Shiny Buttons game.png

At this point, we have only used regular icons. You can also set a selected Icon for each JButton (i.e., a different image) by doing this: (aJButton.setSelectedIcon(new ImageIcon("..."));. This just has to be set once, when you create the JButtons. Add the following array to your code and make sure that you download the additional 7 selected icon images (shown above). Use the array to set the appropriate selected icon for each JButton according to its current color.

public static ImageIcon[] selectedIcons = {
new ImageIcon("SelectedRedButton.png"),
new ImageIcon("SelectedOrangeButton.png"),
new ImageIcon("SelectedYellowButton.png"),
new ImageIcon("SelectedGreenButton.png"),
new ImageIcon("SelectedBlueButton.png"),
new ImageIcon("SelectedLightGrayButton.png"),
new ImageIcon("SelectedDarkGrayButton.png")
};

Adjust your code so that all JButtons are NOT selected except for the selectedButton, which should be selected. You can set aJButton to be selected by saying: aJButton.setSelected(true); JAVA will then show the appropriate image icon automatically. As a result, the background of the button image should be colored in to a non-white color.

5. Adjust your code so that when the user has already selected a game piece, and then an adjacent game piece is selected that lies beside (i.e., to its immediate left, right, up or down position) the selected game piece (i.e., the selectedButton) then this piece should swap positions with the selectedButton's piece. Note that you should NOT swap JButtons ... just game pieces. However, you WILL need to ensure that the JButtons' regular and selected icons are properly swapped also. Just create an appropriate method in the ShinyButtons model class to swap two game pieces when given their rows and columns ... then update() the GUI accordingly. Once the swap is made, the selectedButton should be set to null so that no JButtons are selected anymore. You should see the swap happen visually on the window. If the game piece that was pressed was NOT beside the selectedButton's piece, then no swap is made and the selectedButton should be set to null, so that no buttons remain selected.

6. Adjust your code so that it automatically highlights any horizontal or vertical sequences of 3 or more game pieces if they are the same color.

981_Shiny Buttons game1.png

In order to determine which game pieces to highlight (i.e., to select), you will need to write some code in the ShinyButtons class. Create the following attribute: private boolean[][] selectionTable; Create a method in the ShinyButtons class called processTable() which resets the selectionTable to have all false values and then checks all rows for sequences of same color pieces and then checks the columns for sequences of same color pieces. Here is some pseudocode for checking the rows:

FOR each row r FROM 0 TO 7 DO {
set matches to 0
FOR each column c FROM 1 TO 7 DO {
IF (piece at (r, c) has same color as piece at (r, c-1)) THEN {
increase matches by 1
IF (matches >= 2) THEN
Select all three pieces at row r from column c-2 to column c.
}
ELSE set matches to 0
}
}

Do something similar for the columns. Make sure that whenever a new game is started, then the processTable() method is called. Also, make sure that it is called whenever two pieces' positions are swapped (from step 5).

(2) Completing Game:

Now it is time to complete the game

1. Write a public void cleanTable() method in the ShinyGame class that causes all selected sequences of game pieces to be removed from the game board. As a piece is removed from a row, all game pieces above it will drop down one row and a new game piece will appear at the top row of the board (within the same column that the removed piece was taken away). The method should work from the bottom row upwards and look for selected pieces to remove. Here is the pseudocode:

set newPieceAdded to FALSE
set r = 7
WHILE (r >= 0) DO {
newPieceAdded = FALSE
FOR each column c FROM 0 TO 7 DO {
IF (piece at (r, c) is selected) THEN {
// move the pieces down now ...
FOR each row r2 FROM r TO 1 DO {
selection of piece at (r2, c) = selection of piece at (r2-1, c)
piece at (r2, c) = piece at (r2-1, c);
}
// Add a new piece
set the piece color for piece at (0, c) to a valid random color index
unselect piece at (0, c)
set newPieceAdded to TRUE
}
}
IF (newPieceAdded is FALSE) THEN
decrease r by 1
}

To test this, add a private Timer timer; attribute to the ShinyButtonsApp. Add the following
Timer and its event handler within the constructor:

timer = new Timer(500,new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Write code here to clean the table, process it, and then update
}});
timer.start(); // This line starts the timer which will happen every 0.5 seconds

2. Finally, we need to work at getting the game to keep score. To record the score, you will need to update your processTable() method so that whenever the number of matches is 2 or more, you update the score. Make sure to update the score for both rows and columns, separately. Each time you find a sequence of 3 or more similar colored buttons, you will update the score as follows (where S is the size of the sequence):

2002_Shiny Buttons game2.png

So, for example, here is the increase in score that should occur depending on the sequence size:

528_Shiny Buttons game3.png

Make sure to test your code carefully. However, it will be hard to test sequences of 6, 7 or 8 ... these can only occur my chance if new buttons are added to a row/column which happen to match the others in the row. However, you should be able to test sequences of size 3, 4 or 5. Please note, though, that this game never ends ... so the score will increase until the user quits.

Keep in mind that nothing special needs to happen with horizontal and vertical sequences that cross. For example, in the picture above you will notice a yellow vertical sequence of 4 and two horizontal sequences of 3.

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M9739700
  • Price:- $70

Priced at Now at $70, Verified Solution

Have any Question?


Related Questions in Computer Engineering

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As