Question

Practically dying here with this, need help ASAP, just need to do hide(), hideBoth(), and matchFound(),...

Practically dying here with this, need help ASAP, just need to do hide(), hideBoth(), and matchFound(), then implement them, so far in my version i tentatively made them, but I don't know ho to implement them so i posted the original code before i made my version of the three methods listed above. Need help fast, this is ridiculous.

Implement just one requirement at a time. For example, try implementing the case where after the user clicks 2 squares, both squares are hidden. (Then implement the other cases). In the game, the user clicks two squares at a time. Both colors are shown, but if they don't match they are hidden again. Need help finishing and getting the objective done. ___________________________________________________________ Lab4.java ___________________________________________________________ package application; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.stage.Stage; import javafx.scene.Parent; import javafx.scene.Scene; /** * Memory Game * This application is a basic memory game. The user is shown a 2x4 grid of gray squares. * A square is "revealed" when the user clicks on it - the color of the square is then shown. * Squares are gray when "hidden", and can be red, green, blue, or yellow when shown. * * The user begins by revealing 2 squares. If both squares are the same color, the squares * remain shown, otherwise the squares are hidden again (and turn gray). * * The game ends when the user has matched all of the squares (all colors are shown). */ public class Lab4 extends Application { @Override public void start(Stage primaryStage) { try { // Load the FXML file for the game board Parent root = FXMLLoader.load(getClass().getResource("/board.fxml")); // Set the scene onto the stage primaryStage.setScene(new Scene(root, 800, 400)); // Display the board to the user primaryStage.show(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { // Launches the application (calls start method in the process) launch(args); } } ___________________________________________________________ MainController.java ___________________________________________________________ package application.controller; import application.model.Board; import javafx.concurrent.Task; import javafx.concurrent.WorkerStateEvent; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.scene.control.Button; public class MainController implements EventHandler{ @FXML Button zero0, zero1, zero2, zero3, one0, one1, one2, one3; private Board gameBoard = new Board(); @Override public void handle(ActionEvent event) { Button selected = (Button) event.getSource(); System.out.println("User selected square: " + selected.toString()); String color = gameBoard.reveal(selected.getId()); if( color!=null ) selected.setStyle("-fx-background-color: #" + color ); sleep( selected, 1 ); // NEW System.out.println( gameBoard ); } /** * Sleep method handles waiting for some period of time before changing * the color of the given Button back to default (light gray color). * * @param selected Button clicked by the user * @param seconds Integer value of number of seconds to sleep */ public void sleep( Button selected, int seconds ) { Task sleeper = new Task() { @Override protected Void call() throws Exception { try { Thread.sleep(seconds*1000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } }; EventHandler e = new EventHandler() { @Override public void handle(WorkerStateEvent event) { selected.setStyle(""); } }; sleeper.setOnSucceeded( e ); new Thread(sleeper).start(); } } ___________________________________________________________ Board.java ___________________________________________________________ package application.model; import javafx.scene.paint.Color; public class Board { private Square[][] squares; public Board() { this.squares = new Square[2][4]; squares[0][0] = new Square(Color.RED); squares[0][1] = new Square(Color.BLUE); squares[0][2] = new Square(Color.GREEN); squares[0][3] = new Square(Color.YELLOW); squares[1][0] = new Square(Color.GREEN); squares[1][1] = new Square(Color.RED); squares[1][2] = new Square(Color.YELLOW); squares[1][3] = new Square(Color.BLUE); } public void hide( int x, int y ) { // TODO: Hide the given square // Hint: call the corresponding method in the Square class to help you hide it. } public void hideBoth() { // TODO: Hide both squares - the user's 1st & 2nd choices this round // Hint: this should be called if no match is found // Hint 2: this method should call a hide method twice, one for each square. } public boolean matchFound() { // TODO: Check to see if a match is found, and return true if so return false; } public String reveal( String buttonName ) { int x = buttonName.startsWith("zero") ? 0 : 1; int y; if( buttonName.endsWith("zero") ) y = 0; else if( buttonName.endsWith("one") ) y = 1; else if( buttonName.endsWith("two") ) y = 2; else y = 3; return this.reveal(x,y); } public String reveal( int x, int y ) { squares[x][y].reveal(); return squares[x][y].getColorAsCode(); } /** * ToString method returns a String representation of the current state of the game board. * * @return String Board colors currently revealed. */ public String toString() { String ret = squares[0][0] + ", " + squares[0][1] + ", "; ret += squares[0][2] + ", " + squares[0][3] + "\n"; ret += squares[1][0] + ", " + squares[1][1] + ", "; ret += squares[1][2] + ", " + squares[1][3]; return ret; } } ___________________________________________________________ Square.java ___________________________________________________________ package application.model; import javafx.scene.paint.Color; public class Square { private boolean isDisplayed; private Color color; private static final String BLUE = "0000FF"; private static final String GREEN = "00FF00"; private static final String RED = "FF0000"; private static final String YELLOW = "FFFF00"; public Square( Color c ) { this.color = c; this.isDisplayed = false; } public Color getDisplayedColor() { if( this.isDisplayed() ) return this.getColor(); else return Color.GRAY; } public Color getColor() { return this.color; } public boolean isDisplayed() { return isDisplayed; } public void hide() { this.isDisplayed = false; } public void reveal() { this.isDisplayed = true; } public String getColorAsCode() { if( !isDisplayed() ) return Color.GRAY.toString(); if( color.equals(Color.RED) ) return RED; else if( color.equals(Color.GREEN) ) return GREEN; else if( color.equals(Color.BLUE) ) return BLUE; else return YELLOW; } public String getColorAsString() { if( !isDisplayed() ) return "GRAY"; if( color.equals(Color.YELLOW) ) return "YELLOW"; else if( color.equals(Color.GREEN) ) return "GREEN"; else if( color.equals(Color.BLUE) ) return "BLUE"; else if( color.equals(Color.RED) ) return "RED"; else return "GRAY"; } public String toString() { return this.getColorAsString(); } } ___________________________________________________________ board.fxml ___________________________________________________________

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the Board.java below.

CODE

============================

import javafx.scene.paint.Color;

public class Board {

private Square[][] squares;

public Board() {

this.squares = new Square[2][4];

squares[0][0] = new Square(Color.RED);

squares[0][1] = new Square(Color.BLUE);

squares[0][2] = new Square(Color.GREEN);

squares[0][3] = new Square(Color.YELLOW);

squares[1][0] = new Square(Color.GREEN);

squares[1][1] = new Square(Color.RED);

squares[1][2] = new Square(Color.YELLOW);

squares[1][3] = new Square(Color.BLUE);

}

public void hide( int x, int y ) {

squares[x][y].hide();

}

public void hideBoth(int x1, int y1, int x2, int y2) {

hide(x1, y1);

hide(x2, y2);

}

public boolean matchFound(int x1, int y1, int x2, int y2) {

if(reveal(x1, y1).equals(reveal(x2, y2))) {

return true;

}

hideBoth(x1, y1, x2, y2);

return false;

}

public String reveal( String buttonName ) {

int x = buttonName.startsWith("zero") ? 0 : 1;

int y;

if( buttonName.endsWith("zero") )

y = 0;

else if( buttonName.endsWith("one") )

y = 1;

else if( buttonName.endsWith("two") )

y = 2;

else

y = 3;

return this.reveal(x,y);

}

public String reveal( int x, int y ) {

squares[x][y].reveal();

return squares[x][y].getColorAsCode();

}

/**

* ToString method returns a String representation of the current state of the game board.

* @return String Board colors currently revealed.

*/

public String toString() {

String ret = squares[0][0] + ", " + squares[0][1] + ", ";

ret += squares[0][2] + ", " + squares[0][3] + "\n";

ret += squares[1][0] + ", " + squares[1][1] + ", ";

ret += squares[1][2] + ", " + squares[1][3];

return ret;

}

}

How to Use

==============================

To use matchFound(), you need to obtain the value of x1, y1, x2, y2.

You can refer to reveal(String button) function to see how to obtain the value of x and y of a square.

