Menu

[Solved]Introduction Goal Project Implement Program Allows User Play Version Find Treasure Game Ve Q37282664

Introduction

The goal of this project is to implement a program that allowsthe user to play a version of find the treasure game. In thisversion, the goal of the user is to find several treasures hiddenin a 5 x 5 board. Initially, a user-specified number of treasuresare randomly placed on the board, in which all cells are in greycolor (so the user could not see where the treasures are located).The user finds the treasures by clicking on select cells on theboard. If the user clicked on a cell that contains a treasure, thecell changes the color to green. If the user clicked on a cell thatdoes not contain a treasure, the cell changes the color to red. Theuser is only allowed to make 6 attempts. The user wins the game ifall treasures are found. The initial 600×600 window should havefive rows and five columns.

Initialization

Before the program displays the board, it should ask the userfor how many treasures to place on the board. The number input bythe user should be between 1 and 5 (including). Then, the programshould declare the arrays with sizes as follows:

  • boolean[] Found = new boolean[size];
  • int[] treasureX = new int[size];
  • int[] treasureY = new int[size];
  • int[] selectedX = new int[7];
  • int[] selectedY = new int[7];

The program should initially assign all positions in the arrayFound as false.

On the other hand, each element of treasureX and treasureYarrays should contain random numbers between 1 and 5. Make surethat both treasureX and treasureY do not contain duplicated values.Initialize the selectedX and selectedY arrays to contain only value-1.

Attempt to discover a cell

An attempt by the user is a click on a cell on the board. Thelocation of the clicked cell (x, y) should be recorded in theselectedX and selectedY array. If the location (x, y) matches alocation, say at index k, in the treasureX and treasureY arrays,the program should set the element at index kin the Found array totrue and set the corresponding cell on the board to the greencolor. If the location of the selected cell does not match anylocation recorded in the treasureX and treasureY arrays, theprogram should set the cell to red color.

The game will end if the user finds all the treasures beforeselecting up to 6 different cells. In that case, the user will havewon the game. In contrast, the user loses the game if runs out ofattempts with no success to find all the treasures.

Initial Zip File

You can start your project by downloading project3.zip. Thisfile contains DrawingPanel2.java and an initial version ofFindTheTreasure.java.

DrawingPanel2.java

DrawingPanel2.java has additional methods for closing the windowand getting information about the state of the mouse. Here are themethods available to you.

Method NameDescriptiongetGraphics( )returns a Graphics object that the program can draw onsetBackground(color)    sets the background colorsleep(milliseconds)pause for a given number of milliseconds (1000 millisecondsequals one second)close( )closes the windowgetMouseX( )returns the X position of the mousegetMouseY( )returns the Y position of the mousegetClickX( )returns the X position of the last mouse clickgetClickY( )returns the Y position of the last mouse clickmousePressed( )returns true if any mouse button is pressed

If you run the initial version of Concentration.java, you willsee information about the mouse in the status bar of the window. Ifyou look more closely at the code in Concentration.java, you willfind out that the code uses these methods to repeatedly ask aboutthe status of the mouse. This is a bad way to program windowbehavior because a lot of time is wasted in loops waiting forsomething to happen (called busy waiting orspinning). Instead, the program should beevent-driven, but we don’t have enough time to discussChapter 14 of your textbook.

FindTheTreasure.java

The initial version of FindTheTreasure.java implements thefollowing pseudocode:

  1. Initialize the window with 1 x 5 squares so that the colors ofthe random location of the treasures are hidden.
  2. Initialize one array that store the hidden treasurescoordinates.
  3. It places (not randomly) the treasure in the square 0, 1.
  4. It verifies if the only attempt that the user takes is wherethe the only treasure was placed.

You should study the code in FindTheTreasure.java to gain anunderstanding of how the code implements the above behavior.Describe what is stored in each variable. For the getClickRow andgetClickColumn methods, determine the correspondence between thereturn values and locations of the mouse clicks. Figure out how theprogram knows if a treasure is stored in a particular cell usingthe arrays treasureX and treasureY.

Change to N number of treasures

For this project, you need to identify all the code whichassumes that the game has one row and five columns and modify thecode to handle five rows and five columns.

Change only 6 attempts

For this project, you need to identify all the code whichassumes that the game has only 6 possible attempts for the user toselect a new square.

The following sections discuss features that should beimplemented for Project 3.

Randomizing the Locations of the N number of treasures

The game is not very interesting if the locations and the numberof treasures are the same every time. It would be better if theuser selects how many, between 1 and 5, treasures wants to belocated in random locations to be different every time. That is,the treasureX and treasureY arrays should be put in randomnumbers.

A simple way to implement the randomizeLocations method is tofollow the following pseudocode:

  1. Repeat the times equal to the number of treasures desired bythe user:
  2. Generate two random numbers col and row between 0 and theselected number by the user – 1
  3. Go to look if those coordinates are not stored already in thetreasureX and treasureY arrays.
    1. If they are already stored in any of the indexes of the arrays,the loop should iterate once again to generate new random numbersfor the same iteration.
    2. 2. If the coordinates are not in the arrays, they will bestored in the specific iteration of the loop indexes. That is colcorresponds to treasureX and row corresponds to treasureY.

Detecting the End of the Game

Instead of an infinite loop, the game should end when the usereither has found all the treasures or has ran out of the 6attempts. After finding all the treasures, the game should be over.Run the following code after the game is over. The basicfunctionality of finding if the only treasure was correctlyselected is implemented in the program provided asFindTheTreasure.java. You need to make the changes to verify if thecomplete set of treasures has been found.

g.setFont(new Font(“SansSerif”, Font.BOLD, 100));g.setColor(Color.BLACK);g.drawString(“The game is over!”, 0, 300);

Fixing the Clicking on Revealed Cells Bug

The game currently allows the user to select a square in theonly row. Make the changes for the board to be out of 5 x 5 cellsas it is shown in the next figure. The user should also be able toselect any of those squares. You can also see there that there is alegend in the top left corner that shows how many attempts are leftto use.

The program should be modified to ensure that a revealed squarecannot be selected. To do this, the program needs to remember whichsquares have been revealed. One way of implementing this idea is tohave two additional integer arrays that will store the coordinatesof the selected squares. For example, for the image when the userhas selected the first square the values stored in those arrayswould be selectedX[5] = 1 and selectedY[5] = 1. The index 5corresponds to the 6th attempt. <

The Files in project3 below:

FindTheTreasure.Java

import java.awt.*;
import java.util.*;

/*
* This program is a non complete functional version of the find thetreasure game.
* In this version, the game has only one row.
*/

public class FindTheTreasureProvided {
public static void main(String[] args) {
DrawingPanel2 panel = new DrawingPanel2(600,200);
Graphics g = panel.getGraphics();
int size;
int attempts = 0;
int row = -1;
int col = -1;
int pRow = 0;
int pCol = 0;
int r = 0;
int c = 0;
boolean ret = false;

System.out.println(“Project 3 written by test”);

// Get the number of treasures wanted, assign it to the variablesize
// Get it by a function that you declare to get the validnumber
size = 1;
// Convert these variables to arrays to hold the values of found,xPos, yPos of size of the number obtained
boolean found = false;
int xPos = 1;
int yPos = 0;

// Initialize values all found to false, xPos and yPos are nonrepeated random coordinates
// Draws the contour of the squares
// You may change it to do it for different rows

for (c = 0; c < 5; c++) {
g.drawRect(c * 100 + 50, r * 100 + 50, 100, 100);
}
// Fills the squares
g.setColor(Color.LIGHT_GRAY);
r = 0;
// Adds color to the squares
// You may change it to do it for different rows
for (c = 0; c < 5; c++) {
g.fillRect(c * 100 + 51, r * 100 + 51, 99, 99);
}

// Verifies one time if the position selected is correct
row = getClickRow(panel);
col = getClickColumn(panel);
while ((row < 0 || col < 0) && !(pRow == row&& pCol == row)) {
row = getClickRow(panel);
col = getClickColumn(panel);
}

if (pRow != row || pCol != col) {
pRow = row;
pCol = col;
ret = verifyPos(xPos, yPos, row, col); // should be converted toverify all treasures
if (row > -1 && col > -1) {
if (ret)
g.setColor(Color.GREEN);
else
g.setColor(Color.RED);
g.fillRect(col * 100 + 51, row * 100 + 51, 99, 99);
}
//creates the box which congratulated the user when the game isover
if (isFinished(ret)){ // change it to send and verify the array offound
g.setColor(Color.WHITE);
g.fillRect(75, 75, 450, 50);
g.setColor(Color.BLACK);
g.drawRect(75, 75, 450, 50);
g.drawString(“CONGRATULATIONS! You have won the game”, 100,100);
}

}
if (!isFinished(ret))
{
g.setColor(Color.WHITE);
g.fillRect(75, 75, 450, 50);
g.setColor(Color.BLACK);
g.drawRect(75, 75, 450, 50);
g.drawString(“You have lost the game”, 100, 100);
}
}

// this method determines where your clicker is in respsects tothe row you are on and passes it on to the main method
public static int getClickRow(DrawingPanel2 panel) {
int x = panel.getClickX();
int y = panel.getClickY();
if (x > 50 && x < 550 && y > 50 &&y < 150) {
return 0; // tells the program your clicker is on the firstrow
}
// Add here how to return the other rows
else {
return -1; // tells the program you aren’t in the grey boxarea
}
}
public static int getClickColumn(DrawingPanel2 panel) {
int x = panel.getClickX();
int y = panel.getClickY();
if (x > 50 && x < 150 && y > 50 &&y < 550) {
return 0; // dimensions of the first column
}
else if (x > 150 && x < 250 && y > 50&& y < 550) {
return 1; // dimensions of the second column
}
else if (x > 250 && x < 350 && y > 50&& y < 550) {
return 2; // dimensions of the third column
}
else if (x > 350 && x < 450 && y > 50&& y < 550) {
return 3; // dimensions of the fourth column
}
else if (x > 450 && x < 550 && y > 50&& y < 550) {
return 4; // dimensions of the fifth column
}
else {
return -1;
}
}
    public static boolean verifyPos(int xPos, intyPos, int row, int col){ // this method should modify all arraysaccordingly
boolean success = false;

if (yPos == row && xPos == col){
success = true;
}

return success;
}
public static boolean isFinished(boolean found){
// Convert to verify if all the treasures have been found
if (found == false)
return false;
return true;
}
}

Expert Answer


Answer to Introduction The goal of this project is to implement a program that allows the user to play a version of find the treas… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *