Morning all,
Okay im trying to write a small game based on a pen and paper game i used to play at school. I have a grid of points spaced evenly on the playing board, and each player takes turns to draw a horizontal or vertical line in between any 2 points, as 4 sides of any square are captured then that square belongs to the player who captured the last side and closed the square, player with the most squares at the end of the game wins. Simple…
I have 2 prefabs, a boardPiece which is the tiles the board is made up from and is to be captured by the player and an Anchor, which is the points that the grid is made up from, and there is one at every corner of each boardPiece.
In my main script i build a grid of Anchors, then a grid of boardPieces, and for each boardPiece i work out which anchors are on its corners. Like so:
public int BoardWidth; //configurable in editor, board hieght will be the same so board is square
int BoardHeight;
int AnchorWidth;
public GameObject BoardPieceType;
public int BoardPieceSize;
public GameObject AnchorType;
GameObject[,] GameBoard;
GameObject[,] AnchorBoard;
// Use this for initialization
void Start () {
BoardHeight = BoardWidth; //makes a square board
AnchorWidth = BoardHeight + 1;
Debug.Log("Starting board generation");
CreateBoard();
}
void CreateAnchorPieces()
{
AnchorBoard = new GameObject[AnchorWidth, AnchorWidth];
for (int x = 0; x < AnchorWidth; x++)
{
for (int y = 0; y < AnchorWidth; y++)
{
AnchorBoard[x, y] = (GameObject)Instantiate(AnchorType, new Vector3((x * BoardPieceSize) - (BoardPieceSize / 2), (y * BoardPieceSize) - (BoardPieceSize / 2), 0), Quaternion.AngleAxis(-90, new Vector3(1, 0, 0)));
AnchorBoard[x, y].name = "Anchor" + x + y;
}
}
}
void CreateBoardPieces()
{
GameBoard = new GameObject[BoardWidth, BoardHeight];
for (int x = 0; x < BoardWidth; x++)
{
for (int y = 0; y < BoardHeight; y++)
{
Debug.Log("x: " + x + " z: " + y + " type: " + BoardPieceType);
GameBoard[x, y] = (GameObject)Instantiate(BoardPieceType, new Vector3((x * BoardPieceSize), (y * BoardPieceSize), 0), Quaternion.identity);
GameBoard[x, y].name = "Piece" + x + y;
GameBoard[x, y].GetComponent<BoardPiece>().bottomLeftAnchor = GameObject.Find("Anchor" + x + y);
GameBoard[x, y].GetComponent<BoardPiece>().topLeftAnchor = GameObject.Find("Anchor" + x + (y + 1));
GameBoard[x, y].GetComponent<BoardPiece>().bottomRightAnchor = GameObject.Find("Anchor" + (x + 1) + y);
GameBoard[x, y].GetComponent<BoardPiece>().topRightAnchor = GameObject.Find("Anchor" + (x + 1) + (y + 1));
}
}
}
void CreateBoard()
{
//now create the anchor points
CreateAnchorPieces();
//create the boardPieces
CreateBoardPieces();
}
So now i have my board created, and each boardPiece has a list of 4 Anchors that represent each corner or the piece.
The part i am now stuck at is working out a way to capture 1 whole side when aline is drawn between 2 anchors.
My thoughts so far are to give the BoardPiece 4 more properties, north, south, east, west. When a player draws a line between 2 anchors i would have to loop through each boardpiece and any that have those 2 anchors attached to them i could somehow work out which side they would capture and then mark that side (eg. north) as captured. Once a side is marked as captured i check the boardPiece to see if all the sides are now captured and if they are then give that player the boardPiece.
Having not written a whole game before and really wanting to take this one all the way to completion for once, i feel like i am making this way too complicated than it needs to be?
Sorry for the long winded post, Any help or advice would be appreciated.
Cheers