/// <summary> Get all the possible tiles a queen can move to given a position </summary> public static List <(int, int)> GetPossibleMoves(Piece piece) { // Get the position of the specified piece int x = piece.position.X; int y = piece.position.Y; // Instatiate the list of possible moves List <(int, int)> possibleMoves = new List <(int, int)>(); // A queen's movement can be described as a rook and bishop put together // Add a rook's possible moves to the list possibleMoves = Rook.GetPossibleMoves(piece); // Add a bishop's possible moves to the list possibleMoves.AddRange(Bishop.GetPossibleMoves(piece)); // Return the list of possible moves return(possibleMoves); }
public Board() { for (int i = 0; i < 8; i++) { Figures[i, 1] = new Pawn(FigureColor.Black); Figures[i, 6] = new Pawn(FigureColor.White); } Figures[1, 0] = new Knight(FigureColor.Black); Figures[6, 0] = new Knight(FigureColor.Black); Figures[1, 7] = new Knight(FigureColor.White); Figures[6, 7] = new Knight(FigureColor.White); Figures[0, 0] = new Rook(FigureColor.Black); Figures[7, 0] = new Rook(FigureColor.Black); Figures[0, 7] = new Rook(FigureColor.White); Figures[7, 7] = new Rook(FigureColor.White); Figures[2, 0] = new Bishop(FigureColor.Black); Figures[5, 0] = new Bishop(FigureColor.Black); Figures[2, 7] = new Bishop(FigureColor.White); Figures[5, 7] = new Bishop(FigureColor.White); Figures[3, 0] = new Queen(FigureColor.Black); Figures[3, 7] = new Queen(FigureColor.White); Figures[4, 0] = new King(FigureColor.Black); Figures[4, 7] = new King(FigureColor.White); }
/// <summary> /// Checks is pawn can promotion /// </summary> /// <param name="color">To checks if a black or white</param> public void MakePromotion(Color color) { if (color is Color.Black) { if (Position.Row == 7) { Console.WriteLine("This pawn was promote, you can promotion a new piece."); Console.WriteLine("Choose any T: Tower, B: Bishop, N: Knight and Q: Queen: "); string choose = Console.ReadLine().ToUpper(); Promotions promotion = 0; if (Enum.TryParse <Promotions>(choose, out promotion)) { Position pos = Position; Board.RemovePiece(Position); switch (promotion) { case Promotions.T: Board.PutPiece(new Rook(Board, color), pos); break; case Promotions.B: Board.PutPiece(new Bishop(Board, color), pos); break; case Promotions.N: Board.PutPiece(new Knight(Board, color), pos); break; case Promotions.Q: Board.PutPiece(new Queen(Board, color), pos); break; } } else { throw new BoardException("Invalid promotion"); } } } else if (color is Color.White) { if (Position.Row == 0) { Console.WriteLine("This pawn was promote, you can promotion a new piece."); Console.WriteLine("Choose any T: Tower, B: Bishop, N: Knight and Q: Queen: "); string choose = Console.ReadLine().ToUpper(); Promotions promotion; if (Enum.TryParse(choose, out promotion)) { Position pos = Position; Board.RemovePiece(Position); StringBuilder text = new StringBuilder(); switch (promotion) { case Promotions.T: Rook rook = new Rook(Board, color); Board.PutPiece(rook, pos); text.AppendLine(string.Concat("Promote pawn to: ", rook.GetType())); text.AppendLine(string.Concat("ChessPosition: ", rook.Position.ToChessPosition(), " CanvasPosition(Column, Row): ", rook.Position.Column, ", ", rook.Position.Row)); break; case Promotions.B: Bishop bishop = new Bishop(Board, color); Board.PutPiece(bishop, pos); text.AppendLine(string.Concat("Promote pawn to: ", bishop.GetType())); text.AppendLine(string.Concat("ChessPosition: ", bishop.Position.ToChessPosition(), " CanvasPosition(Column, Row): ", bishop.Position.Column, ", ", bishop.Position.Row)); break; case Promotions.N: Knight knight = new Knight(Board, color); Board.PutPiece(knight, pos); text.AppendLine(string.Concat("Promote pawn to: ", knight.GetType())); text.AppendLine(string.Concat("ChessPosition: ", knight.Position.ToChessPosition(), " CanvasPosition(Column, Row): ", knight.Position.Column, ", ", knight.Position.Row)); break; case Promotions.Q: Queen queen = new Queen(Board, color); Board.PutPiece(queen, pos); text.AppendLine(string.Concat("Promote pawn to: ", queen.GetType())); text.AppendLine(string.Concat("ChessPosition: ", queen.Position.ToChessPosition(), " CanvasPosition(Column, Row): ", queen.Position.Column, ", ", queen.Position.Row)); break; } text.AppendLine(string.Concat("Color: ", color)); WriteLog.ChecksLogs(text); } else { throw new BoardException("Invalid promotion"); } } } }
//Nhóm hàm protected #region Nhóm hàm protected //Lê Thanh Tâm + Nguyễn Quang Thạch + Huỳnh Viết Thám //Di chuyển quân cờ P đến vị trí hiện tại protected void MoveFromP(ref AbstractChessPiece p) { string movement = ""; //Nếu quân cờ P không phải là chốt, vì trong quy ước ghi biên bản, chốt không ghi tên if (p.cName.Substring(1, 1) != "P") { movement = p.cName.Substring(1, 1); } //Bổ sung quân cờ đi movement += Convert.ToChar(97 + p.column).ToString() + (p.row + 1).ToString(); //Cộng điểm từ quân cờ bị ăn player[turn % 2].score += cScore; //Nếu ăn được quân đối phương (không phải là di chuyển đến ô trống) if (cScore != 0) { //Kí hiệu của nước đi ăn quân là chữ x movement += "x"; //Thêm quân cờ ăn được vào thông tin người chơi player[turn % 2].AddEatenPiece(this.Image, this.cName); } else { //Kí hiệu nước đi di chuyển thông thường movement += "-"; } //Bổ sung điểm đến movement += Convert.ToChar(97 + column).ToString() + (row + 1).ToString(); //Hủy quân cờ hiện tại Dispose(); //Di chuyển đến vị trí mới và xóa vị trí cũ #region Di chuyển đến vị trí mới và xóa vị trí cũ //Khởi tạo lại quân cờ dựa vào tên của quân cờ P string nc = p.cName; int t = 0; if (nc[0] == 'b') { t = 1; } switch (nc[1]) { case 'P': { if (t == 0) { cw[row, column] = new WhitePawn(row, column); } else { cw[row, column] = new BlackPawn(row, column); } }; break; case 'R': cw[row, column] = new Rook(row, column, t);; break; case 'N': cw[row, column] = new Knight(row, column, t);; break; case 'B': cw[row, column] = new Bishop(row, column, t);; break; case 'Q': cw[row, column] = new Queen(row, column, t);; break; case 'K': cw[row, column] = new King(row, column, t);; break; } cw[row, column].moved = true; instance.Controls.Add(cw[row, column]); //Xóa quân cờ ở vị trí cũ p.Reset_Temp(); int r = p.row, c = p.column; p.Dispose(); cw[r, c] = new AbstractChessPiece(r, c); instance.Controls.Add(cw[r, c]); #endregion //Nếu quân cờ di chuyển là Vua, kiểm tra có phải thực hiện Nhập thành hay không if (cw[row, column].GetType() == typeof(King)) { //Đo khoảng cách bước đi, nếu abs = 2 tức là Vua đã đi 2 bước ~ Nhập thành int n = column - p.column; if (n == 2) { cw[row, 5].Dispose(); cw[row, 5] = new Rook(row, 5, t); cw[row, 5].moved = true; instance.Controls.Add(cw[row, 5]); cw[row, 7].Dispose(); cw[row, 7] = new AbstractChessPiece(row, 7); instance.Controls.Add(cw[row, 7]); movement = "O-O"; } else if (n == -2) { cw[row, 3].Dispose(); cw[row, 3] = new Rook(row, 3, t); cw[row, 3].moved = true; instance.Controls.Add(cw[row, 3]); cw[row, 0].Dispose(); cw[row, 0] = new AbstractChessPiece(row, 0); instance.Controls.Add(cw[row, 0]); movement = "O-O-O"; } } //Nếu quân cờ di chuyển là Chốt else if (cw[row, column].Get_name.Substring(1, 1) == "P") { //Chốt đi tới dòng cuối cùng, Trắng đi đến dòng 7, Đen đi đến dòng 0 if (row == ((turn + 1) % 2) * 7) { //Hiện form lựa chọn phong cấp Promotion exm = new Promotion(turn); //Nếu hiện form nhưng không lựa chọn thì form liên tục mở đến khi nào chọn xong while (exm.Get_typename == typeof(AbstractChessPiece).Name.ToString()) { exm.ShowDialog(); } //Hủy quân cờ hiện tại cw[row, column].Dispose(); //Tái tạo quân cờ dựa trên lựa chọn switch (exm.Get_typename) { case "Queen": cw[row, column] = new Queen(row, column, turn); break; case "Rook": cw[row, column] = new Rook(row, column, turn); break; case "Bishop": cw[row, column] = new Bishop(row, column, turn); break; case "Knight": cw[row, column] = new Knight(row, column, turn); break; } //Ghi chép phong cờ là kí tự = movement += "=" + cw[row, column].Get_name.Substring(1, 1); instance.Controls.Add(cw[row, column]); } //Quân cờ đi là Chốt, và thuộc tính bắt chốt qua đường đang tồn tại else if (EP != null) { //Kiểm tra xem có phải Chốt vừa thực hiện bắt chốt qua đường hay không //Vì bản chất Chốt đi thẳng, ăn chéo nên phức tạp hơn if (column == EP.y && Math.Abs(row - EP.x) == 1 && (p.row - EP.x == 0) && (Math.Abs(p.column - EP.y) == 1)) { player[turn % 2].AddEatenPiece(cw[EP.x, EP.y].Image, cw[EP.x, EP.y].cName); player[turn % 2].score += cw[EP.x, EP.y].cScore; cw[EP.x, EP.y].Dispose(); cw[EP.x, EP.y] = new AbstractChessPiece(EP.x, EP.y); instance.Controls.Add(cw[EP.x, EP.y]); movement += "x"; } } } //Quân cờ thực hiện bắt chốt qua đường hay không cũng xem như là không còn khả năng bắt chốt qua đường nữa EP = null; //Nếu chốt khởi đầu đi 2 bước if (cw[row, column].Get_name.Substring(1, 1) == "P" && (Math.Abs(row - p.row) == 2)) { EP = new Piece(row, column); } //Ghi chép vào nhật kí instance.lbHistory.Items.Add(movement); instance.lbHistory.TopIndex = instance.lbHistory.Items.Count - 1; }
public Board(int colorSel) //player selected color as input { int horizontal = 0; //for setting up the board keeping track of which horizonal line it is on int pieceNum = 0; //for tracking which piece in pieces is being setup on the board if (colorSel == 0) //sets all pieces to default locations and colors for white selection { while (horizontal < 8) { for (int i = 0; i < 8; i++) { if (i == 0) { if (horizontal == 0 || horizontal == 7) //rooks { chessPieces[pieceNum] = new Rook(pieceNum, 1); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 1 || horizontal == 6) //knights { chessPieces[pieceNum] = new Knight(pieceNum, 1); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 2 || horizontal == 5) //bishops { chessPieces[pieceNum] = new Bishop(pieceNum, 1); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 3) //queen { chessPieces[pieceNum] = new Queen(pieceNum, 1); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 4) //king { chessPieces[pieceNum] = new King(pieceNum, 1); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } } else if (i == 1) //pawns { chessPieces[pieceNum] = new Pawn(pieceNum, 1); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (i == 6) //pawns { chessPieces[pieceNum] = new Pawn(pieceNum, 0); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (i == 7) //rooks { if (horizontal == 0 || horizontal == 7) { chessPieces[pieceNum] = new Rook(pieceNum, 0); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 1 || horizontal == 6) //knights { chessPieces[pieceNum] = new Knight(pieceNum, 0); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 2 || horizontal == 5) //bishops { chessPieces[pieceNum] = new Bishop(pieceNum, 0); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 3) //queen { chessPieces[pieceNum] = new Queen(pieceNum, 0); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 4) //king { chessPieces[pieceNum] = new King(pieceNum, 0); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } } } horizontal++; } } else //sets all pieces to default locations and colors for black selection { while (horizontal < 8) { for (int i = 0; i < 8; i++) { if (i == 0) { if (horizontal == 0 || horizontal == 7) //rooks { chessPieces[pieceNum] = new Rook(pieceNum, 0); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 1 || horizontal == 6) //knights { chessPieces[pieceNum] = new Knight(pieceNum, 0); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 2 || horizontal == 5) //bishops { chessPieces[pieceNum] = new Bishop(pieceNum, 0); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 3) //queen { chessPieces[pieceNum] = new Queen(pieceNum, 0); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 4) //king { chessPieces[pieceNum] = new King(pieceNum, 0); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } } else if (i == 1) //pawns { chessPieces[pieceNum] = new Pawn(pieceNum, 0); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (i == 6) //pawns { chessPieces[pieceNum] = new Pawn(pieceNum, 1); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (i == 7) //rooks { if (horizontal == 0 || horizontal == 7) { chessPieces[pieceNum] = new Rook(pieceNum, 1); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 1 || horizontal == 6) //knights { chessPieces[pieceNum] = new Knight(pieceNum, 1); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 2 || horizontal == 5) //bishops { chessPieces[pieceNum] = new Bishop(pieceNum, 1); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 3) //queen { chessPieces[pieceNum] = new Queen(pieceNum, 1); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } else if (horizontal == 4) //king { chessPieces[pieceNum] = new King(pieceNum, 1); chessPieces[pieceNum].setLocation(horizontal, i); chessBoard[horizontal, i] = chessPieces[pieceNum]; pieceNum++; } } } horizontal++; } } }
public void load() //used to load a previously saved game { Controls.Clear(); var projectFolder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName; var file = Path.Combine(projectFolder, @"SaveData\save.txt"); Char[] loadData = File.ReadAllText(file).ToCharArray(); int i = 0; int j = 0; ChessPiece[] pcs = new ChessPiece[32]; while (i < loadData.Length) //starts from the first char in the save file which is the type of the first piece in the list //creates new pieces from the data in the save file and adds them to a list of chesspieces to be passed to the board creation { if (loadData[i] == '1') { i++; pcs[j] = new Pawn(j, loadData[i] - 48); i++; if (loadData[i] == '-') { pcs[j].setLocation(-1, -1); i += 4; } else { pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48); i += 2; } if (loadData[i] != '0') { ((Pawn)pcs[j]).setFirstMove(loadData[i] - 48); i++; } else { i++; } j++; } if (loadData[i] == '2') { i++; pcs[j] = new Knight(j, loadData[i] - 48); i++; if (loadData[i] == '-') { pcs[j].setLocation(-1, -1); i += 4; } else { pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48); i += 2; } j++; } if (loadData[i] == '3') { i++; pcs[j] = new Bishop(j, loadData[i] - 48); i++; if (loadData[i] == '-') { pcs[j].setLocation(-1, -1); i += 4; } else { pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48); i += 2; } j++; } if (loadData[i] == '4') { i++; pcs[j] = new Rook(j, loadData[i] - 48); i++; if (loadData[i] == '-') { pcs[j].setLocation(-1, -1); i += 4; } else { pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48); i += 2; } if (loadData[i] != '0') { ((Rook)pcs[j]).setFirstMove(loadData[i] - 48); i++; } else { i++; } j++; } if (loadData[i] == '5') { i++; pcs[j] = new Queen(j, loadData[i] - 48); i++; if (loadData[i] == '-') { pcs[j].setLocation(-1, -1); i += 4; } else { pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48); i += 2; } j++; } if (loadData[i] == '6') { i++; pcs[j] = new King(j, loadData[i] - 48); i++; if (loadData[i] == '-') { pcs[j].setLocation(-1, -1); i += 4; } else { pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48); i += 2; } if (loadData[i] != '0') { ((King)pcs[j]).setFirstMove(loadData[i] - 48); i++; } else { i++; } j++; } if (loadData[i] == ':')//: means the next char is the color selection { i++; colorSel = ((int)loadData[i] - 48); i++; turn = loadData[i] - 48; break; } } game = new Board(colorSel, pcs); initializeGame(); }
//Lê Thanh Tâm + Nguyễn Quang Thạch + Huỳnh Viết Thám //Khởi tạo vị trí quân cờ khi load lại game, đọc từ chuỗi 128 kí tự được lưu trên file từ trước public void BuildChessBoardFromString(string t) { RestartBoard(); string str1 = t.Substring(0, 128); t = t.Remove(0, str1.Length + 1); int n = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { string s = str1.Substring(n, 2); int c = 0; AbstractChessPiece p = new AbstractChessPiece(); if (s[1] == '0') { p = new AbstractChessPiece(i, j); } else if (s[0] == 'b') { c = 1; } switch (s[1]) { case 'R': p = new Rook(i, j, c); break; case 'N': p = new Knight(i, j, c); break; case 'B': p = new Bishop(i, j, c); break; case 'Q': p = new Queen(i, j, c); break; case 'K': p = new King(i, j, c); break; case 'P': { if (c == 0) { p = new WhitePawn(i, j); } else { p = new BlackPawn(i, j); } }; break; } if (cw[i, j] == null) { cw[i, j] = p; Controls.Add(cw[i, j]); } else if (cw[i, j].Get_name != p.Get_name) { cw[i, j].Dispose(); cw[i, j] = p; Controls.Add(cw[i, j]); } n += 2; } } string str2 = t.Substring(0, t.IndexOf("\n")); t = t.Remove(0, str2.Length + 1); Int32.TryParse(str2, out turn); turn = turn % 2; pbDevil.Location = pt[turn]; string str3 = t.Substring(0, t.IndexOf("\n")); t = t.Remove(0, str3.Length + 1); Int32.TryParse(str3, out player[0].time); string str4 = t.Substring(0, t.IndexOf("\n")); t = t.Remove(0, str4.Length + 1); Int32.TryParse(str4, out player[1].time); string str5 = t.Substring(0, t.IndexOf("\n")); t = t.Remove(0, str5.Length + 1); Int32.TryParse(str5, out player[0].score); string str6 = t.Substring(0, t.IndexOf("\n")); t = t.Remove(0, str6.Length + 1); Int32.TryParse(str6, out player[1].score); string str7 = t.Substring(0, t.IndexOf("\n")); player[0].eaten = str7; t = t.Remove(0, str7.Length + 1); string str8 = t.Substring(0, t.IndexOf("\n")); player[1].eaten = str8; t = t.Remove(0, str8.Length + 1); if (turn % 2 == 0) { Timer1.Start(); Timer2.Stop(); } else { Timer2.Start(); Timer1.Stop(); } cw[0, 0].GetAdvantage(); Player.LoadPlayerInfo(); }