Design Class - How To Organize Classes For UNO Card Game

Mar 20, 2014

I'm currently taking a computer program design class which has done a lot for my understanding of how to organize classes, but isn't giving me challenging enough assignments and I don't believe it's going to be covering interfaces and abstract classes, which is a shame. So I've been digging into these topics myself and decided to work on my own program (an Uno game program) that would utilize everything we've been learning and give me some practice with GUIs.

My current plan:

Have an abstract UnoCard class that determines the basic properties/methods common to all cards. Create a class for each card type extending from UnoCard, which would be - the generic card (number and color), action cards (skip, reverse, draw two), and special cards (wild, wild draw four, and blank).

Two enums, one for color, one for rank (which includes the numbers, as well as the action and special card ranks (reverse, wild, exc.) ).

A deck class would have an ArrayList <UnoCard> property and it's constructor would initialize a fresh deck.

A hand class that also has an ArrayList <UnoCards> where it gets said cards from the deck class.

A discard pile class, which contains the cards discarded and the current card in play.

A "board" class (haven't figured out a better name for it yet) which would determine/keep track of the number of players/hands, the turn order, the locations of the cards, and the winning condition.

Area of confusion and concern I'm having:

From what I've read, I want to avoid circular dependency. So if that's the case, when a card type effects the state of a "hand" or the turn order or really anything else, then in what class do I place the method(s) that effect that? If I place it in the specific card class, wouldn't that create a circular dependency? So would it be better then to have the hand class figure out what can be done with a specific card and what that specific card effects (which wouldn't that hinder the cohesion of the class?)?

I was also thinking a possible solution might be to have the non-generic card types contain methods that return values as apposed to manipulating higher level classes, such as a boolean drawCards which returns true if cards need to be drawn, false otherwise (same for skip, reverse, exc.).Then maybe the board class can determine what to do if those values are true or false (which actually seems more convoluted since only one value would be allowed to be true at any given time).

The other solution I was considering is to have a single UnoCardRules class, which serves the sole function of providing methods to determine the effects of each card, that way each card class can only worry about defining the card's state.

View Replies


ADVERTISEMENT

Deck Card Game - Creating Classes For Code

May 17, 2014

I created the below code and it works fine. It is a deck card game. I just want to divide my code into classes so that it seems more organized.

Java Code:

package getimage;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.*;

[code]....

View Replies View Related

Trading Card Game - Each Card Can Be Moved Around Like Chess Pieces

May 10, 2014

So I'm making a trading card game and each card can be moved around like chess pieces. So far, I've made the cards simple rectangles and detect if clicks are made within that rectangle and then move them appropriately, but I'm not sure if that's the best solution.

View Replies View Related

How To Design Classes

Mar 29, 2014

design a class to conduct a survey of three hospitals. you want to know how many sectors (eg operation, children, gastronomic) each hospitals have, and how many doctors there are in each sector.

what is the process of class design?

View Replies View Related

Card Game War Comparing Rank

Oct 30, 2014

I am making the card game War, I have gotten fairly far without a huge snag but I have been working on this. I need to compare 2 cards to find the larger of the 2. I have a function that will do that, but it is comparing the wrong numbers. The function is comparing their index values but I need it to compare the card value. The card value is found at rank(card).

import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Collections;
 
[Code] ....

View Replies View Related

Implementing A Card Game In JAVA

Apr 19, 2015

I am facing with a JAVA code needed for my assignment.We are implementing a Card Game.. There is a Card Class which i have not included in the code below .We have a Dec class that has various arrays..

privateCard[] masterPack >> Is a master Pack that is initialized once and has all the 52 cards.

private Card[] cards >> Is the array that has the card values from masterPack array.

My problem is :

After I called the Deck object D1, it goes to the Deck COnstrucutor OK.It then populates the values of masterPack as per the allowmasterPack method.Then it stores the values of masterPack into cards array in the init method..I get an exception when line below is executed..

cards[pack * 52 + k]= masterPack[k];

I get a java.lang.NullPointerException error..

