- Home /
Trying to Create a Custom array for Memory Game
Hey All,
I'm working off this template for a game I built from a book but am trying to customize it to fit my needs. The problem I'm running into is that no matter what I set the values for var row & cols, it always draws a 4x4 grid but I would like to be able to draw different size grids, like 2x4, 8x5, etc. The code I'm using is below. Any help would be appreciated. Thanks!
pragma strict
import System.Collections.Generic;
var cols:int = 4; var rows:int = 4; var totalCards:int = rows cols; var matchesNeededToWin:int = totalCards 0.5; var matchesMade:int = 0; var cardW:int = 250; var cardH:int = 100; var aCards:List.; var aGrid:Card[,]; var aCardsFlipped:List.; var playerCanClick:boolean; var playerHasWon:boolean = false;
function Start () { playerCanClick = true;
aCards = new List.<Card>();
aGrid = new Card[rows,cols];
aCardsFlipped = new List.<Card>();
BuildDeck();
for(var i:int = 0; i<rows; i++)
{
for(var j:int = 0; j<cols; j++)
{
var someNum:int = Random.Range(0, aCards.Count);
aGrid[i,j] = aCards[someNum];
aCards.RemoveAt(someNum);
}
}
}
var customSkin:GUISkin;
function BuildDeck(){ var totalNames:int = 4; var card:Card; var id:int = 0;
for(var i:int = 0; i<totalNames; i++)
{
var aNameList:List.<String> = new List.<String>();
aNameList.Add("Match");
aNameList.Add("Match");
aNameList.Add("Match");
aNameList.Add("Match");
for(var j:int = 0; j<2; j++)
{
var someNum:int = Random.Range(0, aNameList.Count);
var theMissingPart:String = aNameList[someNum];
aNameList.RemoveAt(someNum);
card = new Card("name" + (i+1) + "Missing" + theMissingPart, id);
aCards.Add(card);
card = new Card("name" + (i+1) + theMissingPart, id);
aCards.Add(card);
id++;
}
}
}
function OnGUI () {
GUI.skin = customSkin;
GUILayout.BeginArea(Rect(0,0,Screen.width,Screen.height));
BuildGrid();
if(playerHasWon) BuildWinPrompt();
GUILayout.EndArea();
}
function BuildWinPrompt(){ var winPromptW:int = 120; var winPromptH:int = 90; var halfScreenW:float = Screen.width/2; var halfScreenH:float = Screen.height/2; var halfPromptW:int = winPromptW/2; var halfPromptH:int = winPromptH/2;
GUI.BeginGroup(Rect(halfScreenW-halfPromptW, halfScreenH-halfPromptH, winPromptW, winPromptH));
GUI.Box(Rect(0,0, winPromptW, winPromptH), "You Win");
var buttonW:int = 80;
var buttonH:int = 20;
if(GUI.Button(Rect(halfPromptW-(buttonW/2), halfPromptH-(buttonH/2), buttonW, buttonH), "Next Level"))
{
Application.LoadLevel("Title");
}
GUI.EndGroup();
}
function BuildGrid() { GUILayout.BeginVertical(); GUILayout.FlexibleSpace(); for(var i:int = 0; i
if(card.isFaceUp)
{
img = card.img;
}else{
img = "set1_1";
}
}
if(GUILayout.Button(Resources.Load(img),GUILayout.Width(250))) { if(playerCanClick) { FlipCardFaceUp(card); } Debug.Log(card.img); } } GUI.enabled = true; GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); } GUILayout.FlexibleSpace(); GUILayout.EndVertical(); }
function FlipCardFaceUp(card:Card){ card.isFaceUp = true; if(aCardsFlipped.IndexOf(card) <0) { aCardsFlipped.Add(card);
if(aCardsFlipped.Count == 2)
{
playerCanClick = false;
yield WaitForSeconds (1);
if(aCardsFlipped[0].id == aCardsFlipped[1].id)
{
aCardsFlipped[0].isMatched = true;
aCardsFlipped[1].isMatched = true;
matchesMade ++;
if(matchesMade >= matchesNeededToWin)
{
playerHasWon = true;
}
}else{
aCardsFlipped[0].isFaceUp = false;
aCardsFlipped[1].isFaceUp = false;
}
aCardsFlipped[0].isFaceUp = false;
aCardsFlipped[1].isFaceUp = false;
aCardsFlipped = new List.<Card>();
playerCanClick = true;
}
}
}
class Card extends System.Object { var isFaceUp:boolean = false; var isMatched:boolean = false; var img:String; var id:int;
function Card(img:String, id:int){
this.img = img;
this.id = id;
}
}
Your answer
Follow this Question
Related Questions
RTS Grid Initialisation 0 Answers
[c#] Create visible appearence of a grid using texture or models 1 Answer
Push into Two-Dimensional Array 2 Answers
Which is the best way to make a 2D grid? 1 Answer
Grid matching issue 0 Answers