Search Unity

Capturing sides of a square

Discussion in 'Scripting' started by lemDace, Jan 27, 2013.

  1. lemDace

    lemDace

    Joined:
    Feb 27, 2012
    Posts:
    13
    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:

    Code (csharp):
    1.  
    2.  public int BoardWidth; //configurable in editor, board hieght will be the same so board is square
    3.     int BoardHeight;
    4.     int AnchorWidth;
    5.  
    6.  
    7.     public GameObject BoardPieceType;
    8.     public int BoardPieceSize;
    9.     public GameObject AnchorType;
    10.  
    11.     GameObject[,] GameBoard;
    12.     GameObject[,] AnchorBoard;
    13.  
    14. // Use this for initialization
    15.     void Start () {
    16.  
    17.         BoardHeight = BoardWidth; //makes a square board
    18.         AnchorWidth = BoardHeight + 1;
    19.        
    20.         Debug.Log("Starting board generation");
    21.  
    22.         CreateBoard();
    23.  
    24.     }
    25.  
    26.     void CreateAnchorPieces()
    27.     {
    28.         AnchorBoard = new GameObject[AnchorWidth, AnchorWidth];
    29.  
    30.         for (int x = 0; x < AnchorWidth; x++)
    31.         {
    32.             for (int y = 0; y < AnchorWidth; y++)
    33.             {
    34.                 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)));
    35.                 AnchorBoard[x, y].name = "Anchor" + x + y;
    36.             }
    37.         }
    38.     }
    39.    
    40.     void CreateBoardPieces()
    41.     {
    42.         GameBoard = new GameObject[BoardWidth, BoardHeight];
    43.  
    44.         for (int x = 0; x < BoardWidth; x++)
    45.         {
    46.             for (int y = 0; y < BoardHeight; y++)
    47.             {
    48.                 Debug.Log("x: " + x + " z: " + y + " type: " + BoardPieceType);
    49.                 GameBoard[x, y] = (GameObject)Instantiate(BoardPieceType, new Vector3((x * BoardPieceSize), (y * BoardPieceSize), 0), Quaternion.identity);
    50.                 GameBoard[x, y].name = "Piece" + x + y;
    51.                 GameBoard[x, y].GetComponent<BoardPiece>().bottomLeftAnchor = GameObject.Find("Anchor" + x + y);
    52.                 GameBoard[x, y].GetComponent<BoardPiece>().topLeftAnchor = GameObject.Find("Anchor" + x + (y + 1));
    53.                 GameBoard[x, y].GetComponent<BoardPiece>().bottomRightAnchor = GameObject.Find("Anchor" + (x + 1) + y);
    54.                 GameBoard[x, y].GetComponent<BoardPiece>().topRightAnchor = GameObject.Find("Anchor" + (x + 1) + (y + 1));
    55.             }
    56.         }
    57.     }
    58.  
    59.     void CreateBoard()
    60.     {
    61.         //now create the anchor points
    62.         CreateAnchorPieces();
    63.         //create the boardPieces
    64.         CreateBoardPieces();
    65.     }
    66.  
    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
     
  2. Timer

    Timer

    Joined:
    Jan 14, 2013
    Posts:
    35
    I also used to play this game when I was in school.

    I read first 4 lines of your problem and I am not sure what approach you are taking.

    I would create it in this way.

    1. lets say there are 7 X 7 squares that can be filled. I will create a plane or cube and make it a prefab and create 49 innstances.

    2. For line I will use simple non proportionally scaled cube with a trigger component and kinematic rigidbody attached to it. then on mouse click instantiate
    one of the line.

    3. inner cube will cast rays in all 4 direction to detect if it's surrounded by 4 line (count all the rays hitting to 4).

    4. that all, if all 4 rays are hitting then find the player with the last move.
     
  3. lemDace

    lemDace

    Joined:
    Feb 27, 2012
    Posts:
    13
    Thanks for the response, I didnt think of using raytraces to check if a blocks sided had been captured, I will give this a try in the morning and see what I come up with.

    Are raytraces costly if I do lots of them? the way I was thinking involved lots of getComponent calls to retrieve public variables of scripts attached to my game objects, but from reading, the getComponent calls can be costly on performance if used too much?
     
  4. Timer

    Timer

    Joined:
    Jan 14, 2013
    Posts:
    35
    I don't know how costly ray tracing is but I always tend to rely on this for simple collision . A slight modification thought, instead of casting a ray from all the prefabs all the time,Cast rays only from the surrounding block( if the line is vertical then left and right block and if it's horizontal than top and down block) just after any particular line is drawn and then check if all 4 side have been drawn, much simpler.
     
  5. jihen007

    jihen007

    Joined:
    Feb 22, 2017
    Posts:
    8
    hello !
    I 'm developping the same game as u told before and
    i have the same issue now so can you tell me how you did it ? i tried with raycast but it does nothing .