473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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 3817
r035198x
13,262 8TB
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
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 8TB
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 Expert Mod 8TB
@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...not "setDeckOfCards"...not "populateDeckOfCards"....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
ok you are right that is bad programming on my part... sorry
Oct 2 '09 #6
JosAH
11,448 Expert 8TB
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 Expert Mod 8TB
@JosAH
Show off!

: )
Oct 2 '09 #8
JosAH
11,448 Expert 8TB
@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
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
Frinavale
9,735 Expert Mod 8TB
Well the first thing you need to do is create/declare something that will represent the Deck Of Cards. An ArrayList<Cards> will work fine.

Have you learned about variable scope yet?

You could declare the deck of cards at the Class Level or at Method Level (let's just stick with these 2 scopes for now)...

If you declare the card deck in the getDeckOfCards method then the deck of cards is destroyed once the method is finished executing. If you need to use this deck of cards anywhere else then you either have to return the deck of cards to the calling code, or you need to declare the deck of cards at the class level.

If you declare the deck of cards as a member of the GameOfSet class then it will exist for as long as the GameOfSet exists. In this case I would rename the method from getDeckOfCards to "populateDeckOfCards" because "get" implies that whatever is calling the method will be getting something back from the method. If another class needs to use the same deck of cards then I would implement the getDeckOfCards() to return the deck of cards you declared as a member of the GameOfSet class.

Anyways, the important point to take away from this is that you need to declare some sort of container that will represent the deck of cards....otherwise you won't have anywhere to put the cards.

Once you've declared the DeckOfCards container you need to loop through all of the card possibilities in order to create a card for each possibility.

You already have a good idea of how to do this. How you do the loop doesn't really matter. The easiest way to for a beginner to understand would be to use For Loops....so keep on track with what you were doing.

So, for each possibility create 1 card and add it to the ArrayList<Card> that represents the deck of cards.

Once you are finished adding all of the cards to the deck return the deck (or not if it's a class member)

If you are going to return the deck then you need to change your getDeckOfCards method so that it returns the deck of cards. So you'll need to change the method's signature to:

public ArrayList<Card> getDeckOfCards()

Give this a try.
If you're still stuck, post your updated code and we can discuss it :)

-Frinny
Oct 2 '09 #11
No we have not learned about scopes yet nor recursion. And yes what you are saying about for each possibility create 1 card and add it to the ArrayList<Card> that represents the deck of cards is what I was trying to do.

That was what I am stuck on. I wasn't sure how to set up the for loops and also where to place the add method to add the card that is created to the deck.

I am thinking that I should only have to call the add method once, but I am not sure where to place that add method. What I mean by add method is something like this

cards.add(Card).
"Card" is the card being created from the nested for loops. So card should be like Card ("1", "solid", "purple", "oval" ).

What you are saying here "So, for each possibility create 1 card and add it to the ArrayList<Card> that represents the deck of cards." Would I place the add method after all of the for loops? Anyways here is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. public class GameOfSet
  2. {
  3.  
  4.     ArrayList<Card> cards;   //An array list of card objects
  5.  
  6.  
  7.  
  8.     public GameOfSet()
  9.     {
  10.  
  11.          cards = new ArrayList<Card>(); 
  12.         Card cards = new Card ("1", "solid", "purple", "oval" );              //One card from the Cards ArraysList that has 4 perameters 
  13.  
  14.     }
  15.  
  16.     public ArrayList<Card> populateDeckOfCards()
  17.     {      
  18.      ArrayList<Cards>();                                                                         //An ArrayList that will represent DeckOfCards like a container  
  19.       ArrayList<String> number = new ArrayList<String>(3);                     //ArrayList of numbers
  20.       number.add("1");
  21.       number.add("2");
  22.       number.add("3");
  23.  
  24.       ArrayList<String>  color = new ArrayList<String>(3);                     //ArrayList of colors
  25.       color.add("red");
  26.       color.add("green");
  27.       color.add("purple ");
  28.  
  29.       ArrayList<String> pattern = new ArrayList<String>(3);                 //ArrayList of patterns
  30.       pattern.add("solid");
  31.       pattern.add("striped");
  32.       pattern.add("open");
  33.  
  34.       ArrayList<String> shape = new ArrayList<String>(3);                   //ArrayList of Shapes
  35.        shape.add("diamond");
  36.        shape.add("squiggle");
  37.        shape.add("oval");
  38.  
  39.  
  40.          for(int i = 0; i < number.size(); i++)
  41.          {
  42.  
  43.              for(int j = 0; j < color.size(); j++)
  44.              {
  45.                   Card deck = cards.get(i);
  46.  
  47.                   for(int k = 0; k < pattern.size(); k++)
  48.                   {
  49.  
  50.  
  51.                         for(int m = 0; m < shape.size(); m++)
  52.                         {
  53.  
  54.                         }
  55.                     }
  56.                 }
  57.             }                     
  58.  
  59.     }   
  60. }
As you can see I changed the signature of the method so that it could return something, but at the same time I am having trouble creating the container for the populateDeckOfCards. (Line 18)
Oct 2 '09 #12
Frinavale
9,735 Expert Mod 8TB
The add method that I'm talking about is part of the ArrayList class.
Sun (the makers of Java) has a website that lists all of the classes along with the class's methods etc (the API). This documentation can be found here. Bookmark this resource because every time you need help using a Java class you can refer to it for help before asking us.

Anyways, like I was saying, the add method is a member of the ArrayList class.

First you need to instantiate the ArrayList class before you can use it...I'm not sure if you know what instantiate means...it means to "create an instance of".

There are classes (they are like blue prints...for a house) and then there are instances of classes (the actual house).

Before you can use any variable you have to instantiate it (create an instance of it) or else it doesn't exist and you will get Null Reference errors if you try to use it.

Ok so create a new instance of the ArrayList class to use as your deckOfCards:

Expand|Select|Wrap|Line Numbers
  1.  ArrayList<Cards> deckOfCards = new ArrayList<Cards>(81);     
  2.  
Now if you want to add a card to the deck you call the ArrayList's add method, providing it a card:
Expand|Select|Wrap|Line Numbers
  1. Card theCard = new Card(.......);
  2. deckOfCards.add(theCard);
Make sense?
Oct 2 '09 #13
Frinavale
9,735 Expert Mod 8TB
I was just reviewing your code and I don't have a clue what you're trying to do at line 18.

What you did is create a class member variable "cards"...I've been referring to this as the deckOfCards.

So you already have a container (an ArrayList) to put the cards into when you create them.

You have already correctly instantiated this container in the constructor. (It might be a good idea to rename your "cards" variable to deckOfCards since it is representing a "deck of cards")


Great!

Don't know what you're doing at line 18.....just delete that.

Also remove line 12....there's no need to have that there. You should be creating new cards (not at line 12) in the inner most for loop instead (line 53)...and then adding it to the deck of cards (blah that name...adding it to your "cards" variable).

You need to call the "populateDeckOfCards" method in the constructor.

Since you are going to be populating a class variable, you don't need to return anything. So, (sorry) change your populateDeckOfCards method signature back to returning void:

public void populateDeckOfCards()

Now in the populateDeckOfCards you want to add to the card to the deck of cards (the "cards" variable) by using the ArrayList.add() method (**hint: line 53**)......
Oct 2 '09 #14
Yes I am following you so far. I did create a seperate class called Card like you said here is the code for it just in case you need to see it:

Expand|Select|Wrap|Line Numbers
  1. public class Card
  2. {
  3.     public String number;        
  4.     public String color;   
  5.     public String pattern;
  6.     public String shape;
  7.  
  8.     public Card(String number, String color, String pattern, String shape)
  9.     {
  10.         this.number = number;
  11.         this.color = color;
  12.         this. pattern = pattern;
  13.         this.shape = shape;   
  14.     }
  15.  
  16.     public String getNumber()
  17.     {
  18.  
  19.         return number;
  20.     }
  21.  
  22.     /**
  23.      * returns color
  24.      */
  25.  
  26.     public String getColor()
  27.    {
  28.         return color;
  29.     }
  30.  
  31.     /**
  32.      * returns pattern
  33.      */
  34.     public String pattern()
  35.     {
  36.  
  37.         return pattern;
  38.     }
  39.  
  40.     /**
  41.      * returns shape
  42.      */
  43.     public String shape()
  44.     {  
  45.         return shape;
  46.     }   
  47. }
Yea I tried using the add method from the JAVA API earlier, I was typing it wrong. OK So here is my code again:

Expand|Select|Wrap|Line Numbers
  1. public class GameOfSet
  2. {
  3.  
  4.     ArrayList<Card> cards;   //An array list of card objects
  5.  
  6.     public GameOfSet()
  7.     {
  8.  
  9.          cards = new ArrayList<Card>();  
  10.           ArrayList<Card> populateDeckOfCards = new ArrayList<Card>(81); 
  11.     }
  12.  
  13.  
  14.     public  void populateDeckOfCards()
  15.     {      
  16.      ArrayList<String>deckOfCards = new ArrayList<String>(81);   
  17.  
  18.       ArrayList<String> number = new ArrayList<String>(3);                     //ArrayList of numbers
  19.       number.add("1");
  20.       number.add("2");
  21.       number.add("3");
  22.  
  23.       ArrayList<String>  color = new ArrayList<String>(3);                     //ArrayList of colors
  24.       color.add("red");
  25.       color.add("green");
  26.       color.add("purple ");
  27.  
  28.       ArrayList<String> pattern = new ArrayList<String>(3);                 //ArrayList of patterns
  29.       pattern.add("solid");
  30.       pattern.add("striped");
  31.       pattern.add("open");
  32.  
  33.       ArrayList<String> shape = new ArrayList<String>(3);                   //ArrayList of Shapes
  34.        shape.add("diamond");
  35.        shape.add("squiggle");
  36.        shape.add("oval");
  37.  
  38.  
  39.          for(int i = 0; i < number.size(); i++)
  40.          {
  41.  
  42.              for(int j = 0; j < color.size(); j++)
  43.              {
  44.                   Card deck = cards.get(i);
  45.  
  46.                   for(int k = 0; k < pattern.size(); k++)
  47.                   {
  48.  
  49.                        Card theCard = new Card("","","","");
  50.                        populateDeckOfCards().add(theCard);
  51.  
  52.  
  53.                         for(int m = 0; m < shape.size(); m++)
  54.                         {
  55.  
  56.                         }
  57.  
  58.                 }
  59.             }                     
  60.  
  61.     }   
  62. }
  63. }
Im getting an error message at line 50 stating void cannot be dereferenced. I tried calling the populateDeckOfCards method in the constructor and one thing I changed from what you originally stated was

Expand|Select|Wrap|Line Numbers
  1.  ArrayList<Cards> populateDeckOfCards = new ArrayList<Cards>(81); 
to
Expand|Select|Wrap|Line Numbers
  1. ArrayList<Card> populateDeckOfCards = new ArrayList<Card>(81); 
I was getting an error stating cannot find symbol Cards.
Oct 3 '09 #15
Frinavale
9,735 Expert Mod 8TB
We need to stop for a second.
You have to understand scope before we can fix this problem.

Actually, firstly you should know that you should not have variables with the same name as a function/method.

Right now you have a method named "populateDeckOfCards()".
You also have declared an ArrayList named "populateDeckOfCards" in your constructor method....and for some reason you're trying to access it in your populateDeckOfCards() method but.......

This is a very confusing thing to do and is probably the source of your problems.

With that aside we have to go over the concept of scope and classes.

As I stated before, a class is like a blue print for creating Objects. An Object is something that exists in memory.

Objects/Classes have members. Members can be simple variables (like Integers, and Strings and such), but they can also be methods that will modify Object's state or do things.

In your case you have a Card class.
A card has a few different members:
  • public String number;
  • public String color;
  • public String pattern;
  • public String shape;

You have declared them as part of the class. This means that these variables will exist as long as the Card Object exists in memory. Once you're finished with the Card Object, they are destroyed and are no longer accessible. The scope of these methods are at the class level.

The card class also has a bunch of methods:
  • public Card(String number, String color, String pattern, String shape) <-- the constructor
  • public String getNumber()
  • public String getColor()
  • public String pattern()<--you should to rename this to getPattern because you already have a String variable named pattern.
  • public String shape()<-- you should to rename this to getShape because you already have a String variable named shape.

These methods in the Card class have access to class members (the variables above)....and if you had declared the Strings as "private" the these strings would Only be accessible to the methods that are members of the Card class. (But you declared them public...which is weird...especially since you've provided get methods to retrieve them....)


Let's add another method to the Card class...let's create a method named PrintCard. Since this PrintCard method is part of the Card class it has access to all of the member variables of the Card class and so it is able to print the card details to the screen:

Expand|Select|Wrap|Line Numbers
  1. public void PrintCard(){
  2.   StringBuilder theOutput=new StringBuilder();
  3.   theOutput.append(number);
  4.   theOutput.append(color);
  5.   theOutput.append(pattern);
  6.   theOutput.append(shape);
  7.   System.out.println(theOutput.toString() );
  8. }
The variable "theOutput" in the PrintCard method has a method level scope. This means that theOutput cannot be accessed out side of the PrintCard method. It is only available within the PrintCard method. It also means that theOuput is destroyed as soon as the method is finished executing.

Hope you're with me so far.

Say we have a loop and we declare a variable inside the loop. That variable only exists until the loop is finished executing. It cannot be access outside of the loop.

As you can see, variables have life-spans. It is important to take care in where you declare them because this will have a big impact on how things work.

Now let's look at the GameOfSet class.
It has one class member variable:
  • ArrayList<Card> cards;

This means that this variable is accessible to all of the methods in the GameOfSet class.

The important thing is that this variable has to exist before you can use it....or else you're going to get Null Reference errors (like I explained before).

That is the whole purpose to the Constructor method. The constructor method is used to instantiate all of the class member variables. It is called when you use the "new" keyword to instantiate an Object of the class.

For example, when you create a new instance of the GameOfSet like:

GameOfSet theGame = new GameOfSet();

The constructor method is called. It is the constructor's responsibility to make sure that all of the class member variables are instantiated.

Therefore, your constructor needs to at least have the following:
Expand|Select|Wrap|Line Numbers
  1. public GameOfSet() {
  2.          cards = new ArrayList<Card>();  
  3. }
Now your "cards" ArrayList variable is instantiated and can be used by all of the methods in the GameOfSet class.

Your GameOfSet has one method:
  • public void populateDeckOfCards()

This method doesn't return anything...
What is the purpose of having this method then?

It is supposed to populate the class member variable "cards" with Card Objects.

You should call this method after you have instantiated the "cards" ArrayList in the constructor. That way the "cards" variable will not only be instantiated, but it will also have data in it!

So your constructor should look like:
Expand|Select|Wrap|Line Numbers
  1. public GameOfSet() {
  2.          cards = new ArrayList<Card>();  
  3.           populateDeckOfCards();
  4. }
Now to address your problem on line 50.

You have:
populateDeckOfCards().add(theCard);

First of all populateDeckOfCards is the name of the method. In Java you execute a method by calling it's name and providing it with parameters (eg: populateDeckOfCards() will call the populateDeckOfCards method). Then you have .add(theCard) but how is that supposed to work (especially since the method doesn't return anything)? .... In otherwords this really doesn't make any sense and that is why the compiler is telling you that there is a problem.

Secondly, you cannot access any variables that were declared in the constructor method in the populateDeckOfCards() method. So even if you changed the name of the variable that was declared in the constructor method, it is not accessible in the populateDeckOfCards() because this is how scope works.

But the "cards" variable is accessible in the populateDeckOfCards() method.
Because it is has been declared at the class level...therefore has scope for every method in the class.


Later I'll tell you about "public" and "private" scope.
For now I'll let you figure out how to populate the cards variable :)

-Frinny
Oct 3 '09 #16
Ok Thanks for the response I can probably figure it, Ill get back to you if i am still stuck
Oct 4 '09 #17
JosAH
11,448 Expert 8TB
@Frinavale
Although it can be confusing there's technically nothing wrong with it; member and method names live in separate symbol tables and the compiler can unambiguously recoginise where to look: if a name is followed by an open parenthesis it must be a method, otherwise it's a member. Have a look:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Method  {
  3.  
  4.     static final int method= 0;
  5.     static final int METHOD= 1;
  6.  
  7.     static int method(int method) {
  8.         return (method == Method.method)?
  9.             Method.METHOD:
  10.             method(method-Method.METHOD)*method;
  11.     }
  12.     public static void main(String[] args) {
  13.  
  14.         System.out.println(method(5));
  15.     }
  16. }
  17.  
kind regards,

Jos
Oct 4 '09 #18
Frinavale
9,735 Expert Mod 8TB
This is true for Java but please remember that I haven't used Java for quite a few years....and in VB.NET this sort of thing could be pretty bad. Especially if you declared a public class member array with the same name as a public method. In VB.NET arrays use () instead of [] and so there would be ambiguity (I don't even think it's possible to do this actually...I think the compiler would complain...mind you I would start complaining way before the compiler!)

I would try to keep things as clear as possible, and even though it is possible to name a variable with the same name as a method (or even have mulitple variables with the same name in different cases) it is going to be confusing to anyone looking at the code and I would strongly be apposed to suggesting the idea.

The problem in this case is that the OP was clearly confused by the name of the variable in the constructor, the method name, how to use/call methods and how to use variables.

Besides that it just feels wrong to have a public string "pattern" and a public method "pattern()".....they both do return the pattern string...what's the point really? The pattern string should be private in my mind and interaction should take place through the get/set methods associated with this member. This will allow for possible future design changes to the private pattern string member without effecting the public API.

I don't think that the OP understands the concept of public and private scope modifiers yet....so I was going to just leave that alone :)

Besides that your Method class with the class members: int method and int METHOD, that has an int method() method that takes an int method as a parameter and then makes a recursive call to itself is pretty gross.

Yuck...
It's like some kid just showed me a deformed bug with multiple body parts that looked like heads.
Thanks for that Jos....bleh....

It would make a lot more sense if you had named things appropriately:
Expand|Select|Wrap|Line Numbers
  1. public class FactorialCalculator{
  2.     static final int ZERO = 0;
  3.     static final int ONE = 1;
  4.     static int factorial(int number){
  5.         return (number == ZERO) ?  
  6.           ONE :  
  7.           factorial(number - ONE) * number;
  8.     }
  9.  
  10.     public static void main(String[] args) {
  11.           System.out.println(factorial(5));
  12.     }
  13. }
-Frinny
Oct 5 '09 #19
JosAH
11,448 Expert 8TB
@Frinavale
True, all true but your previous post insinuated (to me at least) that this practice would be incorrect which it isn't. If someone writes code like I did in my previous example he deserves to be defenestrated head first. Sun's naming convention partially solves the problem because methods shoud be (or start with) a verb while members are supposed to be nouns.

kind regards,

Jos
Oct 5 '09 #20
Ok its been about three weeks since we talked about this program. I made a couple of adjustments like creating the deck of cards inside the constructor and creating a method that creates 12 random cards. Each card that is created randomly will be different meaning each card will have a different number, color, pattern and shape.

Now I am trying to create the actual play of the game. I am going to have the game played by using the console (kinda confused on what that actually is) instead of GUI such as JFrame or an applet. The way I understand the console is that it would be the printed out (as output) on to the terminal window.

The way I started doing this is by using System.out.println. So right now the part that I am stuck on is actually displaying or printing the 12 random cards onto the screen. The way I tried it is shown in comments at the bottom of the program. I know what I am trying to do doesnt make sense, but I am still working on it.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.util.*;
  3. import javax.swing.*;
  4. import java.awt.*;
  5.  
  6. /**
  7.  * Write a description of class GameOfSet here.
  8.  * @author (Michael Brown) 
  9.  * @version (a version number or a date)
  10.  */
  11. public class GameOfSet
  12. {
  13.  
  14.     ArrayList<Card> deckOfCards;   //An array list of card objects
  15.  
  16.  
  17.     /**
  18.      * Constructor for objects of class GameOfSet
  19.      * @param String to determine Shape and String to determine color
  20.      * @param number can be 1-3
  21.      * @param color can be red, green, purple
  22.      * @param pattern can be solid, stripped, open
  23.      * @param shape can be diamond, squiggle, oval
  24.      * 
  25.      */
  26.     public GameOfSet()
  27.     {
  28.         deckOfCards = new ArrayList<Card>(81);          //"Card" is what the arraylist is made up of
  29.  
  30.       ArrayList<String> number = new ArrayList<String>(3);                     //ArrayList of numbers
  31.       number.add("1");
  32.       number.add("2");
  33.       number.add("3");
  34.  
  35.       ArrayList<String>  color = new ArrayList<String>(3);                     //ArrayList of colors
  36.       color.add("red");
  37.       color.add("green");
  38.       color.add("purple ");
  39.  
  40.       ArrayList<String> pattern = new ArrayList<String>(3);                 //ArrayList of patterns
  41.       pattern.add("solid");
  42.       pattern.add("striped");
  43.       pattern.add("open");
  44.  
  45.       ArrayList<String> shape = new ArrayList<String>(3);                   //ArrayList of Shapes
  46.        shape.add("diamond");
  47.        shape.add("squiggle");
  48.        shape.add("oval");
  49.  
  50.       Card theCard = null;
  51.          for(int i = 0; i < number.size(); i++)
  52.          {
  53.  
  54.              for(int j = 0; j < color.size(); j++)
  55.              {
  56.  
  57.                   for(int k = 0; k < pattern.size(); k++)
  58.                   {
  59.  
  60.                         for(int m = 0; m < shape.size(); m++)
  61.                         {
  62.                              theCard = new Card(number.get(i),color.get(j),pattern.get(k),shape.get(m));
  63.                              deckOfCards.add(theCard);    // adds the new card that is being created to the deck of cards array list
  64.                       }
  65.  
  66.                 }
  67.             }                     
  68.  
  69.           }   
  70.     }
  71.  
  72.      /**
  73.       * To allow 12 cards from the deckOfCards ArrayList  to be choosen at random 
  74.       * Then laid out in a grid of 3 rows with 4 cards each
  75.       */
  76.      public ArrayList<Card> createRandomNumberOfCards()
  77.      {
  78.          Random generator = new Random();                       //Creating the random class
  79.          ArrayList <Integer> list = new ArrayList <Integer>();
  80.          int randomIndex = 0;                //A random interger (range 1 to 12) so random class creates 12 random cards
  81.          int x = 12;
  82.          ArrayList <Card> randomCards = new ArrayList <Card>();         //Another ArrayList, when a card is pulled from the deckOfCard ArrayList, it is stored into the randomCards arrayList
  83.          while(x>0) 
  84.          {
  85.              randomIndex = generator.nextInt(81);               //randomIndex is a random number ranging from 0 to 81 cards 
  86.  
  87.              while(list.contains(new Integer(randomIndex)))  //this is a nested list that make sure that there is more then one of the same random card being returned. 
  88.              {
  89.                 randomIndex = generator.nextInt(81);               //If there is one of the same card being returned then it will simply make a new random card 
  90.             }
  91.             list.add(randomIndex);                                         // value at randomIndex is being added to the list of integers
  92.             x--;
  93.  
  94.  
  95.          }
  96.               for(int y= 0; y < 12; y++)
  97.              {
  98.                                                                                                            //gets a number from the "list" ArrayList. Then deckOfCards
  99.                  randomCards.add(deckOfCards.get(list.get(y)));  //uses that number to get the value from the deckOfcards ArraysList and places it into the random card class
  100.  
  101.              }
  102.  
  103.              return  randomCards;                                                       //This is the random card being returned 
  104.       }
  105.  
  106.  
  107.       /**
  108.        * To create a grid that contains 12 cards
  109.        * 3 rows and 4 columns
  110.        */
  111.  
  112.       public static void main (String[] args)
  113.       {
  114.         System.out.println("Welcome to the Game Of Set");
  115.         System.out.println("12 random cards out of 81 are choosen randomly");
  116.         //System.out.println(Card(String number(),color(), pattern(), shape()));  //Prints out the 12 random cards 
  117.         //System.out.println( ArrayList <Card> randomCards ) ;
  118.        // System.out.println(Card randomCards());
  119.         }
  120.  
  121.  
  122. }
  123.  
Oct 21 '09 #21
Frinavale
9,735 Expert Mod 8TB
In post 16 I gave an example of a method that prints a card to a screen.

You will have to add a public method to the Card class ("printCard" or "displayCard" or something) that will output the card details to the screen.

Then you will have to add a public method to the GameOfSet class ("printDeck" or "displayDeck"...) that will loop through all of the cards in the deck of cards and call the method that outputs the card details.

Or you could just write a toString method for the Card class that returns a String representation of the card....then loop through the deck of cards and output the card details to the screen by calling the toString method for each card in the deck.

The important thing here is that in order to achieve what you want to achieve you need to loop through all of the cards in the deck of cards and call a method (or methods) that will result in the card details being printed to the screen.

-Frinny
Oct 21 '09 #22
JosAH
11,448 Expert 8TB
@Frinavale
Don't do that; implement a public toString() method that returns a String representation of the object; PrintStreams and Writers call that method when they have to print a non-primitive value; it's much more flexible like that.

kind regards,

Jos
Oct 22 '09 #23
Before I explain what I am stuck on, I will try to explain what I do. I agree with you Jos that going with the ToString method. Just in case you didn't know I have another class named Card, which is for creating a Card. I think it will make sense if I place the toString method inside the card class. I will use the toString method to create 12 random cards.

Then, inside my GameOfSet class I will create a main method to call the toString method, which creates 12 random cards and prints out the 12 random cards using System.out.println.

Now the problem that I am having is that I am not too familiar with using the toString so I am going to need your help on how to create the 12 random cards inside the toString method of my Card class.



Expand|Select|Wrap|Line Numbers
  1. public class Card
  2. {
  3.     public String number;        
  4.     public String color;   
  5.     public String pattern;
  6.     public String shape;
  7.  
  8.      /**
  9.       * a deck of cards 
  10.       * @param number of objects on cards
  11.       * @param color of shape and pattern
  12.       * @param what type of pattern
  13.       * @param what type of shape
  14.       */
  15.     public Card(String number, String color, String pattern, String shape)
  16.     {
  17.         this.number = number;
  18.         this.color = color;
  19.         this. pattern = pattern;
  20.         this.shape = shape;   
  21.     }
  22.  
  23.     /**
  24.      * number of objects
  25.      * only 1-3
  26.      */
  27.     public String getNumber()
  28.     {
  29.  
  30.         return number;
  31.     }
  32.  
  33.     /**
  34.      * returns color
  35.      */
  36.  
  37.     public String getColor()
  38.    {
  39.         return color;
  40.     }
  41.  
  42.     /**
  43.      * returns pattern
  44.      */
  45.     public String pattern()
  46.     {
  47.  
  48.         return pattern;
  49.     }
  50.  
  51.     /**
  52.      * returns shape
  53.      */
  54.     public String shape()
  55.     {  
  56.         return shape;
  57.     }
  58.  
  59.     public String toString ()
  60.       {
  61.           randomCards (String number, color, pattern, shape);
  62.         return  "Here are the 12 random cards "  + new ArrayList<Card> randomCards(12);   //12 random cards are being shown to the user
  63.  
  64.  
  65.         }
  66.  
  67. }
  68.  
  69.  
Oct 27 '09 #24
Frinavale
9,735 Expert Mod 8TB
I think it would be best to create 12 random Cards Objects in the GameOfSet class...

Loop through the 12 random cards and call their toString() methods. The toString() method should return a string that contains the Card object's details. You can print this string to the screen by passing it to the the System.out.println() method.

The reason i think it would be best to create the 12 random Card Objects in the GameOfSet class is because a Card Object represents a single Card....it does not represent 12 cards, just one card; therefore, it doesn't make sense to have the Card's toString() method print 12 cards (how can it really? it only has access to that single card's details)
Oct 28 '09 #25
Yes, but in my public ArrayList<Card> createRandomNumberOfCards()method I have 12 random cards being created. Which means my randomCards arrayList holds 12 random cards from the deck of cards arrayList.

So what I am trying to say is I am not sure how to print those 12 random cards from the randomCards ArrayList using the toString method.

Here is what I doing now
Expand|Select|Wrap|Line Numbers
  1.    public String toString ()
  2.       {
  3.           for(int i = 0, i < randomCards.size, i++)
  4.          {
  5.             System.out.println(randomCards())
  6.  
  7.            return randomCards();
  8.  
  9.  
  10.         }
This is in the game of Set class
Oct 29 '09 #26
Frinavale
9,735 Expert Mod 8TB
@falconsx23
Ok, declare a variable to hold the ArrayList (the 12 random cards), then loop through each Card in the ArrayList and call it's toString method to retrieve the string containing the card details.

For example:

Expand|Select|Wrap|Line Numbers
  1. ArrayList<Card> randomCards = createRandomNumberOfCards();
  2. for(int i= 0; i < randomCards.size(); i++){
  3.    String cardDetails = randomCards[i].toString();
  4.    System.System.out.println(cardDetails);
  5. }

Please be aware that the Card class's toString() method has to return a String containing the card's details.

For example:
Expand|Select|Wrap|Line Numbers
  1. public String toString(){
  2.   StringBuilder theOutput=new StringBuilder();
  3.   theOutput.append(" number:");
  4.   theOutput.append(number);
  5.   theOutput.append(" colour:");
  6.   theOutput.append(color);
  7.   theOutput.append(" pattern:");
  8.   theOutput.append(pattern);
  9.   theOutput.append(" shape:");
  10.   theOutput.append(shape);
  11.   theOutput.append(" ");
  12.   return theOutput.toString();
  13. }
Oct 29 '09 #27
Ok what you are saying is making a little more sense. But before I am able to fully understand what you are saying,


Expand|Select|Wrap|Line Numbers
  1.     ArrayList<Card> randomCards = createRandomNumberOfCards();
  2.     for(int i= 0; i < randomCards.size(); i++){
  3.        String cardDetails = randomCards[i].toString();
  4.        System.System.out.println(cardDetails);
  5.     }
where should this be placed. To me it looks like it should be placed inside the toString method, but I am not sure. When I tried to, I received many errors, unless I was doing it wrong. And do you still feel that my toString method should be placed in the gameOfSetClass or Card class?
Oct 29 '09 #28
Frinavale
9,735 Expert Mod 8TB
It cannot go in the Card's toString() method.

Because the Card's toString method returns a string representation of that instance of the Card class (that Card Object).

It needs to go in the function that is using the ArrayList of 12 card objects.
The function where that is happening is in the GameOfSet class....

-Frinny
Oct 29 '09 #29
Thanks I got that part done. Now Im trying to have the program ask the user to enter an actually set. For 3 cards to form a set they must be all different or all alike within each category. For example.

Expand|Select|Wrap|Line Numbers
  1. 0  number:3 color:green pattern:open shape:squiggle 
  2. 1  number:1 color:red pattern:open shape:diamond 
  3. 2  number:1 color:purple  pattern:solid shape:squiggle 
  4. 3  number:1 color:green pattern:striped shape:diamond 
  5. 4  number:1 color:red pattern:solid shape:diamond
  6. 5  number:1 color:purple  pattern:solid shape:diamond 
  7. 6  number:1 color:green pattern:open shape:squiggle 
  8. 7  number:2 color:green pattern:open shape:diamond 
  9. 8  number:3 color:purple  pattern:solid shape:squiggle 
  10. 9  number:1 color:purple  pattern:striped shape:squiggle 
  11. 10  number:1 color:red pattern:striped shape:diamond 
  12. 11  number:3 color:red pattern:striped shape:diamond

Cards 1, 4, and 10 is an example of a set since they are all alike in every category except pattern.

The problem that I am having for now is how to code this. The only way I can think of is using if statements. Any suggestions? Im working on it now.

Here is some of the code that I fixed up from last time.

Expand|Select|Wrap|Line Numbers
  1.  public void playGame()
  2.       {
  3.          ArrayList<Card> randomCards = createRandomNumberOfCards();
  4.           for(int i= 0; i < randomCards.size(); i++)   //checking for every  card in the randomCard ArrayList
  5.           {
  6.  
  7.  
  8.               String cardDetails = randomCards.get(i).toString();    //using the toString of the Card method (which converts the details of the cards into a String 
  9.               System.out.println(i + " " + cardDetails);   //prints out the cards (details of the cards) into the terminal window 


}


I wrote the toString() method the same way you had it.

The code below is what I am working on in the GameOfSet class.

Expand|Select|Wrap|Line Numbers
  1.  Card one = randomCards.get(1);
  2.           Card two = randomCards.get(2);
  3.           Card three = randomCards.get(3);
  4.           Card four = randomCards.get(4);
  5.           Card five = randomCards.get(5);
  6.           Card six = randomCards.get(6);
  7.           Card seven = randomCards.get(7);
  8.           Card eight = randomCards.get(8);
  9.           Card nine = randomCards.get(9);
  10.           Card ten = randomCards.get(10);
  11.           Card eleven = randomCards.get(11);
  12.  
  13.           System.out.println("Pick your 1st card for a set  " +  one);
  14.           Scanner in = new Scanner(System.in);
  15.           String setH = in.nextLine(); 
  16.  
  17.           System.out.println("Pick your 2nd card for a set");
  18.           System.out.println("Pick your 3rd card for a set");
I made it where each card has a variable. Now Im trying to think of a way where I can have it where the user enters one of those card variables, then another and another and then the program will compare each of those cards to each other to determine if they are a set.
Nov 4 '09 #30
So far I got my game to ask a user to input a card to determine a set. It first asks the user to enter one card (0 - eleven making 12). It asks the user to enter one card and then another and then a third card.

What I am stuck on is how do I get my program to ask the user to enter again if the user enters the wrong data. For instance the only way you can enter card one is by typing one. If the user enters 1 or fg instead of one, how do I get my program to ask the user to enter another card.

As you can see my program has tons of else if statements. I could not find an easier way to create that data. If you guys see a better and easier way can you let me know.

Expand|Select|Wrap|Line Numbers
  1.  public void playGame()
  2.       {
  3.          ArrayList<Card> randomCards = createRandomNumberOfCards();
  4.           for(int i= 0; i < randomCards.size(); i++)   //checking for every  card in the randomCard ArrayList
  5.           {
  6.  
  7.  
  8.               String cardDetails = randomCards.get(i).toString();    //using the toString of the Card method (which converts the details of the cards into a String 
  9.               System.out.println(i + " " + cardDetails);   //prints out the cards (details of the cards) into the terminal window 
  10.  
  11.  
  12.  
  13.         }
  14.  
  15.           //These cards are set to a varabiable to 
  16.           //make it easier for the user to input
  17.           Card zero = randomCards.get(0);
  18.           Card one = randomCards.get(1);
  19.           Card two = randomCards.get(2);
  20.           Card three = randomCards.get(3);
  21.           Card four = randomCards.get(4);
  22.           Card five = randomCards.get(5);
  23.           Card six = randomCards.get(6);
  24.           Card seven = randomCards.get(7);
  25.           Card eight = randomCards.get(8);
  26.           Card nine = randomCards.get(9);
  27.           Card ten = randomCards.get(10);
  28.           Card eleven = randomCards.get(11);
  29.  
  30.  
  31.           System.out.println("Pick your 1st card for the set  ");
  32.           Scanner in = new Scanner(System.in);
  33.           String setH = in.nextLine();
  34.  
  35.           if(setH.equals("one" ))
  36.           {
  37.              System.out.println(one);
  38.           //    System.out.println("Pick your second card for the set ");
  39.             //  String setJ = in.nextLine();
  40.              // if(setJ.equals("two"))
  41.               //{
  42.                 //  System.out.println(two);
  43.                //}
  44.  
  45.           }
  46.             else if(setH.equals("three"))
  47.             {
  48.                 System.out.println(three);
  49.             }
  50.              else if(setH.equals("four"))
  51.             {
  52.                 System.out.println(four);
  53.             }
  54.              else if(setH.equals("four"))
  55.             {
  56.                 System.out.println(four);
  57.             }
  58.              else if(setH.equals("five"))
  59.             {
  60.                 System.out.println(five);
  61.             }
  62.              else if(setH.equals("six"))
  63.             {
  64.                 System.out.println(six);
  65.             }
  66.              else if(setH.equals("seven"))
  67.             {
  68.                 System.out.println(seven);
  69.             }
  70.              else if(setH.equals("eight"))
  71.             {
  72.                 System.out.println(eight);
  73.             }
  74.              else if(setH.equals("nine"))
  75.             {
  76.                 System.out.println(nine);
  77.             }
  78.              else if(setH.equals("ten"))
  79.             {
  80.                 System.out.println(ten);
  81.             }
  82.              else if(setH.equals("eleven"))
  83.             {
  84.                 System.out.println(eleven);
  85.             }
  86.  
  87.  
  88.  
  89.               System.out.println("Pick your second card for the set ");
  90.               String setJ = in.nextLine();
  91.                if(setJ.equals("zero"))
  92.                {
  93.                    System.out.println(zero);
  94.                 }
  95.                   else if(setJ.equals("one"))
  96.                   {
  97.                       System.out.println(one);
  98.                     }
  99.               else if(setJ.equals("two"))
  100.               {
  101.                   System.out.println(two);
  102.                 }
  103.               else if(setJ.equals("three"))
  104.               {
  105.                   System.out.println(three);
  106.                 }
  107.                 else if(setJ.equals("four"))
  108.                 {
  109.                     System.out.println(five);
  110.                 }
  111.                   else if(setJ.equals("six"))
  112.                   {
  113.                       System.out.println(six);
  114.                     }
  115.               else if(setJ.equals("seven"))
  116.                 {
  117.                     System.out.println(seven);
  118.                 }
  119.               else if(setJ.equals("eight"))
  120.               {
  121.                   System.out.println(eight);
  122.                 }
  123.               else if(setJ.equals("eight"))
  124.               {
  125.                   System.out.println(eight);
  126.                 }
  127.               else if(setJ.equals("eight"))
  128.               {
  129.                  System.out.println(eight);
  130.                 }
  131.               else if(setJ.equals("nine"))
  132.               {
  133.                     System.out.println(nine);
  134.                 }
  135.               else if(setJ.equals("ten"))
  136.               {
  137.                   System.out.println(ten);
  138.                 }
  139.                   else if(setJ.equals("eleven"))
  140.             {
  141.                 System.out.println(eleven);
  142.             }
  143.  
  144.              System.out.println("Pick your third card for a set ");
  145.               String setF = in.nextLine();
  146.                if(setF.equals("zero"))
  147.                {
  148.                    System.out.println(zero);
  149.                 }
  150.                 else if(setF.equals("one"))
  151.                 {
  152.                     System.out.println(one);
  153.                 }
  154.                 else if(setF.equals("two"))
  155.                 {
  156.                     System.out.println(two);
  157.                 }
  158.                 else if(setF.equals("three"))
  159.                 {
  160.                     System.out.println(three);
  161.                 }
  162.                 else if(setF.equals("four"))
  163.                 {
  164.                     System.out.println(four);
  165.                 }
  166.                 else if(setF.equals("five"))
  167.                 {
  168.                     System.out.println(five);
  169.                 }
  170.                 else if(setF.equals("six"))
  171.                 {
  172.                     System.out.println(six);
  173.                 }
  174.                 else if(setF.equals("seven"))
  175.                 {
  176.                     System.out.println(seven);
  177.                 }
  178.                 else if(setF.equals("eight"))
  179.                 {
  180.                     System.out.println(eight);
  181.                 }
  182.                 else if(setF.equals("nine"))
  183.                 {
  184.                     System.out.println(nine);
  185.                 }
  186.                 else if(setF.equals("ten"))
  187.                 {
  188.                     System.out.println(ten);
  189.                 }
  190.                 else if(setF.equals("eleven"))
  191.                 {
  192.                     System.out.println(eleven);
  193.                 }
Nov 6 '09 #31
ok I figured that part. I just placed my program in a while loop using a Boolean.
Now I need help with how can I actually determine a set. I just dont see how can I say if card one's color, number, and pattern equals card two's color , number, and pattern Then it is a set.

By the look of this I know I have to use an if statement. I'm just dont know how to code it.

Expand|Select|Wrap|Line Numbers
  1.   public void playGame()
  2.       {
  3.          ArrayList<Card> randomCards = createRandomNumberOfCards();
  4.           for(int i= 0; i < randomCards.size(); i++)   //checking for every  card in the randomCard ArrayList
  5.           {
  6.  
  7.  
  8.               String cardDetails = randomCards.get(i).toString();    //using the toString of the Card method (which converts the details of the cards into a String 
  9.               System.out.println(i + " " + cardDetails);   //prints out the cards (details of the cards) into the terminal window 
  10.  
  11.  
  12.  
  13.         }
  14.  
  15.           //These cards are set to a varabiable to 
  16.           //make it easier for the user to input
  17.           Card zero = randomCards.get(0);
  18.           Card one = randomCards.get(1);
  19.           Card two = randomCards.get(2);
  20.           Card three = randomCards.get(3);
  21.           Card four = randomCards.get(4);
  22.           Card five = randomCards.get(5);
  23.           Card six = randomCards.get(6);
  24.           Card seven = randomCards.get(7);
  25.           Card eight = randomCards.get(8);
  26.           Card nine = randomCards.get(9);
  27.           Card ten = randomCards.get(10);
  28.           Card eleven = randomCards.get(11);
  29.  
  30.           Boolean correct = false;
  31.           while (!correct){
  32.               System.out.println("Pick your 1st card for the set  ");
  33.                 Scanner in = new Scanner(System.in);
  34.                 String setH = in.nextLine();
  35.                 if (setH.equals( "zero")){
  36.  
  37.                     correct = true;
  38.                     System.out.println(zero);
  39.                 }
  40.                 else if(setH.equals( "one"))
  41.                 {
  42.                     System.out.println(one);
  43.                     correct = true;
  44.                 }
  45.  
  46.                   else if(setH.equals( "two"))
  47.                 {
  48.                     System.out.println(two);
  49.                     correct = true;
  50.                 }
  51.                   else if(setH.equals( "three"))
  52.                 {
  53.                     System.out.println(three);
  54.                     correct = true;
  55.                 }
  56.                   else if(setH.equals( "four"))
  57.                 {
  58.                     System.out.println(four);
  59.                     correct = true;
  60.                 }
  61.                   else if(setH.equals( "five"))
  62.                 {
  63.                     System.out.println(five);
  64.                     correct = true;
  65.                 }
  66.                   else if(setH.equals( "six"))
  67.                 {
  68.                     System.out.println(six);
  69.                     correct = true;
  70.                 }
  71.                   else if(setH.equals( "seven"))
  72.                 {
  73.                     System.out.println(seven);
  74.                     correct = true;
  75.                 }
  76.                   else if(setH.equals( "eight"))
  77.                 {
  78.                     System.out.println(eight);
  79.                     correct = true;
  80.                 }
  81.                   else if(setH.equals( "nine"))
  82.                 {
  83.                     System.out.println(nine);
  84.                     correct = true;
  85.                 }
  86.                   else if(setH.equals( "ten"))
  87.                 {
  88.                     System.out.println(ten);
  89.                     correct = true;
  90.                 }
  91.                   else if(setH.equals( "eleven"))
  92.                 {
  93.                     System.out.println(eleven);
  94.                     correct = true;
  95.                 }
  96.  
  97.                 else {
  98.                     correct = false;
  99.                     System.out.println("That is not a correct card please enter again");
  100.                 }
I followed this procedure two more times to allow the user to select up to three cards.
Nov 7 '09 #32
Ok I finished my game of set. If anyone is still looking at this thread I just wanna say thanks for the help.

GameOfSet Class
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.util.*;
  3. import javax.swing.*;
  4. import java.awt.*;
  5.  
  6. /**
  7.  * Write a description of class GameOfSet here.
  8.  * @author (Michael Brown) 
  9.  * @version (a version number or a date)
  10.  */
  11. public class GameOfSet
  12. {
  13.  
  14.     ArrayList<Card> deckOfCards;   //An array list of card objects
  15.  
  16.  
  17.     /**
  18.      * Constructor for objects of class GameOfSet
  19.      * @param String to determine Shape and String to determine color
  20.      * @param number can be 1-3
  21.      * @param color can be red, green, purple
  22.      * @param pattern can be solid, stripped, open
  23.      * @param shape can be diamond, squiggle, oval
  24.      * 
  25.      */
  26.     public GameOfSet()
  27.     {
  28.         deckOfCards = new ArrayList<Card>(81);          //"Card" is what the arraylist is made up of
  29.  
  30.       ArrayList<String> number = new ArrayList<String>(3);                     //ArrayList of numbers
  31.       number.add("1");
  32.       number.add("2");
  33.       number.add("3");
  34.  
  35.       ArrayList<String>  color = new ArrayList<String>(3);                     //ArrayList of colors
  36.       color.add("red");
  37.       color.add("green");
  38.       color.add("purple ");
  39.  
  40.       ArrayList<String> pattern = new ArrayList<String>(3);                 //ArrayList of patterns
  41.       pattern.add("solid");
  42.       pattern.add("striped");
  43.       pattern.add("open");
  44.  
  45.       ArrayList<String> shape = new ArrayList<String>(3);                   //ArrayList of Shapes
  46.        shape.add("diamond");
  47.        shape.add("squiggle");
  48.        shape.add("oval");
  49.  
  50.       Card theCard = null;
  51.          for(int i = 0; i < number.size(); i++)
  52.          {
  53.  
  54.              for(int j = 0; j < color.size(); j++)
  55.              {
  56.  
  57.                   for(int k = 0; k < pattern.size(); k++)
  58.                   {
  59.  
  60.                         for(int m = 0; m < shape.size(); m++)
  61.                         {
  62.                              theCard = new Card(number.get(i),color.get(j),pattern.get(k),shape.get(m));
  63.                              deckOfCards.add(theCard);    // adds the new card that is being created to the deck of cards array list
  64.                       }
  65.  
  66.                 }
  67.             }                     
  68.  
  69.           }   
  70.     }
  71.  
  72.      /**
  73.       * To allow 12 cards from the deckOfCards ArrayList  to be choosen at random 
  74.       * Then laid out in a grid of 3 rows with 4 cards each
  75.       */
  76.      public ArrayList<Card> createRandomNumberOfCards()
  77.      {
  78.          Random generator = new Random();                       //Creating the random class
  79.          ArrayList <Integer> list = new ArrayList <Integer>();
  80.          int randomIndex = 0;                //A random interger (range 1 to 12) so random class creates 12 random cards
  81.          int x = 12;
  82.          ArrayList <Card> randomCards = new ArrayList <Card>();         //Another ArrayList, when a card is pulled from the deckOfCard ArrayList, it is stored into the randomCards arrayList
  83.          while(x>0) 
  84.          {
  85.              randomIndex = generator.nextInt(81);               //randomIndex is a random number ranging from 0 to 81 cards 
  86.  
  87.              while(list.contains(new Integer(randomIndex)))  //this is a nested list that make sure that there is more then one of the same random card being returned. 
  88.              {
  89.                 randomIndex = generator.nextInt(81);               //If there is one of the same card being returned then it will simply make a new random card 
  90.             }
  91.             list.add(randomIndex);                                         // value at randomIndex is being added to the list of integers
  92.             x--;
  93.  
  94.  
  95.          }
  96.               for(int y= 0; y < 12; y++)
  97.              {
  98.                                                                                                            //gets a number from the "list" ArrayList. Then deckOfCards
  99.                  randomCards.add(deckOfCards.get(list.get(y)));  //uses that number to get the value from the deckOfcards ArraysList and places it into the random card 
  100.  
  101.              }
  102.  
  103.              return  randomCards;                                                       //This is the random card being returned 
  104.       }
  105.  
  106.  
  107.       /**
  108.        * To create a grid that contains 12 cards
  109.        * 3 rows and 4 columns
  110.        */
  111.  
  112.       public void playGame()
  113.       {
  114.          ArrayList<Card> randomCards = createRandomNumberOfCards();
  115.           for(int i= 0; i < randomCards.size(); i++)   //checking for every  card in the randomCard ArrayList
  116.           {
  117.  
  118.  
  119.               String cardDetails = randomCards.get(i).toString();    //using the toString of the Card method (which converts the details of the cards into a String 
  120.               System.out.println(i + " " + cardDetails);   //prints out the cards (details of the cards) into the terminal window 
  121.  
  122.  
  123.  
  124.         }
  125.  
  126.           //These cards are set to a varabiable to 
  127.           //make it easier for the user to input
  128.           Card zero = null;
  129.           Card one = null;
  130.           Card two = null;
  131.  
  132.           Boolean correct = false;
  133.           while (!correct){
  134.               System.out.println("Pick your 1st card for the set  ");
  135.                 Scanner in = new Scanner(System.in);
  136.                 String setH = in.nextLine();
  137.                 int number = Integer.parseInt(setH);
  138.                 if (number >= 0 && number <= 11){
  139.  
  140.                     correct = true;
  141.                     System.out.println(randomCards.get(number));
  142.                     zero = randomCards.get(number);
  143.  
  144.                 }
  145.  
  146.  
  147.                 else {
  148.                     correct = false;
  149.                     System.out.println("That is not a correct card please enter again");
  150.                 }
  151.  
  152.             }
  153.  
  154.  
  155.  
  156.            Boolean correcta = false;
  157.           while (!correcta){
  158.               System.out.println("Pick your second card for the set  ");
  159.                 Scanner in = new Scanner(System.in);
  160.                 String setJ = in.nextLine();
  161.                 int number = Integer.parseInt(setJ);
  162.                 if ( number >= 0 && number <= 11){
  163.  
  164.                     correcta = true;
  165.                     System.out.println(randomCards.get(number));
  166.                     one = randomCards.get(number);
  167.                 }
  168.  
  169.  
  170.                 else
  171.                 {
  172.                     correct = false;
  173.                     System.out.println("That is not a correct card please enter again");
  174.                 }
  175.             }
  176.  
  177.              Boolean correctb = false;
  178.           while (!correctb){
  179.               System.out.println("Pick your third card for the set  ");
  180.                 Scanner in = new Scanner(System.in);
  181.                 String setV = in.nextLine();
  182.                  int number = Integer.parseInt(setV);
  183.                 if ( number >= 0 && number <= 11){
  184.  
  185.                     correctb = true;
  186.                     System.out.println(randomCards.get(number));
  187.                     two = randomCards.get(number);
  188.                 }
  189.  
  190.                 else
  191.                 {
  192.                        correct = false;
  193.                     System.out.println("That is not a correct card please enter again");
  194.                 }
  195.  
  196.  
  197.  
  198.             }
  199.  
  200.            //setV
  201.            //setH
  202.            //setJ
  203.            int same = 0;
  204.  
  205.            if(zero.getNumber().equals(one.getNumber()) && one.getNumber().equals(two.getNumber()))
  206.            {
  207.                same ++;
  208.             } 
  209.  
  210.            if(zero.getColor().equals(one.getColor()) && one.getColor().equals(two.getNumber()))
  211.            {
  212.                same ++;
  213.             }
  214.              if(zero.pattern().equals(one.pattern()) && one.pattern().equals(two.pattern()))
  215.               {
  216.                   same ++;
  217.                 }
  218.              if(zero.shape().equals(one.shape()) && one.shape().equals(two.shape()))
  219.                {
  220.                    same ++;
  221.                 }
  222.  
  223.                 if( same >= 3)
  224.                 {
  225.                     System.out.println("This is a set you win");
  226.                 } 
  227.  
  228.             if(! zero.getNumber().equals(one.getNumber()) && ! one.getNumber().equals(two.getNumber()))
  229.             {
  230.                 same ++;
  231.             }
  232.              if(! zero.getColor().equals(one.getColor()) && ! one.getColor().equals(two.getNumber()))
  233.              {
  234.                  same ++;
  235.               }
  236.  
  237.               if(! zero.pattern().equals(one.pattern()) && ! one.pattern().equals(two.pattern()))
  238.                {
  239.                    same ++;
  240.                 }
  241.                if(zero.shape().equals(one.shape()) && one.shape().equals(two.shape()))
  242.                 {
  243.                     same ++;
  244.                 }
  245.                 if(same >= 3)
  246.                 {
  247.                   System.out.println("This is a set you win");
  248.               }
  249.  
  250.  
  251.         }
  252.  
  253.  
  254.  
  255. }


Card Class



Expand|Select|Wrap|Line Numbers
  1. public class Card
  2. {
  3.     public String number;        
  4.     public String color;   
  5.     public String pattern;
  6.     public String shape;
  7.  
  8.      /**
  9.       * a deck of cards 
  10.       * @param number of objects on cards
  11.       * @param color of shape and pattern
  12.       * @param what type of pattern
  13.       * @param what type of shape
  14.       */
  15.     public Card(String number, String color, String pattern, String shape)
  16.     {
  17.         this.number = number;
  18.         this.color = color;
  19.         this. pattern = pattern;
  20.         this.shape = shape;   
  21.     }
  22.  
  23.     /**
  24.      * number of objects
  25.      * only 1-3
  26.      */
  27.     public String getNumber()
  28.     {
  29.  
  30.         return number;
  31.     }
  32.  
  33.     /**
  34.      * returns color
  35.      */
  36.  
  37.     public String getColor()
  38.    {
  39.         return color;
  40.     }
  41.  
  42.     /**
  43.      * returns pattern
  44.      */
  45.     public String pattern()
  46.     {
  47.  
  48.         return pattern;
  49.     }
  50.  
  51.     /**
  52.      * returns shape
  53.      */
  54.     public String shape()
  55.     {  
  56.         return shape;
  57.     }
  58.  
  59.     public String toString()
  60.     {
  61.  
  62.       // return Card(String number, String color, String pattern, String shape);
  63.          StringBuilder theOutput=new StringBuilder();
  64.          theOutput.append(" number:");
  65.          theOutput.append(number);
  66.          theOutput.append(" color:");
  67.          theOutput.append(color);
  68.          theOutput.append(" pattern:");
  69.          theOutput.append(pattern);
  70.          theOutput.append(" shape:");
  71.          theOutput.append(shape);
  72.          theOutput.append(" "); 
  73.        //  System.out.println(theOutput);
  74.          return theOutput.toString();
  75.  
  76.     }
  77.  
  78. }
Nov 11 '09 #33

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

Similar topics

23
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...
4
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... ...
15
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...
7
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
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...
0
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...
5
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...
5
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,...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.