Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Card Game

Discussion in 'Scripting' started by unity_vGVNBMGyUg0BlQ, Apr 20, 2019.

  1. unity_vGVNBMGyUg0BlQ

    unity_vGVNBMGyUg0BlQ

    Joined:
    Apr 20, 2019
    Posts:
    2
    Hi all - a unity and C# newbie here.

    I've been trying, as my first unity project, to recreate a card game i wrote a while back in python using the pygame library. The game is similar (although with some significant differences) to this https://en.wikipedia.org/wiki/Switch_(card_game).

    In my original python game there were four classes (in a single python file):

    - Card - stored the value, suit and image of a single card
    - Deck - instantiated a deck of card objects and contained methods to shuffle and deal
    - Player - took input from the player and allowed valid moves to be played
    - Opponent - computed possible runs from the opponents hand, assigned scores to each (based on my understanduing of the game) and then played the one with the highest score
    -In addition to this there were global functions (which i believe are not allowed in C#) to control turns and draw the game window.
    -Card objects were stored in lists: player hand, opponent hand, shuffled deck, pile, ect, and were transferred from one list to another each turn.

    My question is this - what is the best way to recreate this in unity? Should I have separate scripts for the deck and the players (I have so far assumed so), should i have a separate game object for the card class? a separate script? or can I have this class defined in the same script as the deck class? What does it mean to define a class outside of the monobehavior inheriting class?

    I have had a good go at it but i can tell there's something I'm just not grasping about unity or C#. I have created something that looks superficially like my original game (with a single player) but, at the end of the day, I can tell it's a non starter and needs significant changes.

    In my deck gameobject's script I am able to transfer card objects (containing amongst other things value and suit) from the shuffled deck to the players "arraylist" hand (which is a separate script on a different gameobject) and to instantiate these cards as prefab children of the player gameobject - and therefore draw them on screen and be able to move them from the player script. however from the player script i am unable to access the suit and value of the objects in my hand arraylist - I recieve the following error message when i try.

    "object" does not contain a definition for "value" and no accessuible extention method "value" accepting a first arguement of type object"

    however there is the correct number of these "objects" in the players hand.

    I hope someone can help me out and will be very grateful for any pointers or tips at all really - and i hope my question is clearly enough stated. I am a self taught programmer with no real C# or unity experience to speak of (and only a bit of python!).

    Thank you all in advance :).
     

    Attached Files:

  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    General program structure is something you'll develop over time, not something that can be "taught" in the traditional sense and there are dozens of valid architectures each with their own merits and evangelists who swear up and down that "their way is the right way". The only important aspect of making a game is that you get it done at some point.

    As for your error, it looks like your hand variable is declared as either an object or one of the old-school ArrayList collections before generics in C# were a thing. Change it to an appropriate type, most likely List<Card>, so the compiler can infer the type and recognize that there's a value field that you want to access.

    One more thing: You're using the | logical operator when you actually want || the conditional operator: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/
     
  3. unity_vGVNBMGyUg0BlQ

    unity_vGVNBMGyUg0BlQ

    Joined:
    Apr 20, 2019
    Posts:
    2
    Thank you for your help and advice - changing the hand from an ArrayList to a List<Card> seems to have done the trick! :) . As for my programs structure I will just go for whatever mad thing I can get to work - and then try to improve it after that :)