Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The first thing we'll do is create a script and attach it to the empty called MiddleOfBoard. Call the script BoardManager. The next thing you'll

The first thing we'll do is create a script and attach it to the empty called MiddleOfBoard. Call the script "BoardManager". The next thing you'll want to do is create a 2D array of chess pieces (which will be integers - we'll talk about that in a minute). To do that, create a member variable and then allocate memory to hold it. Remember, it's 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 here's the idea. What we'll arbitrarily assign numbers to chess pieces. For example, we'll 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, we'll know that the Black King is there. Let's 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; I've provided it below, so add it to your class. Similar to previous labs, we'll 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 you've done it correctly.

Populating the Array

Now that we have -1s in all the slots, the next thing we'll do is again non-visual. We'll populate the internal 2D array with the initial layout of the board. Originally I had said that we'll use numbers to represent the pieces, and that's still true. HOWEVER, I don't want you to assign numbers directly into the array (e.g. board[2,3] = 4). Instead, we'll 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 (don't do this): static const int BLACK_KING = 0; static const tint BLACK_QUEEN = 1;

... and so on. However, there's a better way. That's why the creators of languages came up withenumerations - 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 don't 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 you're unsure what that is, look at the 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

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

For the first concept, let's focus on the bottom row of the board. For simplicity, I've 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, we'll 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 it's:

1.1142857 units

Let's walk through it. I know that the first piece I put down (the rook/castle) is at (0, 0, 0). Where's 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 you're thinking... You have 32 pieces on the chessboard, so you'll calculate the exact position of each piece. DO NOT do that! Instead, let's 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 piece's 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, I'll 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 with AI-Powered 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

Students also viewed these Databases questions