Exemplo n.º 1
0
        /// <param name="width">Board width</param>
        /// <param name="height">Board height</param>
        /// <param name="side">Hexagon side length</param>
        /// <param name="xOffset">X coordinate offset</param>
        /// <param name="yOffset">Y coordinate offset</param>
        public Board(int width, int height, int side, int xOffset, int yOffset, BoardState boardState, List <Player> players, HexagonalTest.DTOClass dataTransfer)
        {
            this.width          = width;
            this.height         = height;
            this.nonWaterFields = width * height;
            this.side           = side;
            this.transferObject = dataTransfer;

            this.xOffset    = xOffset;
            this.yOffset    = yOffset;
            this.boardState = boardState;
            this.players    = players;
            hexes           = new Hex[height, width];  //opposite of what we'd expect

            textPosX = new int[height, width];         // MaHa
            textPosY = new int[height, width];         // MaHa

            float h = Hexagonal.Math.CalculateH(side); // short side
            float r = Hexagonal.Math.CalculateR(side); // long side

            //Initalize, fill and shuffle field helper to give each player the same amount of fields on the start
            this.fieldHelper = new List <int>(width * height);
            int f = 0;

            while (f < fieldHelper.Capacity)
            {
                for (int p = players.Count - 1; p >= 0 && f < fieldHelper.Capacity; p--)
                {
                    fieldHelper.Add(p);
                    f++;
                }
            }
            Math.Shuffle <int>(fieldHelper);

            //Preload some dice results
            RandomGenerator.getInstance().initialize();

            //
            // Calculate pixel info..remove?
            // because of staggering, need to add an extra r/h
            float hexWidth  = 0;
            float hexHeight = 0;

            hexWidth         = side + h;
            hexHeight        = r + r;
            this.pixelWidth  = (width * hexWidth) + h;
            this.pixelHeight = (height * hexHeight) + r;

            bool inTopRow      = false;
            bool inBottomRow   = false;
            bool inLeftColumn  = false;
            bool inRightColumn = false;
            bool isTopLeft     = false;
            bool isTopRight    = false;
            bool isBotomLeft   = false;
            bool isBottomRight = false;

            // f = field number in 2D plane
            f = 0;
            // i = y coordinate (rows), j = x coordinate (columns) of the hex tiles 2D plane
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    Player player = getPlayerByID(fieldHelper[f]);

                    bool isBorderWater = i == 0 || j == 0 || i == height - 1 || j == width - 1;
                    isBorderWater &= RandomGenerator.getInstance().rollTheDice(1) < 3;

                    bool isInnerWater = i > 1 && j > 1 && i < height - 2 && j < width - 2;
                    isInnerWater &= RandomGenerator.getInstance().rollTheDice(1) == 1;

                    bool isWater = isBorderWater || isInnerWater;
                    Hex  current = new Hex(side, isWater ? Hex.WaterColor : player.Color, j, i, isWater ? 0 : 1, isWater);

                    if (isWater)
                    {
                        nonWaterFields--;
                    }
                    else
                    {
                        player.addField();
                    }

                    f++;
                    // Set position booleans

                    #region Position Booleans

                    if (i == 0)
                    {
                        inTopRow = true;
                    }
                    else
                    {
                        inTopRow = false;
                    }

                    if (i == height - 1)
                    {
                        inBottomRow = true;
                    }
                    else
                    {
                        inBottomRow = false;
                    }

                    if (j == 0)
                    {
                        inLeftColumn = true;
                    }
                    else
                    {
                        inLeftColumn = false;
                    }

                    if (j == width - 1)
                    {
                        inRightColumn = true;
                    }
                    else
                    {
                        inRightColumn = false;
                    }

                    if (inTopRow && inLeftColumn)
                    {
                        isTopLeft = true;
                    }
                    else
                    {
                        isTopLeft = false;
                    }

                    if (inTopRow && inRightColumn)
                    {
                        isTopRight = true;
                    }
                    else
                    {
                        isTopRight = false;
                    }

                    if (inBottomRow && inLeftColumn)
                    {
                        isBotomLeft = true;
                    }
                    else
                    {
                        isBotomLeft = false;
                    }

                    if (inBottomRow && inRightColumn)
                    {
                        isBottomRight = true;
                    }
                    else
                    {
                        isBottomRight = false;
                    }

                    #endregion

                    //
                    // Calculate Hex positions
                    //
                    if (isTopLeft)
                    {
                        //First hex
                        current.Initialize(0 + h + xOffset, 0 + yOffset);
                        hexes[0, 0] = current;
                    }
                    else
                    {
                        if (inLeftColumn)
                        {
                            // Calculate from hex above
                            current.Initialize(hexes[i - 1, j].Points[(int)Hexagonal.FlatVertice.BottomLeft]);
                            hexes[i, j] = current;
                        }
                        else
                        {
                            // Calculate from Hex to the left and need to stagger the columns
                            if (j % 2 == 0)
                            {
                                // Calculate from Hex to left's Upper Right Vertice plus h and R offset
                                float x = hexes[i, j - 1].Points[(int)Hexagonal.FlatVertice.UpperRight].X;
                                float y = hexes[i, j - 1].Points[(int)Hexagonal.FlatVertice.UpperRight].Y;
                                x += h;
                                y -= r;
                                current.Initialize(x, y);
                                hexes[i, j] = current;
                            }
                            else
                            {
                                // Calculate from Hex to left's Middle Right Vertice
                                current.Initialize(hexes[i, j - 1].Points[(int)Hexagonal.FlatVertice.MiddleRight]);
                                hexes[i, j] = current;
                            }
                        }
                    }
                }
            }

            this._playersByColor = players.ToDictionary(x => x.Color);

            //Give players extra dices at the start.
            //Each player gets total number of fields minus his largest patch plus MAX_DICE
            foreach (Player player in players)
            {
                int largestPatch = FindLargesPatchForPlayer(player);
                this.distributeDices(player, (int)((nonWaterFields) * 0.2 - largestPatch + MAX_DICE));
                player.Reward = largestPatch;
            }
        }
Exemplo n.º 2
0
 public BoardBuilder withBoardState(Hexagonal.BoardState boardState)
 {
     this.boardState = boardState;
     return(this);
 }
Exemplo n.º 3
0
        private void Initialize(int width, int height, int side, Hexagonal.HexOrientation orientation, int xOffset, int yOffset, Color boardColor)
        {
            this.width       = width;
            this.height      = height;
            this.xOffset     = xOffset;
            this.yOffset     = yOffset;
            this.side        = side;
            this.orientation = orientation;
            hexes            = new Hex[height, width];  //opposite of what we'd expect
            this.boardState  = new BoardState();
            this.boardColor  = boardColor;

            float h = Hexagonal.HexMath.CalculateH(side);             // short side
            float r = Hexagonal.HexMath.CalculateR(side);             // long side

            //
            // Calculate pixel info..remove?
            // because of staggering, need to add an extra r/h
            float hexWidth  = 0;
            float hexHeight = 0;

            switch (orientation)
            {
            case HexOrientation.Flat:
                hexWidth         = side + h;
                hexHeight        = r + r;
                this.pixelWidth  = (width * hexWidth) + h;
                this.pixelHeight = (height * hexHeight) + r;
                break;

            case HexOrientation.Pointy:
                hexWidth         = r + r;
                hexHeight        = side + h;
                this.pixelWidth  = (width * hexWidth) + r;
                this.pixelHeight = (height * hexHeight) + h;
                break;

            default:
                break;
            }


            bool inTopRow      = false;
            bool inBottomRow   = false;
            bool inLeftColumn  = false;
            bool inRightColumn = false;
            bool isTopLeft     = false;
            bool isTopRight    = false;
            bool isBotomLeft   = false;
            bool isBottomRight = false;


            // i = y coordinate (rows), j = x coordinate (columns) of the hex tiles 2D plane
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    // Set position booleans

                    if (i == 0)
                    {
                        inTopRow = true;
                    }
                    else
                    {
                        inTopRow = false;
                    }

                    if (i == height - 1)
                    {
                        inBottomRow = true;
                    }
                    else
                    {
                        inBottomRow = false;
                    }

                    if (j == 0)
                    {
                        inLeftColumn = true;
                    }
                    else
                    {
                        inLeftColumn = false;
                    }

                    if (j == width - 1)
                    {
                        inRightColumn = true;
                    }
                    else
                    {
                        inRightColumn = false;
                    }

                    if (inTopRow && inLeftColumn)
                    {
                        isTopLeft = true;
                    }
                    else
                    {
                        isTopLeft = false;
                    }

                    if (inTopRow && inRightColumn)
                    {
                        isTopRight = true;
                    }
                    else
                    {
                        isTopRight = false;
                    }

                    if (inBottomRow && inLeftColumn)
                    {
                        isBotomLeft = true;
                    }
                    else
                    {
                        isBotomLeft = false;
                    }

                    if (inBottomRow && inRightColumn)
                    {
                        isBottomRight = true;
                    }
                    else
                    {
                        isBottomRight = false;
                    }

                    //
                    // Calculate Hex positions
                    //
                    if (isTopLeft)
                    {
                        //First hex
                        switch (orientation)
                        {
                        case HexOrientation.Flat:
                            hexes[0, 0] = new Hex(0 + h + xOffset, 0 + yOffset, 0, 0, side, orientation, boardColor);
                            break;

                        case HexOrientation.Pointy:
                            hexes[0, 0] = new Hex(0 + r + xOffset, 0 + yOffset, 0, 0, side, orientation, boardColor);
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    {
                        switch (orientation)
                        {
                        case HexOrientation.Flat:
                            if (inLeftColumn)
                            {
                                // Calculate from hex above
                                hexes[i, j] = new Hex(hexes[i - 1, j].Points[(int)Hexagonal.FlatVertice.BottomLeft], i, j, side, orientation, boardColor);
                            }
                            else
                            {
                                // Calculate from Hex to the left and need to stagger the columns
                                if (j % 2 == 0)
                                {
                                    // Calculate from Hex to left's Upper Right Vertice plus h and R offset
                                    float x = hexes[i, j - 1].Points[(int)Hexagonal.FlatVertice.UpperRight].X;
                                    float y = hexes[i, j - 1].Points[(int)Hexagonal.FlatVertice.UpperRight].Y;
                                    x          += h;
                                    y          -= r;
                                    hexes[i, j] = new Hex(x, y, i, j, side, orientation, boardColor);
                                }
                                else
                                {
                                    // Calculate from Hex to left's Middle Right Vertice
                                    hexes[i, j] = new Hex(hexes[i, j - 1].Points[(int)Hexagonal.FlatVertice.MiddleRight], i, j, side, orientation, boardColor);
                                }
                            }
                            break;

                        case HexOrientation.Pointy:
                            if (inLeftColumn)
                            {
                                // Calculate from hex above and need to stagger the rows
                                if (i % 2 == 0)
                                {
                                    hexes[i, j] = new Hex(hexes[i - 1, j].Points[(int)Hexagonal.PointyVertice.BottomLeft], i, j, side, orientation, boardColor);
                                }
                                else
                                {
                                    hexes[i, j] = new Hex(hexes[i - 1, j].Points[(int)Hexagonal.PointyVertice.BottomRight], i, j, side, orientation, boardColor);
                                }
                            }
                            else
                            {
                                // Calculate from Hex to the left
                                float x = hexes[i, j - 1].Points[(int)Hexagonal.PointyVertice.UpperRight].X;
                                float y = hexes[i, j - 1].Points[(int)Hexagonal.PointyVertice.UpperRight].Y;
                                x          += r;
                                y          -= h;
                                hexes[i, j] = new Hex(x, y, i, j, side, orientation, boardColor);
                            }
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void Initialize(int width, int height, int side, Hexagonal.HexOrientation orientation, int xOffset, int yOffset, int row = 0, int col = 0)
        {
            this.width       = width;
            this.height      = height;
            this.xOffset     = xOffset;
            this.yOffset     = yOffset;
            this.side        = side;
            this.orientation = orientation;
            hexes            = new Hex[height, width];  //opposite of what we'd expect
            this.boardState  = new BoardState();

            float h = Hexagonal.MathUtil.CalculateH(side);             // short side
            float r = Hexagonal.MathUtil.CalculateR(side);             // long side

            int offsetRow = row - this.activeRow;
            int offsetCol = col - this.activeCol;

            //
            // Calculate pixel info..remove?
            // because of staggering, need to add an extra r/h
            float hexWidth  = 0;
            float hexHeight = 0;

            switch (orientation)
            {
            case HexOrientation.Flat:
                hexWidth         = side + h;
                hexHeight        = r + r;
                this.pixelWidth  = (width * hexWidth) + h;
                this.pixelHeight = (height * hexHeight) + r;
                break;

            case HexOrientation.Pointy:
                hexWidth         = r + r;
                hexHeight        = side + h;
                this.pixelWidth  = (width * hexWidth) + r;
                this.pixelHeight = (height * hexHeight) + h;
                break;

            default:
                break;
            }


            bool inTopRow      = false;
            bool inBottomRow   = false;
            bool inLeftColumn  = false;
            bool inRightColumn = false;
            bool isTopLeft     = false;
            bool isTopRight    = false;
            bool isBotomLeft   = false;
            bool isBottomRight = false;


            // i = y coordinate (rows), j = x coordinate (columns) of the hex tiles 2D plane


            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    // Set position booleans
                    #region Position Booleans
                    if (i == 0)
                    {
                        inTopRow = true;
                    }
                    else
                    {
                        inTopRow = false;
                    }

                    if (i == height - 1)
                    {
                        inBottomRow = true;
                    }
                    else
                    {
                        inBottomRow = false;
                    }

                    if (j == 0)
                    {
                        inLeftColumn = true;
                    }
                    else
                    {
                        inLeftColumn = false;
                    }

                    if (j == width - 1)
                    {
                        inRightColumn = true;
                    }
                    else
                    {
                        inRightColumn = false;
                    }

                    if (inTopRow && inLeftColumn)
                    {
                        isTopLeft = true;
                    }
                    else
                    {
                        isTopLeft = false;
                    }

                    if (inTopRow && inRightColumn)
                    {
                        isTopRight = true;
                    }
                    else
                    {
                        isTopRight = false;
                    }

                    if (inBottomRow && inLeftColumn)
                    {
                        isBotomLeft = true;
                    }
                    else
                    {
                        isBotomLeft = false;
                    }

                    if (inBottomRow && inRightColumn)
                    {
                        isBottomRight = true;
                    }
                    else
                    {
                        isBottomRight = false;
                    }
                    #endregion

                    //
                    // Calculate Hex positions
                    //
                    if (isTopLeft)
                    {
                        //First hex
                        switch (orientation)
                        {
                        case HexOrientation.Flat:
                            hexes[0, 0] = new Hex(0 + h + xOffset, 0 + yOffset, side, i + offsetCol, j + offsetRow, orientation);
                            break;

                        case HexOrientation.Pointy:
                            hexes[0, 0] = new Hex(0 + r + xOffset, 0 + yOffset, side, i + offsetCol, j + offsetRow, orientation);
                            break;

                        default:
                            break;
                        }
                    }
                    else
                    {
                        switch (orientation)
                        {
                        case HexOrientation.Flat:
                            if (inLeftColumn)
                            {
                                // Calculate from hex above
                                hexes[i, j] = new Hex(hexes[i - 1, j].Points[(int)Hexagonal.FlatVertice.BottomLeft], side, i + offsetCol, j + offsetRow, orientation);
                            }
                            else
                            {
                                // Calculate from Hex to the left and need to stagger the columns
                                if (j % 2 == 0)
                                {
                                    // Calculate from Hex to left's Upper Right Vertice plus h and R offset
                                    float x = hexes[i, j - 1].Points[(int)Hexagonal.FlatVertice.UpperRight].X;
                                    float y = hexes[i, j - 1].Points[(int)Hexagonal.FlatVertice.UpperRight].Y;
                                    x          += h;
                                    y          -= r;
                                    hexes[i, j] = new Hex(x, y, side, i + offsetCol, j + offsetRow, orientation);
                                }
                                else
                                {
                                    // Calculate from Hex to left's Middle Right Vertice
                                    hexes[i, j] = new Hex(hexes[i, j - 1].Points[(int)Hexagonal.FlatVertice.MiddleRight], side, i + offsetCol - (offsetRow % 2), j + offsetRow, orientation);
                                }
                            }
                            break;

                        case HexOrientation.Pointy:
                            if (inLeftColumn)
                            {
                                // Calculate from hex above and need to stagger the rows
                                if (i % 2 == 0)
                                {
                                    hexes[i, j] = new Hex(hexes[i - 1, j].Points[(int)Hexagonal.PointyVertice.BottomLeft], side, i + offsetCol, j + offsetRow, orientation);
                                }
                                else
                                {
                                    hexes[i, j] = new Hex(hexes[i - 1, j].Points[(int)Hexagonal.PointyVertice.BottomRight], side, i + offsetCol, j + offsetRow, orientation);
                                }
                            }
                            else
                            {
                                // Calculate from Hex to the left
                                float x = hexes[i, j - 1].Points[(int)Hexagonal.PointyVertice.UpperRight].X;
                                float y = hexes[i, j - 1].Points[(int)Hexagonal.PointyVertice.UpperRight].Y;
                                x          += r;
                                y          -= h;
                                hexes[i, j] = new Hex(x, y, side, i + offsetCol, j + offsetRow, orientation);
                            }
                            break;

                        default:
                            break;
                        }
                    }

                    if (m_data != null)
                    {
                        Console.WriteLine("not null!");
                        int r1 = (int)hexes[i, j].Row;
                        int c1 = (int)hexes[i, j].Col;
                        if (r1 < m_data.width && r1 >= 0 && c1 < m_data.height && c1 >= 0)
                        {
                            int state = m_data.getGridStae(r1, c1);
                            Console.WriteLine("state is" + state);
                            switch (state)
                            {
                            case 0:
                                hexes[i, j].HexState.setBackgroundColor(System.Drawing.Color.White);
                                break;

                            case 1:
                                hexes[i, j].HexState.setBackgroundColor(System.Drawing.Color.Red);
                                break;

                            case 2:

                            case 3:

                            default:
                                break;
                            }
                        }
                    }

                    if (i == coreY && j == coreX)
                    {
                        this.BoardState.ActiveHex = hexes[i, j];
                        this.activeRow            = j;
                        this.activeCol            = i;
                    }
                }
            }
        }