public Game() { //players declared blackSide = new Player("b"); whiteSide = new Player("w"); players = new List <Player>(); players.Add(blackSide); players.Add(whiteSide); //board declared board = new Board(); //pieces declared //KINGS bkk = new King("bkk", 3, 0, "b"); wkk = new King("wkk", 3, 7, "w"); //QUEENS bq = new Queen("bq", 4, 0, "b"); wq = new Queen("wq", 4, 7, "w"); //ROOKS br1 = new Rook("br1", 0, 0, "b"); br2 = new Rook("br2", 7, 0, "b"); wr1 = new Rook("wr1", 0, 7, "w"); wr2 = new Rook("wr2", 7, 7, "w"); //KNIGHTS bk1 = new Knight("bk1", 1, 0, "b"); bk2 = new Knight("bk2", 6, 0, "b"); wk1 = new Knight("wk1", 1, 7, "w"); wk2 = new Knight("wk2", 6, 7, "w"); //BISHOPS bb1 = new Bishop("bb1", 2, 0, "b"); bb2 = new Bishop("bb2", 5, 0, "b"); wb1 = new Bishop("wb1", 2, 7, "w"); wb2 = new Bishop("wb2", 5, 7, "w"); //BLACK PAWNS bp1 = new Pawn("bp1", 0, 1, "b"); bp2 = new Pawn("bp2", 1, 1, "b"); bp3 = new Pawn("bp3", 2, 1, "b"); bp4 = new Pawn("bp4", 3, 1, "b"); bp5 = new Pawn("bp5", 4, 1, "b"); bp6 = new Pawn("bp6", 5, 1, "b"); bp7 = new Pawn("bp7", 6, 1, "b"); bp8 = new Pawn("bp8", 7, 1, "b"); //WHITE PAWNS wp1 = new Pawn("wp1", 0, 6, "w"); wp2 = new Pawn("wp2", 1, 6, "w"); wp3 = new Pawn("wp3", 2, 6, "w"); wp4 = new Pawn("wp4", 3, 6, "w"); wp5 = new Pawn("wp5", 4, 6, "w"); wp6 = new Pawn("wp6", 5, 6, "w"); wp7 = new Pawn("wp7", 6, 6, "w"); wp8 = new Pawn("wp8", 7, 6, "w"); //adding piece to player list //KINGS blackSide.onBoard.Add(this.bkk); whiteSide.onBoard.Add(this.wkk); //QUEEN blackSide.onBoard.Add(this.bq); whiteSide.onBoard.Add(this.wq); //ROOKS blackSide.onBoard.Add(this.br1); blackSide.onBoard.Add(this.br2); whiteSide.onBoard.Add(this.wr1); whiteSide.onBoard.Add(this.wr2); //BISHOPS blackSide.onBoard.Add(this.bb1); blackSide.onBoard.Add(this.bb2); whiteSide.onBoard.Add(this.wb1); whiteSide.onBoard.Add(this.wb2); //KNIGHTS blackSide.onBoard.Add(this.bk1); blackSide.onBoard.Add(this.bk2); whiteSide.onBoard.Add(this.wk1); whiteSide.onBoard.Add(this.wk2); //BLACK PAWNS blackSide.onBoard.Add(this.bp1); blackSide.onBoard.Add(this.bp2); blackSide.onBoard.Add(this.bp3); blackSide.onBoard.Add(this.bp4); blackSide.onBoard.Add(this.bp5); blackSide.onBoard.Add(this.bp6); blackSide.onBoard.Add(this.bp7); blackSide.onBoard.Add(this.bp8); //WHITE PAWNS whiteSide.onBoard.Add(this.wp1); whiteSide.onBoard.Add(this.wp2); whiteSide.onBoard.Add(this.wp3); whiteSide.onBoard.Add(this.wp4); whiteSide.onBoard.Add(this.wp5); whiteSide.onBoard.Add(this.wp6); whiteSide.onBoard.Add(this.wp7); whiteSide.onBoard.Add(this.wp8); //pos assignment //KINGS this.board.game_board[3, 0] = this.bkk; this.board.game_board[3, 7] = this.wkk; //QUEEN this.board.game_board[4, 0] = this.bq; this.board.game_board[4, 7] = this.wq; //ROOKS this.board.game_board[0, 0] = this.br1; this.board.game_board[7, 0] = this.br2; this.board.game_board[0, 7] = this.wr1; this.board.game_board[7, 7] = this.wr2; //KNIGHTS this.board.game_board[1, 0] = this.bk1; this.board.game_board[6, 0] = this.bk2; this.board.game_board[1, 7] = this.wk1; this.board.game_board[6, 7] = this.wk2; //BISHOPS this.board.game_board[2, 0] = this.bb1; this.board.game_board[5, 0] = this.bb2; this.board.game_board[2, 7] = this.wb1; this.board.game_board[5, 7] = this.wb2; //BLACK PAWN this.board.game_board[0, 1] = this.bp1; this.board.game_board[1, 1] = this.bp2; this.board.game_board[2, 1] = this.bp3; this.board.game_board[3, 1] = this.bp4; this.board.game_board[4, 1] = this.bp5; this.board.game_board[5, 1] = this.bp6; this.board.game_board[6, 1] = this.bp7; this.board.game_board[7, 1] = this.bp8; //WHITE PAWN this.board.game_board[0, 6] = this.wp1; this.board.game_board[1, 6] = this.wp2; this.board.game_board[2, 6] = this.wp3; this.board.game_board[3, 6] = this.wp4; this.board.game_board[4, 6] = this.wp5; this.board.game_board[5, 6] = this.wp6; this.board.game_board[6, 6] = this.wp7; this.board.game_board[7, 6] = this.wp8; board.updateOneDAryAndList(); }
public void updateKingList() { int[][] moves = new int[][] { new int[] { x, y + 1 }, new int[] { x, y - 1 }, new int[] { x + 1, y + 1 }, new int[] { x - 1, y - 1 }, new int[] { x - 1, y + 1 }, new int[] { x + 1, y - 1 }, new int[] { x - 1, y }, new int[] { x + 1, y } }; Player enemy = new Player("temp"); King enemyKing = new King("temp", 0, 0, "none"); if (this.color == "b") { enemy = Program.game.whiteSide; enemyKing = Program.game.wkk; } else if (this.color == "w") { enemy = Program.game.blackSide; enemyKing = Program.game.bkk; } //king checking each other causing stack over flow; foreach (int[] ary in moves) { //Console.WriteLine("before the loop: " + ary[0]+", "+ary[1]); if (ary[0] >= 0 && ary[1] >= 0 && ary[0] <= 7 && ary[1] <= 7) { bool valid = true; foreach (Piece p in enemy.onBoard) { if (p.Id != "bkk" && p.Id != "wkk") { //here is the problem if (p.checkMove(ary[0], ary[1])) { valid = false; } } else if (p.Id == "bkk" || p.Id == "wkk") { foreach (int[] enemeyAry in enemyKing.avaliableMoves) { if (enemeyAry == ary) { valid = false; } } } } //if the piece next to the king is a enmey piece but cap but caping it would result in a check //the move should not be allowed if ((valid && Program.game.board.game_board[ary[0], ary[1]] == null) || (valid && Program.game.board.game_board[ary[0], ary[1]].color != this.color)) { avaliableMoves.Add(ary); } } } }