Chess-like project

Let me start by saying that I’m fairly new to Unity and programming in general. I’m currently teaching myself C# and I’ve been going through the tutorials, the forums and several other resources. This is my first post and I plan to get involved in the community. I’m very passionate about game design and though I’m just starting out, I’m dedicated to learning and making this a second home.

I have a game that began as a board game roughly a decade ago and I have been trying to convert it into a digital format.

The game has movement similar to chess regarding the grid based movement and restricted movement patterns. I plan to keep the mechanic of one hit kills and add mechanics such as ranged attacks and abilities that alter attack and movement patterns and styles.

I realize that this is a big task to tackle and I’ve got a ways to go, but I’ve decided to start small and see if I can figure out some of the basic mechanics. I’ve been at this for a little while and I understand the basics of programming. I’m familiar with variables, loops, operators, classes, functions. I understand how to move objects, destroy objects, fire projectiles, keep scores and a few other tidbits. I’m just a little fuzzy on the best approach to handling the board and movement. I’ve done some research, though, and this is what I’ve found:

The best way to handle grid based strategy is to build a grid using an array, and use raycasting to determine each piece’s location. Then I understand that there is an algorithm for determining legal moves.

You can use the projector to draw grid lines. There is also a way to illuminate a tile with the projector by projecting a texture, I believe it has something to do with ‘SetPixel’ or something similar.

Here are some of the problems I’m facing, the solutions I’ve considered and the questions I have:

Problems:
I respect the idea of drawing a grid but the majority of my playing fields are not going to be uniform. I won’t have a 10x10 or a 12x12 grid. It will be more of an odd shape made of tiles.

Also, if I were to go with the approach of creating an array of tiles, I vaguely understand how you can assign a unit to a location in the array but I’m totally lost as to the algorithm for determining where the piece can move to.

Approaches:
I’ve thought of just making the tiles individually and using colliders to determine whether or not the space is occupied or if it can be moved to. You would click on the unit and it would send out raycasts in the directions the unit can move and the colliders would return whether or not a space exists, if it is occupied and then give you movement options. The tiles would detect whether or not they’d been hit by a Raycast and if they weren’t occupied, they would become clickable and the unit would move to their transform. If an enemy piece were on the space, when the player’s unit hit the enemy piece, it would destroy it.

I really like the idea of using raycasts but 1. I can’t help but wonder if there is something I’m not understanding about the array approach that might solve my problem and 2. I have certain movements that skip spaces or make turns(similar to a knight in chess) and I’m not sure how I’d fire a raycast from a new location or make the original turn.

I’ve also considered instantiating space checker objects at the appropriate locations in relation to the unit and if the space checker finds a space that is able to be moved to, it would become clickable and so on. With this approach, though, I’m worried that the massive amount of game objects might slow down the game or become hard to sort through and organize.

I’ve looked up videos on grid based movement and turn based strategy but the majority of games rely on a range of spaces as opposed to a specific movement like chess.

Questions:
When drawing an array, could I make it so certain tiles weren’t drawn and couldn’t be moved to, if so how?

Once a ray is cast to determine whether or not there is an available space to be moved to, is there a way to flip a bool on the space that’s been hit by the raycast?

I could make the spaces black and white as in chess in regards to the units that skip spaces, but how would I go about making a piece move like a knight in chess?

Lastly, am I going about this the right way or is there something I’m missing?

I realize this post is pretty lengthy and I do apologize.
I don’t expect to be handed code and I certainly don’t want the work done for me. I’m looking to learn as I want this to be a growing experience. I realize I’ve got a lot of challenges ahead of me but I’m in this for the long haul.
If someone could give me some advice, refer me to some tutorials I may have missed or just point me in the right direction, I would be most appreciative.
Also, if I’ve formatted this post incorrectly, I’m open to criticism on how I can better request assistance in the future. I appreciate anyone who has taken the time to read this.

Thank you.

1 Like

“Once a ray is cast to determine whether or not there is an available space to be moved to, is there a way to flip a bool on the space that’s been hit by the raycast?”

