打发无聊时光小游戏——五子棋游戏(控制台版)
程序员文章站
2022-04-07 15:48:12
...
最近写了一个关于Python的五子棋游戏,也算是一个小项目,我这次写的五子棋游戏为控制台版,主要具有以下功能:
-
可以循环打印棋盘;
-
对战双方有一方胜利时,打印胜利棋盘及赢家;
-
判断棋子是否超出棋盘的范围;
关于这个游戏大概思路,我画了个较为粗略的结构图,如下:
本人画功不是太好,请见谅。
下面是这个游戏的完整代码:
#encoding:utf-8
_date_ = "2020/3/31 13:21"
# 游戏是否结束
finish = False
# 当前下棋者标记
flagNum = 1
# 当前下棋者棋子
flagch = '*'
# 当前棋子的横坐标
x = 0
# 当前棋子的纵坐标
y = 0
print('\033[1;37;41m---------简易五子棋游戏(控制台版)---------\033[0m')
# 棋盘初始化
checkerboard = []
for i in range(10):
checkerboard.append([])
for j in range(10):
checkerboard[i].append('-')
def msg():
# 输出最后的胜利的棋盘
print("\033[1;37;41m------------------------------------")
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 True:
# 打印棋盘
print("\033[1;30;46m-------------------------------------")
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:
flagch = '*'
# 粉字黑底
print('\033[1;37;45m请*输入棋子坐标(例如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')
continue
# 判断棋子上、下、左、右方向
# 判断棋子左侧
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
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
and checkerboard[x + 3][y] == flagch
and checkerboard[x + 4][y] == flagch):
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;
上一篇: 扫雷