public class debug
{
public static void main(String[] args)
{
int pack = 1;
Dec D1;
D1 = new Dec(pack);

[oode]...

View Replies View Related

5 Card Stud Game - Constantly Repainting Screen

Apr 11, 2014

I am currently making a 5 card stud game for my Java Programming class at my highschool. I am deciding to go a little above expectations and make my poker game a little more fun than just comparing a few cards. However I want there to be text that is changing on my screen, but the repaint() is not the most reliable way to make the screen update, I am wanting a way for have the program constantly update the screen so that if I do something like someLabel.setText("Text"); then the text will update automatically whenever it is changed.

Here is my declarations and main/constructor method

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class FiveCard extends JFrame implements ActionListener {

[Code] .....

Here is the method addMainPage, which is called in the constructor. Whenever the enterButton is pressed, the actionListener directs it to loadBoard();

I use GridBag Layout, so to skip through all of the constraints

public void addMainPage() {
for (int i = 0; i < 4; i++) {
players[i] = new Player();
}
Icon cardBackImage = new ImageIcon("back.gif");

[Code] .....

Game intro is a method that changes the text of a label every second using .setText(), however it does not work because the enterButton is pressed, then after loadBoard() and gameIntro() is ran, it repaints the frame with the board objects and the gameIntro's final .setText() command.The action listener is simple...

public void actionPerformed(ActionEvent e) {
if (e.getSource() == enterButton) {
players[0].setName(playerNameField.getText());
loadBoard();
}
p.repaint();
}

View Replies View Related

Design And Implement Word Guessing Game

Dec 29, 2013

I have a project about programing a game in java . In this project you are required to design and implement a word guessing game. In the game, user selects one of the available dictionaries (animals, plants, technical, names, etc. ), and the program randomly chooses one word from that dictionary and displays it by scrambling the letters. The user tries to guess the word while a guess counter starts to count down (lets assume it starts from 5).

While counter counts towards zero, the program chooses -randomly- two misplaced letters and puts them into correct positions to reveal the word more and give the user a hint. However, giving a new hint reduces the total value of the question. Lets say if the initial value of the question is 500 when all letters are scrambled, it reduces to 400 after 1st incorrect guess and hint, then after every incorrect guess it reduces more. You can determine the value of the question by the length of the word. The other details of the game is as follows:

1.You must design at least three classes (for example game, scrambledword, and dictionary).

2.Dictionaries are written in text files. If you design a class for dictionary, it must be constructed by a user chosen category ((animals, plants, technical, names, etc). In the constructor, the dictionary text file corresponding to this category must be opened and read; and words in that dictionary must be read into memory (e.g. String array). You must submit your dictionaries with your code. Do not share dictionaries between friends.

3.The game will record your total points and name in a text file where a hall of fame which has different users and their total points in 10 questions. You can design and implement a class for this.

4.Bonus+5%: Put user interface modules textboxes, buttons, etc Or

a.Use class hierarchy

b.Use of thread and timers and reducing the question value by timer instead of guesses.

View Replies View Related

Design Open Answer Quiz Game

Oct 31, 2014

I want to design an open-answer quiz game using Java. Basically, the plan is to design a quiz where the name of a medicine is displayed as "the question" and the player will need to input the unique code (as "the answer") of the medicine in an open box (all the codes are two characters). The list has about 204 named medicine (with potentially more to be included at a later stage). The questions will not appear in the same order for each session restarted; they will appear randomly.

The player will have 60 seconds, and for each correct answer, will score 1 point and add 2 seconds to the timer. The player will also have 6 "lives", and for each incorrect answer, the player will lose a life, with no effect on the time.The idea of the game is to support the player memorise as many of these medicine codes as possible. How this can be achieved from total grassroots.

View Replies View Related

Change Frame And Focus In Card Layout From External Class / Trigger

Feb 18, 2014

I've constructed 4 different levels and allow the user to select the level they want to play from a central JPanel in a Card Layout system. My problem is that once a level is completed, I can't switch the JPanel which is displayed to start the next level, since I don't know how to access the original JPanel which acts as a driver for the other panels.

MainFrame.java
Java Code: import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

[code]....

View Replies View Related

Design A Class To Represent A Rectangle

Jan 8, 2015

I was told to design a class named Rectangle to represent a rectangle.The class contains:

■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height.
■ A no-arg constructor that creates a default rectangle.
■ A constructor that creates a rectangle with the specified width and height.
■ A method named getArea() that returns the area of this rectangle.
■ A method named getPerimeter() that returns the perimeter.

Draw the UML diagram for the class and then implement the class. Write a test program that creates two Rectangle objects one with width 4 and height 40 and the other with width 3.5 and height 35.9. Display the width, height, area,and perimeter of each rectangle in this order.Here is my code for the Rectangle Class:

class Rectangle
{
double width;
double height;
public Rectangle() {
width = 1;
height = 1;

[code]....

The error that I am given when I compile the driver is as follows:constructor Rectangle in class Rectangle cannot be applied to given types; required: no arguments; found:int,int; reason: actual and formal argument lists differ in length.

View Replies View Related

Create Word Game Using Classes And Objects

Feb 14, 2015

For class, we need to create a word game using classes and objects.

The game is played in rounds. The player is presented with a word that is missing letters. The player has to fill in the missing spaces with their letter guesses. The words presented are chosen with a random number generator which has been provided for us. At the end of the game, the player is shown their score.

In steps, I have to:

-Welcome the player.
-Present the puzzle.
-Allow the player to fill in the blanks.
-Have the program check responses for correct/incorrect input.
-End the game if they have three misses, or continue if they complete the puzzle.

Now, to start, I have a class for the number generator, a class to store the array of 25 words, and a class for the game itself.

View Replies View Related

Way To Organize Incoming Strings

Oct 8, 2014

If I wanted to take strings in from the user through a GUI say day, month, year. I am looking for a way to organize those incoming strings. I am going to add this information to a queue. However, I want the people who put in the earliest day, month, and year to be put into the queue first followed by the next earliest so on and so forth.I was thinking what I would need to make a method that would compare the information.

View Replies View Related

Proper Way To Organize Code Into Blocks

Feb 16, 2015

When I run this (entering 12 for both questions) I get this error:

java.lang.ArrayIndexOutOfBoundsException: 12
at MathTablesTwo.main(MathTablesTwo.java:22)

Also, what would be the proper way to organize my code into blocks?

import java.util.Scanner;
public class MathTablesTwo {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);

[code]....

View Replies View Related

Create Two Inner Classes To ObjectFactory Class

Dec 15, 2014

I'm trying to get to grips with the AndEngine, so I've recently gotten hold of their Cookbook, but there's a section that's confusing. I'm using the Eclipse IDE with the section is asking that I create two inner classes to an ObjectFactory class. I'm not sure if what it wants me to do is doable in java or specifically in Eclipse. This is the section of code the book is asking me to create.

Java Code:

package com.example.helloworld;

public class ObjectFactory {
public static LargeObject createLargeObject(final int pX, final in pY){
return new LargeObject(pX,pY);
}
public static SmallObject createLargeObject(final int pX, final in pY){
return new SmallObject(pX,pY);
}
} mh_sh_highlight_all('java');

Now this is returning errors that I should declare these two as individual classes, but I feel like I am missing something. Earlier the book asks you to create this BaseObject class.

Java Code:

package com.example.helloworld;

public class BaseObject {
@SuppressWarnings("unused")
private int mX;
@SuppressWarnings("unused")
private int mY;

[code]...

It then mentions that the two classes LargeObject and SmallObject are inner classes of BaseObject and extends this class. The ObjectFactory class is meant to determine which subtype of the base class that needs, as well as define the objects properties.

View Replies View Related

Read CSV Files And Organize Data In Java?

Apr 13, 2014

I am trying to write a program that read from a csv file called matches.csv.

A single football match can end with a win or a draw: in first case the winner team get 3 points and the loser none, in the second case of draw each of the two teams get 1 point.

For example, the first line of the file matches.txt is as follow:

In the file it contains the following data.

17/08/2013ArsenalAston Villa13
24/08/2013Aston VillaLiverpool01

This means that a match has been played on the 17/08/2013 where Arsenal scored 1 goal while Aston Villa 3 goals: thus Arsenal got 0 points while Aston Villa 3 points.

How can I structure my output to make it make it read

Position Team Played Points
1 Aston Villa 2 3
2 Liverpool 1 3
3 Arsenal 1 0

import java.io.File;

import java.io.FileNotFoundException;
import java.util.Scanner;
 public class Teams { 
public static void main(String[] args) {
String fileName = "matches.csv";
File file = new File(fileName);

[code]....

View Replies View Related

How To Organize Queries In Java Source Code

Oct 7, 2014

Lately I've been working with JDBC and writing queries in some Java programs. I've noticed that I keep experimenting with where I place my blocks of querying code. I've tried creating a private method in the class where the query is used, I've created utility classes where I can call the query when needed, and sometimes I've just put it in a localized block of code where I need to. Also, most often these queries are one-shot queries where I need to pull the data to populate a JTable.

In short, this has made me realize that I don't have a solid practice for organizing my queries in Java. So my question is "Is there a practice for organizing query code?".

View Replies View Related

How To Apportion Responsibilities And Organize Object Creation

Aug 15, 2014

I have a couple of objects, which I will call height and width. The latter may depend on the former and I access them through an interface:

interface Height
{
double getHeight();
}
interface Width
{
double getWidth( Height height );
}

I then have something I will call a RectangleMaker, which represents some set of rectangles that can be made. It takes a list of heights and widths and keeps track of which ones have been selected and which ones can still be made. For example, the possible heights might be 2 or 3 and the possible widths 3 or 4. It needs to determine if it can make a rectangle with a specific area and if selected to make that rectangle, disallow any other heights. So if I said, you are in charge of 2 x 3 rectangles, it could still potentially also make 2 x 4 rectangles, but 3 x 3 rectangles would no longer be an option. For the most part I think these details are irrelevant to my question, which is really about organization and assignment of responsibilities.

interface RectangleMaker
{
void setHeights( ArrayList<Height> );
void setWidths( ArrayList<Width> );
boolean isAreaAvailable( double Area );
void selectDimension( Height height, Width width );
Height listAllowedHeight();
ArrayList<Width> listAllowedWidths();
}

Now I have a new requirement. The lists of heights now need to be associated with a source, as do the widths. I should keep track of a list of RectangleMakers and pick the 'most appropriate' one for a particular area. The rule is to sort first on the height source and then on the width source and the first one able to handle the area, gets the job. So I created two enums heightSource and widthSource and had RectangleMaker implement Comparable, so I can make an ArrayList<RectangleMaker> and sort it based on the rules. Then I traverse the list and the first one that returns isAreaAvailable() true gets the job.

The final bit is that these sources also imply a specific set of Heights or Widths. How I get that set varies, it may be a fixed value or values, or might be read from a file. So in principle I could have:

ArrayList<Height> buildHeights( RectangleMaker.SOURCE source )
{
switch ( source )
{
}
}

and have a lot of specific code that builds each list by whatever method is appropriate. I still need to deal with the fact I might need additional information to build the lists. For example, one source might require a min, max and increment and another might require a file name. So I started working in the direction of more interfaces.

interface HeightList
{
ArrayList<Height> getHeights();
RectangleMaker.Source getSource();
}

I am not totally comfortable with my enum lists. They solve the sorting problem, but I am not exactly sure which class should define them. Right now they are defined by the RectangleMaker. I would need to update this class every time I added an implementation of HeightList or WidthList.

I was also thinking that since the list is built from a specific source, that source should be associated with the list. That would lead me to make this change:

interface RectangleMaker
{
void setHeights( HeightList heightList );
void setWidths( WidthList widthList );
boolean isAreaAvailable( double Area );
void selectDimension( Height height, Width width );
Height listAllowedHeight();
ArrayList<Width> listAllowedWidths();
}

It seems maybe there should be a factory in here somewhere, but this is where I am having trouble sorting out exactly who has what responsibility. I can do this sort of thing with my HeightList interface:

class SpacedHeight implements HeightList
{
int start;
int end;
int step;
ArrayList<Height> heights;
RectangleMaker.SOURCE source;

[Code] ....

Should I be thinking of putting one more layer over all of this? What complicates my thinking are two things: multiple instances may have the same source and some of these instances are dynamic. For example, two SpacedHeight instances may have different ranges, but they are both SpacedHeight and it doesn't matter which gets picked first. Exactly what SpacedHeight instances get created is determined by prompting the user for the values. If the heights come from a file, every instance would be associated with a different source and the file names would be hard-coded.

I think I want to make a HeightFactory and I think then it would make sense to move my enum definitions there. I see how I would do that if I could hard-code a specific instance of a HeightList with a specific enum. I am less clear on how to handle the case where the factory needs different parameters for different HeightList implementations.

View Replies View Related

Tile Based Map Editor - Organize Layout

Apr 13, 2014

I'm trying to program a tile-based map editor and most of it is going quite smoothly except trying to organize the layout. Originally I was going to use 2 frames, one for the map, and 1 for the tile set, but after reading about frames I learned that that is bad practice and is also inconvenient because there is no way to have both frames in focus at once (so you need to click an extra time to gain focus when switching windows). So what I'd like to do is create a single frame application that holds 3 panels. One for the map, one for the tile set, and one for tile settings. This is basically what it would look like:

Note that the tile settings panel uses GUI elements (a border, a JLabel (which starts out empty), a JComboBox, and a JTextField) while the other 2 panels are just drawing panels*

Now I have tried multiple things which either did not work visually (panels were inside panels) or did not compile. One thing I tried was using BoxLayout to put the 2 tile panels vertically within a temporary panel which I then tried to add into the frame after the map panel (with FlowLayout), but that made it look like this: I honestly don't know what else I did that I should write here because in retrospect many of the tings I tried were silly or I don't remember exactly what I did. What would be the best layout to doing what I want?

View Replies View Related

Two Classes - How To Use Members Of Color Class Without Using Extends

Nov 4, 2014

I have two classes in two different files.

Color.java and Light.java

The Color class:

public class Color {
private int red;
private int green;
private int blue;
public Color(){
red = 0;
green = 0;
blue = 0;
}

And i have the Light class :

public class Light {
private Color color1;
private boolean switchedon;

public Light(int red, int green, int blue){
//dont know what to write here . how can i use the members of the Color class here ? without using extends.
}
}

View Replies View Related

Why Super Classes Always Declared As Interfaces And Not Abstract Class

Dec 1, 2014

While reading the design patter book, i got one doubt ,There is a List an interface having sub classes ArrayList, LinkedList etc.,

Q1) My question is Why they declared the List as interface rather than Abstract class?

Q2) i read some site -

List l = new ArrayList(); Why it is GOOD line?
ArrayList l = new ArrayList() ; Why it is BAD line?

Answer required with detailed information for Q1 and Q2.

View Replies View Related

Reading Class Files And Search For Access To Blacklisted Classes?

Oct 14, 2014

I am currently building a Plugin system for an application and I wanted to add a little security feature.

I have read many tutorials on this and they basically all came down to the same answer:

"Its complicated: You need a custom class loader and security manager and ... and even then its still tricky"

So I thought, before I load the classes dynamically I will simply read the class files. Why bother with a SecurityManager or ClassLoader if I can simply whitelist all allowed classes and search for all illegal Class access before I even load anything.

View Replies View Related

Game Class - Nothing Will Print Into Console

May 7, 2014

I'm new to java, and i'm trying to make a gaming, but for some reason nothing will print into the console. I want it to have the system to print out the frames, and ticks, but nothing will happen.

package ca.trey.game; 
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
 
[Code] .....

View Replies View Related

Text From The Class Game Doesn't Update

May 3, 2014

I have a main class that hold the frame and paints, a class for drawing my game, and an image that is drawn too.
I want to count the amount of times I click on this "android" (right now it counts if i click the screen) but the text from the class Game doesn't update.

androidX = Main.width - android.getWidth();

doesn't work. In my head the image should be inside the borders but it isn't. Have I calculated the pixels wrong or am I missing something?

public class Main extends JPanel{
public static final int width = 600;
public static final int height = 600;
Game game = new Game();
Android android = new Android();
public void paint(Graphics g) {
super.paint(g);
game.render(g);
android.render(g);

[code]....

View Replies View Related

Video Game - Collision Detection Class?

Apr 30, 2014

I use eclipse as my IDE. I have decided to make my own video game and somebody had sent me a class that could be used for collision detection. The problem with the collision detection class was that it made a box around an object and if something else had touched the object, the collision detection would work. My problem with this class is what happens when i want to use circles? I cant have a box drawn around it. Then it wouldn't work as i would want. Is there a pixel perfect collision detection class out there I can use? It'll be useful in my journey to become successful in computer science! By the way this is in the java language.

View Replies View Related

Hangman Game Using Class - It Keeps On Bringing Up Exception Error

Apr 23, 2015

I wrote a program that would play a game of Hangman. It plays the game but it keeps on bringing up a exception error. This is my class code

import java.util.ArrayList;
class Assignment2Q4
{
private final ArrayList<Character> guessList = new ArrayList<>();
private final char[] charArray;
private final String secretWord;
private int guesses;

[Code] ....

This is the output that is given:

Enter the secret word:
pizza
Guess a letter:
d
?????
Guess a letter:

[Code] .....

Also when I guess the first letter of the word correctly it stops the program without going further on with the guessing.

Like for ex. P????

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved