Example #1
0
        private void onChessFieldClicked(object parameter)
        {
            // identify the field that was clicked
            string fieldname = parameter as string;
            var    position  = new ChessPosition(fieldname);

            // determine the chess piece that was clicked (default: null)
            var targetPiece = _session.Board.IsCapturedAt(position) ? (ChessPieceAtPos?)new ChessPieceAtPos(position, _session.Board.GetPieceAt(position)) : null;

            // set drawing piece (if an allied piece is selected)
            if (targetPiece?.Piece.Color == _player.Side)
            {
                // remember the drawing piece
                _drawingPiece = targetPiece;

                // get all potantial draws for the piece
                _potentialDraws = _session.Game.GetDraws(true).Where(x => x.OldPosition == _drawingPiece?.Position).ToList();

                // highlight the target fields of the draws (first reset all highlightings, just for good measure)
                foreach (var field in Board.Fields)
                {
                    field.UpdateHighlight(false);
                }
                foreach (var field in Board.Fields)
                {
                    if (_potentialDraws.Any(x => x.NewPosition == field.Position))
                    {
                        field.UpdateHighlight(true);
                    }
                }
                if (_potentialDraws?.Count > 0)
                {
                    Board.Fields[position.GetHashCode()].UpdateHighlight(true);
                }
            }
            // only continue if an allied piece was selected before
            else if (_drawingPiece != null && _potentialDraws?.Count > 0 && _potentialDraws.Any(x => x.NewPosition == position))
            {
                // get the selected draw
                var drawToMake = _potentialDraws.First(x => x.NewPosition == position);
                _player.PassDraw(drawToMake);
            }
        }
        public void ConstructorTest()
        {
            // test if all valid chess positions can be created and have the correct features
            for (int row = 0; row < 8; row++)
            {
                for (int column = 0; column < 8; column++)
                {
                    ChessPosition position;
                    string        fieldName = $"{ (char)(column + 'A') }{ (char)(row + '1') }";
                    byte          hashCode  = (byte)(row * 8 + column);

                    // create a new chess position instance and check if the row, column and hash code values are set as expected
                    position = new ChessPosition(fieldName);
                    Assert.True(position.Row == row && position.Column == column && position.GetHashCode() == hashCode);

                    // create a new chess position instance and check if the row, column and hash code values are set as expected
                    position = new ChessPosition(new Tuple <int, int>(row, column));
                    Assert.True(position.Row == row && position.Column == column && position.GetHashCode() == hashCode);

                    // create a new chess position instance and check if the row, column and hash code values are set as expected
                    position = new ChessPosition(row, column);
                    Assert.True(position.Row == row && position.Column == column && position.GetHashCode() == hashCode);

                    // create a new chess position instance and check if the row, column and hash code values are set as expected
                    position = new ChessPosition((byte)(row * 8 + column));
                    Assert.True(position.Row == row && position.Column == column && position.GetHashCode() == hashCode);
                }
            }

            // test if several invalid chess positions are rejected
            try
            {
                // create invalid chess position (should throw an exception)
                new ChessPosition("I9");
                Assert.True(false);
            }
            catch (Exception) { /* nothing to do here ... */ }

            try
            {
                // create invalid chess position (should throw an exception)
                new ChessPosition("?0");
                Assert.True(false);
            }
            catch (Exception) { /* nothing to do here ... */ }

            try
            {
                // create invalid chess position (should throw an exception)
                new ChessPosition(8, 8);
                Assert.True(false);
            }
            catch (Exception) { /* nothing to do here ... */ }

            try
            {
                // create invalid chess position (should throw an exception)
                new ChessPosition(-1, -1);
                Assert.True(false);
            }
            catch (Exception) { /* nothing to do here ... */ }

            try
            {
                // create invalid chess position (should throw an exception)
                new ChessPosition(72);
                Assert.True(false);
            }
            catch (Exception) { /* nothing to do here ... */ }

            try
            {
                // create invalid chess position (should throw an exception)
                new ChessPosition(255);
                Assert.True(false);
            }
            catch (Exception) { /* nothing to do here ... */ }
        }