示例#1
0
        private void OnVertexClick(object sender, EventArgs e)
        {
            var currentPlayer = _gameController.CurrentPlayer;
            ButtonWithVertex curVertex;
            Pen pen;
            Road road;
            Settlement piece;
            switch(_context)
            {
                case Context.None:
                    break;
                case Context.PlaceCity:
                    var settlement = new Settlement(currentPlayer, SettlementType.City);

                    try
                    {
                        _board.PlacePiece(settlement,
                                          ((ButtonWithVertex) sender)._vertex.Index);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(
                            Resources.settlementPlacementError,
                            Resources.invalidLocation,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation,
                            MessageBoxDefaultButton.Button1);
                        return;
                    }

                    currentPlayer.Buy(settlement);

                    ((Button) sender).BackColor = currentPlayer.GetDrawColor();
                    ((Button) sender).Text = Resources.C;

                    _context = Context.None;
                    break;
                case Context.RoadBuildingFirstVertex:
                case Context.PlaceRoadFirstVertex:
                    _roadFirstVertex = sender as ButtonWithVertex;
                    _vertexOriginalColor = _roadFirstVertex.BackColor;
                    _roadFirstVertex.BackColor = Color.Yellow;

                    if (_context == Context.PlaceRoadFirstVertex)
                    {
                        _context = Context.PlaceRoadSecondVertex;
                    } else
                    {
                        _context = Context.RoadBuildingSecondVertex;
                    }
                    break;

                case Context.RoadBuildingSecondVertex:
                case Context.PlaceRoadSecondVertex:

                    curVertex = sender as ButtonWithVertex;

                    if (!curVertex._vertex.Neighbors.Contains(_roadFirstVertex._vertex))
                    {
                        MessageBox.Show(
                            Resources.neighboringVertexError,
                            Resources.invalidLocation,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation,
                            MessageBoxDefaultButton.Button1);
                        return;
                    }

                    if (curVertex._vertex.HasRoad(_roadFirstVertex._vertex))
                    {
                        MessageBox.Show(
                            Resources.roadExistsError,
                            Resources.invalidLocation,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation,
                            MessageBoxDefaultButton.Button1);
                        return;
                    }

                    _roadFirstVertex.BackColor = _vertexOriginalColor;

                    road = new Road(currentPlayer);

                    try
                    {
                        _board.PlacePiece(road, curVertex._vertex, _roadFirstVertex._vertex);
                    } catch (Exception ex)
                    {
                        MessageBox.Show(
                            Resources.roadPlacementError,
                            Resources.invalidLocation,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation,
                            MessageBoxDefaultButton.Button1);
                        return;
                    }

                    if (_context == Context.PlaceRoadSecondVertex)
                    {
                        currentPlayer.Buy(road);
                    }

                    pen = new Pen(currentPlayer.GetDrawColor(), 10);
                    CreateGraphics().DrawLine(pen, curVertex.Location.X + 10, curVertex.Location.Y + 10, _roadFirstVertex.Location.X + 10, _roadFirstVertex.Location.Y + 10);

                    if (_context == Context.PlaceRoadSecondVertex || _roadBuildingFirst)
                    {
                        _roadBuildingFirst = false;
                        _context = Context.None;
                    } else
                    {
                        _roadBuildingFirst = true;
                        _context = Context.RoadBuildingFirstVertex;
                    }
                    break;

                case Context.PlaceVillage:
                    piece = new Settlement(currentPlayer, SettlementType.Village);

                    try
                    {
                        _board.PlacePiece(piece, ((ButtonWithVertex)sender)._vertex.Index);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(
                            Resources.settlementPlacementError,
                            Resources.invalidLocation,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation,
                            MessageBoxDefaultButton.Button1);
                        return;
                    }

                    currentPlayer.Buy(piece);

                    ((Button) sender).BackColor = currentPlayer.GetDrawColor();
                    ((Button) sender).Text = Resources.V;

                    _context = Context.None;
                    break;

                 case Context.Trade:
                    _context = Context.None;
                    break;

                case Context.PlaceVillageSetup:
                    piece = new Settlement(currentPlayer, SettlementType.Village);

                    try
                    {
                        _board.PlacePieceSetup(piece, ((ButtonWithVertex)sender)._vertex.Index);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(
                            Resources.settlementPlacementError,
                            Resources.invalidLocation,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation,
                            MessageBoxDefaultButton.Button1);
                        return;
                    }

                    ((Button) sender).BackColor = currentPlayer.GetDrawColor();
                    ((Button) sender).Text = Resources.V;

                    _roadFirstVertex = sender as ButtonWithVertex;

                    _context = Context.PlaceRoadSetup;
                    GameSetup();
                    break;

                case Context.PlaceRoadSetup:
                    curVertex = sender as ButtonWithVertex;

                    if (!curVertex._vertex.Neighbors.Contains(_roadFirstVertex._vertex))
                    {
                        MessageBox.Show(
                            Resources.neighboringVertexError,
                            Resources.invalidLocation,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation,
                            MessageBoxDefaultButton.Button1);
                        return;
                    }

                    if (curVertex._vertex.HasRoad(_roadFirstVertex._vertex))
                    {
                        MessageBox.Show(
                            Resources.roadExistsError,
                            Resources.invalidLocation,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation,
                            MessageBoxDefaultButton.Button1);
                        return;
                    }

                    road = new Road(currentPlayer);

                    try
                    {
                        _board.PlacePieceSetup(road, curVertex._vertex, _roadFirstVertex._vertex);
                    } catch (Exception ex)
                    {
                        MessageBox.Show(
                            Resources.roadPlacementError,
                            Resources.invalidLocation,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation,
                            MessageBoxDefaultButton.Button1);
                        return;
                    }

                    pen = new Pen(currentPlayer.GetDrawColor(), 10);
                    CreateGraphics().DrawLine(pen, curVertex.Location.X + 10, curVertex.Location.Y + 10, _roadFirstVertex.Location.X + 10, _roadFirstVertex.Location.Y + 10);

                    _context = Context.PlaceVillageSetup;
                    NextPlayerSetup();
                    GameSetup();
                    break;

                default:
                    break;
            }
        }
示例#2
0
        private void DrawVertexButtons(PointF[] points)
        {
            if (seaTileIndices.Contains(_tileCount-1) || portTileIndices.Contains(_tileCount-1))
            {
                return;
            }

            var vertices = _board.TerrainTiles[_terrainCount - 1].Vertices;

            for (var i = 0; i < points.Length; i++ )
            {
                var vertex = vertices[i];
                var location = points[i];

                if (_alreadyDrawnButtons.Contains(vertex))
                {
                    continue;
                }

                var b = new ButtonWithVertex();
                b.Width = 25;
                b.Height = 25;
                b.Location = new Point((int)location.X - b.Width/2, (int)location.Y - b.Height/2);
                b.Name = "btn_Vertex" + vertex.Index;
                b._vertex = vertex;
                b.Click += new EventHandler(OnVertexClick);
                b.TabStop = false;
                //b.Text = vertex.Index.ToString();
                b.BackColor = Color.Transparent;

                Controls.Add(b);
                _alreadyDrawnButtons.Add(vertex);
            }
        }