1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

C# question (java related)

Discussion in 'Programming & Webmastering' started by spy2520, Feb 26, 2010.

  1. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    I'm totally green on C#. Just got bored and decided to port one of my java apps to C# to see how it would go. Turns out its 90% cut and paste lol. Anyway...

    In java i have a for loop that basically reads "for each suit in cards.suits, <execute code>".

    This is written in java as "for (Card.suits S: Card.suits.values()){execute code}".

    How would i go about doing this in C#?
     
  2. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    It something like this i think: "foreach (suits S in Card.suits)"

    but not quite that. Cards.suits is an enumerated type also.
     
  3. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    I tend to avoid foreach statements in C# because I end up having to go back and change it to a for loop anyway (collection was altered error). Basically...
    Code:
    IF AN ARRAY (fixed length):
    for (int i = 0; i < Card.suits.Length; i++)
    {
      Card.suits[i];
    }
    
    IF A LIST (variable length):
    for (int i = 0; i < Card.suits.Count; i++)
    {
      Card.suits[i];
    }
    
    FOREACH STATEMENT:
    foreach (Suit suit in Card.suits)
    {
      suit
    }
    What is Card.suits declared as? Also, what does Card.suits.values() return?
     
    spy2520 says thanks.
    Crunching for Team TPU
  4. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    Card.suits is an enumerated type, it looks like "protected enum suits {Hearts, Diamonds, Clubs, Spades}" in java, and similar in c# except 'protected' didnt work how i expected it to.

    And it appears in C# that Card.suits.values() doesnt exist.
     
  5. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.77/day)
    Thanks Received:
    5,620
    Location:
    Cheeseland (Wisconsin, USA)
    What methods does your card class expose?
     
  6. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    An enum is just a constant number (with names attached). The closest I think you can get to .values() would be Card.suits.GetNames() (returns strings) or Card.suits.GetValues() (returns numerical type).

    I think this should work for you:
    Code:
    foreach (Card.suits S in Card.suits.GetValues())
    {
    }

    Enumerator types should be declared either public or internal (only this assembly can access it). I perfer public and I always place them in "Enumerators.cs" without an encompassing class. For example:
    Code:
    namespace MyAppNamespace
    {
      public enum Suits : byte { Hearts, Diamonds, Clubs, Spakes };
    }
    Just type "Suits" and you got it.
     
    Last edited: Feb 26, 2010
    spy2520 says thanks.
    Crunching for Team TPU
  7. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    The Card class literally just has getFace(), printFace(), and ToString(). All 3 are public currently. This for loop is actually in CardDeck which will contain 52 instances of the Card class in an array.
     
  8. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    Why is "byte" there? Is this necessary or is Suits{ Hearts, Diamonds, Clubs, Spakes } all that is needed?
     
  9. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    By the way, a great thing about C# is it has properties:
    Code:
    private Suits _Face;
    public Suits Face
    {
      get { return _Face; }
    }
    Because it only has a "get" member, Card.Face would be read-only. "get" in the name is therefore redundant. ;)

    Just for reference, here is an example with get and set:
    Code:
    private Suits _Face;
    public Suits Face
    {
      get { return _Face; }
      set { _Face = value; }
    }
     
    spy2520 says thanks.
    Crunching for Team TPU
  10. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    i see.
     
  11. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    I think the default for enumerators is int (32-bit signed integer). By explicitly declaring it byte (8-bit unsigned integer), it takes up 1/4th the memory. It isn't necessary but good practice.

    Put verbally in that context, "Suits inherits byte" just as System.Byte inherits System.Object (everything in C# ultimately comes back to Object).
     
    spy2520 says thanks.
    Crunching for Team TPU
  12. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    oh that makes alot of sense. Thanks
     
  13. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    i'm still having an issue with the for loop. I have attached an image. Even intellisense only wants to give me the elements in Suits. I cant get anything like getValues or anything. I must be missing something simple. I have Enumerators.cs and have two enumerated types (Suits and Faces), i wrote them how you did in your example.
     

    Attached Files:

  14. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    Crunching for Team TPU
  15. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    ok cool i'll try that.
     
  16. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    ok i got foreach (Suits S in Enum.GetValues(typeof(Suits))) with no squiggly lines. THANK YOU. On to the next roadblock...
     
  17. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    Good thing I linked you. I had another brain fart on typeof. XD

    I need to get to bed. :shadedshu
     
    Crunching for Team TPU
  18. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    yup, thanks.
     
  19. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    alright i just gotta create a GUI now, should be fun since i never bothered in java, just printed to the console It seems C# needs just about everything public as opposed to java.
     
  20. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    ok heres the long post

    Code:
    using System;
    using System.Collections;
    
    namespace blackJackPort
    {
        public class Game
        {
    
            public class Hand
            {
                public int handValue = 0;
                ArrayList handCards = new ArrayList();
    
    
               public void addCard(Card card)
                {
                    if (card.getFace().Equals("Ace") && handValue > 10) card.value = 1;
                    handCards.Add(card);
                    handValue += card.value;
                    Bust();
                }
                
                bool Bust()
                {
                    if (handValue > 21) return true;
                    else return false;
                }
                
               public string printCards()
                {
    			string CardList = "";
    			foreach (Card x in handCards)
                    {
    			    CardList += x.toString() + "\n";	
    			    }return CardList;
    		    }
    
    
            }
            
            public CardDeck Deck;
            public int card;
            public Random rand = new Random();
            public Hand DealerHand, PlayerHand;
    
    
           public void initialDeal(){
    			initializeDeck();
    			DealerHand = new Hand(); PlayerHand = new Hand();
    			DealPlayer(); DealDealer();
    			DealPlayer(); DealDealer();
    			//System.out.println(printHand());
    			}
           public void initializeDeck()
            {
                Deck.fillDeck();
               
            }
    
            public void DealPlayer()
            {
                card = rand.Next(52);
                Card newCard = new Card(Deck.cards[card].value, Deck.cards[card].suit, Deck.cards[card].face);
                if (newCard.value == 0) DealPlayer();
                else
                {
                    PlayerHand.addCard(newCard);
                    Deck.cards[card].value = 0;
                }
    
            }
            public void DealDealer()
            {
                card = rand.Next(52);
                Card newCard = new Card(Deck.cards[card].value, Deck.cards[card].suit, Deck.cards[card].face);
                if (newCard.value == 0) DealDealer();
                else
                {
                    DealerHand.addCard(newCard);
                    Deck.cards[card].value = 0;
                }
    
            }
            //void Bust(int Hand){
            //	if (Hand > 21) System.out.println("BUST");
    
            //}
           public String printHand()
            {
                return ("\nDealer: " + DealerHand.handValue + " " + "Player: " + PlayerHand.handValue + "\n");
            }
           public String printAllHands()
            {
                return ("Player Hand: \n" + PlayerHand.printCards() + "\nDealer Cards: \n" + DealerHand.printCards());
            }
           public Boolean playerWins()
            {
                if (DealerHand.handValue > 21 || (PlayerHand.handValue > DealerHand.handValue && PlayerHand.handValue <= 21)) return true;
                else return false;
            }
        }
    
    
    }
    
    Code:
    using System;
    
    namespace blackJackPort
    {
       public class Card
        {
            //public enum suits { Hearts, Diamonds, Clubs, Spades }
            //public enum faces { Face2, Face3, Face4, Face5, Face6, Face7, Face8, Face9, Face10, King, Queen, Jack, Ace }
            public Faces face;
            public Suits suit;
    
            public int value
            {
                get { return value; }
                set { value = 0; }
    
            }
            public Card(int value, Suits suit, Faces face)
            {
                this.suit = suit; this.value = value;
    
                this.face = face;
                if (face == Faces.Ace)
                {
                    this.value = 11;
                }
            }
    
            public string printFace()
            {
                return this.face.ToString();
            }
            public string toString()
            {
                string words;
                if (face.ToString().Length > 3 && face.ToString().Contains("Face"))
                    words = face.ToString().Substring(4) + " of " + suit + ": " + value;
                else words = face + " of " + suit + ": " + value;
                return words;
            }
            public Faces getFace()
            {
                return face;
            }
    
        }
       public class CardDeck
        {
            public Card[] cards;
            int count = 0;
            int MaxCards = 52;
    
            CardDeck()
            {
                cards = new Card[MaxCards];
            }
    
            void addCard(int value, Suits suit, Faces face)
            {
                cards[count++] = new Card(value, suit, face);
            }
    
    
            public void fillDeck(){
    		
    	foreach (Suits S in Enum.GetValues(typeof(Suits))){
    		int counter = 2;
    		foreach (Faces F in Enum.GetValues(typeof(Suits))){
    			if (counter < 10){
    				addCard(counter,S,F);
    				counter++;
    			}else addCard(10, S,F);
    		}	
    		}			
    							}
            public String toString()
            {
                String CardList = "";
                for (int x = 0; x < MaxCards; x++)
                {
                    CardList += cards[x].toString() + "\n";
                } return CardList;
            }
            public void removeCard(int index)
            {
                cards[index].value = 0;
            }
        }
    }
    Code:
    namespace blackJackPort
    {
        public enum Suits : byte { Hearts, Diamonds, Clubs, Spades };
        public enum Faces : byte { Face2, Face3, Face4, Face5, Face6, Face7, Face8, Face9, Face10, King, Queen, Jack, Ace };
    }

    Theres all my code, now for the question...
     
  21. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace blackJackPort
    {
        public partial class Form1 : Form
        {
    
            public Game game
            {
                get { return game; }
                set { game = new Game(); }
            }
            public Form1()
            {
                InitializeComponent();
                
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
               
            }
    
            private void ExitButton_Click(object sender, EventArgs e)
            {
                Close();
            }
    
            private void newButton_Click(object sender, EventArgs e)
            {
                hitButton.Enabled = true;
                stayButton.Enabled = true;
                [B]game.initializeDeck();[/B]
                playerHandLabel.Text = "Player Hand";
                dealerHandLabel.Text = "Dealer Hand";
            }
        }
    }
    
    The bold part is where i am having problems. When its time to initialize the deck the program freezes and VS locks up, i dont get an error message so its kinda hard to diagnose. Does anything seem wrong? I know its alot to ask looking through all the code but i cannot see where i went wrong.

    Even if i comment out all the code in CardDeck.InitializeDeck() it still locks up, so i'm not too sure its even the code causing the problem.
     
  22. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    VS caught it for me (make sure it is in debug mode):
    StackOverflowException @ line 17 in Form1.cs
    Code:
            public Game game
            {
                [b]get { return game; }[/b]
                set { game = new Game(); }
            }
    That's a circular reference and the variable "game" does not exist although the member name "game" does; this should fix it:
    Code:
            private Game game = new Game();
            public Game Game
            {
                get { return game; }
            }
    Next, fillDeck() throws a NullReferenceException because Deck is null. I'm not sure how you want to fix that but it generally involves "new Deck()" before it reaches fillDeck().
     
    spy2520 says thanks.
    Crunching for Team TPU
  23. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    oh ok thanks, i'll get it straight.
     
  24. spy2520 New Member

    Joined:
    Mar 8, 2006
    Messages:
    498 (0.15/day)
    Thanks Received:
    16
    i think my problem is that VS is crashing before i can see whats going on in debug mode, it doesnt care about my breakpoints. Must be an issue with the beta. We'll see.
     
  25. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    14,234 (6.20/day)
    Thanks Received:
    4,012
    Location:
    IA, USA
    Oh, you are using Visual Studio 2010 beta? That could be it. I am running Visual Studio 2008 Team Suite.
     
    Crunching for Team TPU

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page