Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, so I am almost finished with my domino game which involves java, I need to have a working flip method so it swaps the

Hello, so I am almost finished with my domino game which involves java, I need to have a working flip method so it swaps the numbers around,so the first piece would be 6 on top and 1 on the bottom if i called the flip method. image text in transcribed

// This program is copyright VUW. // You are granted permission to use it to construct your answer to a COMP102 assignment. // You may not distribute it in any other way without permission.

/* Code for COMP102 - 2022T3, Assignment 5 * Name: * Username: * ID: */

import ecs100.*; import java.util.*; import java.awt.Color; import java.util.ArrayList;

/** YOUR DOCUMENTATION COMMENT */

public class DominoGame{ public static final int NUM_HAND = 5; // Number of dominos in hand

// Fields: hand, table and selectedPos private Domino[] hand;; // the hand (fixed size array of Dominos) private ArrayList table; // the table (variable sized list of Dominos)

private int selectedPos = 0; // selected position in the hand. private int first; private int second; // (You shouldn't add any more fields for core or completion)

/** YOUR DOCUMENTATION COMMENT */ public DominoGame() { // Initialize the hand and table fields hand = new Domino[5]; table = new ArrayList(); first = (int)(Math.random() * 6) + 1; second = (int)(Math.random() * 6) + 1; }

public void restart(){ table.clear(); // Set the hand field to be empty for (int i = 0; i

this.redraw(); }

/** YOUR DOCUMENTATION COMMENT */ public void pickup(){ for (int i = 0; i

this.redraw(); }

/** YOUR DOCUMENTATION COMMENT */ public void removeDomino(){ // Remove the domino at the selected position in the hand (if there is one) if (hand[selectedPos] != null) { hand[selectedPos] = null; } this.redraw(); }

/** YOUR DOCUMENTATION COMMENT */ public void placeDomino(){ // Move the domino at the selected position in the hand (if there is one) to the table if (hand[selectedPos] != null) { table.add(hand[selectedPos]); hand[selectedPos] = null; }

this.redraw(); }

public void Domino() { // Initialize the numbers on the domino to be random values first = (int)(Math.random() * 6) + 1; second = (int)(Math.random() * 6) + 1; }

public int getFirst() { return first; }

public int getSecond() { return second; }

public void flipNums() { // Swap the values of first and second int temp = first; first = second; second = temp; }

public void flipDomino() { int temp = first; first = second; second = temp; }

/** YOUR DOCUMENTATION COMMENT */ public void moveToLeftEnd(){ if (selectedPos > 0) { Domino temp = hand[selectedPos]; hand[selectedPos] = hand[selectedPos - 1]; hand[selectedPos - 1] = temp; selectedPos--; }

this.redraw(); }

/** YOUR DOCUMENTATION COMMENT */ public void moveToRightEnd(){ if (selectedPos

this.redraw(); }

//======================================================== // Methods for GUI and display. ALL WRITTEN FOR YOU //========================================================

/** * Set up the GUI with buttons and mouse. * Start the first game. */ public void setupGUI(){ UI.setMouseListener( this::doMouse ); UI.addButton("Pickup", this::pickup); UI.addButton("Put back", this::removeDomino); UI.addButton("Place", this::placeDomino); UI.addButton("Flip", this::flipDomino); UI.addButton("Left", this::moveToLeftEnd); UI.addButton("Right", this::moveToRightEnd); UI.addButton("Suggest", this::suggestDomino); UI.addButton("Restart", this::restart); UI.addButton("Quit", UI::quit);

UI.setWindowSize(1100,500); UI.setDivider(0.0); // graphics pane only this.restart(); }

// constants for the layout public static final int HAND_LEFT = 80; // x-position of the center of the leftmost Domino in the hand public static final int HAND_Y = 60; // y-Position of all the Dominos in the hand

public static final int TABLE_LEFT = 60; public static final int TABLE_Y = 170;

public static final int SPACING = 4; // distance between Dominos when laid out.

/** * Redraw the table and the hand. */ public void redraw(){ UI.clearGraphics(); this.drawHand(); this.drawTable(); }

/** * Draws the outline of the hand, * draws all the Dominos in the hand, * highlights the selected position in some way */ public void drawHand(){ for (int t=0; t

}

/** * Draws the list of Dominos on the table, 7 to a row * Note, has to wrap around to a new row when it gets to the * edge of the table */ public void drawTable(){ int x = TABLE_LEFT; int y = TABLE_Y; int count = 0; for (Domino domino : this.table){ domino.draw(x, y, true); x = x + Domino.LENGTH+SPACING; count++; if (count == 7){ x = TABLE_LEFT; y = y + Domino.WIDTH+3*SPACING; count = 0; } }

}

/** * Allows the user to select a position in the hand using the mouse. * If the mouse is released over the hand, then sets selectedPos * to be the index into the hand array. * Redraws the hand and table */ public void doMouse(String action, double x, double y){ if (action.equals("released")){ if (Math.abs(y - HAND_Y) = HAND_LEFT-Domino.LENGTH/2 && x

/** * Creates an object, calls setupGUI, and calls restart. */ public static void main(String[] args){ DominoGame obj = new DominoGame(); obj.setupGUI(); obj.restart(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

More Books

Students also viewed these Databases questions