public void SetPiece(CellID target, EConnectFourCellContent content)
        {
            EConnectFourCellContent oldContent = _cells[target].Content;

            _cells[target].SetContent(content);
            CellStatusChanged(this, new CellStatusChangedEventArgs(target, content, oldContent));
        }
Exemplo n.º 2
0
        private bool VerticalWin()
        {
            for (int col = 0; col < _board.NumCols; col++)
            {
                int matchCounter = 0;

                for (int row = _board.NumRows - 1; row >= 0; row--)
                {
                    if (_board.GetCell(CellID.Create(row, col)).Content == playerToContent(ActivePlayer))
                    {
                        matchCounter++;
                    }
                    else
                    {
                        matchCounter = 0;
                    }

                    if (matchCounter == 4)
                    {
                        Console.WriteLine("VERTICALWIN");
                        return(true);
                    }
                }
            }
            return(false);
        }
        public CellID SetPieceToEmpty(int row, int col)
        {
            var cellid = CellID.Create(row, col);

            SetPiece(cellid, EConnectFourCellContent.Empty);
            return(cellid);


            throw new InvalidOperationException();
        }
        private CellID getLowestEmptyRowOfCol(int col)
        {
            for (int row = NumRows - 1; row >= 0; row--)
            {
                var cellid = CellID.Create(row, col);
                if (_cells[cellid].Content == EConnectFourCellContent.Empty)
                {
                    return(cellid);
                }
            }

            throw new ColFullException(col);
        }
        public bool IsValidMove(CellID target)
        {
            try
            {
                if (getLowestEmptyRowOfCol(target.Col).GetType().Name == "CellID")
                {
                    return(true);
                }
            }
            catch (ColFullException ex)
            {
                return(false);
            }

            return(false);
        }
Exemplo n.º 6
0
        public void SetPiece(CellID target)
        {
            if (!IsValidMove(target))
            {
                return;
            }

            var targetCell = _board.GetCell(target);
            var cellid     = _board.SetPiece(target.Row, target.Col, playerToContent(ActivePlayer));

            if (isGameFinished(cellid))
            {
                GameFinished(this, new GameEndedEventArgs(ActivePlayer));
            }

            nextTurn();
        }
 private CellID keyFor(int row, int col)
 {
     return(CellID.Create(row, col));
 }
 public FourConnectCellModel GetCell(CellID cellId)
 {
     return(_cells[cellId]);
 }
Exemplo n.º 9
0
 private bool isGameFinished(CellID target)
 {
     return(HorizontalWin() || VerticalWin() || DiagonalWin());
 }
Exemplo n.º 10
0
 public bool IsValidMove(CellID target)
 {
     return(_board.IsValidMove(target));
 }
Exemplo n.º 11
0
        private bool DiagonalWin()

        /*
         *      4
         *  _______  5
         *  | O O O  O  O O O
         * 3 | O O O  O  O O O
         *  | O O O  O  O O O
         *
         *  | O O O  O  O O O
         * 2 | O O O  O  O O O
         *  | O O O  O  O O O
         * ______
         * 1         6
         *
         *
         */
        {
            //1
            for (int col = 0; col + 4 < _board.NumCols; col++)
            {
                int row     = _board.NumRows - 1;
                int counter = 0;
                for (int diagCol = col; row >= 0 && diagCol < _board.NumCols; diagCol++)
                {
                    if (_board.GetCell(CellID.Create(row, diagCol)).Content == playerToContent(ActivePlayer))
                    {
                        counter++;
                    }
                    else
                    {
                        counter = 0;
                    }
                    if (counter == 4)
                    {
                        Console.WriteLine("DIAGONALWIN 1");
                        return(true);
                    }
                    row--;
                }
            }

            //2
            for (int row = _board.NumRows - 1; row - 3 > 0; row--)
            {
                int col     = 0;
                int counter = 0;
                for (int diagRow = row; diagRow >= 0 && col >= 0; diagRow--)
                {
                    if (_board.GetCell(CellID.Create(diagRow, col)).Content == playerToContent(ActivePlayer))
                    {
                        counter++;
                    }
                    else
                    {
                        counter = 0;
                    }
                    if (counter == 4)
                    {
                        Console.WriteLine("DIAGONALWIN 2");
                        return(true);
                    }
                    col++;
                }
            }

            //3
            for (int row = 0; row + 3 < _board.NumRows; row++)
            {
                int col     = 0;
                int counter = 0;
                for (int diagRow = row; diagRow < _board.NumRows && col < _board.NumCols; diagRow++)
                {
                    if (_board.GetCell(CellID.Create(diagRow, col)).Content == playerToContent(ActivePlayer))
                    {
                        counter++;
                    }
                    else
                    {
                        counter = 0;
                    }
                    if (counter == 4)
                    {
                        Console.WriteLine("DIAGONALWIN 3");
                        return(true);
                    }
                    col++;
                }
            }

            //4
            for (int col = 0; col + 4 < _board.NumCols; col++)
            {
                int row     = 0;
                int counter = 0;
                for (int diagCol = col; row < _board.NumRows && diagCol < _board.NumCols; diagCol++)
                {
                    if (_board.GetCell(CellID.Create(row, diagCol)).Content == playerToContent(ActivePlayer))
                    {
                        counter++;
                    }
                    else
                    {
                        counter = 0;
                    }
                    if (counter == 4)
                    {
                        Console.WriteLine("DIAGONALWIN 4");
                        return(true);
                    }
                    row++;
                }
            }

            //5
            var leftOverCol = _board.NumCols / 2;

            var topRow      = 0;
            var lastCounter = 0;

            for (int topCol = leftOverCol; topCol < _board.NumCols; topCol++)
            {
                if (_board.GetCell(CellID.Create(topRow, topCol)).Content == playerToContent(ActivePlayer))
                {
                    lastCounter++;
                }
                else
                {
                    lastCounter = 0;
                }
                if (lastCounter == 4)
                {
                    Console.WriteLine("DIAGONALWIN 5");
                    return(true);
                }
                topRow++;
            }

            var bottomRow = _board.NumRows - 1;

            lastCounter = 0;

            for (int bottomCol = leftOverCol; bottomCol < _board.NumCols; bottomCol++)
            {
                if (_board.GetCell(CellID.Create(bottomRow, bottomCol)).Content == playerToContent(ActivePlayer))
                {
                    lastCounter++;
                }
                else
                {
                    lastCounter = 0;
                }
                if (lastCounter == 4)
                {
                    Console.WriteLine("DIAGONALWIN 6");
                    return(true);
                }
                bottomRow--;
            }
            return(false);
        }