public override bool check(int x, int y, ref board b) { bool isMove; { if ((Math.Abs(this.x - x) == 1 || Math.Abs(this.y - y) == 1) && Math.Abs(this.x - x) + Math.Abs(this.y - y) != 0) { if (b.b[y, x] == null) { isMove = true; } else { isMove = team ? b.b[y, x][0] == 'B' : b.b[y, x][0] == 'W'; } } else { isMove = false; } } return(isMove); }
static bool Move_BQueen(square start_sq, square end_sq, int start_ind, int end_ind, board classic) { bool success = true; List <Char> Let_list = new List <Char> { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' }; int let_diff = Let_list.IndexOf(end_sq.Position[0]) - Let_list.IndexOf(start_sq.Position[0]); //calc difference in number(row) int num_diff = Convert.ToInt32(end_sq.Position[1]) - Convert.ToInt32(start_sq.Position[1]); //calc difference in letter(col) if (end_sq.IsEmpty() || end_sq.IsWhite()) { if (let_diff == num_diff) //if difference between letter(col) and row(num) are equal, end is diag from start (pos slope) { //used for up right and down left movement, (pos=pos, neg=neg) if (num_diff > 0) //up right { for (int i = start_ind - 7; i != end_ind; i -= 7) { if (classic.total_board[i].IsEmpty() == false) { return(false); } } } if (num_diff < 0)//down left { for (int i = start_ind + 7; i != end_ind; i += 7) { if (classic.total_board[i].IsEmpty() == false) { return(false); } } } } else if (let_diff == -1 * num_diff) //if diff between letter(col) and row(num) are opposites, end is diag from start (neg slope) { if (num_diff < 0) //down right { for (int i = start_ind + 9; i != end_ind; i += 9) { if (classic.total_board[i].IsEmpty() == false) { return(false); } } } if (num_diff > 0)//up left { for (int i = start_ind - 9; i != end_ind; i -= 9) { if (classic.total_board[i].IsEmpty() == false) { return(false); } } } } else if ((start_sq.Position[0] == end_sq.Position[0]) && (end_ind - start_ind < 0))//if moving up { for (int i = start_ind - 8; i != end_ind; i -= 8) { if (classic.total_board[i].IsEmpty() == false) { return(false); } } } else if ((start_sq.Position[0] == end_sq.Position[0]) && (end_ind - start_ind > 0))//if moving down { for (int i = start_ind + 8; i != end_ind; i += 8) { if (classic.total_board[i].IsEmpty() == false) { return(false); } //if not empty } } else if ((start_sq.Position[1] == end_sq.Position[1]) && (start_ind > end_ind)) // if moving left { for (int i = start_ind - 1; i != end_ind; i -= 1) { if (classic.total_board[i].IsEmpty() == false) { return(false); } } } else if ((start_sq.Position[1] == end_sq.Position[1]) && (end_ind > start_ind)) // if moving right { for (int i = start_ind + 1; i != end_ind; i += 1) { if (classic.total_board[i].IsEmpty() == false) { return(false); } } } else { return(false); } } else { return(false); } end_sq.State = "BQ"; start_sq.State = "__"; return(success); }
static bool Move_BRook(square start_sq, square end_sq, int start_ind, int end_ind, board classic)//move Black Rook { bool success = true; if (end_sq.IsWhite() || end_sq.IsEmpty()) { if ((start_sq.Position[0] == end_sq.Position[0]) && (end_ind - start_ind < 0))//if moving up { for (int i = start_ind - 8; i != end_ind; i -= 8) { if (classic.total_board[i].IsEmpty() == false) { return(false); } } } if ((start_sq.Position[0] == end_sq.Position[0]) && (end_ind - start_ind > 0))//if moving down { for (int i = start_ind + 8; i != end_ind; i += 8) { if (classic.total_board[i].IsEmpty() == false) { return(false); } } } if ((start_sq.Position[1] == end_sq.Position[1]) && (start_ind > end_ind)) // if moving left { for (int i = start_ind - 1; i != end_ind; i -= 1) { if (classic.total_board[i].IsEmpty() == false) { return(false); } } } if ((start_sq.Position[1] == end_sq.Position[1]) && (end_ind > start_ind)) // if moving right { for (int i = start_ind + 1; i != end_ind; i += 1) { if (classic.total_board[i].IsEmpty() == false) { return(false); } } } } else { return(false); } end_sq.State = "BR"; start_sq.State = "__"; return(success); }
static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.Black; Console.Clear(); string user_move = ""; string start_str = ""; string end_str = ""; bool white_turn = true; bool move_bool = false; string Help = "White starts first, to make a move, enter the square you would like to move your piece from,\nfollowed by the square you would like to move your piece to. For Example, E2E3, moves your E pawn up one square.To show this message again, enter HELP \nTo leave the program, enter EXIT \nand remember, always have fun!!"; board Classic_Test = Classic_board(); Console.WriteLine("Welcome to C# Chess!!"); Console.WriteLine(Help); while (user_move != "EXIT") { Classic_Test.Display(); bool Black_Win = true; bool White_Win = true; foreach (square elem in Classic_Test.total_board)//check for promotions or white/black wins { if (elem.GetState() == "WK") { Black_Win = false;//and this } if (elem.GetState() == "BK") { White_Win = false;//do something with this } if (elem.State == "WP" && elem.Position[1] == '8') { Promote(elem); Classic_Test.Display(); } if (elem.GetState() == "BP" && elem.Position[1] == '1') { Promote(elem); Classic_Test.Display(); } } if (Black_Win == true) { Console.WriteLine("\n Congratulations Black, You captured the White King and are victorius!!!"); Console.ReadLine(); break; } if (White_Win == true) { Console.WriteLine("\n Congratulations White, You captured the Black King and are victorius!!!"); Console.ReadLine(); break; } if (white_turn == true) { Console.Write("Make your move, White: "); } else { Console.Write("Make your move, Black: "); } user_move = Console.ReadLine().ToUpper(); if (user_move == "EXIT") { break; } if (user_move == "HELP") { Console.WriteLine(Help); continue; } if (user_move.Length != 4) { Console.WriteLine("Input should only be 4 charachters long, try again!"); continue; } start_str = user_move.Substring(0, 2); end_str = user_move.Substring(2, 2); int start_ind, end_ind; try { start_ind = Classic_Test.Occ_dict[start_str]; end_ind = Classic_Test.Occ_dict[end_str]; } catch (KeyNotFoundException ex) { Console.WriteLine("Incorrect input format, please try again!"); continue; } square start_sq = Classic_Test.total_board[start_ind];//sq has pos(A4) and state(__/WP/BN) square end_sq = Classic_Test.total_board[end_ind]; if ((start_sq.State[0] == 'W' && white_turn == true) || (start_sq.State[0] == 'B' && white_turn == false)) { if (start_sq.State == "WP") { move_bool = Move_WPawn(start_sq, end_sq, start_ind, end_ind); } else if (start_sq.State == "BP") { move_bool = Move_BPawn(start_sq, end_sq, start_ind, end_ind); } else if (start_sq.State == "WN") { move_bool = Move_WKnight(start_sq, end_sq, start_ind, end_ind); } else if (start_sq.State == "BN") { move_bool = Move_BKnight(start_sq, end_sq, start_ind, end_ind); } else if (start_sq.State == "WR") { move_bool = Move_WRook(start_sq, end_sq, start_ind, end_ind, Classic_Test); } else if (start_sq.State == "BR") { move_bool = Move_BRook(start_sq, end_sq, start_ind, end_ind, Classic_Test); } else if (start_sq.State == "WK") { move_bool = Move_WKing(start_sq, end_sq, start_ind, end_ind); } else if (start_sq.State == "BK") { move_bool = Move_BKing(start_sq, end_sq, start_ind, end_ind); } else if (start_sq.State == "WB") { move_bool = Move_WBishop(start_sq, end_sq, start_ind, end_ind, Classic_Test); } else if (start_sq.State == "BB") { move_bool = Move_BBishop(start_sq, end_sq, start_ind, end_ind, Classic_Test); } else if (start_sq.State == "WQ") { move_bool = Move_WQueen(start_sq, end_sq, start_ind, end_ind, Classic_Test); } else if (start_sq.State == "BQ") { move_bool = Move_BQueen(start_sq, end_sq, start_ind, end_ind, Classic_Test); } else { move_bool = false; } } else { move_bool = false; } if (move_bool == false) { Console.WriteLine("Invalid Move, Please try again"); continue; } else//if move was correct and made { if (white_turn == true)//and it was whites move { white_turn = false;//switch to blacks move } else { white_turn = true; } //else it was just blacks move, switch to whites move } }//end of while } //end of main
static board Classic_board() { board Classic = new board(8); square A1 = new square("A1", "WR"); square B1 = new square("B1", "WN"); square C1 = new square("C1", "WB"); square D1 = new square("D1", "WQ"); square E1 = new square("E1", "WK"); square F1 = new square("F1", "WB"); square G1 = new square("G1", "WN"); square H1 = new square("H1", "WR"); square A2 = new square("A2", "WP"); square B2 = new square("B2", "WP"); square C2 = new square("C2", "WP"); square D2 = new square("D2", "WP"); square E2 = new square("E2", "WP"); square F2 = new square("F2", "WP"); square G2 = new square("G2", "WP"); square H2 = new square("H2", "WP"); square A3 = new square("A3", "__"); square B3 = new square("B3", "__"); square C3 = new square("C3", "__"); square D3 = new square("D3", "__"); square E3 = new square("E3", "__"); square F3 = new square("F3", "__"); square G3 = new square("G3", "__"); square H3 = new square("H3", "__"); square A4 = new square("A4", "__"); square B4 = new square("B4", "__"); square C4 = new square("C4", "__"); square D4 = new square("D4", "__"); square E4 = new square("E4", "__"); square F4 = new square("F4", "__"); square G4 = new square("G4", "__"); square H4 = new square("H4", "__"); square A5 = new square("A5", "__"); square B5 = new square("B5", "__"); square C5 = new square("C5", "__"); square D5 = new square("D5", "__"); square E5 = new square("E5", "__"); square F5 = new square("F5", "__"); square G5 = new square("G5", "__"); square H5 = new square("H5", "__"); square A6 = new square("A6", "__"); square B6 = new square("B6", "__"); square C6 = new square("C6", "__"); square D6 = new square("D6", "__"); square E6 = new square("E6", "__"); square F6 = new square("F6", "__"); square G6 = new square("G6", "__"); square H6 = new square("H6", "__"); square A7 = new square("A7", "BP"); square B7 = new square("B7", "BP"); square C7 = new square("C7", "BP"); square D7 = new square("D7", "BP"); square E7 = new square("E7", "BP"); square F7 = new square("F7", "BP"); square G7 = new square("G7", "BP"); square H7 = new square("H7", "BP"); square A8 = new square("A8", "BR"); square B8 = new square("B8", "BN"); square C8 = new square("C8", "BB"); square D8 = new square("D8", "BQ"); square E8 = new square("E8", "BK"); square F8 = new square("F8", "BB"); square G8 = new square("G8", "BN"); square H8 = new square("H8", "BR"); Classic.Add_sq(A8); Classic.Add_sq(B8); Classic.Add_sq(C8); Classic.Add_sq(D8); Classic.Add_sq(E8); Classic.Add_sq(F8); Classic.Add_sq(G8); Classic.Add_sq(H8); Classic.Add_sq(A7); Classic.Add_sq(B7); Classic.Add_sq(C7); Classic.Add_sq(D7); Classic.Add_sq(E7); Classic.Add_sq(F7); Classic.Add_sq(G7); Classic.Add_sq(H7); Classic.Add_sq(A6); Classic.Add_sq(B6); Classic.Add_sq(C6); Classic.Add_sq(D6); Classic.Add_sq(E6); Classic.Add_sq(F6); Classic.Add_sq(G6); Classic.Add_sq(H6); Classic.Add_sq(A5); Classic.Add_sq(B5); Classic.Add_sq(C5); Classic.Add_sq(D5); Classic.Add_sq(E5); Classic.Add_sq(F5); Classic.Add_sq(G5); Classic.Add_sq(H5); Classic.Add_sq(A4); Classic.Add_sq(B4); Classic.Add_sq(C4); Classic.Add_sq(D4); Classic.Add_sq(E4); Classic.Add_sq(F4); Classic.Add_sq(G4); Classic.Add_sq(H4); Classic.Add_sq(A3); Classic.Add_sq(B3); Classic.Add_sq(C3); Classic.Add_sq(D3); Classic.Add_sq(E3); Classic.Add_sq(F3); Classic.Add_sq(G3); Classic.Add_sq(H3); Classic.Add_sq(A2); Classic.Add_sq(B2); Classic.Add_sq(C2); Classic.Add_sq(D2); Classic.Add_sq(E2); Classic.Add_sq(F2); Classic.Add_sq(G2); Classic.Add_sq(H2); Classic.Add_sq(A1); Classic.Add_sq(B1); Classic.Add_sq(C1); Classic.Add_sq(D1); Classic.Add_sq(E1); Classic.Add_sq(F1); Classic.Add_sq(G1); Classic.Add_sq(H1); Classic.Add_Mpos("A8", 0); Classic.Add_Mpos("B8", 1); Classic.Add_Mpos("C8", 2); Classic.Add_Mpos("D8", 3); Classic.Add_Mpos("E8", 4); Classic.Add_Mpos("F8", 5); Classic.Add_Mpos("G8", 6); Classic.Add_Mpos("H8", 7); Classic.Add_Mpos("A7", 8); Classic.Add_Mpos("B7", 9); Classic.Add_Mpos("C7", 10); Classic.Add_Mpos("D7", 11); Classic.Add_Mpos("E7", 12); Classic.Add_Mpos("F7", 13); Classic.Add_Mpos("G7", 14); Classic.Add_Mpos("H7", 15); Classic.Add_Mpos("A6", 16); Classic.Add_Mpos("B6", 17); Classic.Add_Mpos("C6", 18); Classic.Add_Mpos("D6", 19); Classic.Add_Mpos("E6", 20); Classic.Add_Mpos("F6", 21); Classic.Add_Mpos("G6", 22); Classic.Add_Mpos("H6", 23); Classic.Add_Mpos("A5", 24); Classic.Add_Mpos("B5", 25); Classic.Add_Mpos("C5", 26); Classic.Add_Mpos("D5", 27); Classic.Add_Mpos("E5", 28); Classic.Add_Mpos("F5", 29); Classic.Add_Mpos("G5", 30); Classic.Add_Mpos("H5", 31); Classic.Add_Mpos("A4", 32); Classic.Add_Mpos("B4", 33); Classic.Add_Mpos("C4", 34); Classic.Add_Mpos("D4", 35); Classic.Add_Mpos("E4", 36); Classic.Add_Mpos("F4", 37); Classic.Add_Mpos("G4", 38); Classic.Add_Mpos("H4", 39); Classic.Add_Mpos("A3", 40); Classic.Add_Mpos("B3", 41); Classic.Add_Mpos("C3", 42); Classic.Add_Mpos("D3", 43); Classic.Add_Mpos("E3", 44); Classic.Add_Mpos("F3", 45); Classic.Add_Mpos("G3", 46); Classic.Add_Mpos("H3", 47); Classic.Add_Mpos("A2", 48); Classic.Add_Mpos("B2", 49); Classic.Add_Mpos("C2", 50); Classic.Add_Mpos("D2", 51); Classic.Add_Mpos("E2", 52); Classic.Add_Mpos("F2", 53); Classic.Add_Mpos("G2", 54); Classic.Add_Mpos("H2", 55); Classic.Add_Mpos("A1", 56); Classic.Add_Mpos("B1", 57); Classic.Add_Mpos("C1", 58); Classic.Add_Mpos("D1", 59); Classic.Add_Mpos("E1", 60); Classic.Add_Mpos("F1", 61); Classic.Add_Mpos("G1", 62); Classic.Add_Mpos("H1", 63); return(Classic); }
public override bool check(int x, int y, ref board b) { bool isMove, isTwo = false; if (team) { isMove = x == this.x && y < this.y; if (!moved) { isMove &= this.y - y <= 2; isTwo = this.y - y == 2 ? true : false; } else { isMove &= this.y - y == 1; } } else { isMove = x == this.x && y > this.y; if (!moved && isMove) { isMove &= y - this.y <= 2; isTwo = y - this.y == 2 ? true : false; } else { isMove &= y - this.y == 1; } } bool isEat; if (team) { isEat = this.y - y == 1 && Math.Abs(this.x - x) == 1 && b.b[y, x] != null && b.b[y, x][0] == 'B'; } else { isEat = y - this.y == 1 && Math.Abs(this.x - x) == 1 && b.b[y, x] != null && b.b[y, x][0] == 'W'; } bool isSpace = false; if (!isEat && isMove) { isSpace = b.b[y, x] == null; if (isTwo && isSpace) { if (team) { isSpace &= b.b[this.y - 1, x] == null; } else { isSpace &= b.b[this.y + 1, x] == null; } } } return(isEat || isMove && isSpace); }
static void Main(string[] args) { board b = new board(); b.print(); Console.WriteLine("Белые ходят первые"); pawn[,] pawns = new pawn[2, 8]; for (int i = 0; i < 8; ++i) { pawns[0, i] = new pawn(i, 6, true); pawns[1, i] = new pawn(i, 1, false); } rook[,] rooks = new rook[2, 2]; for (int i = 0; i < 8; i += 7) { rooks[0, i % 2] = new rook(i, 7, true); rooks[1, i % 2] = new rook(i, 0, false); } horse[,] horses = new horse[2, 2]; horses[0, 0] = new horse(1, 7, true); horses[0, 1] = new horse(6, 7, true); horses[1, 0] = new horse(1, 0, false); horses[1, 1] = new horse(6, 0, false); bishop[,] bishops = new bishop[2, 2]; bishops[0, 0] = new bishop(2, 7, true); bishops[0, 1] = new bishop(5, 7, true); bishops[1, 0] = new bishop(2, 0, false); bishops[1, 1] = new bishop(5, 0, false); queen[,] queens = new queen[2, 1]; queens[0, 0] = new queen(4, 7, true); queens[1, 0] = new queen(3, 0, false); king[,] kings = new king[2, 1]; kings[0, 0] = new king(3, 7, true); kings[1, 0] = new king(4, 0, false); bool turn = false; string move = Console.ReadLine(); move = move.ToLower(); while (move != "exit") { string[] splittedMove = move.Split('-'); if (splittedMove[0] == "" || splittedMove.Length != 2) { Console.WriteLine("Ход не введён или введён не верно, используйте формат \"e2-e4\""); } else { bool check = splittedMove[0].Length == 2 && splittedMove[1].Length == 2; check &= splittedMove[0][0] >= 'a' && splittedMove[0][0] <= 'h' && splittedMove[0][1] >= '1' && splittedMove[0][1] <= '8'; check &= splittedMove[1][0] >= 'a' && splittedMove[1][0] <= 'h' && splittedMove[1][1] >= '1' && splittedMove[1][1] <= '8'; if (check) { int startX, startY, endX, endY; startX = splittedMove[0][0] - 'a'; startY = '8' - splittedMove[0][1]; endX = splittedMove[1][0] - 'a'; endY = '8' - splittedMove[1][1]; if (b.b[startY, startX] == null) { Console.WriteLine("Начальная позиция пуста"); } else { if (b.b[startY, startX][0] == 'W' ^ turn) { switch (b.b[startY, startX]) { case "WP": { for (int i = 0; i < 8; ++i) { if (pawns[0, i].isIt(startX, startY) && pawns[0, i].isAlive) { int r = pawns[0, i].move(endX, endY, ref b); if (r == 0) { turn = !turn; } break; } } break; } case "BP": { for (int i = 0; i < 8; ++i) { if (pawns[1, i].isIt(startX, startY) && pawns[1, i].isAlive) { int r = pawns[1, i].move(endX, endY, ref b); if (r == 0) { turn = !turn; } break; } } break; } case "WR": { for (int i = 0; i < 2; ++i) { if (rooks[0, i].isIt(startX, startY) && rooks[0, i].isAlive) { int r = rooks[0, i].move(endX, endY, ref b); if (r == 0) { turn = !turn; } break; } } break; } case "BR": { for (int i = 0; i < 2; ++i) { if (rooks[1, i].isIt(startX, startY) && rooks[1, i].isAlive) { int r = rooks[1, i].move(endX, endY, ref b); if (r == 0) { turn = !turn; } break; } } break; } case "WH": { for (int i = 0; i < 2; ++i) { if (horses[0, i].isIt(startX, startY) && horses[0, i].isAlive) { int r = horses[0, i].move(endX, endY, ref b); if (r == 0) { turn = !turn; } break; } } break; } case "BH": { for (int i = 0; i < 2; ++i) { if (horses[1, i].isIt(startX, startY) && horses[1, i].isAlive) { int r = horses[1, i].move(endX, endY, ref b); if (r == 0) { turn = !turn; } break; } } break; } case "WB": { for (int i = 0; i < 2; ++i) { if (bishops[0, i].isIt(startX, startY) && bishops[0, i].isAlive) { int r = bishops[0, i].move(endX, endY, ref b); if (r == 0) { turn = !turn; } break; } } break; } case "BB": { for (int i = 0; i < 2; ++i) { if (bishops[1, i].isIt(startX, startY) && bishops[1, i].isAlive) { int r = bishops[1, i].move(endX, endY, ref b); if (r == 0) { turn = !turn; } break; } } break; } case "WK": { if (kings[0, 0].isIt(startX, startY) && kings[0, 0].isAlive) { int r = kings[0, 0].move(endX, endY, ref b); if (r == 0) { turn = !turn; } } break; } case "BK": { if (kings[1, 0].isIt(startX, startY) && kings[1, 0].isAlive) { int r = kings[1, 0].move(endX, endY, ref b); if (r == 0) { turn = !turn; } } break; } case "WQ": { if (queens[0, 0].isIt(startX, startY) && queens[0, 0].isAlive) { int r = queens[0, 0].move(endX, endY, ref b); if (r == 0) { turn = !turn; } } break; } case "BQ": { if (queens[1, 0].isIt(startX, startY) && queens[1, 0].isAlive) { int r = queens[1, 0].move(endX, endY, ref b); if (r == 0) { turn = !turn; } } break; } } } else { Console.WriteLine("Сейчас ход противоположного игрока"); } } } else { Console.WriteLine("Ход введён не верно, используйте формат \"e2-e4\""); } } move = Console.ReadLine(); move = move.ToLower(); } Console.WriteLine("Игра окончена. Нажмите любую клавишу для выхода"); Console.ReadKey(); }
public abstract int move(int x, int y, ref board b);
public abstract bool check(int x, int y, ref board b);
public override bool check(int x, int y, ref board b) { bool isMove = true; { if (this.x == x && this.y != y) { int t = this.y; t += this.y > y ? -1 : 1; while (t != y) { if (b.b[t, x] != null) { isMove = false; break; } t += this.y > y ? -1 : 1; } if (t == y) { isMove = b.b[t, x] == null; if (!isMove) { if (team) { isMove |= b.b[t, x][0] == 'B'; } else { isMove |= b.b[t, x][0] == 'W'; } } } } else if (this.x != x && this.y == y) { int t = this.x; t += this.x > x ? -1 : 1; while (t != x) { if (b.b[y, t] != null) { isMove = false; break; } t += this.x > x ? -1 : 1; } if (t == x) { isMove = b.b[y, t] == null; if (!isMove) { isMove = team ? b.b[t, x][0] == 'B' : b.b[t, x][0] == 'W'; } } } else { isMove = false; } } return(isMove); }