A. using raycast hit will determine what the ray hit. you can then getComponent<> from the colliders gamobject containing a bool which you can then toggle on or off.

“I could make the spaces black and white as in chess in regards to the units that skip spaces, but how would I go about making a piece move like a knight in chess?”

A. you could create a class of movement direcitons for each movement type. eg moving like a knight would have forward,forward, right or forward, right,right etc.

your first question is a sounds difficult. I did something similar when creating a random grass generator. i made a grid array of vecotr3’s above the terrain that fired a ray down towards the terrain. if the ray hit a collider in a layer marked as a no go zone then it doesnt create the grass or in your case the grid cube.

Thank you. That definitely gives me some more insight. Learning a little more every day.

I probably won’t use an array for this project.

I also thought about instantiating colliders around the unit where it could move. If the colliders hit a wall or obstacle, they would be destroyed. If they hit an allied piece, they’d be destroyed. Then just make the space checkers clickable and move the selected piece to that transform.

Does that sound feasible?

yes thats sounds like it could work. maybe set the colliders as triggers and use OnTriggerEnter(). but the objects in the scene will need to have rigid bodies to detect the collisions as it requires physics.

That makes sense. I’ll have to keep that in mind.
I really appreciate your help.

The simplest way to do this is to maintain several two dimensional arrays that contain various data points.

  • A GameObject[ ][ ] for the tile pieces (could also be a MonoBehaviour[ ][ ] or some other custom script array if you need data about each tile)
  • A bool[ ][ ] for tiles occupied by enemies
  • A bool[ ][ ] for tiles occupied by allies

Then each game piece would maintain the X and Y “coordinate” representing what tile they are on currently along with their movement rules. This makes it easier to do more complex movement rules like not being able to move through allied tiles and you’re not relying on non-deterministic physical interactions to represent deterministic hardcoded rules.

That being said - I’d still probably put colliders on the tiles just to make the mouse click interaction as straightforward as possible.

3 Likes

Hm. That makes a lot of sense as well.
I understand the underlying concept of arrays, I’ll just have to do some more research to solidify my understanding.
It would be convenient if they were saved to coordinates.

I can see how the idea of instantiating Space Checkers could quickly make the board less uniform and therefore make it harder to keep track of where everything is at. It would make it almost impossible to develop any kind of AI either.

As I go, I’m seeing that there a a thousand ways to solve any one problem, but the overall goal is to make your system as universal, modular and streamlined as possible.

So how would I go about storing each unit’s movement parameters?
Do you know of something I can reference?

I actually think I may have found something to get me started.

If you have any other input, though, I’m all ears. :slight_smile:

Thank you so very much, by the way! I’ve made more progress in the last 48 hours than I have in the last couple weeks.

To start the information you need to answer the question; Can this piece move from A tile to B tile? are

  • Does B tile actually exist on the board?
  • Is another piece already on B tile?

I’d actually come at it from a different direction and evaluate every tile a piece is allowed to move to. This way I don’t have to keep checking the rules every time the player clicks a tile to move to and I can do fancy effects on the available tiles somewhere down the line.

Taking the most basic example first - a piece that can move 1 tile in any direction.

int numRows, numCols; // size of our grid

int X, Y; // assume we set these based on the piece's current tile

List<GameObject> availableTiles; // list of tiles we're allowed to move to

// check if moving forward is allowed (not on upper edge of grid)
if (Y + 1 < numRows)
{
    availableTiles.Add(tiles[X][Y + 1]);
    // check the two forward diagonal directions
    if (X + 1 < numCols) availableTiles.Add([X + 1][Y + 1]);
    if (X - 1 >= 0) availableTiles.Add([X - 1][Y + 1]);
}

// check if moving backward is allowed (not on lower edge of grid)
if (Y - 1 >= 0)
{
    availableTiles.Add(tiles[X][Y - 1]);
    // check two backward diagonal directions
    if (X + 1 < numCols) availableTiles.Add([X + 1][Y - 1]);
    if (X - 1 >= 0) availableTiles.Add([X - 1][Y - 1]);
}

