Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

URGENT: I need help with a unity project involving 2d arrays. You might already have an idea about what 2D arrays look like. Basically, were

URGENT: I need help with a unity project involving 2d arrays.

You might already have an idea about what 2D arrays look like. Basically, were extending the concept of a 1D array (which had a single index number per slot) into the next dimension, such that we have a grid of slots. Consequently, well have to reference a slot with two indices (x, y) instead of just one. I like to think of a 2D array as a chessboard, which is exactly what were going to create in this lab.

Getting Started: Scene Setup

To begin, download the starting point for the lab http://ksuweb.kennesaw.edu/~rgesick1/cse%201302c/Labs%20unity/lab03%20chess/Lab3Start.zip.

Open up the project (Assets->Lab3.unity) and run it. Youll see that the camera kites around the center of an empty chess board. What well do in this project is populate the chessboard with pieces, and well store that information in a 2D array.

The first thing well do is create a script and attach it to the empty called MiddleOfBoard. Call the script BoardManager. The next thing youll want to do is create a 2D array of chess pieces (which will be integers - well talk about that in a minute). To do that, create a member variable and then allocate memory to hold it. Remember, its a 2-step thing, so we allocate memory in the Start() method (which is essentially the constructor of the class).

public class BoardManager : MonoBehaviour {

private int[,] board;

// Use this for initialization

void Start () {

board = new int[8,8];

}

// Update is called once per frame

void Update () {

}

}

So heres the idea. What well arbitrarily assign numbers to chess pieces. For example, well say that the Black King is 0, the Black Queen is 1 and so on. That way, if we look at a particular slot in the 2D array, if we see a 0, well know that the Black King is there. Lets also assume that if we see a -1 that no piece currently occupies that slot.

Before we do that, I always like to write a PrintBoard method to be able to see the state of the 2D array; Ive provided it below, so add it to your class. Similar to previous labs, well create a single string and then print it out using Debug.Log. The means to drop to a new line. Notice how the nested loop is structured as well as the notation of [x, y];

void PrintBoard() {

string s = "";

for (int y = 0; y < 8; y++) {

for (int x = 0; x < 8; x++) {

s += board[x,y]+"|";

}

s+=" ";

}

Debug.Log(s);

}

What you need to do:

Write a method called InitializeBoard() that fills each slot with -1.

Call this method from Start(). Print the board out to make sure youve done it correctly.

Populating the Array

Now that we have -1s in all the slots, the next thing well do is again non-visual. Well populate the internal 2D array with the initial layout of the board. Originally I had said that well use numbers to represent the pieces, and thats still true. HOWEVER, I dont want you to assign numbers directly into the array (e.g. board[2,3] = 4). Instead, well create a series of constants to make things easier to read as well as prevent bugs. One way that you could do this is something like this (dont do this): static const int BLACK_KING = 0; static const tint BLACK_QUEEN = 1;

and so on. However, theres a better way. Thats why the creators of languages came up with enumerations or enums for short. It does exactly what the code above does, but has a slight different syntax. Include the code below as a member variable to your class:

enum Pieces {

BLACK_KING,

BLACK_QUEEN,

BLACK_BISHOP,

BLACK_KNIGHT,

BLACK_ROOK,

BLACK_PAWN,

WHITE_KING,

WHITE_QUEEN,

WHITE_BISHOP,

WHITE_KNIGHT,

WHITE_ROOK,

WHITE_PAWN

};

The nice thing now is that we dont have to remember what number the WHITE_ROOK is, we can just use it! For example, I can now populate an entire row of pawns like this:

for (int i = 0; i<8; i++) {

board[i,1] = (int)Pieces.WHITE_PAWN;

}

Notice that I had to access the enums using the Pieces variable name, and also had to type-cast that into an int.

What you need to do:

Write a method called PopulateBoard() that populates the board with an initial chess layout. If youre unsure what that is, look at the HYPERLINK "http://media-2.web.britannica.com/eb-media/71/7471-004-C94F7C98.jpg" initial configuration of a game of chess.

Call PopulateBoard() from Start() before you print it. Verify that you have the correct layout by running it and looking at the printout.

Populating the Scene

Its time to start putting pieces on the board, but before we do that, you need to understand a few things. First, were going to have to figure out where to put the piece. Next, well see how to create a piece using a Prefab instead of a public variable.

For the first concept, lets focus on the bottom row of the board. For simplicity, Ive shifted the entire board down such that (0,0,0) is the middle of the bottom-left square. Remember, the scene is 3D, so to position a piece, well be calculating the x position (how far over) and the z position (how deep into the scene). The question is, how far apart is each piece? I measured it, and its:

1.1142857 units

Lets walk through it. I know that the first piece I put down (the rook/castle) is at (0, 0, 0). Wheres the next piece? At (1.1142857, 0, 0). Where the next piece? At (2.2285714, 0, 0). The next? At (3.3428571, 0, 0). I know what youre thinking You have 32 pieces on the chessboard, so youll calculate the exact position of each piece.DO NOT do that! Instead, lets be smart about things and calculate it instead.

I know that the x position of each piece is a multiple of 1.1142857, so as I walk through the loop, I can multiply the pieces x index by 1.1142857. Fortunately, I can do the same thing with the y position, since we have a perfectly square board.

Now, the last thing we have to do is figure out how to Instantiate() a new model from the Prefabs. To help with this process, we need a method that takes in a number and returns the name of the Prefab it should generate. To save time, Ill give you this code:

string GetPrefabName(int enumNumber) {

switch (enumNumber) {

case (int)Pieces.BLACK_KING: return "bKing"; break;

case (int)Pieces.BLACK_QUEEN: return "bQueen"; break;

case (int)Pieces.BLACK_BISHOP: return "bBishop"; break;

case (int)Pieces.BLACK_KNIGHT: return "bKnight"; break;

case (int)Pieces.BLACK_ROOK: return "bRook"; break;

case (int)Pieces.BLACK_PAWN: return "bPawn"; break;

case (int)Pieces.WHITE_KING: return "wKing"; break;

case (int)Pieces.WHITE_QUEEN: return "wQueen"; break;

case (int)Pieces.WHITE_BISHOP: return "wBishop"; break;

case (int)Pieces.WHITE_KNIGHT: return "wKnight"; break;

case (int)Pieces.WHITE_ROOK: return "wRook"; break;

case (int)Pieces.WHITE_PAWN: return "wPawn"; break;

}

return "";

}

To actually create a chess piece, use Instantiate with the 3 parameters being Resources.Load( the name of the game object), its position and Quaternion.identity);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Repairing And Querying Databases Under Aggregate Constraints

Authors: Sergio Flesca ,Filippo Furfaro ,Francesco Parisi

2011th Edition

146141640X, 978-1461416401

Students also viewed these Databases questions

Question

Are robots going to displace all workers? Explain your answer.

Answered: 1 week ago