Beispiel #1
0
 public GameOverEventArgs(GameStatus status, WinningLine winningLine,
                          int row, int column) : this()
 {
     this.Status      = status;
     this.WinningLine = winningLine;
     this.Row         = row;
     this.Column      = column;
 }
Beispiel #2
0
 public MoveResponse(Guid guid,
                     Player player, Player nextPlayer,
                     int row, int column,
                     MoveStatus moveStatus, GameStatus gameStatus, WinningLine winningLine) : base(guid)
 {
     Player      = player;
     NextPlayer  = nextPlayer;
     Row         = row;
     Column      = column;
     MoveStatus  = moveStatus;
     GameStatus  = gameStatus;
     WinningLine = winningLine;
 }
Beispiel #3
0
        /// <summary>
        /// Performs the move for 'player' and updates the variables.
        /// </summary>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <param name="ctoken"></param>
        void MakeMove(int row, int column, CancellationToken token)
        {
            var opponent = OpponentPlayer(player);
            var iPlayer  = (int)this.player.Color;

            this.winningLine = WinningLine.None;

            log.Debug("Dump lines for player {0} before move", player);
            DumpLines(iPlayer);

            /* Each square of the board is part of 20 different lines. It adds
             * one to the number of pieces in each of these lines. Then it
             * updates the value for each of the 5 squares in each of the 20
             * lines.
             */

            // Horizontal lines, from left to right.
            for (int k = 0; k < 5; ++k)
            {
                if (token.IsCancellationRequested)
                {
                    log.Debug("MakeMove was interrupted due to a cancellation request");
                    break;
                }

                var column1 = column - k;   // X
                var row1    = row;          // Y

                if ((0 <= column1) && (column1 < this.board.Size - 4))
                {
                    Add(ref this.line[0][row1][column1][iPlayer]);  // add one to line

                    if (this.gameWon && (this.winningLine == WinningLine.None))
                    {
                        log.Info("{0} won - winning line is Horiz", this.player);
                        this.winningLine = WinningLine.Horiz;
                    }

                    // Updates values for the 5 squares in the line.
                    for (int l = 0; l < 5; ++l)
                    {
                        if (token.IsCancellationRequested)
                        {
                            log.Debug("MakeMove was interrupted due to a cancellation request");
                            break;
                        }

                        if (column1 + l < this.board.Size - 4)
                        {
                            Update(this.line[0][row1][column1], this.value[row1][column1 + l], opponent);
                        }
                    }
                }
            }

            // Diagonal lines, from lower left to upper right.
            for (int k = 0; k < 5; ++k)
            {
                if (token.IsCancellationRequested)
                {
                    log.Debug("MakeMove was interrupted due to a cancellation request");
                    break;
                }

                var column1 = column - k;   // X
                var row1    = row + k;      // Y

                if ((0 <= column1) && (column1 < this.board.Size - 4) &&
                    (0 <= row1) && (row1 < this.board.Size - 4))
                {
                    Add(ref this.line[1][row1][column1][iPlayer]);

                    if (this.gameWon && (this.winningLine == WinningLine.None))
                    {
                        log.Info("{0} won - winning line is DownLeft", this.player);
                        this.winningLine = WinningLine.DownLeft;
                    }

                    for (int l = 0; l < 5; ++l)
                    {
                        if (token.IsCancellationRequested)
                        {
                            log.Debug("MakeMove was interrupted due to a cancellation request");
                            break;
                        }

                        if ((row1 - l >= 0) && (column1 + l < this.board.Size - 4))
                        {
                            Update(this.line[1][row1][column1],
                                   this.value[row1 - l][column1 + l], opponent);
                        }
                    }
                }
            }

            // Diagonal lines, down right to upper left.
            for (int k = 0; k < 5; ++k)
            {
                if (token.IsCancellationRequested)
                {
                    log.Debug("MakeMove was interrupted due to a cancellation request");
                    break;
                }

                var column1 = column + k;
                var row1    = row + k;

                if ((0 <= column1) && (column1 < this.board.Size - 4) &&
                    (0 <= row1) && (row1 < this.board.Size - 4))
                {
                    Add(ref this.line[3][row1][column1][iPlayer]);

                    if (this.gameWon && (this.winningLine == WinningLine.None))
                    {
                        log.Info("{0} won - winning line is DownRight", this.player);
                        this.winningLine = WinningLine.DownRight;
                    }

                    for (int l = 0; l < 5; ++l)
                    {
                        if (token.IsCancellationRequested)
                        {
                            log.Debug("MakeMove was interrupted due to a cancellation request");
                            break;
                        }

                        if ((row1 - l >= 0) && (column1 - l >= 0))
                        {
                            Update(this.line[3][row1][column1],
                                   this.value[row1 - l][column1 - l], opponent);
                        }
                    }
                }
            }

            // Vertical lines, from down to up
            for (int k = 0; k < 5; ++k)
            {
                if (token.IsCancellationRequested)
                {
                    log.Debug("MakeMove was interrupted due to a cancellation request");
                    break;
                }

                var column1 = column;
                var row1    = row + k;

                if ((0 <= row1) && (row1 < this.board.Size - 4))
                {
                    Add(ref this.line[2][row1][column1][iPlayer]);

                    if (this.gameWon && (this.winningLine == WinningLine.None))
                    {
                        log.Info("{0} won - winning line is Vert", this.player);
                        this.winningLine = WinningLine.Vert;
                    }

                    for (int l = 0; l < 5; ++l)
                    {
                        if (token.IsCancellationRequested)
                        {
                            log.Debug("MakeMove was interrupted due to a cancellation request");
                            break;
                        }

                        if (row1 - l >= 0)
                        {
                            Update(this.line[2][row1][column1],
                                   this.value[row1 - l][column1], opponent);
                        }
                    }
                }
            }

            log.Debug("Dump lines and values for player {0} after move", player);
            DumpLines(iPlayer);
            DumpValues(iPlayer);

            //
            OnMoveMade(new MoveMade(this.guid, this.player, row, column));
        }