- Home /
How to Instantiate Objects based on a list of integers
Hey, I've made multiple games now including a platformer, pong/breakout style game, and a top-down game. However, I'm still learning C# and I'm stuck trying to make a DeckController script for my card game. Originally I had created an array for the deck but realized that an array would cause problems when I started trying to add and remove values from the array so now I'm using a list.
I have assigned integers to the list, each as a seperate card id and I was able to successfully shuffle the list . However I have no clue how to instantiate gameobjects to a position in the ui or scene for each card based on the integers I assigned. I am having loads of trouble and I know a card game is difficult to make but it's what I'm interested in.
Hopefully someone can help me or put me on the right path to learn since I'm not the best at working with the Unity classes and namespaces.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeckController : MonoBehaviour {
int FireballID = 1;
int PyroblastID = 2;
int IceBlockID = 3;
public List<int> Deck = new List<int>();
public void Shuffle(List<int> DeckToShuffle)
{
for (int i = 0; i < DeckToShuffle.Count; i++)
{
int temp = DeckToShuffle[i];
int randomIndex = Random.Range(i, DeckToShuffle.Count);
DeckToShuffle[i] = DeckToShuffle[randomIndex];
DeckToShuffle[randomIndex] = temp;
}
}
// Use this for initialization
void Start () {
Deck.Add(PyroblastID);
Deck.Add(FireballID);
Deck.Add(IceBlockID);
Shuffle(Deck);
}
// Update is called once per frame
void Update () {
}
}
It's curious to me that they call it a list, because from a more general CompSci perspective a list usually implies a linked list, which this isn't (fortunately). It is a generic (that is type independent) implementation of an array, but you're right that it is better suited than a simple array because of the features of the container.
I would recommend you consider making a card class, and think of the deck as a list of cards, not a list of integers. Doing this means you can include information about each card's location (possibly a GameObject link, which then has it's own location information and perhaps artwork).
Further, if the deck is larger than 3 cards, there are many ways to shuffle - but the one you've posted might not work so well on a large deck. One fairly efficient means would be to include, in a card class, a numeric value (perhaps a 64 bit integer or float), I might call "order" or "deckposition". To shuffle, you could then rip through the list (from first to last element), assigning a random value to this deckposition member of each card. Then, sort the list by deckposition. Doing this sorts the deck, but by a random value, meaning shuffled. Repeating the process a random (few) times ensures a shuffled deck, and it's fairly quick, rather more easily written.
I wouldn't say List implies a linked list. It just means that the length can change. List is an interface (not in C# but generally in Computer Science) and Array Lists are a common and fast implementation of it. Adding and removing elements at the end has an amortized cost of O(1) but you still get O(1) for random access.
I hesitated to respond because I don't want my text to seem rude or confrontational, but if you'll accept this reply in the spirit of discussion I'll take the risk.
While I understand your point about C#'s List, the problem I have is that works dating from Don $$anonymous$$nuth's legendary encyclopedia on computer science from the late 60's through all texts that reference data structures and related algorithms into the 21st century, the word list is exclusively reserved for structures using links precisely to avoid confusion about the implications of the underlying structures on performance when compared to structures like arrays. $$anonymous$$y own college days were in the 80's, which prompts a perspective on the use of the word "List" as an implication that promotes confusion, but those learning computer science from C# (or Java) are induced into a counter vocabulary. Even in the context of C#, an array list represents a redundant phrase when one accepts that List is implemented as an array. It was also a point of contention, at one point from the 80's, to refer to binary trees as linked list structures, which they are, because the performance and method of implementing the tree is so different from that of a linked list. In a way this is like an English speaking student of French learning that zero is an egg ;) Borland was once criticized for creating an unordered container they called a bag, which had no relevance to implementation and performance characteristics as a result (from their OWL library in the early 90's).