I am making a game called Set, it is a card game: here is a brief description of the rules:
The game of Set is played with 81 cards, each of which has 4 properties (number, pattern,
color and shape). The number of objects is 1, 2, or 3. The 3 possible patterns are solid,
striped, and open. The 3 possible colors are red, green, and purple. The 3 possible
shapes are diamond, squiggle, and oval. The 81 cards comprise all possible combinations
of patterns, colors and shapes. Some example cards are:
• 1 Striped red oval
• 2 Solid green squiggle
• 3 Open purple diamond
Ok now what I am having trouble with is creating my deck of 81 cards. The way I am trying to do it is by creating a buch of nested for loops (should only be 4) that goes through each array and gets a number, color, shape and pattern. If nested right I should be able to create a deck of cards that has 81 possible combination's of numbers, colors, shapes and patterns. - import java.util.Scanner;
-
import java.util.*;
-
/**
-
* Write a description of class GameOfSet here.
-
* @author (Michael Brown)
-
* @version (a version number or a date)
-
*/
-
public class GameOfSet
-
{
-
-
ArrayList<Card> cards; //An array list of card objects
-
-
-
/**
-
* Constructor for objects of class GameOfSet
-
* @param String to determine Shape and String to determine color
-
* @param number can be 1-3
-
* @param color can be red, green, purple
-
* @param pattern can be solid, stripped, open
-
* @param shape can be diamond, squiggle, oval
-
*
-
*/
-
public GameOfSet()
-
{
-
-
cards = new ArrayList<Card>();
-
-
Card cards = new Card ("1", "solid", "purple", "oval" ); //One card from the Cards ArraysList that has 4 perameters
-
-
-
-
-
}
-
-
/**
-
* Deck of 81 cards
-
* nested for loop will create 81 possible
-
* combinations of number, color, pattern, and shape
-
* each card will have one number, pattern, color and shape
-
*/
-
-
public void getDeckOfCards()
-
{
-
ArrayList<String> number = new ArrayList<String>(3); //ArrayList of numbers
-
number.add("1");
-
number.add("2");
-
number.add("3");
-
-
ArrayList<String> color = new ArrayList<String>(3); //ArrayList of colors
-
color.add("red");
-
color.add("green");
-
color.add("purple ");
-
-
ArrayList<String> pattern = new ArrayList<String>(3); //ArrayList of patterns
-
pattern.add("solid");
-
pattern.add("striped");
-
pattern.add("open");
-
-
ArrayList<String> shape = new ArrayList<String>(3); //ArrayList of Shapes
-
shape.add("diamond");
-
shape.add("squiggle");
-
shape.add("oval");
-
-
-
for(int i = 0; i < number.size(); i++)
-
{
-
-
for(int j = 0; j < color.size(); j++)
-
{
-
Card deck = cards.get(i);
-
-
for(int k = 0; k < pattern.size(); k++)
-
{
-
-
-
for(int m = 0; m < shape.size(); m++)
-
{
-
-
}
-
}
-
}
-
}
-
-
}
-
-
-
}
-
As you can see I have Card deck = cards.get(i);, well I should also be able to get j, k, and m. But I have no idea where I should put that. And after I get that settled, I should be able to put what ever is at i, k, k, and m, and add that to the deck.
32 3867
A method called getDeckOfCards should not be void. It must be returning a List<Card> instead. Enums will help to clean up the code and logic
Right now I want to just create a deck of 81 cards for that method. Im not sure If i should be returning anything yet. Also I am confused on the Enums link to help clean up the code. I will continue looking at it though.
Suppose you have an enum called Color.
The Color.values() method gives you an array of all the colors.
Frinavale 9,735
Recognized Expert Moderator Expert @falconsx23
Who told you that there should only be 4 nested loops...or even that you need use for-loops to solve this problem? Seems a bit weird...you could use something like recursion to solve the problem and then there wouldn't be 4 nested loops...
I think that the point that r035198x was getting at is that it would probably be a lot easier if you were to use enums because you have a constant list of values for your numbers, patterns, colours, and shapes.
Anyways, I'm not sure why you're having a problem with solving the problem.
If you write it out as psudo code it's pretty obvious how the loop structure should look and what you should be doing: - For each number from 1 - 3
-
For each colour in my list/enum of shapes
-
For each shape in my list/enum of shapes
-
For each pattern in my list/enum of patterns
-
Add a new card(number, colour, shape, pattern) to my deck of cards
-
Get the next pattern
-
Get the next shape
-
Get the next colour
-
Get the next number
How much more simple can it get?
It really helps if you give your variables meaningful names...instead of using a,b, c or i,j,k,l,m use things like "number", "colour", "shape" and "pattern". @falconsx23
What do you mean you're not sure if you should be returning anything....
The name of the method is getDeckOfCards...n ot "setDeckOfCards "...not "populateDeckOf Cards"....it's getDeckOfCards.
It seems very obvious that when someone calls the getDeckOfCards method the are expecting to get a deck of cards....how are they supposed to get anything if you don't give them something (return something to them) when they call the method? Again, pay attention to names :)
-Frinny
ok you are right that is bad programming on my part... sorry
JosAH 11,448
Recognized Expert MVP
For any number n in the range [0,81) you can do the following: -
int number= n/27;
-
int pattern= n/9%3;
-
int color= n/3%3;
-
int shape= n%3;
-
So a single loop will generate all your different cards; no recursion nor nested loops are needed.
kind regards,
Jos
Frinavale 9,735
Recognized Expert Moderator Expert JosAH 11,448
Recognized Expert MVP @Frinavale
Not at all; write down all numbers in the range 0 ... 80 in ternary (base 3) notation and you'll see: 0000 ... 2222. There are much trickier things one can do with this notion in any base.
kind regards,
Jos (ex-moderator)
Ok Frinavale, while I was working on this program my Lab Teacher Assistant suggested that using nested for loops will help me out, and what I have up there he said I was on the right track. Also keep in mind that I am still a beginner at Java and no expert. So what may be obvious to you is not to me and I apologize for that.
JosAH thanks alot for the response and I will try out the way you did as soon as I can get my program up.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: BlackHawke |
last post by:
Hello!
This is my second post. Ppl really helped me with the first. I hope there
are answers for this one as well
I own a game company (www.aepoxgames.net) releasing the beta for our first
sci-fi space game in about 2 weeks (www.andromedaonline.net)
We have multiple servers, but the game engine (Game Server Program) is a
Java program (don't ask why) on a windows 2003 enterprise system with dual
|
by: The_Incubator |
last post by:
As the subject suggests, I am interested in using Python as a scripting
language for a game that is primarily implemented in C++, and I am also
interested in using generators in those scripts...
Initially I was justing looking at using Python for some event
scripting. So basically an event would trigger an object to run the
appropriate Python script, which would be run in it's entirety and
return control to the C++ code.
After looking...
|
by: Michael Rybak |
last post by:
hi, everyone.
I'm writing a 2-players game that should support network mode. I'm now
testing it on 1 PC since I don't have 2. I directly use sockets, and
both client and server do computations, the only data transfered is
user mouse/kbd input.
It works synchronously, but somehow, when I play in client window,
both client and server have 17 fps, while when playing in server
window, server has 44 fps while client ...
|
by: DaVinci |
last post by:
I am writing a pong game.but met some problem.
the ball function to control the scrolling ball,
void ball(int starty,int startx)
{
int di ,i;
int dj,j;
di = 1;
dj = 1;
i = starty;
|
by: peterggmss |
last post by:
This is a slot machine game, 2 forms. One is the actual game (frmMachine) and the other is in the background and randomizes the images shown on frmMachine. I need to make frmMachine wait for frmRandom, i tried it with the Sleep API and a Do/Until Loop and neither worked, could you please help me, I have my code below.
frmMachine
'Slot Machine, (c) 2006 Peter Browne, GPL Licensed
'
'Peter Browne
'Sheridan Corporate Centre
'2155 Leanne...
| |
by: raypjr |
last post by:
Hi everyone. I need a little help with some parts of a word guessing game I'm working on. I have some parts done but unsure about others and could use a little advice. Any help is very much appreciated.Here is the code to give more detail:
Dim GameOver As Boolean
Dim NumWords As Integer, ThreeWordList(1000) As String, ThreeWordMeaning(1000) As String
Dim R As Integer, WordsLeft(1000) As Integer
Dim SecretWord As String,...
|
by: koonda |
last post by:
Hi all,
I am a student and I have a project due 20th of this month, I mean May 20, 2007 after 8 days. The project is about creating a Connect Four Game. I have found some code examples on the internet which helped me little bit. But there are lot of problems I am facing developing the whole game. I have drawn the Board and the two players can play. The players numbers can fill the board, but I have problem implementing the winner for the...
|
by: colin |
last post by:
Hi,
Ive got a 3d model editor wich im developing
with XNA c# development environment,
using the game window to display the wireframe mesh in 3d.
however I need to use some other windows too,
such as a Form type window wich displays lists of numerical details
about objects in the 3d view and allows objects to be selected from a list
as an alternative to selecting them on the 3d view
as they maybe underneath another object.
|
by: Shark2026 |
last post by:
Hi there I need to make a Craps game for my class. Here are the parameters for it.
In the game of craps, a pass line bet proceeds as follows. Two six-sided dice are rolled; the first roll of the dice in a craps round is called the “come out roll.” A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12 automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes “the point.” The...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |