Hey all,
I’m sitting here trying to convert Ryan Creighton’s (Unity 3D Game Development By Example for Beginners) “Robot Repair” game into C# since that is the programming language that my boss asked me to learn and I’ve been stuck here for quite some time.
The three errors I’m currently getting are -
(98,53) Type string[]' does not contain a definition for
RemoveAt’ and no extension method `RemoveAt’ of type string (are you missing a using directive or an assembly reference?)
The other two errors are at 102 / 105’s aCards.Add(card); saying that there is no definition / extension method for Add.
I’ve tried dropping a ArrayList here and using List so I wouldn’t have to mess with typecasting as well as changing the way I write everything but I just don’t know enough I guess.
The original code up in JS is (up to this point):
class Card extends System.Object
var cols:int = 4; //the number of cols in the grid
var rows:int = 4; //the number of rows in the grid
var totalCards:int = col*rows;
var matchesNeededToWin:int = totalCards * 0.5;
var matchesMade:int = 0;
var cardW:int = 100;
var cardH:int = 100;
var aCards:Array; //Store all the cards we create here
var aGrid:Array; //Keep track of shuffled, dealt cards
var aCardsFlipped:ArrayList; //Store the two cards the player flips over
var playerCanClick:boolean;
var playerHasWon:boolean;
function Start()
{
playerCanClick = true;
//Initialize Arrays as empty lists
aCards = new Array();
aGrid = new Array();
aCardsFlipped = new ArrayList();
BuildDeck // my problem area :(
for(i=0; i<rows; i++)
{
aGrid *= new Array();*
for(j=0; j<cols; j++)
{
aGrid*[j] = new Card();*
}
}
}
function OnGUI()
{
GUILayout.BeginArea (Rect (0,0,Screen.width, Screen.height));
BuildGrid();
GUILayout.EndArea();
}
function BuildGrid()
{
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
for (i=0; i<rows, i++)
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
for(j=0; j<cols; j++)
{
var card:Object = aGrid*[j];*
if (GUILayout.Button(Resources.Load(card.img),
GUILayout.Width(cardW)))
{
Debug.Log(card.img);
}
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
}
function BuildDeck()
{
var totalRobots:int = 4;
var card:Object; // stores reference to a card
for (i=0; i<totalRobots; i++)
{
var aRobotsParts:Array = [“Head”, “Arms”, “Leg”];
for (j=0; j<2; j++)
{
var someNum:int = Random.Range(0, aRobotParts.length);
var theMissingPart:String = aRobotParts[someNum];
aRobotParts.RemoveAt(someNum);
card = new Card(“robot” + (i+1) + “Missing” + theMissingPart);
aCards.Add(card);
card = new Card(“robot” + (i+1) + theMissingPart);
aCards.Add(card);
}
}
}
var isFaceUp:boolean = false;
var isMatched:boolean = false;
var img:String;
function Card(img:String)
{
this.img = img;
}
}
Is as far as I’ve gotten. The basic idea is to create a grid of cards distributed randomly. The player then flips over cards to find pairs like in Memory.
This is as far as I’ve gotten.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameScript : MonoBehaviour {
* public int cols = 4; // the number of columns in the card grid*
* public int rows = 4; // the number of rows in the card grid*
* public int totalCards = 16;*
* private int matchesNeededToWin = 8;*
* private int matchesMade = 0;*
* private int cardW = 100;*
* private int cardH = 100;*
* Card[] aCards; // We’ll store all the cards we create in this Array *
* Card[,] aGrid; // This array will keep track of the shuffled, dealt cards.*
* ArrayList aCardsFlipped = new ArrayList();*
* Card card = new Card();*
* private bool playerCanClick;*
* private bool playerHasWon = false;*
* // Use this for initialization*
* void Start () {*
* playerCanClick = true;*
* aCards = new Card[totalCards];*
* aGrid = new Card[rows,cols];*
* aCardsFlipped = new ArrayList();*
* for (int i = 0; i < rows; i++){*
* for (int j = 0; j < cols; j++){*
* aGrid [i,j] = new Card();*
* }*
* }*
* BuildDeck();*
* }*
* // Update is called once per frame*
* void Update ()*
* {*
* }*
* void OnGUI ()*
* {*
* GUILayout.BeginArea (new Rect (0, 0, Screen.width, Screen.height));*
* BuildGrid();*
* GUILayout.EndArea();*
* }*
* void BuildGrid ()*
* {*
* GUILayout.BeginVertical();*
* GUILayout.FlexibleSpace();*
* for (int i=0; i<rows; i++)*
* {*
* GUILayout.BeginHorizontal();*
* GUILayout.FlexibleSpace();*
* for (int j = 0; j<cols; j++)*
* {*
* //Card card = new Card();*
* card = aGrid [i,j];*
* if (GUILayout.Button((Texture2D)Resources.Load(card.img),*
* GUILayout.Width (cardW)))*
* {*
* Debug.Log (cardW);*
* }*
* }*
* GUILayout.FlexibleSpace();*
* GUILayout.EndHorizontal();*
* } *
* GUILayout.FlexibleSpace(); *
* GUILayout.EndVertical();*
* }*
* void BuildDeck ()*
* {*
* int totalRobots = 4; // there are four robots*
* Card card;*
* for (int i=0; i<totalRobots; i++)*
* {*
* string[] aRobotParts = {“Head”,“Arm”,“Leg”};*
* for (int j=0; j<2; j++)*
* {*
* int someNum = Random.Range (0,totalRobots);*
* string theMissingPart = (string)aRobotParts[someNum];*
* aRobotParts.RemoveAt(someNum);*
* card = new Card(“robot” + (i+1) + “Missing” + theMissingPart);*
* aCards.Add(card);*
* card = new Card (“robot” + (i+1) + theMissingPart);*
* aCards.Add(card);*
* }*
* }*
* }*
* } *
* public class Card : System.Object {*
* private bool isFaceUp = false;*
* private bool isMatched = false;*
* public string img;*
* public Card(){*
* img = “robot”;*
* }*
* public Card(string str){*
* this.img = str;*
* }*
* }*
I apologize if there is some rule against posting code out of books. If so, I will remove the original author’s code. I’m sorry for posting so much but I’ve been banging my head on the desk for DAYS! I know there is probably some other way I could have done it but I know there should be a way to write the same thing in C# and I’ll need to get that knowledge under my belt one day. No one here knows C# - they’re too busy being profound in C++ and whatnot.
Thanks for any help you can give. I’d really appreciate it.