Python实现简易五⼦棋(代码+注释)运⾏效果:
完整代码+注释:
finish = False # 游戏是否结束
flagNum = 1  # 当前下棋者标记
flagch = '*'  # 当前下棋者棋⼦
x = 0    # 当前棋⼦的横坐标
y = 0    # 当前棋⼦的纵坐标
print('---------五⼦棋游戏(控制台版)---------')
# 棋盘初始化
checkerboard=[]
for i in range(10):
checkerboard.append([])
for j in range(10):
checkerboard[i].append('-')
def msg():
# 输出最后胜利的棋盘
print("\033[1;37;44m--------------------------------")
print("  1  2  3  4  5  6  7  8  9  10")
for i in range(len(checkerboard)):
print(chr(i + ord('A')) + " ", end=' ')
for j in range(len(checkerboard[i])):
print(checkerboard[i][j] + " ", end=' ')
print()
print("--------------------------------\033[0m")
# 输出赢家
if (flagNum == 1):
print('\033[32m*棋胜利!***\033[0m')
else:
print('\033[32mo棋胜利!***\033[0m')
while not finish:
# 打印棋盘
print("\033[1;30;46m--------------------------------")
print("  1  2  3  4  5  6  7  8  9  10")
for i in range(len(checkerboard)):
pil船公司print(chr(i + ord('A')) + " ", end=' ');
for j in range(len(checkerboard[i])):
print(checkerboard[i][j] + " ", end=' ')
print()
print("--------------------------------\033[0m")
# 判断当前下棋者
if flagNum == 1:
flagch = '*'
print('\033[1;37;40m请*输⼊棋⼦坐标(例如A1):\033[0m', end=' ') # ⽩字⿊底
else:
flagch = 'o'
print('\033[1;30;42m请o输⼊棋⼦坐标(例如J5):\033[0m', end=' ') # ⿊字绿底    # 输⼊棋⼦坐标
str = input()
ch = str[0] # 获取第⼀个字符的⼤写形式
x = ord(ch) - 65
y = int(str[1]) - 1
# 判断坐标是否在棋盘之内
if (x < 0 or x > 9 or y < 0 or y > 9):
print('\033[31m***您输⼊的坐标有误请重新输⼊!***\033[0m')
continue
# 判断坐标上是否有棋⼦
if (checkerboard[x][y] == '-'):
if (flagNum == 1):
checkerboard[x][y] = '*'
else:
checkerboard[x][y] = 'o'
else:
print('\033[31m******您输⼊位置已经有其他棋⼦,请重新输⼊!\033[0m')
python语言入门视频
continue
# 判断棋⼦左侧
excel表格转换成xml格式if (y - 4 >= 0):
if (checkerboard[x][y - 1] == flagch
and checkerboard[x][y - 2] == flagch
and checkerboard[x][y - 3] == flagch
and checkerboard[x][y - 4] == flagch):
finish = True
msg()
# 判断棋⼦右侧
if (y + 4 <= 9):
if (checkerboard[x][y + 1] == flagch
and checkerboard[x][y + 2] == flagch
and checkerboard[x][y + 3] == flagch
and checkerboard[x][y + 4] == flagch):
finish = True
msg()
# 判断棋⼦上⽅
if (x - 4 >= 0):
if (checkerboard[x - 1][y] == flagch
and checkerboard[x - 2][y] == flagch
python基础代码注释and checkerboard[x - 3][y] == flagch
and checkerboard[x - 4][y] == flagch):
finish = True
msg()
# 判断棋⼦下⽅
if (x + 4 <= 9):
if (checkerboard[x + 1][y] == flagch
and checkerboard[x + 2][y] == flagch
preferred time什么意思and checkerboard[x + 3][y] == flagch
and checkerboard[x + 4][y] == flagch):
kubernetes读音
finish = True
msg()
# 判断棋⼦右上⽅向
if (x - 4 >= 0 and y - 4 >= 0):
if (checkerboard[x - 1][y - 1] == flagch
and checkerboard[x - 2][y - 2] == flagch
and checkerboard[x - 3][y - 3] == flagch
and checkerboard[x - 4][y - 4] == flagch):
finish = True
msg()
# 判断棋⼦右下⽅向
if (x + 4 <= 9 and y - 4 >= 0):
if (checkerboard[x + 1][y - 1] == flagch
and checkerboard[x + 2][y - 2] == flagch                and checkerboard[x + 3][y - 3] == flagch                and checkerboard[x + 4][y - 4] == flagch):            finish = True
msg()
# 判断棋⼦左上⽅向
if (x - 4 >= 0 and y + 4 <= 9):
if (checkerboard[x - 1][y + 1] == flagch
and checkerboard[x - 2][y + 2] == flagch                and checkerboard[x - 3][y + 3] == flagch                and checkerboard[x - 4][y + 4] == flagch):            finish = True
msg()
# 判断棋⼦左下⽅向
if (x + 4 <= 9 and y + 4 <= 9):
if (checkerboard[x + 1][y + 1] == flagch
and checkerboard[x + 2][y + 2] == flagch                and checkerboard[x + 3][y + 3] == flagch                and checkerboard[x + 4][y + 4] == flagch):            finish = True
msg()
flagNum *= -1; # 更换下棋者标记

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。