Search Unity

removing from list<new>

Discussion in 'Scripting' started by Josenifftodd, Feb 8, 2016.

  1. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Hello, I'm trying to make it so my deck shuffles and when it draws the cards to the player it removes the cards it has given to player.

    I've managed to make it so when I press "S" it takes a random card from the Deck and puts it into the player1 pile.

    I'm not sure if I'm doing this correct if not could someone point me in the right direction?
    as you can see I'm trying to draw 6 cards into the players hand.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class deal_cards : MonoBehaviour
    6. {
    7.  
    8.     // Use this for initialization
    9.     public bool shuffling = false;
    10.     public List<GameObject> deckofCards= new List<GameObject>();
    11.     public List<GameObject> player1Hand = new List<GameObject>();
    12.  
    13.     void Start()
    14.     {
    15.  
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (Input.GetKeyDown(KeyCode.S))
    22.         {
    23.            int newNumber = Random.Range(0, deckofCards.Count);
    24.            // for (int i = 0; i < letters.Count; i++)
    25.             {
    26.                 player1Hand.Add(deckofCards[newNumber]);
    27.                 //letters.Remove(tempLetters[newNumber]);
    28.             }
    29.         }
    30.         if(Input.GetKeyDown(KeyCode.S) && player1Hand.Count == 6)
    31.         {
    32.  
    33.         }
    34.     }
    35. }
     
  2. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    Looks good.

    Add to player list then remove from deck list.

    You could even make the deckofcards and playershand classes and add a removefromdeck and addtodeck method and expose them. You could go as far as making the cards themselves a class so that you can give each one an individual set of attributes
     
  3. pixpusher2

    pixpusher2

    Joined:
    Oct 30, 2013
    Posts:
    121
    Just something quick:
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.S))
    2. {
    3.     if (deckofCards.Count >= 6)
    4.     {
    5.         // Draw 6 cards into Player's hand
    6.         for (int i = 0; i < 6; i++)
    7.         {
    8.             int newNumber = Random.Range(0, deckofCards.Count);
    9.  
    10.             player1Hand.Add(deckofCards[newNumber]);
    11.             deckofCards.RemoveAt(newNumber);
    12.         }
    13.  
    14.         // Show Player's hand when done
    15.         for (int i = 0; i < player1Hand.Count; i++)
    16.         {
    17.             Debug.Log("Player has " + player1Hand[i]);
    18.         }
    19.     }
    20.     else
    21.     {
    22.         Debug.Log("Not enough cards. Only " + deckofCards.Count + " remains");
    23.     }
    24. }
     
  4. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Be aware, though, that if remove an element in the middle of the list, you're going to have an empty space.

    [x,y,z]
    RemoveAt(1)
    [x, null, z]

    And now you have a Count of 2, but 3 spaces in the list.

    You can take care of this by moving the last item to the empty space.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    ... no. That's not how List.RemoveAt works. x will be on 0, and z will be on 1.
     
  6. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    OK. Will it shift all the remaining items one down?
     
  7. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    You don't need to if using a list, since remove will do that for you. You should be able to keep grabbing things using Random.Range(0, list.Count)
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Yes.
     
  9. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I kinda understand what you mean pal, could you do a quick example of what you mean fully so i can get a better understanding as I'm still learning. You dont have to make a extremely detailed example just a simple example showing me what you mean :)
     
  10. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Cheers pal, would I use the same way to shuffle a deck from one list to another list? using the random.range?
     
  11. Magnumstar

    Magnumstar

    Joined:
    Oct 3, 2015
    Posts:
    304
    Lists resize automatically. I would deft go with pixpushes example since its functionally sound and I'm on an iPad now and cannot give you an example. I think that is e best solution.
     
  12. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Yehhh sounds good :)
     
  13. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I've made it so i can shuffle the deck and then put the randomly picked cards from the shuffled deck into the players, the only problem I'm having is that it duplicates the card which is dealt. Is they anyway I can check to see if the prefab is being used, bare in mind I wanna load the cards from the resource folder in the future so I can add more decks to play with.
     
  14. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    119
    I'm making a card game too, so let me lend you a hand ;).
    This is how I manage to shuffle the "Deck". This will be done once, at the beginning of the game so you don't have to do Random.Range every time.
    Code (CSharp):
    1. public void Shuffle<T>(this IList<T> ts) {
    2.         var count = ts.Count;
    3.         var last = count - 1;
    4.         for (var i = 0; i < last; ++i) {
    5.             var r = Random.Range(i, count);
    6.             var tmp = ts[i];
    7.             ts[i] = ts[r];
    8.             ts[r] = tmp;
    9.         }
    10.     }
    After we shuffle the list we convert it to a Queue.
    Code (CSharp):
    1. CardsQueue = new Queue<CardsClass>(ShuffledList);

    Now if we need to draw the next card, just dequeue the top one from the deck and it will be removed automatically from the deck.
    Code (CSharp):
    1. CardClass nextcard = CardsQueue.Dequeue();
    2. //Do whatever you want with card
    3. nextcard.GivetoPlayer();
    Hope it helps.
     
  15. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I've already got it to shuffle the cards by putting the deck randomly into another then getting it to draw said amount from shuffle deck into the players hand... for what the cards are going to do, i was thinking more dragging and dropping the cards as both players will be dragging the cards from each hand to start a pile. :p Cheers though i see how it looks for the first card on the deck so that comes in handy