|
Yechilishi:
Dastur quyidagicha bo'lishi mumkin
|
bet | 3/6 | Sana | 26.11.2023 | Hajmi | 224.1 Kb. | | #106015 |
Bog'liq algo 1 a Xizmat koʻrsatish tizimi , algort 4Yechilishi:
Dastur quyidagicha bo'lishi mumkin:
python
def is_valid_move(move):
# Tekshirish uchun to'g'ri formatda kiritilganligini tekshirish
if len(move) != 5 or move[2] != "-":
return False
# Harflar va sonlar to'g'ri kiritilganligini tekshirish
if not move[0].isalpha() or not move[1].isdigit() or \
not move[3].isalpha() or not move[4].isdigit():
return False
# Harflar A dan H gacha, sonlar 1 dan 8 gacha bo'lishini tekshirish
if move[0] < "A" or move[0] > "H" or int(move[1]) < 1 or int(move[1]) > 8 or \
move[3] < "A" or move[3] > "H" or int(move[4]) < 1 or int(move[4]) > 8:
return False
# Ot faqat gorizontal yoki vertikal ravishda yurishi mumkin
if move[0] != move[3] and move[1] != move[4]:
return False
# Yurish uchun bo'sh maydon yoki o'ziga tegishli figuralar yo'qmi tekshirish
# Bu joyda biz faqat ot uchun tekshirish qilamiz
if move[0] == move[3]:
row = move[0]
col_start = min(int(move[1]), int(move[4])) + 1
col_end = max(int(move[1]), int(move[4]))
for col in range(col_start, col_end):
if board[row + str(col)] != " ":
return False
else:
col = move[1]
row_start = min(move[0], move[3])
row_end = max(move[0], move[3])
for row in range(ord(row_start)+1, ord(row_end)):
if board[chr(row) + col] != " ":
return False
# Barcha tekshirishlardan o'tmagan bo'lsa yurish to'g'ri
return True
# Misol uchun shaxmat doskasini lug'at ko'rinishida yaratamiz
board = {
"A1": "R", "B1": "N", "C1": "B", "D1": "Q", "E1": "K", "F1": "B", "G1": "N", "H1": "R",
"A2": "P", "B2": "P", "C2": "P", "D2": "P", "E2": "P", "F2": "P", "G2": "P", "H2": "P",
|
| |