// check if moving left is allowed
if (X - 1 >= 0)
    availableTiles.Add(tiles[X - 1][Y]);

// check if moving right is allowed
if (X + 1 < numCols)
    availableTiles.Add(tiles[X + 1][Y]);

The above doesn’t check tile occupation but that would be straight forward checking of the boolean arrays at the given indices.

Then when you click on a tile to move check that the move is legal

if (availableTiles.Contains(clickedTile))
{
    // remove boolean indicator from current tile
    alliedTiles[piece.X][piece.Y] = false;
    piece.transform.position = clickedTile.transform.position;
    piece.X = clickedTile.X;
    piece.Y = clickedTile.Y;
    // update boolean for new tile to indicate it is occupied by an ally
    alliedTiles[piece.X][piece.Y] = true;
}

If you encapsulated that logic into a method called

List<GameObject> MoveAnyDirection(int numSpaces)

and replaced 1 with numSpaces you could evaluate movement for all pieces that could move any number of spaces in any direction.

2 Likes

Ah! It’s like a light bulb went off in my head. This is so much less intimidating than it was when I first started.

I agree more with this approach. Makes everything more customizable and modular, which is exactly what I want.

And then I can just instantiate colliders at the availableTile coordinates for the clickable bits.

You’re a godsend, sir.
I appreciate you investing in a beginner.

Designs like this are great for MVC thinking. Use Unity to do the view and controller parts. (That means rendering and input.) But build a separate system for the model (game logic).

An array is a great starting place for your model. I would make it a 2D array, that way you can treat it exactly like coordinates. I would put all of the data for each tile inside a custom data class for the array.

Movement is thus pretty simple. A knight simply checks if the space 2 up and 1 left is valid, and so forth. Stepping around the array is simple a case of adding or subtracting one.

Deal with non existent tiles by setting the array locations to null, or some other value that represents non existence.

I would also strongly suggest wrapping the array to hide the implementation details from the rest of your script. That let’s you mess with logic without breaking other classes.

As a teaser, once you get good at scripting you can even use an image to store the array data. Letting you build a really easy level editor. Don’t worry about this now, but it is possible.

1 Like

Yes, I’m very excited for that!

I’ve done some digging in the asset store as well for tools that might help me as well.
I stumbled across this.

From what I can see, this would complete the majority of the work for me.
Granted I would definitely study the inter-workings to ensure I had a clear understanding.
Am I right in assuming so?

Assets are great for including prebuilt solutions in your project. They aren’t a substitute for knowing how to code. But they can save you weeks or months of development time.

1 Like

I’m working on a tile based system myself, Here’s how I did it-

1.) a “Tile” class for keeping any info on the actual spaces.
2.) an “Actor” class for keeping info on the moving pieces. A member variable of Actor could keep track of the current space it occupies.
2a.) you could implement an Interface to distinguish the rules for how different actors can move.
3.) a “Board” class for keeping info on the entire map. It could handle map generation, as well as keeping a 2D Array of “Tiles.” Any element without a tile would simply be null. You would reference specific tiles by their [X,Y] coordinates in the array.
4.) a “Manager” class would keep track of turns, events, and reference the board instance whenever it needs to check a tile.
5.) an “Input” class would be responsible for grabbing the user input from controls and sending it to the Manager upon request.

2 Likes

That’s good information to know! I’ll do some studying on Interfaces as it seems that would be a great asset to me moving forward.
I’m a little fuzzy on building the grid and I’m not entirely sure on how to click on a tile and then save it’s coordinates, but I know that the idea is when you click on a tile it would check to make sure a piece is there, and then save the coordinates and the piece on a variable to apply all of the movement rules to.
All of this information seems to be fairly accessible, though.
I feel confident moving forward. :slight_smile:

Again, I can’t say it enough, all of you have been incredibly helpful.

2 Likes