Exemplo n.º 1
0
 private void Form_Closing(object sender, FormClosingEventArgs e)
 {
     _graphicsEngine = null;
     _board          = null;
 }
Exemplo n.º 2
0
        public async Task StartGame()
        {
            gameStartTime = DateTime.Now;
            var timer = new System.Windows.Forms.Timer();

            timer.Interval = (1000);
            timer.Tick    += new EventHandler(UpdateTimerValues);
            timer.Start();

            buttonTestBoard.Enabled = false;
            var boardSize = _referee.Size;

            saveGameToolStripMenuItem.Enabled            = false;
            loadGameToolStripMenuItem.Enabled            = false;
            reloadConfigurationToolStripMenuItem.Enabled = false;
            _playThrough                    = new List <Bitmap>();
            totalMillisecondsPlayer1        = 0;
            totalMillisecondsPlayer1        = 0;
            preTurnTotalMillisecondsPlayer1 = 0;
            preTurnTotalMillisecondsPlayer2 = 0;

            _board = new Board(boardSize,
                               boardSize,
                               25,
                               HexOrientation.Pointy
                               )
            {
                BoardState =
                {
                    BackgroundColor      = _backgroundColor,
                    GridPenWidth         =                2,
                    ActiveHexBorderColor = Color.Red,
                    ActiveHexBorderWidth = 2
                }
            };

            _graphicsEngine = new GraphicsEngine(_board, 20, 20);


            Refresh();
            _playThrough.Add(_graphicsEngine.CreateImage());

            // make the edges colourful!
            ColourBoard(boardSize);

            lblCurrentPlayer.Text = "Current Player: Blue";

            try
            {
                while (_referee.WinningPlayer == null)
                {
                    //Console.WriteLine("Player taking turn: " + referee.CurrentPlayer().PlayerNumber);

                    if (_referee.lastHexForPlayer1 != null && _referee.lastHexForPlayer2 != null)
                    {
                        var lastHex = _board.Hexes[
                            _referee.CurrentPlayer().PlayerNumber == 1
                            ? _referee.lastHexForPlayer1.Item1
                            : _referee.lastHexForPlayer2.Item1,
                            _referee.CurrentPlayer().PlayerNumber == 1
                            ? _referee.lastHexForPlayer1.Item2
                            : _referee.lastHexForPlayer2.Item2];

                        ChangeHexColor(lastHex,
                                       _referee.CurrentPlayer().PlayerNumber == 1 ? _takenBeforeByPlayer1 : _takenBeforeByPlayer2);
                    }

                    playerTurnTimer = Stopwatch.StartNew();
                    var playerNumber = _referee.CurrentPlayer().PlayerNumber;
                    var hexTaken     = await(_referee.TakeTurn(_referee.CurrentPlayer()));
                    playerTurnTimer.Stop();

                    if (playerNumber == 1)
                    {
                        totalMillisecondsPlayer1        = preTurnTotalMillisecondsPlayer1 + playerTurnTimer.ElapsedMilliseconds;
                        preTurnTotalMillisecondsPlayer1 = totalMillisecondsPlayer1;
                        lblCurrentPlayer.Text           = "Current Player: Red";
                    }
                    else
                    {
                        totalMillisecondsPlayer2        = preTurnTotalMillisecondsPlayer2 + playerTurnTimer.ElapsedMilliseconds;
                        preTurnTotalMillisecondsPlayer2 = totalMillisecondsPlayer2;
                        lblCurrentPlayer.Text           = "Current Player: Blue";
                    }

                    UpdateTimerValues(null, null);
                    Refresh();

                    if (hexTaken != null)
                    {
                        var boardHex = _board.Hexes[hexTaken.Item1, hexTaken.Item2];

                        if (_referee.WinningPlayer != null)
                        {
                            ChangeHexColor(boardHex, _referee.CurrentPlayer().PlayerNumber == 2
                            ? _lastTakenByPlayer2Colour
                            : _lastTakenByPlayer1Colour);
                        }
                        else
                        {
                            ChangeHexColor(boardHex, _referee.CurrentPlayer().PlayerNumber == 1
                                ? _lastTakenByPlayer2Colour
                                : _lastTakenByPlayer1Colour);
                        }


                        _playThrough.Add(_graphicsEngine.CreateImage());
                    }

                    this.Refresh();
                }

                // Show the winning path
                var colorForWinningPath = _referee.CurrentPlayer().PlayerNumber == 1 ? _lastTakenByPlayer1Colour : _lastTakenByPlayer2Colour;
                foreach (var hex in _referee.winningPath)
                {
                    ChangeHexColor(GetBoardHexFromCoordinates(hex.Row, hex.Column), colorForWinningPath);
                }


                this.Refresh();
                _playThrough.Add(_graphicsEngine.CreateImage());
                if (_referee.WinningPlayer.PlayerNumber == 1)
                {
                    blueWins++;
                }
                else
                {
                    redWins++;
                }
                //MessageBox.Show("The winner is " + _referee.WinningPlayer.Name + ", player #" + _referee.WinningPlayer.PlayerNumber, "Winner!");
            }
            catch (Exception e)
            {
                MessageBox.Show("No winner today!", "Drat");
                Console.WriteLine("No winner today!");
            }
        }