473,748 Members | 6,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Game of Set program, need help with for loop nesting and more

55 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.util.*;
  3. /**
  4.  * Write a description of class GameOfSet here.
  5.  * @author (Michael Brown) 
  6.  * @version (a version number or a date)
  7.  */
  8. public class GameOfSet
  9. {
  10.  
  11.     ArrayList<Card> cards;   //An array list of card objects
  12.  
  13.  
  14.     /**
  15.      * Constructor for objects of class GameOfSet
  16.      * @param String to determine Shape and String to determine color
  17.      * @param number can be 1-3
  18.      * @param color can be red, green, purple
  19.      * @param pattern can be solid, stripped, open
  20.      * @param shape can be diamond, squiggle, oval
  21.      * 
  22.      */
  23.     public GameOfSet()
  24.     {
  25.  
  26.          cards = new ArrayList<Card>();
  27.  
  28.         Card cards = new Card ("1", "solid", "purple", "oval" );              //One card from the Cards ArraysList that has 4 perameters 
  29.  
  30.  
  31.  
  32.  
  33.     }
  34.  
  35.     /**
  36.      * Deck of 81 cards
  37.      * nested for loop will create 81 possible 
  38.      * combinations of number, color, pattern, and shape
  39.      * each card will have one number, pattern, color and shape
  40.      */
  41.  
  42.     public void getDeckOfCards()
  43.     {      
  44.       ArrayList<String> number = new ArrayList<String>(3);                     //ArrayList of numbers
  45.       number.add("1");
  46.       number.add("2");
  47.       number.add("3");
  48.  
  49.       ArrayList<String>  color = new ArrayList<String>(3);                     //ArrayList of colors
  50.       color.add("red");
  51.       color.add("green");
  52.       color.add("purple ");
  53.  
  54.       ArrayList<String> pattern = new ArrayList<String>(3);                 //ArrayList of patterns
  55.       pattern.add("solid");
  56.       pattern.add("striped");
  57.       pattern.add("open");
  58.  
  59.       ArrayList<String> shape = new ArrayList<String>(3);                   //ArrayList of Shapes
  60.        shape.add("diamond");
  61.        shape.add("squiggle");
  62.        shape.add("oval");
  63.  
  64.  
  65.          for(int i = 0; i < number.size(); i++)
  66.          {
  67.  
  68.              for(int j = 0; j < color.size(); j++)
  69.              {
  70.                   Card deck = cards.get(i);
  71.  
  72.                   for(int k = 0; k < pattern.size(); k++)
  73.                   {
  74.  
  75.  
  76.                         for(int m = 0; m < shape.size(); m++)
  77.                         {
  78.  
  79.                         }
  80.                     }
  81.                 }
  82.             }                     
  83.  
  84.     }
  85.  
  86.  
  87. }
  88.  

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.
Sep 30 '09 #1
32 3867
r035198x
13,262 MVP
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
Sep 30 '09 #2
falconsx23
55 New Member
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.
Oct 1 '09 #3
r035198x
13,262 MVP
Suppose you have an enum called Color.
The Color.values() method gives you an array of all the colors.
Oct 1 '09 #4
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:
Expand|Select|Wrap|Line Numbers
  1. For each number from 1 - 3 
  2.   For each colour in my list/enum of shapes
  3.     For each shape in my list/enum of shapes
  4.       For each pattern in my list/enum of patterns
  5.         Add a new card(number, colour, shape, pattern) to my deck of cards
  6.       Get the next pattern
  7.     Get the next shape
  8.   Get the next colour
  9. 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
Oct 1 '09 #5
falconsx23
55 New Member
ok you are right that is bad programming on my part... sorry
Oct 2 '09 #6
JosAH
11,448 Recognized Expert MVP
For any number n in the range [0,81) you can do the following:

Expand|Select|Wrap|Line Numbers
  1. int number= n/27;
  2. int pattern= n/9%3;
  3. int color= n/3%3;
  4. int shape= n%3;
  5.  
So a single loop will generate all your different cards; no recursion nor nested loops are needed.

kind regards,

Jos
Oct 2 '09 #7
Frinavale
9,735 Recognized Expert Moderator Expert
@JosAH
Show off!

: )
Oct 2 '09 #8
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)
Oct 2 '09 #9
falconsx23
55 New Member
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.
Oct 2 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

23
3658
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
4
3789
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...
15
4490
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 ...
7
3198
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;
1
5686
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...
0
1719
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,...
5
3551
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...
5
5351
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.
1
8460
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...
0
8991
marktang
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...
0
9541
Oralloy
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...
0
9370
jinu1996
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...
1
9321
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,...
0
9247
tracyyun
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...
1
6796
isladogs
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...
0
4602
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...
1
3312
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
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.