Bài tập lớn môn trí tuệ nhân tạo
Họ và tên: Lê Văn Dũng
Lớp: 21CT1
Đề bài: Xây dựng ứng dụng đánh Cờ vua với máy tính
import pygame
# Khởi tạo module pygame
pygame.init()
#kết nói drive để lấy hình ảnh các quân cờ và bàn cơ
from google.colab import drive
drive.mount('/content/gdrive')
#Xây dựng bàn cờ và hình ảnh các quân cờ
WIDTH = 800
HEIGHT = 800
screen = pygame.display.set_mode([WIDTH, HEIGHT])
pygame.display.set_caption('Trò chơi Cờ vua 2 người chơi')
font = pygame.font.Font('freesansbold.ttf', 20)
medium_font = pygame.font.Font('freesansbold.ttf', 40)
big_font = pygame.font.Font('freesansbold.ttf', 50) timer =
pygame.time.Clock() fps = 60
white_pieces = ['xe', 'mã', 'tượng', 'vua', 'hậu', 'tượng', 'mã', 'xe',
'tốt', 'tốt', 'tốt', 'tốt', 'tốt', 'tốt', 'tốt', 'tốt']
white_locations = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0),(0, 1), (1,
1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]
black_pieces = ['xe', 'mã', 'tượng', 'vua', 'hậu', 'tượng', 'mã', 'xe',
'tốt', 'tốt', 'tốt', 'tốt', 'tốt', 'tốt', 'tốt', 'tốt']
black_locations = [(0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7),(0, 6), (1,
6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7,
6)] captured_pieces_white = []
captured_pieces_black = [] turn_step = 0
selection = 100
valid_moves = []
#hình ảnh đc tải lên thư mục MyDrive
hinh_anh_hau_trang = pygame.image.load('/content/gdrive/MyDrive/wk.jpg')
hinh_anh_hau_trang = pygame.transform.scale(hinh_anh_hau_trang, (80, 80))
hinh_anh_hau_trang_nho = pygame.transform.scale(hinh_anh_hau_trang, (45,
45))
hinh_anh_vua_trang = pygame.image.load('/content/gdrive/MyDrive/vuaden.jpg')
hinh_anh_vua_trang = pygame.transform.scale(hinh_anh_vua_trang, (80, 80))
hinh_anh_vua_trang_nho = pygame.transform.scale(hinh_anh_vua_trang, (45,
45))
hinh_anh_xe_trang = pygame.image.load('/content/gdrive/MyDrive/xeden.jpg')
hinh_anh_xe_trang = pygame.transform.scale(hinh_anh_xe_trang, (80, 80))
hinh_anh_xe_trang_nho = pygame.transform.scale(hinh_anh_xe_trang, (45, 45))
hinh_anh_tuong_trang =
pygame.image.load('/content/gdrive/MyDrive/tinhden.jpg')
hinh_anh_tuong_trang = pygame.transform.scale(hinh_anh_tuong_trang, (80, 80))
hinh_anh_tuong_trang_nho = pygame.transform.scale(hinh_anh_tuong_trang, (45,
45))
hinh_anh_ma_trang = pygame.image.load('/content/gdrive/MyDrive/maden.jpg')
hinh_anh_ma_trang = pygame.transform.scale(hinh_anh_ma_trang, (80, 80))
hinh_anh_ma_trang_nho = pygame.transform.scale(hinh_anh_ma_trang, (45, 45))
hinh_anh_tot_trang = pygame.image.load('/content/gdrive/MyDrive/totden.jpg')
hinh_anh_tot_trang = pygame.transform.scale(hinh_anh_tot_trang, (65, 65))
hinh_anh_tot_trang_nho = pygame.transform.scale(hinh_anh_tot_trang, (45, 45))
hinh_anh_hau_den = pygame.image.load('/content/gdrive/MyDrive/wk.jpg')
hinh_anh_hau_den = pygame.transform.scale(hinh_anh_hau_den, (80, 80))
hinh_anh_hau_den_nho = pygame.transform.scale(hinh_anh_hau_den, (45, 45))
hinh_anh_vua_den = pygame.image.load('/content/gdrive/MyDrive/vuaden.jpg')
hinh_anh_vua_den = pygame.transform.scale(hinh_anh_vua_den, (80, 80))
hinh_anh_vua_den_nho = pygame.transform.scale(hinh_anh_vua_den, (45, 45))
hinh_anh_xe_den = pygame.image.load('/content/gdrive/MyDrive/xeden.jpg')
hinh_anh_xe_den = pygame.transform.scale(hinh_anh_xe_den, (80, 80))
hinh_anh_xe_den_nho = pygame.transform.scale(hinh_anh_xe_den, (45, 45))
hinh_anh_tuong_den = pygame.image.load('/content/gdrive/MyDrive/tinhden.jpg')
hinh_anh_tuong_den = pygame.transform.scale(hinh_anh_tuong_den, (80, 80))
hinh_anh_tuong_den_nho = pygame.transform.scale(hinh_anh_tuong_den, (45,
45))
hinh_anh_ma_den =
pygame.image.load('/content/gdrive/MyDrive/maden.jpg') hinh_anh_ma_den
= pygame.transform.scale(hinh_anh_ma_den, (80, 80)) hinh_anh_ma_den_nho
= pygame.transform.scale(hinh_anh_ma_den, (45, 45)) hinh_anh_tot_den =
pygame.image.load('/content/gdrive/MyDrive/totden.jpg') hinh_anh_tot_den =
pygame.transform.scale(hinh_anh_tot_den, (65, 65)) hinh_anh_tot_den_nho =
pygame.transform.scale(hinh_anh_tot_den, (45, 45)) board =
pygame.image.load('/content/gdrive/MyDrive/banco.png') board =
pygame.transform.scale(board, (800, 800))
#Vẽ bàn cờ def
ve_ban_co():
screen.blit(board, (0, 0)) #Vẽ các
quân cờ def ve_quan_co(): for i
in range(len(white_pieces)): if
white_pieces[i] == 'hậu':
screen.blit(hinh_anh_hau_trang, (white_locations[i][0] * 100,
white_locations[i][1] * 100))
if white_pieces[i] == 'vua':
screen.blit(hinh_anh_vua_trang, (white_locations[i][0] * 100,
white_locations[i][1] * 100))
if white_pieces[i] == 'xe':
screen.blit(hinh_anh_xe_trang
, (white_locations[i][0] * 100,
white_locations[i][1] * 100)) if
white_pieces[i] == 'tượng':
screen.blit(hinh_anh_tuong_trang, (white_locations[i][0] * 100,
white_locations[i][1] * 100))
if white_pieces[i] == 'ma':
screen.blit(hinh_anh_ma_trang, (white_locations[i][0] * 100,
white_locations[i][1] * 100))
if white_pieces[i] == 'tốt':
screen.blit(hinh_anh_tot_trang, (white_locations[i][0] * 100,
white_locations[i][1] * 100)) for i
in range(len(black_pieces)): if
black_pieces[i] == 'hậu':
screen.blit(hinh_anh_hau_den, (black_locations[i][0] * 100,
black_locations[i][1] * 100))
if black_pieces[i] == 'vua':
screen.blit(hinh_anh_vua_den, (black_locations[i][0] * 100,
black_locations[i][1] * 100))
if black_pieces[i] == 'xe':
screen.blit(hinh_anh_xe_den, (black_locations[i][0] * 100,
black_locations[i][1] * 100)) if
black_pieces[i] == 'tượng':
screen.blit(hinh_anh_tuong_den, (black_locations[i][0] * 100,
black_locations[i][1] * 100))
if black_pieces[i] == 'ma':
screen.blit(hinh_anh_ma_den,
(black_locations[i][0] * 100,
black_locations[i][1] * 100))
if black_pieces[i] == 'tốt':
screen.blit(hinh_anh_tot_den, (black_locations[i][0] * 100,
black_locations[i][1] * 100))
#kiểm tra quân hậu def
kiem_tra_hau(x, y):
hau = [] c = 1 while x + c < 8
and y + c < 8: if (x + c, y + c) in
white_locations:
break
hau.append((x + c, y + c)) if
(x + c, y + c) in black_locations:
break c += 1 c = 1
while x + c < 8 and y - c >= 0: if
(x + c, y - c) in white_locations:
break
hau.append((x + c, y - c)) if
(x + c, y - c) in black_locations:
break c += 1 c = 1 while x -
c >= 0 and y + c < 8: if (x - c, y +
c) in white_locations:
break hau.append((x - c, y
+ c)) if (x - c, y + c) in
black_locations:
break c += 1 c = 1
while x - c >= 0 and y - c >= 0: if
(x - c, y - c) in white_locations:
break hau.append((x - c,
y - c)) if (x - c, y - c) in
black_locations:
break c += 1 c = 1
while x + c < 8: if (x + c, y) in
white_locations:
break
hau.append((x + c, y))
if (x + c, y) in
black_locations:
break c += 1 c = 1
while x - c >= 0: if (x - c, y)
in white_locations:
break hau.append((x -
c, y)) if (x - c, y) in
black_locations:
break c += 1 c = 1
while y + c < 8: if (x, y + c) in
white_locations:
break hau.append((x, y
+ c)) if (x, y + c) in
black_locations:
break c += 1 c = 1
while y - c >= 0: if (x, y - c)
in white_locations:
break
hau.append((x, y - c)) if
(x, y - c) in black_locations:
break
c += 1
return hau
#Kiểm tra Vua def
kiem_tra_vua(x, y):
vua = [] if
x - 1 >= 0:
vua.append((x - 1, y))
if x + 1 < 8:
vua.append((x + 1, y))
if y - 1 >= 0:
vua.append((x, y - 1))
if y + 1 < 8:
vua.append((x, y + 1))
if x + 1 < 8 and y + 1 < 8:
vua.append((x + 1, y + 1))
if x - 1 >= 0 and y - 1 >= 0:
vua.append((x - 1, y - 1))
if x - 1 >= 0 and y + 1 < 8:
vua.append((x - 1, y + 1))
if x + 1 < 8 and y - 1 >= 0:
vua.append((x + 1, y - 1))
return vua
#kiểm tra Xe def
kiem_tra_xe(x, y): xe =
[] c = 1 while x + c < 8:
if (x + c, y) in white_locations:
break xe.append((x + c, y)) if (x + c, y)
in black_locations:
break c += 1 c = 1
while x - c >= 0: if (x - c, y) in
white_locations: break
xe.append((x - c, y))
if (x - c, y) in black_locations:
break c += 1
c = 1
while y + c < 8: if (x, y + c) in
white_locations: break xe.append((x, y
+ c)) if (x, y + c) in black_locations:
break c += 1 c
= 1
while y - c >= 0: if (x, y - c) in
white_locations: break xe.append((x, y
- c)) if (x, y - c) in black_locations:
break c += 1
return
xe
#Kiểm tra tịnh def
kiem_tra_tuong(x, y):
tuong = []
if x + 1 < 8 and y + 1 < 8:
tuong.append((x + 1, y + 1))
if x - 1 >= 0 and y + 1 < 8:
tuong.append((x - 1, y + 1))
if x + 1 < 8 and y - 1 >= 0:
tuong.append((x + 1, y - 1))
if x - 1 >= 0 and y - 1 >= 0:
tuong.append((x - 1, y - 1))
return tuong
#kiểm tra mã def
kiem_tra_ma(x, y): ma
= []
if x + 2 < 8 and y + 1 < 8:
ma.append((x + 2, y + 1))
if x - 2 >= 0 and y + 1 < 8:
ma.append((x - 2, y + 1))
if x + 2 < 8 and y - 1 >= 0:
ma.append((x + 2, y - 1))
if x - 2 >= 0 and y - 1 >= 0:
ma.append((x - 2, y - 1))
if x + 1 < 8 and y + 2 < 8:
ma.append((x + 1, y + 2))
if x - 1 >= 0 and y + 2 < 8:
ma.append((x - 1, y + 2))
if x + 1 < 8 and y - 2 >= 0:
ma.append((x + 1, y - 2))
if x - 1 >= 0 and y - 2 >= 0:
ma.append((x - 1, y - 2))
return ma
#kiểm tra tốt def
kiem_tra_tot(x, y, mau): if
mau == 'trang':
if (x, y - 1) not in black_locations and (x, y - 1) not in white_locations:
tot = [(x, y - 1)]
else:
tot = []
if y == 6 and (x, y - 1) not in black_locations and (x, y - 1) not in
white_locations and (x, y - 2) not in black_locations and (x, y - 2) not in
white_locations:
tot.append((x, y - 2))
else:
if (x, y + 1) not in black_locations and (x, y + 1) not in
white_locations:
tot = [(x, y + 1)]
else:
tot = []
if y == 1 and (x, y + 1) not in black_locations and (x, y + 1) not in
white_locations and (x, y + 2) not in black_locations and (x, y + 2) not in
white_locations:
tot.append((x, y + 2))
return tot
#kiểm tra quân cờ def
kiem_tra_quan(x, y):
for i in range(len(white_locations)):
if white_locations[i] == (x, y):
if white_pieces[i] == 'hậu':
return kiem_tra_hau(x, y)
if white_pieces[i] == 'vua':
return kiem_tra_vua(x, y)
if white_pieces[i] == 'xe':
return kiem_tra_xe(x, y)
if white_pieces[i] == 'tượng':
return kiem_tra_tuong(x, y)
if white_pieces[i] == 'ma':
return kiem_tra_ma(x, y)
if white_pieces[i] == 'tốt': return
kiem_tra_tot(x, y, 'trang')
for i in range(len(black_locations)):
if black_locations[i] == (x, y):
if black_pieces[i] == 'hậu':
return kiem_tra_hau(x, y)
if black_pieces[i] == 'vua':
return kiem_tra_vua(x, y)
if black_pieces[i] == 'xe':
return kiem_tra_xe(x, y)
if black_pieces[i] == 'tượng':
return kiem_tra_tuong(x, y)
if black_pieces[i] == 'ma':
return kiem_tra_ma(x, y)
if black_pieces[i] == 'tốt':
return kiem_tra_tot(x, y, 'den')
return []
#kiểm tra vị trí hợp lệ def kiem_tra_hop_le(x,
y, danh_sach_hop_le): for i in
range(len(danh_sach_hop_le)):
if (x, y) == danh_sach_hop_le[i]:
return True return False
#Kiểm tra địch
def kiem_tra_quan_dich(x, y, mau):
if mau == 'trang':
for i in range(len(black_locations)):
if (x, y) == black_locations[i]:
return True
else:
for i in range(len(white_locations)):
if (x, y) == white_locations[i]:
return True
return False
#Kiểm tra thắng def
kiem_tra_thang():
if (4, 0) in white_locations:
if kiem_tra_quan(4, 0) == [] and kiem_tra_quan(3, 0) == [] and
kiem_tra_quan(5, 0) == [] and kiem_tra_quan(2, 0) == [] and kiem_tra_quan(6, 0)
== []:
return 'đen' if (4, 7) in
black_locations:
if kiem_tra_quan(4, 7) == [] and kiem_tra_quan(3, 7) == [] and
kiem_tra_quan(5, 7) == [] and kiem_tra_quan(2, 7) == [] and kiem_tra_quan(6,
7) == []:
return 'trang'
return 'chua' #Kết
thúc def
ket_thuc(van_chu
a):
screen.fill((255,
255, 255))
# Hiển thị thông báo chiến thắng
if van_chua == 'trang':
text = big_font.render('Người chơi Trắng chiến thắng!', True, (0, 0, 0))
else:
text = big_font.render('Người chơi Đen chiến thắng!', True, (0, 0, 0))
textRect = text.get_rect()
textRect.center = (WIDTH // 2, HEIGHT //
2) screen.blit(text, textRect)
pygame.display.update()
pygame.time.delay(3000) pygame.quit()
#ván đang chơi def
van_dang_choi():
global selection, valid_moves, turn_step
screen.fill((255, 255, 255))
ve_ban_co() ve_quan_co()
# Hiển thị lượt của người chơi if
turn_step == 0 or turn_step == 2:
text = font.render('Lượt người chơi Trắng', True, (0, 0, 0))
else:
text = font.render('Lượt người chơi Đen', True, (0, 0, 0))
screen.blit(text, (10, 10))
# Hiển thị quân cờ được chọn
if selection != 100:
pygame.draw.rect(screen, (255, 0, 0), (selection[0] * 100, selection[1] * 100,
100, 100), 4)
# Hiển thị các nước đi hợp lệ
for move in valid_moves:
pygame.draw.circle(screen, (0, 255, 0), (move[0] * 100 + 50, move[1] *
100 + 50), 10)

Preview text:

Bài tập lớn môn trí tuệ nhân tạo Họ và tên: Lê Văn Dũng Lớp: 21CT1
Đề bài: Xây dựng ứng dụng đánh Cờ vua với máy tính import pygame # Khởi tạo module pygame pygame.init()
#kết nói drive để lấy hình ảnh các quân cờ và bàn cơ
from google.colab import drive
drive.mount('/content/gdrive')
#Xây dựng bàn cờ và hình ảnh các quân cờ WIDTH = 800 HEIGHT = 800
screen = pygame.display.set_mode([WIDTH, HEIGHT])
pygame.display.set_caption('Trò chơi Cờ vua 2 người chơi')
font = pygame.font.Font('freesansbold.ttf', 20)
medium_font = pygame.font.Font('freesansbold.ttf', 40)
big_font = pygame.font.Font('freesansbold.ttf', 50) timer = pygame.time.Clock() fps = 60
white_pieces = ['xe', 'mã', 'tượng', 'vua', 'hậu', 'tượng', 'mã', 'xe',
'tốt', 'tốt', 'tốt', 'tốt', 'tốt', 'tốt', 'tốt', 'tốt']
white_locations = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0),(0, 1), (1,
1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]
black_pieces = ['xe', 'mã', 'tượng', 'vua', 'hậu', 'tượng', 'mã', 'xe',
'tốt', 'tốt', 'tốt', 'tốt', 'tốt', 'tốt', 'tốt', 'tốt']
black_locations = [(0, 7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (7, 7),(0, 6), (1,
6), (2, 6), (3, 6), (4, 6), (5, 6), (6, 6), (7,
6)] captured_pieces_white = []
captured_pieces_black = [] turn_step = 0 selection = 100 valid_moves = []
#hình ảnh đc tải lên thư mục MyDrive
hinh_anh_hau_trang = pygame.image.load('/content/gdrive/MyDrive/wk.jpg')
hinh_anh_hau_trang = pygame.transform.scale(hinh_anh_hau_trang, (80, 80))
hinh_anh_hau_trang_nho = pygame.transform.scale(hinh_anh_hau_trang, (45, 45))
hinh_anh_vua_trang = pygame.image.load('/content/gdrive/MyDrive/vuaden.jpg')
hinh_anh_vua_trang = pygame.transform.scale(hinh_anh_vua_trang, (80, 80))
hinh_anh_vua_trang_nho = pygame.transform.scale(hinh_anh_vua_trang, (45, 45))
hinh_anh_xe_trang = pygame.image.load('/content/gdrive/MyDrive/xeden.jpg')
hinh_anh_xe_trang = pygame.transform.scale(hinh_anh_xe_trang, (80, 80))
hinh_anh_xe_trang_nho = pygame.transform.scale(hinh_anh_xe_trang, (45, 45)) hinh_anh_tuong_trang =
pygame.image.load('/content/gdrive/MyDrive/tinhden.jpg')
hinh_anh_tuong_trang = pygame.transform.scale(hinh_anh_tuong_trang, (80, 80))
hinh_anh_tuong_trang_nho = pygame.transform.scale(hinh_anh_tuong_trang, (45, 45))
hinh_anh_ma_trang = pygame.image.load('/content/gdrive/MyDrive/maden.jpg')
hinh_anh_ma_trang = pygame.transform.scale(hinh_anh_ma_trang, (80, 80))
hinh_anh_ma_trang_nho = pygame.transform.scale(hinh_anh_ma_trang, (45, 45))
hinh_anh_tot_trang = pygame.image.load('/content/gdrive/MyDrive/totden.jpg')
hinh_anh_tot_trang = pygame.transform.scale(hinh_anh_tot_trang, (65, 65))
hinh_anh_tot_trang_nho = pygame.transform.scale(hinh_anh_tot_trang, (45, 45))
hinh_anh_hau_den = pygame.image.load('/content/gdrive/MyDrive/wk.jpg')
hinh_anh_hau_den = pygame.transform.scale(hinh_anh_hau_den, (80, 80))
hinh_anh_hau_den_nho = pygame.transform.scale(hinh_anh_hau_den, (45, 45))
hinh_anh_vua_den = pygame.image.load('/content/gdrive/MyDrive/vuaden.jpg')
hinh_anh_vua_den = pygame.transform.scale(hinh_anh_vua_den, (80, 80))
hinh_anh_vua_den_nho = pygame.transform.scale(hinh_anh_vua_den, (45, 45))
hinh_anh_xe_den = pygame.image.load('/content/gdrive/MyDrive/xeden.jpg')
hinh_anh_xe_den = pygame.transform.scale(hinh_anh_xe_den, (80, 80))
hinh_anh_xe_den_nho = pygame.transform.scale(hinh_anh_xe_den, (45, 45))
hinh_anh_tuong_den = pygame.image.load('/content/gdrive/MyDrive/tinhden.jpg')
hinh_anh_tuong_den = pygame.transform.scale(hinh_anh_tuong_den, (80, 80))
hinh_anh_tuong_den_nho = pygame.transform.scale(hinh_anh_tuong_den, (45, 45)) hinh_anh_ma_den =
pygame.image.load('/content/gdrive/MyDrive/maden.jpg') hinh_anh_ma_den
= pygame.transform.scale(hinh_anh_ma_den, (80, 80)) hinh_anh_ma_den_nho
= pygame.transform.scale(hinh_anh_ma_den, (45, 45)) hinh_anh_tot_den =
pygame.image.load('/content/gdrive/MyDrive/totden.jpg') hinh_anh_tot_den =
pygame.transform.scale(hinh_anh_tot_den, (65, 65)) hinh_anh_tot_den_nho =
pygame.transform.scale(hinh_anh_tot_den, (45, 45)) board =
pygame.image.load('/content/gdrive/MyDrive/banco.png') board =
pygame.transform.scale(board, (800, 800)) #Vẽ bàn cờ def ve_ban_co():
screen.blit(board, (0, 0)) #Vẽ các
quân cờ def ve_quan_co(): for i
in range(len(white_pieces)): if white_pieces[i] == 'hậu':
screen.blit(hinh_anh_hau_trang, (white_locations[i][0] * 100, white_locations[i][1] * 100)) if white_pieces[i] == 'vua':
screen.blit(hinh_anh_vua_trang, (white_locations[i][0] * 100, white_locations[i][1] * 100)) if white_pieces[i] == 'xe': screen.blit(hinh_anh_xe_trang
, (white_locations[i][0] * 100,
white_locations[i][1] * 100)) if
white_pieces[i] == 'tượng':
screen.blit(hinh_anh_tuong_trang, (white_locations[i][0] * 100, white_locations[i][1] * 100)) if white_pieces[i] == 'ma':
screen.blit(hinh_anh_ma_trang, (white_locations[i][0] * 100, white_locations[i][1] * 100))
if white_pieces[i] == 'tốt':
screen.blit(hinh_anh_tot_trang, (white_locations[i][0] * 100,
white_locations[i][1] * 100)) for i
in range(len(black_pieces)): if black_pieces[i] == 'hậu':
screen.blit(hinh_anh_hau_den, (black_locations[i][0] * 100, black_locations[i][1] * 100)) if black_pieces[i] == 'vua':
screen.blit(hinh_anh_vua_den, (black_locations[i][0] * 100, black_locations[i][1] * 100)) if black_pieces[i] == 'xe':
screen.blit(hinh_anh_xe_den, (black_locations[i][0] * 100,
black_locations[i][1] * 100)) if
black_pieces[i] == 'tượng':
screen.blit(hinh_anh_tuong_den, (black_locations[i][0] * 100, black_locations[i][1] * 100)) if black_pieces[i] == 'ma': screen.blit(hinh_anh_ma_den, (black_locations[i][0] * 100, black_locations[i][1] * 100))
if black_pieces[i] == 'tốt':
screen.blit(hinh_anh_tot_den, (black_locations[i][0] * 100, black_locations[i][1] * 100)) #kiểm tra quân hậu def kiem_tra_hau(x, y):
hau = [] c = 1 while x + c < 8
and y + c < 8: if (x + c, y + c) in white_locations: break
hau.append((x + c, y + c)) if
(x + c, y + c) in black_locations: break c += 1 c = 1
while x + c < 8 and y - c >= 0: if
(x + c, y - c) in white_locations: break
hau.append((x + c, y - c)) if
(x + c, y - c) in black_locations: break c += 1 c = 1 while x -
c >= 0 and y + c < 8: if (x - c, y + c) in white_locations: break hau.append((x - c, y + c)) if (x - c, y + c) in black_locations: break c += 1 c = 1
while x - c >= 0 and y - c >= 0: if
(x - c, y - c) in white_locations: break hau.append((x - c, y - c)) if (x - c, y - c) in black_locations: break c += 1 c = 1
while x + c < 8: if (x + c, y) in white_locations: break hau.append((x + c, y)) if (x + c, y) in black_locations: break c += 1 c = 1
while x - c >= 0: if (x - c, y) in white_locations: break hau.append((x - c, y)) if (x - c, y) in black_locations: break c += 1 c = 1
while y + c < 8: if (x, y + c) in white_locations: break hau.append((x, y + c)) if (x, y + c) in black_locations: break c += 1 c = 1
while y - c >= 0: if (x, y - c) in white_locations: break hau.append((x, y - c)) if
(x, y - c) in black_locations: break c += 1 return hau #Kiểm tra Vua def kiem_tra_vua(x, y): vua = [] if x - 1 >= 0: vua.append((x - 1, y)) if x + 1 < 8: vua.append((x + 1, y)) if y - 1 >= 0: vua.append((x, y - 1)) if y + 1 < 8: vua.append((x, y + 1))
if x + 1 < 8 and y + 1 < 8: vua.append((x + 1, y + 1))
if x - 1 >= 0 and y - 1 >= 0: vua.append((x - 1, y - 1))
if x - 1 >= 0 and y + 1 < 8: vua.append((x - 1, y + 1))
if x + 1 < 8 and y - 1 >= 0: vua.append((x + 1, y - 1)) return vua #kiểm tra Xe def kiem_tra_xe(x, y): xe = [] c = 1 while x + c < 8:
if (x + c, y) in white_locations:
break xe.append((x + c, y)) if (x + c, y) in black_locations: break c += 1 c = 1
while x - c >= 0: if (x - c, y) in white_locations: break xe.append((x - c, y))
if (x - c, y) in black_locations: break c += 1 c = 1
while y + c < 8: if (x, y + c) in
white_locations: break xe.append((x, y
+ c)) if (x, y + c) in black_locations: break c += 1 c = 1
while y - c >= 0: if (x, y - c) in
white_locations: break xe.append((x, y
- c)) if (x, y - c) in black_locations: break c += 1 return xe #Kiểm tra tịnh def kiem_tra_tuong(x, y): tuong = []
if x + 1 < 8 and y + 1 < 8: tuong.append((x + 1, y + 1))
if x - 1 >= 0 and y + 1 < 8: tuong.append((x - 1, y + 1))
if x + 1 < 8 and y - 1 >= 0: tuong.append((x + 1, y - 1))
if x - 1 >= 0 and y - 1 >= 0: tuong.append((x - 1, y - 1)) return tuong #kiểm tra mã def kiem_tra_ma(x, y): ma = []
if x + 2 < 8 and y + 1 < 8: ma.append((x + 2, y + 1))
if x - 2 >= 0 and y + 1 < 8: ma.append((x - 2, y + 1))
if x + 2 < 8 and y - 1 >= 0: ma.append((x + 2, y - 1))
if x - 2 >= 0 and y - 1 >= 0: ma.append((x - 2, y - 1))
if x + 1 < 8 and y + 2 < 8: ma.append((x + 1, y + 2))
if x - 1 >= 0 and y + 2 < 8: ma.append((x - 1, y + 2))
if x + 1 < 8 and y - 2 >= 0: ma.append((x + 1, y - 2))
if x - 1 >= 0 and y - 2 >= 0: ma.append((x - 1, y - 2)) return ma #kiểm tra tốt def kiem_tra_tot(x, y, mau): if mau == 'trang':
if (x, y - 1) not in black_locations and (x, y - 1) not in white_locations: tot = [(x, y - 1)] else: tot = []
if y == 6 and (x, y - 1) not in black_locations and (x, y - 1) not in
white_locations and (x, y - 2) not in black_locations and (x, y - 2) not in white_locations: tot.append((x, y - 2)) else:
if (x, y + 1) not in black_locations and (x, y + 1) not in white_locations: tot = [(x, y + 1)] else: tot = []
if y == 1 and (x, y + 1) not in black_locations and (x, y + 1) not in
white_locations and (x, y + 2) not in black_locations and (x, y + 2) not in white_locations: tot.append((x, y + 2)) return tot #kiểm tra quân cờ def kiem_tra_quan(x, y):
for i in range(len(white_locations)):
if white_locations[i] == (x, y):
if white_pieces[i] == 'hậu': return kiem_tra_hau(x, y) if white_pieces[i] == 'vua': return kiem_tra_vua(x, y) if white_pieces[i] == 'xe': return kiem_tra_xe(x, y)
if white_pieces[i] == 'tượng': return kiem_tra_tuong(x, y) if white_pieces[i] == 'ma': return kiem_tra_ma(x, y)
if white_pieces[i] == 'tốt': return kiem_tra_tot(x, y, 'trang')
for i in range(len(black_locations)):
if black_locations[i] == (x, y):
if black_pieces[i] == 'hậu': return kiem_tra_hau(x, y) if black_pieces[i] == 'vua': return kiem_tra_vua(x, y) if black_pieces[i] == 'xe': return kiem_tra_xe(x, y)
if black_pieces[i] == 'tượng': return kiem_tra_tuong(x, y) if black_pieces[i] == 'ma': return kiem_tra_ma(x, y)
if black_pieces[i] == 'tốt':
return kiem_tra_tot(x, y, 'den') return []
#kiểm tra vị trí hợp lệ def kiem_tra_hop_le(x,
y, danh_sach_hop_le): for i in range(len(danh_sach_hop_le)):
if (x, y) == danh_sach_hop_le[i]: return True return False #Kiểm tra địch
def kiem_tra_quan_dich(x, y, mau): if mau == 'trang':
for i in range(len(black_locations)):
if (x, y) == black_locations[i]: return True else:
for i in range(len(white_locations)):
if (x, y) == white_locations[i]: return True return False #Kiểm tra thắng def kiem_tra_thang(): if (4, 0) in white_locations:
if kiem_tra_quan(4, 0) == [] and kiem_tra_quan(3, 0) == [] and
kiem_tra_quan(5, 0) == [] and kiem_tra_quan(2, 0) == [] and kiem_tra_quan(6, 0) == []: return 'đen' if (4, 7) in black_locations:
if kiem_tra_quan(4, 7) == [] and kiem_tra_quan(3, 7) == [] and
kiem_tra_quan(5, 7) == [] and kiem_tra_quan(2, 7) == [] and kiem_tra_quan(6, 7) == []: return 'trang' return 'chua' #Kết thúc def ket_thuc(van_chu a): screen.fill((255, 255, 255))
# Hiển thị thông báo chiến thắng if van_chua == 'trang':
text = big_font.render('Người chơi Trắng chiến thắng!', True, (0, 0, 0)) else:
text = big_font.render('Người chơi Đen chiến thắng!', True, (0, 0, 0)) textRect = text.get_rect()
textRect.center = (WIDTH // 2, HEIGHT //
2) screen.blit(text, textRect) pygame.display.update()
pygame.time.delay(3000) pygame.quit() #ván đang chơi def van_dang_choi():
global selection, valid_moves, turn_step screen.fill((255, 255, 255)) ve_ban_co() ve_quan_co()
# Hiển thị lượt của người chơi if
turn_step == 0 or turn_step == 2:
text = font.render('Lượt người chơi Trắng', True, (0, 0, 0)) else:
text = font.render('Lượt người chơi Đen', True, (0, 0, 0)) screen.blit(text, (10, 10))
# Hiển thị quân cờ được chọn if selection != 100:
pygame.draw.rect(screen, (255, 0, 0), (selection[0] * 100, selection[1] * 100, 100, 100), 4)
# Hiển thị các nước đi hợp lệ for move in valid_moves:
pygame.draw.circle(screen, (0, 255, 0), (move[0] * 100 + 50, move[1] * 100 + 50), 10)