private void spawn_all_chess_pieces() { int i; chess_pieces = new List <GameObject>(); chess_pieces_on_board = new chess_piece[8, 8]; en_passant_move = new int[2] { -1, -1 }; // Spawn white chess pieces //King spawn_chess_piece(0, 4, 0, true); //Queen spawn_chess_piece(1, 3, 0, true); //Rooks spawn_chess_piece(2, 0, 0, true); spawn_chess_piece(2, 7, 0, true); //Bishops spawn_chess_piece(3, 2, 0, true); spawn_chess_piece(3, 5, 0, true); //Knights spawn_chess_piece(4, 1, 0, true); spawn_chess_piece(4, 6, 0, true); //Pawns for (i = 0; i < 8; i++) { spawn_chess_piece(5, i, 1, true); } // Spawn blackchess pieces //King spawn_chess_piece(6, 4, 7, false); //Queen spawn_chess_piece(7, 3, 7, false); //Rooks spawn_chess_piece(8, 0, 7, false); spawn_chess_piece(8, 7, 7, false); //Bishops spawn_chess_piece(9, 2, 7, false); spawn_chess_piece(9, 5, 7, false); //Knights spawn_chess_piece(10, 1, 7, false); spawn_chess_piece(10, 6, 7, false); //Pawns for (i = 0; i < 8; i++) { spawn_chess_piece(11, i, 6, false); } }
private void move_chess_piece(int x, int y) { if (allowed_moves[x, y]) { chess_piece help = chess_pieces_on_board[x, y]; if (help != null && help.is_white != is_white_turn) { //Capture a piece //If this is a king if (help.GetType() == typeof(king)) { endgame(); return; } chess_pieces.Remove(help.gameObject); Destroy(help.gameObject); } if (x == en_passant_move[0] && y == en_passant_move[1]) { if (is_white_turn) { help = chess_pieces_on_board[x, y - 1]; } else { help = chess_pieces_on_board[x, y + 1]; } chess_pieces.Remove(help.gameObject); Destroy(help.gameObject); } en_passant_move[0] = en_passant_move[1] = -1; if (selected_chess_piece.GetType() == typeof(pawn)) { if (y == 7) { chess_pieces.Remove(selected_chess_piece.gameObject); Destroy(selected_chess_piece.gameObject); spawn_chess_piece(1, x, y, true); selected_chess_piece = chess_pieces_on_board[x, y]; } if (y == 0) { chess_pieces.Remove(selected_chess_piece.gameObject); Destroy(selected_chess_piece.gameObject); spawn_chess_piece(7, x, y, false); selected_chess_piece = chess_pieces_on_board[x, y]; } if (selected_chess_piece.current_y == 1 && y == 3) { en_passant_move[0] = x; en_passant_move[1] = y - 1; } else if (selected_chess_piece.current_y == 6 && y == 4) { en_passant_move[0] = x; en_passant_move[1] = y + 1; } } if (selected_chess_piece.GetType() == typeof(king)) { if (is_white_turn && x == 2 && y == 0) { help = chess_pieces_on_board[0, 0]; chess_pieces_on_board[0, 0] = null; help.transform.position = get_tile_center(3, 0); help.set_position(3, 0); chess_pieces_on_board[3, 0] = help; } else if (is_white_turn && x == 6 && y == 0) { help = chess_pieces_on_board[7, 0]; chess_pieces_on_board[7, 0] = null; help.transform.position = get_tile_center(5, 0); help.set_position(5, 0); chess_pieces_on_board[5, 0] = help; } else if (!is_white_turn && x == 2 && y == 7) { help = chess_pieces_on_board[0, 7]; chess_pieces_on_board[0, 7] = null; help.transform.position = get_tile_center(3, 7); help.set_position(3, 7); chess_pieces_on_board[3, 7] = help; } else if (!is_white_turn && x == 6 && y == 7) { help = chess_pieces_on_board[7, 7]; chess_pieces_on_board[7, 7] = null; help.transform.position = get_tile_center(5, 7); help.set_position(5, 7); chess_pieces_on_board[5, 7] = help; } } chess_pieces_on_board[selected_chess_piece.current_x, selected_chess_piece.current_y] = null; selected_chess_piece.transform.position = get_tile_center(x, y); selected_chess_piece.set_position(x, y); chess_pieces_on_board[x, y] = selected_chess_piece; if (is_white_turn) { Camera.main.transform.SetPositionAndRotation(new Vector3(4, 5, 9.2f), Quaternion.Euler(50, 180, 0)); } else { Camera.main.transform.SetPositionAndRotation(new Vector3(4, 5, -1.2f), Quaternion.Euler(50, 0, 0)); } is_white_turn = !is_white_turn; } board_highlights.instance.hide_highlight(); selected_chess_piece = null; }