Board Game Development Help
Sorry for the long post, I tried to keep it tight, while still including all pertinent information.
I’m working on a sort of board game and I’m planning out how I’m going to handle everything behind the scenes. I’m a pretty experienced programmer but this is one of my first real Unity projects, so I figured I’d come here to see if anyone more experienced with the engine could add any insight to what I have planned.
The board game doesn’t actually exist but, for our purposes, let’s call it chess. It’s a game with different kinds of pieces on a grid-like playing field. Right now I’m working out how to handle a player select their piece and then select the place for it to move to. Here are the issues I’m tackling:
The Board (!)
I plan to do much more with the board once I have everything working but right now all I need is a standard 10x10 board. Right now, it’s built out of a prefab I made called BoardSquare. I then put them in a 10x10 arrangement and created a prefab out of that called GameBoard.
The beginning of the game allows the custom placement of pieces so the board will start empty, but each BoardSquare has a var which keeps track of which GamePiece (another prefab representing a player’s piece) is occupying it.
If someone has a better suggestion for this, they’re welcome to chime in but the main thing hanging over me right now is the relation between the different BoardSquares. Right now, I have an easy way to get at the BoardSquare’s GamePiece and vice versa, because they store one another as variables. What I need is an easy way to get at surrounding BoardSquares for things like checking where a piece can move.
I have an idea for this but I’m not confident in its efficiency. For the BoardSquare prefabs, I’d like to store variables for ‘up’, ‘right’, ‘down’, and ‘left’ BoardSquares, so that if I have my hands on a BoardSquare, I can easily ‘traverse’ the board by simply accessing its variables.
I had a similar idea where I could simply store references to the BoardSquares in a GameBoard array(100), so that if I wanted to get at (3, 4), I could just access GameBoard.boardArray((3*10) + 4 - 1).
Finally, I considered naming the BoardSquares based on their board coordinates, so a name would be ‘BoardSquare_3-4’ and then I could just locate them using ‘.find’. I am convinced this is not the best way, haha.
So those are my options at the moment. The first idea seems kind of intense but it would be very convenient, and shouldn’t cost an excessive amount beyond the initial storage of the references. The second idea involves storing fewer references but each time I want information, I need to get at the BoardSquare’s parent GameBoard then access a potentially huge array.
Player Selection (!)
NOTE: This game will initially be constructed for use with a controller, so the player will be navigating with arrows and/or a control stick.
For player selection, I plan for the GameBoard to keep track of whose turn it is, as well as the BoardSquare that has already been selected, if any, and which square is highlighted for selection. If no square is selected already, any valid square is fair game (a square occupied by a movable piece, some pieces can’t move). I’ll start the highlighted square somewhere and then if they try to ‘move’ the selection left, I’ll check the squares to the left for another valid square. If it finds one, I switch the highlighted square to that one, etc. (everything will be handled at the square level because they will probably be linked in some way and they always exist, even if they are not occupied by any piece)
If the player selects a highlighted square, the GameBoard stores that square as ‘squareSelected’ and then turns on a variable called ‘possibleMove’ or something like that (I’ll have to work out the difference in appearance and terminology between 'highlighted, ‘selected’, and ‘possible move’) in each square that the occupying piece could move to. Now if the player tries to ‘move’ their selection up, I’ll check if the square up has ‘possibleMove’ as true. If it does, I can change that to the highlighted square. If they select anything, the GameBoard will see that there is already a selection and it will therefore compute the movement of ‘selection’ to ‘highlighted’. Once the original selection is cancelled or a final move is made, I can reverse the highlighting of possible moves on the ‘selection’ so that they appear as they did before in preparation for the next turn.
One problem here I can see is trying to select possible moves to the right of your selected piece but you have an upwards square highlighted. The algorithm I describe would not be able to get to it. I could either change the logic or allow all squares to be highlighted still and just not allow them to be selected as a move. I could also allow the piece selected to be highlighted in addition to possible moves, providing a center point to pass through. It might be best to just always allow the player to highlight every square of the board and simply not allow invalid selections.
EDIT: I see now this same error could exist in my first paragraph of this section. If a player has only two pieces left, say, and they want to select the other one, they wouldn’t be able to if it’s on a diagonal from the currently selected one. Also, I should store a collection of each player’s pieces somewhere in the GameBoard for arbitrary initial selection if needed.
EDIT 2: I suppose I could also store possible moves in the same way I would store the player’s pieces. Then I could create some sort of algorithm so that if the player tries to select to the right and there is nothing there, it can find a best match to the right if any exists in the set.
Movement of the Pieces
This is not as big of a conundrum to me and I will only mention it briefly in case someone has some tips on it. For piece movement, when the player confirms a move, I plan on switching the relationships of the initial BoardSquare, the BoardSquare being moved to, and the GamePiece being moved to reflect the new position. This much is clear.
For the movement, I am simply inexperienced with games. I plan on tracking, within the GamePiece, something like ‘startTime’, and ‘destination’ and using those on each Update() to move the piece closer and closer to the destination until the destination and its location are the same. I’m sure I can work this out but if anyone wanted to throw me any advice on this now, I’d appreciate it.
The Multiplayer Aspect
I haven’t worked much with multiplayer, so once again just fishing for experience before I go exploring on my own. I obviously don’t want the opponent to see the moving player’s selection materials and such. Is there a way to change a material on an object only for one player. Once again, just figured I’d throw this out there, as well, before I start really looking into it on my own.
Also, I’m pretty sure I should try to do this peer-to-peer so I can having to host servers and since it will be a turn-based game. Is that going to be viable with Unity? (just having one player hosting and the other listening to them)
Anyways, thanks so much for taking the time to read! I’d appreciate any help you guys can lend me, especially if anyone’s done anything like this before. I’m just trying to make sure I won’t need to come back and completely overhaul the whole thing later. Cheers!
- Kyle