利用二维表完成五子棋小游戏,在《疯狂Python讲义》第四章的程序中完成了这个游戏的初步功能,但是不全面,故而我在此基础上完善了一下。程序上显示的黑点是棋手下的棋,电脑下的棋是使用随机生成的两个坐标值来控制的。并且增加了判断胜负的功能。完整代码如下:
import random
# 定义棋盘的大小
BOARD_SIZE = 15
# 定义一个二维列表棋盘
BOARD = []
def initBoard():
# 为每个元素赋值,用于控制台画出棋盘
for i in range(BOARD_SIZE):
row = ["╋"] * BOARD_SIZE
BOARD.append(row)
def printBoard():
# 在控制台输出棋盘的方法
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
print(BOARD[i][j], end="")
print()
def check_win(BOARD, WHITE_PIECE, BLACK_PIECE):
# 白棋胜利返回1,黑棋胜利返回-1,否则check_win返回0
size = len(BOARD)
for i in range(size):
for j in range(size):
# 对于每个点,检测横线上的五个
if BOARD[i][j:5 + 1].count(WHITE_PIECE) >= 5:
return 1
if BOARD[i][j:5 + 1].count(BLACK_PIECE) >= 5:
return -1
# 对于每个点,检测列上的五个
count_black = 0
count_white = 0
for m in range(5):
if 0 <= i + m < size:
if BOARD[i+m][j] == WHITE_PIECE:
count_white += 1
elif BOARD[i+m][j] == BLACK_PIECE:
count_black += 1
else:
break
if count_black >= 5:
return -1
if count_white >= 5:
return 1
# 对于每个点,检测斜线上的五个
count_black = 0
count_white = 0
if BOARD[i][j] != '╋':
if BOARD[i][j] == WHITE_PIECE:
count_white = 1
if BOARD[i][j] == BLACK_PIECE:
count_black = 1
for m in range(1, 5):
if i + m < size and j + m < size:
if BOARD[i + m][j + m] == WHITE_PIECE:
count_white += 1
if BOARD[i + m][j + m] == BLACK_PIECE:
count_black += 1
if i - m >= 0 and j + m < size:
if BOARD[i - m][j + m] == WHITE_PIECE:
count_white += 1
if BOARD[i - m][j + m] == BLACK_PIECE:
count_black += 1
if count_black >= 5:
return -1
if count_white >= 5:
return 1
return 0
BLACK_PIECE = '●'
WHITE_PIECE = '○'
win = 0
initBoard()
printBoard()
while 1:
inputStr = input('请输入下棋的坐标(x,y):')
x_str, y_str = intput_list = inputStr.split(sep=",")
if 0 < int(x_str) < BOARD_SIZE and 0 < int(y_str) < BOARD_SIZE:
if BOARD[int(x_str) - 1][int(y_str) - 1] == '╋':
BOARD[int(x_str) - 1][int(y_str) - 1] = BLACK_PIECE
win = check_win(BOARD, WHITE_PIECE, BLACK_PIECE)
if win != 0:
break
else:
print('棋子已经存在,请重新输入!!')
continue
else:
print('坐标超出棋盘,请重新输入!')
continue
x = random.randint(0, BOARD_SIZE)
y = random.randint(0, BOARD_SIZE)
if 0 < x < BOARD_SIZE and 0 < y < BOARD_SIZE:
if BOARD[x - 1][y - 1] == '╋':
BOARD[x - 1][y - 1] = WHITE_PIECE
win = check_win(BOARD, WHITE_PIECE, BLACK_PIECE)
if win != 0:
break
else:
print('棋子已经存在,请重新输入!!')
continue
else:
print('坐标超出棋盘,请重新输入!')
continue
printBoard()
printBoard()
if win == 0:
win = ''
if win == 1:
win = '白棋胜利,'
if win == -1:
win = '黑棋胜利,'
print(win+'游戏结束!')
运行结果如下:
╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ 请输入下棋的坐标(x,y):4,5 ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋○╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ 请输入下棋的坐标(x,y):5,5 ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋○╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋○╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ 请输入下棋的坐标(x,y):6,5 坐标超出棋盘,请重新输入! 请输入下棋的坐标(x,y):6,5 棋子已经存在,请重新输入!! 请输入下棋的坐标(x,y):7,5 ╋╋╋╋╋╋╋╋╋○╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋○╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋○╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ 请输入下棋的坐标(x,y):8,5 ╋╋╋╋╋╋╋╋╋○╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋●╋╋╋╋╋╋╋╋○╋ ╋╋╋╋●╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋○╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ ╋╋╋╋╋╋╋╋╋╋╋╋╋╋╋ 黑棋胜利,游戏结束!
这个程序还是有点小bug,但是编编找了很久都没有找出来,如果有找出来问题在哪的,可以留言哦❥(^_-)



