See, I have a board game with a 2D array acting as the board. There are number in the 2D array, and should a number be placed (by the player) in a spot in the 2D array where a number already resides, the following can happen:

> If the residing number is higher, the invading number is 'killed'.
> If the residing number is lower, it is killed and the invading number takes its place.
> If they are both of the same value, both are killed.

But I can see problems with this approach (players, for example). After doing some research, I found out that other programmers use classes for the pieces.

I'm no expert in classes. I've only used them to store and get values, as well as to perform minor changes to stored data.

How do I go about using a class as a piece? And how do I set said class / piece on the 2D array?

From what I know, it would seem as if I would make 14 classes (1 for each unique piece) and each class would have 2 values - the rank, and the alignment (player 1 or 2). And then I am to, somehow, put this in a 2D array?

Sorry for such a noobish question. Thanks in advanced for those who answered.

I think you have a misunderstanding as to what classes represent.

A class represents a "something" in your case a piece. But, think about how unique each piece is. Do they share a common set of properties? What makes piece A different from piece B.

In most cases you can create a single class file and use that single class to represent every different piece.

So if you have a red piece and orange piece and a green piece. Simply make a single piece class that has a colour property. Then set your colour to whichever one this piece represents. You can do this via number codes or enums (preferred)
Also, you can use inheritence. If the pieces all share common properties, but there are unique properties required for each piece, then you can "inherit" the basic class and create a unique one from that.

I do apologise, I spent a good hour writing a massive post, only for my connection to drop when I hit post, meaning that the text was lost. I don't have time to write it all out again, but hopefully you will find my short summary helpful anyway.

"Do they share a common set of properties?"

Some, but not all. There are 15 'ranks'. Rank 2 repeats six times, and Rank 15 repeats twice, for a total of 21 pieces. Per player.

What I had in mind was 15 classes (LOL) with a defined rank number. I'm not sure if this is the way to go, however.

If I could have just one class for all of them, I don't know where I will set the ranks and the piece alignment.

I'm really sorry for my lack of knowledge on this matter. The most I've ever used out of a class are the get / set functions, mostly to pass one data from the 1st to the 4th form, or something.

Anyway, thanks for the reply.

You don't need 15 different classes.

public class Piece
{
    public Int32 Rank;
    public Point PiecePosition;
}

I don't know if you use a point, but I imagine you have an X, Y grid. So you can set the X and Y of the Point class to the correct grid position.

EDIT: What I mean by a common set of properties is, as you can see above, all the pieces have a rank and a pieceposition. It doesn't matter that some will have different numbers, because you will have more than one "instance". I think this may be where you're getting confused.

Using the class above...

static void Main()
{
    Piece[] myPieceArray = new Piece[15];
    myPieceArray[0].Rank = 1;
    myPieceArray[1].Rank = 2;
    ...
    Console.WriteLine(myPieceArray[0].Rank);
    Console.WriteLine(myPieceArray[1].Rank);
    Console.ReadLine();
}

Ok so what you can see there is that I have a single class (see top of post) but I can make 15 *instances* of that class, which I have inside an array (an array is a collection of the same object, in this example, my array contains 15 Piece objects, all of them are different)

commented: Despite my less than clear question, you got it spot on. +2

Ah! Thanks for that one!

I suppose I could just do something like this:

Public class Piece
{
public int32 rank;
public string player;
}

This may be the solution to my problem. Thanks a lot.

Actually you should do it the other way around...

public class Player
{
    Piece[] PlayerPieces = new Piece[15];
    String PlayerName;
    /* other player related stuff */
}

As the player is an entity in your program and one that requires you to store information about it, you should create a class for it. Like you have seen in my previous posts, I often refer to classes as objects. That's what they are.

A player interacts with your world, albeit in a metaphysical sense, but there is a lot of data you need to store about that player. Their name, their score, what pieces they have etc. A player is an object in your "world".

So is a piece, so is your board. Try and identify every "thing" in your game world. Think about what this "thing" represents and whether it is the same or similar to anything else. This "thing" is an object in your world. Much like you have a fork, or a collection of forks, a television and a car. They are all distinguishable objects of your world.

All of these things should be classes and collectively, these objects represent your world.

EDIT: I really recommended reading as much as you think is sensible on the principles of Object Oriented Programming. It will help you a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.