Question
Hello, I am making a Domino like game and it needs to look like this, i have added instructions on what it has to do
Hello, I am making a Domino like game and it needs to look like this, i have added instructions on what it has to do but i do not know how to write the code please help. I have made a Domino class, sorry for the other person who already answered could you answer again? you have helped me so much thank you
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
private int selectedPos = 0; // selected position in the hand.
// (You shouldn't add any more fields for core or completion)
/** YOUR DOCUMENTATION COMMENT */ public void restart(){ hand = new Domino [NUM_HAND]; table = new ArrayList
this.redraw(); }
/** YOUR DOCUMENTATION COMMENT */ public void pickup(){
this.redraw(); }
/** YOUR DOCUMENTATION COMMENT */ public void removeDomino(){ this.redraw(); }
/** YOUR DOCUMENTATION COMMENT */ public void placeDomino(){ /*# YOUR CODE HERE */
this.redraw(); }
/** YOUR DOCUMENTATION COMMENT */ public void flipDomino(){ /*# YOUR CODE HERE */
this.redraw(); }
/** YOUR DOCUMENTATION COMMENT */ public void moveToLeftEnd(){ /*# YOUR CODE HERE */
this.redraw(); }
/** YOUR DOCUMENTATION COMMENT */ public void moveToRightEnd(){ /*# YOUR CODE HERE */
this.redraw(); }
/** YOUR DOCUMENTATION COMMENT */ public void suggestDomino(){ /*# YOUR CODE HERE */
}
// 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.awt.Color;
/** * Domino * Represents individual Dominos - rectangular tiles with two numbers * The constructor will return a new Domino with two random numbers (0-6) on it * Methods: * draw(double x, double y, boolean horiz) draws the domino centered at (x,y), * horizontally (first number on left, second on right) if horiz is true; * vertically (first number above second) if horiz is false * * flipNums() swaps the two numbers on the domino * getFirst() and getSecond() return the first/second numbers of the domino * */
public class Domino{
private int first; private int secnd;
public static final int WIDTH = 50; // the short dimension of the domino public static final int LENGTH = WIDTH*2; // the long dimension of the domino public static final int DIAM = WIDTH/6; // the diameter of the spots
/** * Construct a new Domino object with a pair of random values on it */ public Domino(){ this.first = (int)(Math.random()*7); // between 0 and 6 inclusive this.secnd = (int)(Math.random()*7); // between 0 and 6 inclusive }
/** * Switches the numbers on the domino */ public void flipNums(){ if (this.first != this.secnd){ int tmp = this.first; this.first = this.secnd; this.secnd = tmp; } }
/** * Return the left number on the Domino */ public int getFirst(){ return this.first; }
/** * Return the right number on the Domino */ public int getSecond(){ return this.secnd; }
/** * Draws the domino centered at the position (x, y) * either vertically or horizontally */ public void draw(double x, double y, boolean horiz){ UI.setLineWidth(2); UI.setColor(Color.black); double wd = (horiz)?LENGTH:WIDTH; double ht = (horiz)?WIDTH:LENGTH; double left = x - wd/2; double top = y - ht/2; UI.fillRect(left, top, wd, ht); UI.setColor(Color.red.darker()); UI.drawRect(left, top, wd, ht);
UI.setLineWidth(2); UI.setColor(Color.gray); if (horiz){ UI.drawLine(x, top+2, x, top+ht-2); UI.setLineWidth(1); this.drawNumber(this.first, x-wd/4, y); this.drawNumber(this.secnd, x+wd/4, y); } else { UI.drawLine(left+2, y, left+wd-2, y); UI.setLineWidth(1); this.drawNumber(this.first, x, y-ht/4); this.drawNumber(this.secnd, x, y+ht/4); } }
/** * Draw the number in the square centered at (x, y) * using white circles * The size of the square is WIDTH times WIDTH */ public void drawNumber(int num, double x, double y){ double xOff = x-DIAM/2; // offset by radius of spots double yOff = y-DIAM/2; // offset by radius of spots double left = xOff-WIDTH*0.25; double centr = xOff; double right = xOff+WIDTH*0.25; double top = yOff-WIDTH*0.25; double mid = yOff; double bot = yOff+WIDTH*0.25; UI.setColor(Color.white); if (num%2 == 1){ // 1, 3, 5 UI.fillOval(centr, mid, DIAM, DIAM); } if (num>1){ // 2, 3, 4, 5, 6 UI.fillOval(left, top, DIAM, DIAM); UI.fillOval(right, bot, DIAM, DIAM); } if (num>3){ //4, 5, 6 UI.fillOval(left, bot, DIAM, DIAM); UI.fillOval(right, top, DIAM, DIAM); } if (num==6){ UI.fillOval(left, mid, DIAM, DIAM); UI.fillOval(right, mid, DIAM, DIAM); } }
}
Dominoes are rectangular tiles that have two numbers on them, usually represented by patterns of dots (no dots represents 0 ). There are many games using dominoes, many of which involve a player picking up a "hand" of dominoes, and then placing them on the "table" according to some rules. The DominoGame program allows a player to play one of these games. The program displays the set of five dominoes in their hand, and the dominoes on the table. It allows the user to - Pick up a new domino to fill an empty space in the hand; - Select a domino in the hand (with the mouse) - Remove the selected domino ("put it back in the bag") - Place the selected domino on the table. - "Flip" the numbers on the selected domino - Rearrange the order of the dominoes in their hand; The program has nine buttons ("Pickup", "Put back", "Place", "Flip", "Left", "Right", "Suggest", "Restart", and "Quit") and also responds to the mouse. The following is a screenshot of the layout of the "hand" and the "table", showing a hand with four dominoes, and a "table" with ten dominoes. The overall design of the DominoGame program is provided, along with declarations of the fields and constants you need, and completed code for the GUI and displaying the hand and table. You have to complete the definitions of the methods that do all the work! A Domino class is also provided. It contains - a constructor for creating a new Domino, - a draw (double x, double y, boolean horiz) method for drawing a Domino centered at (x,y), either vertically or horizontally, and - getFirst() and getsecond() methods for returning the first and second numbers on a Domino - a flipnums () method to flip the numbers on a Domino - constants specifying the size of the dominoes. Read through the provided code to make sure you know what it does. Program Design The program has two fields to keep track of the dominoes: - hand : an array that can hold up to 5 Domi noes. It will contain null in any position that does not have a Domino. - table : an ArrayList of the Dominoes that have been placed on the table. The hand is displayed with a rectangular border and each domino drawn in its place. Empty spaces (containing nuil) are displayed as empty. If there are any empty positions on the hand, the user can use the Pickup button to get a new (random) domino which will be added to the hand at the leftmost empty position. If there are no empty positions, Pickup should do nothing. The user can select a position on the hand using the mouse. The selected position (domino or empty space) is highlighted by drawing a border around it. The index of the selected position is stored in the selectedpos field. If the selected position contains a domino, the user can use the *Put Back* button to remove the selected domino, or the Place button to move the selected domino to the table, or the Flip button to swap the two numbers on the domino. The user can use the Left or Right button to move the contents of the selected position (whether it is a Domino or a space) to the left end of the hand or to the right end of the hand. The table is represented by an ArrayList of Domino objects, which are drawn in rows underneath the hand. At the beginning of the game the table should be empty. Dominoes placed on the table should be added to the end. - Complete the restart( ) method so that it sets the table field to be empty sets the hand field to be empty - Complete the pickup( ) method so that it looks for an empty position in the hand array, and if there is one, creates a new Domino object and puts it at that position in the hand array. - Complete the removedomino() method so that it removes the domino at the selected position in the hand (if there is one). - Complete the placedomino() method so that it moves the domino at the selected position in the hand (if there is one) to the table. - Test your program carefully, to make sure that it works correctly in all cases: - when the hand is empty, when it is full, and when it is only part full. when the selected position is at the ends and when it is not at the ends. Completion - Complete the flipDomino () method so that it flips over the domino at the selected position in the hand (if there is one). - Complete the moveToleftend() and moveTorightand() methods so that they move the contents of the currently selected position on the hand to the left or the right ends of the hand, moving the dominoes and spaces in-between one step to make space. Note that the methods should not lose any dominoes or change the order of the dominoes except for the one being moved. - Complete the suggestDomino ( ) method, which finds a domino in the user's hand which can be placed into a legal position on the table. If there is such a domino, it should tell the user which domino it is; if there is no domino that matches, it should inform the user that there are no posible moves. The basic rule of Dominoes is that If there are no dominoes on the table yet, only a double (a domino with the same number left and right) can be placed on the table. (Strictly, the double domino should be placed vertically, but you don't need to worry about that). A domino can only be placed next to another domino if they have matching numbers (one of the numbers on the domino in the hand is the same as the right-hand number on the last domino on the table). - Let the user move Dominoes around on the hand by dragging them with the mouse. If a Domino is dragged to another Domino, it should be inserted by moving the other Dominoes along. If a Domino is dragged to a space, it can simply be put in the space
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started