コード例 #1
0
        public void CreateTiles()
        {
            try
            {
                Tiles = new Bitmap(50 * 8, 50 * 8);
                var g = Graphics.FromImage(Tiles);

                // draw tiles
                for (int x = 0; x < 8; x++)
                {
                    for (int y = 0; y < 8; y++)
                    {
                        var b  = ((x + y) % 2 == 0) ? BrushWhite : BrushBlack;
                        var bx = ((x + y) % 2 == 0) ? BrushBlack : BrushWhite;

                        int xs = x * TileSize;
                        int ys = y * TileSize;
                        g.FillRectangle(b, xs, ys, TileSize, TileSize);

                        if (LabeledTiles)
                        {
                            int num = x + (7 - y) * 8;
                            g.DrawString(Notation.TileToText(num).ToUpper(), new Font("Arial", 12), bx, xs + 2, ys + 2);
                        }
                    }
                }

                Buffer = new Bitmap(50 * 8, 50 * 8);
            }
            catch (Exception) { }
        }
コード例 #2
0
ファイル: OpeningBook.cs プロジェクト: adh2050/Chess
        /// <summary>
        /// Filters the opening book by playing the specified move.
        /// Removes all other entires from the book (they are redundant after making the move)
        /// </summary>
        /// <param name="moveFrom"></param>
        /// <param name="moveTo"></param>
        public void FilterBook(int moveFrom, int moveTo)
        {
            string text = Notation.TileToText(moveFrom) + Notation.TileToText(moveTo);

            Book = Book.Where(x => x[MovesMade + 1] == text).ToArray();
            MovesMade++;
        }
コード例 #3
0
        public void TestTileToText()
        {
            Assert.AreEqual("e1", Notation.TileToText(4));
            Assert.AreEqual("d5", Notation.TileToText(35));
            Assert.AreEqual("h8", Notation.TileToText(63));

            Assert.AreEqual("", Notation.TileToText(-1));
            Assert.AreEqual("", Notation.TileToText(64));
            Assert.AreEqual("", Notation.TileToText(1235));
        }
コード例 #4
0
ファイル: OpeningBook.cs プロジェクト: adh2050/Chess
        public static string GenerateBook(string[] strippedInputs)
        {
            var moveStrings = strippedInputs;

            int i        = 0;
            var splitter = new char[] { ' ' };
            var sb       = new StringBuilder();

            var board = new Chess.Base.Board(true);

            foreach (var m in moveStrings)
            {
                try
                {
                    Console.WriteLine("Writing game #" + i);
                    //var sb = new StringBuilder();

                    board.InitBoard();
                    var moves   = ABN.ABNToMoves(board, m);
                    var winLose = m.Split(splitter, StringSplitOptions.RemoveEmptyEntries).Last();

                    if (winLose.Contains('1') && winLose.Contains('2'))
                    {
                        sb.Append("D ");
                    }
                    else if (winLose.Trim() == "1-0")
                    {
                        sb.Append("W ");
                    }
                    else if (winLose.Trim() == "0-1")
                    {
                        sb.Append("B ");
                    }
                    else
                    {
                        throw new Exception("No end result found");
                    }

                    for (int j = 0; j < 20 && j < moves.Count; j++)
                    {
                        sb.Append(Notation.TileToText(moves[j].From) + Notation.TileToText(moves[j].To));
                        sb.Append(" ");
                    }

                    sb.Append("\n");
                }
                catch (Exception)
                {
                    Console.WriteLine("Exception writing game #" + i);
                }
                i++;
            }

            return(sb.ToString());
        }
コード例 #5
0
        public override string ToString()
        {
            string output = Notation.GetPieceLetter(Piece) + Notation.TileToText(From) + Notation.TileToText(To);

            if (Promotion != Piece.None)
            {
                output += "=" + Promotion.GetLetter();
            }

            return(output);
        }
コード例 #6
0
        private int GetMovingPiece(Board board, PGNMove move, int fromX, int fromY)
        {
            // figure out which piece is moving
            int from     = -1;
            var pieces   = board.FindByPieceAndColor(move.Piece, board.PlayerTurn);
            var possible = new List <int>();

            foreach (var p in pieces)
            {
                var moves = Moves.GetValidMoves(board, p);
                if (moves.Contains(move.To))
                {
                    possible.Add(p);
                }
            }

            if (possible.Count == 0)
            {
                throw new Exception("No piece has a valid move to tile " + Notation.TileToText(move.To));
            }

            if (possible.Count > 1)
            {
                // filter possible pieces by using the hints
                if (fromX != -1)
                {
                    possible = possible.Where(k => Board.X(k) == fromX).ToList();
                }
                if (fromY != -1)
                {
                    possible = possible.Where(k => Board.Y(k) == fromY).ToList();
                }
            }

            if (possible.Count != 1)
            {
                throw new Exception("No piece has a valid move to tile " + Notation.TileToText(move.To));
            }

            from = possible[0];
            return(from);
        }