Hello, I am having difficulties getting this code to work. I am doing another school assignment and I got pretty far into it before I started having problems and I can not get it to work and I have no clue why. I used the unity references and past codes from my previous labs to complete this one. What I needed to do is in the TODO section of the files or the blue sections.
///////////////////////////////////////////////////////////////////
// Purpose: This function will build the card deck and randomly
// choose a part missing from the robot. You will
// be randomly choosing the image and then pushing it
// onto the card arrays.
//
// Grade: 100/100
///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// Code Explanation:
// In this function, you will be using the arrays that are found within
// the GameScript file, and use them to store the robot cards that will
// be displayed to the user. The robots have lost a random body part
// and you will need to specify these body parts, and then push this
// data onto the aCards array found in the GameScript file
static function BuildDeck()
{
// ****TODO****
// You are required to complete the functionality of this lab and
// ensure that the aCards array found in the script GameScript are
// populated with UNIQUE Robot cards.
// Use the tools that you have been shown throughout the month to
// complete this lab. You will be given some pseudo code with hints
// of how to get this project to work, but in general, it is up to you
// with how you want to implement it to work.
// HINT: READ THE DOCUMENT FOUND WITH THIS PROJECT IN ORDER TO SEE
// HOW THE CARD IMAGES ARE DRAWN AND HOW THIS CODE WORKS. IT ALSO
// BREAKS DOWN THE ELEMENTS OF THE CARD CLASS.
//////////////////////////////////////////////////////////////////////
// CODE CRITERIA
//
//
// Check the Lab documentation to see how the grading will be broken
// down for this assignment
//////////////////////////////////////////////////////////////////////
// Pseudo Code Example
//
// Create a variable to keep track of the ID number, starting from 0
// You can optionally have additional variables to help store other
// data such as a temporary Card class as well as the total number
// of robots, 4.
var id: int = 0;
var totalRobots : int = 4;
var card :Object;
// Loop through the 4 robots
// For each of these robots, within the for loop, create an array
// that stores the names for the three main body parts. Remember,
// from the images, you will see that "Head", "Arm", and "Leg" will
// cover these missing parts
for(i = 0; i < totalRobots; i++)
{
var aMissingRobotPart : Array = ["Head", "Arm", "Leg"];
// Next, you want to have another loop within the primary robot
// loop, since it would be good to have two body parts chosen
// for each of the robots
for(j = 0; j < 2; j++)
{
// Pick a random body part from the array. Use Random.Range to
// store a random number index. Look at previous labs for examples
// of the use of Random.Range or use the API Reference help
var randomNumber : int = Random.Range(0, aMissingRobotPart.length);
// It would probably be a good idea to set this randomly chosen
// robot part string into another String variable. That way you
// can then remove the string from the original array so that it
// will not be used again.
var randomRobotPart : String = aMissingRobotPart[randomNumber];
aMissingRobotPart.RemoveAt(randomNumber);
// Now you want to create both of the cards, one for the actual
// missing part, and a second for the robot missing the part
// A good way to get this done is with string concatenation
// For example if I wanted to bring in a number, add it to the
// string robot, and then add a missing part from a previously
// stored string called missingPart, and having an id number
// variable, it can look like:
// var temp = new Card("robot" + 1 + "Missing" + missingPart, idNumber);
card = new Card("robot" + 1 + "Missing" + randomRobotPart, id);
card = new Card("robot" + 2 + "Missing" + randomRobotPart, id);
// You will want to push these new temporary Card variables into the
// Gamescript.aCards array by using the Push() function. So with the
// temp variable creatd above it will look like:
// Gamescript.aCards.Push(temp);
GameScript.aCards.Push("card");
// Make sure you add both the robot missing the part as well as the
// part itself. If you have an ID number that you are keeping track of
// for the two cards, you want to make sure that the two matching cards
// have the same ID number, but the rest of the matching cards have different
// ID numbers, so updating it after both the matching cards are created
// would be a good idea.
id++;
}
}
}
Here is the short hand of that (without the TODO’s in the way, just code).
[
static function BuildDeck()
{
var id: int = 0;
var totalRobots : int = 4;
var card :Object;
for(i = 0; i < totalRobots; i++)
{
var aMissingRobotPart : Array = ["Head", "Arm", "Leg"];
for(j = 0; j < 2; j++)
{
var randomNumber : int = Random.Range(0, aMissingRobotPart.length);
var randomRobotPart : String = aMissingRobotPart[randomNumber];
aMissingRobotPart.RemoveAt(randomNumber);
card = new Card("robot" + 1 + "Missing" + randomRobotPart, id);
GameScript.aCards.Push(card);
card = new Card("robot" + 1 + "Missing" + randomRobotPart, id);
GameScript.aCards.Push(card);
id++;
}
}
}
There are three other files that are called on to make the program work, but we are not supposed to touch.
CardDefine.js
// Class for the Card game
class Card extends System.Object
{
var isFaceUp : boolean = false;
///////////////////////////////////////////////////////////////////////////////////
// Boolean to check to see if the cards are matching
var isMatched : boolean = false;
// Variables that will hold the id and the image string for this class
var img : String;
var id : int;
function Card(img : String, id : int)
{
this.img = img;
this.id = id;
}
}
Grid.js
// Building the grid of cards and determining what is showing up
function BuildGrid()
{
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
for(i = 0; i < GameScript.rows; i++)
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
for(j = 0; j < GameScript.cols; j++)
{
var card : Object = GameScript.aGrid[i][j];
var img : String;
// Determining if front or back of the card is showing
if(card.isMatched)
{
img = "blank";
}
else
{
if(card.isFaceUp)
{
img = card.img;
}
else
{
img = "wrench";
}
}
GUI.enabled = !card.isMatched;
if(GUILayout.Button(Resources.Load(img), GUILayout.Width(GameScript.cardW)))
{
if(GameScript.playerCanClick)
{
GetComponent(GameScript).FlipCardFaceUp(card);
//GetComponent(GameScript).FlipCardFaceUp(card);
}
Debug.Log(card.img);
}
GUI.enabled = true;
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
GUILayout.FlexibleSpace();
GUILayout.EndVertical(); // I WILL BE IN SHOCK IF THIS RUNS THE FIRST TIME
}
GameScript.js
//////////////////////////////////////////////////////////////
// Purpose: To render the cards to the screen by properly
// allocating the images to the cards
//
// Grade: 50/100
//
// Do this section after you have completed CardDefine!!!!!
//////////////////////////////////////////////////////////////
// The global variables that are needed for this script
static var cols : int = 4; // The number of columns in the card grid
static var rows : int = 4; // The number of rows in the card grid
var totalCards : int = cols * rows; // I think the answer is 16, but I suck at math :)
var matchesNeededToWin : int = totalCards * 0.5; // If there are 16 cards, the player needs to find 8 matches to clear the board
static var matchesMade : int = 0; // At the outset, the player has not made any matches
static var cardW : int = 100; // Each card's width and heightis 100 pixels
static var cardH : int = 100;
static var aCards : Array; // We'll store all the cards we create in this Array
static var aGrid : Array; // This array will keep track of the shuffled, dealt cards
var aCardsFlipped : ArrayList; // This array will store the two cards that the player flips over
static var playerCanClick : boolean; // We'll use this boolean flag to prevent the player from clicking buttons we don't
// want them to because players are stupid
var playerHasWon : boolean = false; // Store whether or not the player has won.
// Function to initialize the data, especially with the arrays
function Start()
{
playerCanClick = true; // I think the player should play now...maybe...
aCards = new Array();
aGrid = new Array();
aCardsFlipped = new ArrayList();
// Making lots of purty cards
CardPopulator.BuildDeck();
for(i = 0; i < rows; i++)
{
aGrid[i] = new Array();
for(j = 0; j < cols; j++)
{
var someNum : int = Random.Range(0, aCards.length);
aGrid[i][j] = aCards[someNum];
aCards.RemoveAt(someNum);
}
}
}
function OnGUI()
{
GUILayout.BeginArea(Rect( 0, 0, Screen.width, Screen.height));
GUILayout.BeginHorizontal();
GetComponent(Grid).BuildGrid();
if(playerHasWon)
{
BuildWinPrompt();
}
GUILayout.EndHorizontal();
GUILayout.EndArea();
//print("buiding grid!");
}
// Function to flip the cards and to store what is actually flipped
// into the array list
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)
{
// Match
// HINT:: Use this similar setup for the TODO
// below!!!
aCardsFlipped[0].isMatched = true;
aCardsFlipped[1].isMatched = true;
matchesMade++;
if(matchesMade >= matchesNeededToWin)
{
playerHasWon = true;
}
}
else
{
aCardsFlipped[0].isFaceUp = false;
aCardsFlipped[1].isFaceUp = false;
}
aCardsFlipped = new ArrayList();
playerCanClick = true;
}
}
}
function BuildWinPrompt()
{
var winPromptW : int = 100;
var winPromptH : int = 90;
var halfScreenW : float = Screen.width * 0.5;
var halfScreenH : float = Screen.height * 0.5;
var halfPromptW : int = winPromptW * 0.5;
var halfPromptH : int = winPromptH * 0.5;
GUI.BeginGroup(Rect(halfScreenW - halfPromptW, halfScreenH - halfPromptH, winPromptW, winPromptH));
GUI.Box(Rect(0, 0, winPromptW, winPromptH), "A Winna is You!!");
if(GUI.Button(Rect(10,40,80,20), "Play Again"))
{
Application.LoadLevel("Game");
}
GUI.EndGroup();
}
The only thing that I need to do for the grade is the CardPopulator.js file which is the first two code links. The others are not to be touched.
I just need help with finding out why my code isn’t working. Any and all help would be greatly appreciated. Thank everyone in advance.