Add a comment
Know the answer?
Add Answer to:
Practically dying here with this, need help ASAP, just need to do hide(), hideBoth(), and matchFound(),...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please I need your help I have the code below but I need to add one...

    Please I need your help I have the code below but I need to add one thing, after player choose "No" to not play the game again, Add a HELP button or page that displays your name, the rules of the game and the month/day/year when you finished the implementation. import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; import javax.swing.event.*; import java.util.Random; public class TicTacToe extends JFrame implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board...

  • Hi, for my Java class I have built a number guessing game. I need to separate...

    Hi, for my Java class I have built a number guessing game. I need to separate my code into two classes and incorporate a try-catch statement. Any help would be appreciated! Here is my code. import javax.swing.JOptionPane; import javax.swing.UIManager; import java.awt.Color; import java.awt.color.*; import java.util.Random; public class game { public static void main (String [] args) { UIManager.put("OptionPane.backround", Color.white); UIManager.put("Pandelbackround", Color.white); UIManager.put("Button.background", Color.white); Random nextRandom = new Random(); int randomNum = nextRandom.nextInt(1000); boolean playerCorrect = false; String keyboardInput; int playerGuess...

  • Java Program The goal of this assignment is to develop an event-based game. You are going...

    Java Program The goal of this assignment is to develop an event-based game. You are going to develop a Pong Game. The game board is a rectangle with a ball bouncing around. On the right wall, you will have a rectangular paddle (1/3 of the wall height). The paddle moves with up and down arrows. Next to the game board, there will be a score board. It will show the count of goals, and count of hits to the paddle....

  • "Polygon Drawer Write an applet that lets the user click on six points. After the sixth point is clicked, the applet should draw a polygon with a vertex at each point the user clicked." import...

    "Polygon Drawer Write an applet that lets the user click on six points. After the sixth point is clicked, the applet should draw a polygon with a vertex at each point the user clicked." import java.awt.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.applet.*; public class PolygonDrawer extends Applet implements MouseListener{ //Declares variables    private int[] X, Y;    private int ptCT;    private final static Color polygonColor = Color.BLUE; //Color stuff    public void init() {        setBackground(Color.BLACK);        addMouseListener(this);        X = new int [450];        Y =...

  • The program given here will draw the Olympic logo. Modify the program as the following: 1.Change...

    The program given here will draw the Olympic logo. Modify the program as the following: 1.Change the class name to Test1OlympicYourFirstNameYourLastName 2.Change the OlympicRingPanel class name to OlympicRingPanelYourfirstNameYourLastName 3.Modify the program so that the first ring will be filled with random color with a random alpha value. I have shown 3 sample output here for your references. 4.Modify the paintComponent so your name will be displayed under the Olympic rings.The color of your name should be random color as well....

  • Need help writing Java code for this question. CircleFrame class is provided below. what happen after...

    Need help writing Java code for this question. CircleFrame class is provided below. what happen after u add the code and follow the requirement? It would be nice to be able to directly tell the model to go into wait mode in the same way that the model's notify method is called. (i.e. notify is called directly on the model, whereas wait is called indirectly through the suspended attribute.) Try altering the the actionPerformed method of the SliderListener innner class...

  • Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically look...

    Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically looking for codes that could make my memory match game more difficult or more interesting.(a timer, difficulty level, color, etc.) GUI must be included in the code. Here is what I got so far: package Card; import javax.swing.JButton; public class Card extends JButton{    private int id;    private boolean...

  • Introduction to Java Programming Question: I’m doing a java game which user clicks on two different...

    Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically looking for codes that could make my memory match game more difficult or more interesting.(a timer, difficulty level, color, etc.) GUI must be included in the code. Here is what I got so far: package Card; import javax.swing.JButton; public class Card extends JButton{    private int id;   ...

  • JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to...

    JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...

  • JAVA JAR HELP...ASAP I have the code that i need for my game Connect 4, but...

    JAVA JAR HELP...ASAP I have the code that i need for my game Connect 4, but i need to create a jar file . the instructions are below. It has to pass some parameters. I am really confused on doing so.l I dont know what to do next. Can someone help me and give detailed descritiopm opn how you ran the jar file in CMD import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Connect4 {               ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT