Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by stephenjohnellis · Jul 17, 2014 at 09:23 AM · arraygridcards

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;
     }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges