Question by
LasseMillek · Dec 20, 2017 at 11:25 AM ·
gamerandomlayoutcards
Un randomize memory cardgame
Hello there. I have made a memory game with a randomizing effect on the cards, with help from a tutorial. Now i want the cards to not be random, but be in the slots i choose, can someone see a solution for this in the code? First script:
public class Playmat : VersionedView
{
public bool gameWon = false;
public int TotalPoints = 0;
public int Points = 0;
public int NumberOfCardsFlipped = 0;
public enum BoardState {Flipping,Comparing}
public BoardState state = BoardState.Flipping;
private static Playmat mat;
public GameObject cardPrefab;
public Layout layout;
public GameObject board;
public List<Card> cards = new List<Card>();
public Card card1;
public Card card2;
// Use this for initialization
void Start ()
{
mat = this;
GameSettings.Instance().SetDifficulty(GameSettings.GameDifficulty.Hard);
CreateLayout();
CreateCardsFromLayout();
CreateCardTypes();
}
public static Playmat GetPlaymat()
{
return mat;
}
public void CreateLayout()
{
board = layout.GetRandomLayout();
}
void CreateCardsFromLayout()
{
foreach(Slot s in layout.slots)
{
GameObject go = GameObject.Instantiate(cardPrefab, s.transform.position, Quaternion.Euler(0, 90, 0)) as GameObject;
go.transform.parent = s.transform;
Destroy(s.GetComponent<BoxCollider>());
cards.Add(go.GetComponent<Card>());
}
}
void CreateCardTypes()
{
for(int i=0;i <=cards.Count/2;i++)
{
Card c1 = cards[i];
Card c2 = cards[cards.Count - 1 - i];
string type = GameSettings.Instance().GetRandomType();
c1.GenerateCard(type);
c2.GenerateCard(type);
}
TotalPoints = cards.Count / 2;
}
public string GetPointsString()
{
return Points.ToString() + "/" + TotalPoints.ToString();
}
public override void DirtyUpdate()
{
switch (state)
{
case BoardState.Comparing:
Compare();
break;
}
}
public void Compare()
{
if(card1.CardType == card2.CardType)
{
NumberOfCardsFlipped = 0;
Points++;
if(TotalPoints == Points)
{
gameWon = true;
}
}
else
{
card1.Unflip();
card2.Unflip();
}
card1 = null;
card2 = null;
state = BoardState.Flipping;
}
public void SetCardsForMatch(Card c)
{
if(card1==null)
{
card1 = c;
state = BoardState.Flipping;
}
else
{
card2 = c;
state = BoardState.Comparing;
}
MarkDirty();
}
}
Game settings:
public class GameSettings
{
private static GameSettings gameSetting;
private GameSettings(){}
public static GameSettings Instance()
{
if (gameSetting == null)
{
gameSetting = new GameSettings();
}
return gameSetting;
}
public enum GameDifficulty { Easy, Medium, Hard }
public GameDifficulty difficulty= GameDifficulty.Easy;
private string[] easyDifficulty = {"Blue", "Yellow", "Green", "Pink", "Purple", "Red", "Orange" };
private string[] mediumDifficulty = {"Blue", "Yellow", "Green", "Pink", "Purple"};
private string[] hardDifficulty = {"Blue", "Yellow", "Green", "Pink", "Purple", "Red", "Orange"};
public List<string> CardTypes
{
get
{
List<string> tempList = new List<string>();
switch (difficulty)
{
case GameDifficulty.Easy:
tempList.AddRange(easyDifficulty);
break;
case GameDifficulty.Medium:
tempList.AddRange(mediumDifficulty);
break;
case GameDifficulty.Hard:
tempList.AddRange(hardDifficulty);
break;
}
return tempList;
}
}
public void SetDifficulty(GameDifficulty diff)
{
difficulty = diff;
}
public string GetRandomType()
{
return CardTypes[Random.Range(0, CardTypes.Count)];
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612144921im_/https://answers.unity.com/themes/thub/images/avi.jpg)