本文共 4378 字,大约阅读时间需要 14 分钟。
最近接触了一些Python小练习题,这些题目既能锻炼基础,又能通过实际操作加深对Python的理解。本文将详细介绍如何开发一个简单的控制台版五子棋游戏。
棋盘默认设置为10x10大小。以下是创建棋盘的实现代码:
def get_checkboard(): checkerboard = [] for i in range(10): checkerboard.append([]) for j in range(10): checkerboard[i].append('-') return checkerboard 为了让玩家能够直观地看到棋盘状态,我们需要实现一个显示棋盘的函数。以下是实现代码:
def show(): print(' 1 2 3 4 5 6 7 8 9 10') for i in range(len(checkerboard)): print(chr(i + ord('A')) + ' ' + ' '.join([str(cell) for cell in checkerboard[i]])) 判断是否获胜是关键部分。每次下棋后需要检查是否满足五连的条件。实现了四种检查方法:行、列、斜和反斜。以下是实现代码:
def check_row(pos, color): start_x = max(0, pos[0] - 4) end_x = min(9, pos[0] + 4) count = 0 for x in range(start_x, end_x + 1): if checkerboard[x][pos[1]] == color: count += 1 if count >= 5: return True else: count = 0 return False
def check_column(pos, color): start_y = max(0, pos[1] - 4) end_y = min(9, pos[1] + 4) count = 0 for y in range(start_y, end_y + 1): if checkerboard[pos[0]][y] == color: count += 1 if count >= 5: return True else: count = 0 return False
def check_oblique1(pos, color): x, y = pos if x >= y: start_y = max(0, y - 4) start_x = start_y + (x - y) end_x = min(9, x + 4) end_y = end_x - (x - y) else: start_x = max(0, x - 4) start_y = start_x - (x - y) end_y = min(9, y + 4) end_x = end_y + (x - y) count = 0 for i, j in zip(range(start_x, end_x + 1), range(start_y, end_y + 1)): if checkerboard[i][j] == color: count += 1 if count >= 5: return True else: count = 0 return False
def check_oblique2(pos, color): s = pos[0] + pos[1] if s <= 9: start_y = max(0, pos[1] - 4) start_x = s - start_y end_x = max(0, pos[0] - 4) end_y = s - end_x else: start_x = min(9, pos[0] + 4) start_y = s - start_x end_y = min(9, pos[1] + 4) end_x = s - end_y tmp = list(range(end_x, start_x + 1)) tmp.reverse() count = 0 for i, j in zip(tmp, range(start_y, end_y + 1)): if checkerboard[i][j] == color: count += 1 if count >= 5: return True else: count = 0 return False
主函数负责处理游戏逻辑和用户输入。以下是实现代码:
checkerboard = get_checkboard()def main(): color = ['M', 'O'] dic = { 'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9 } show() mark = True while mark: choice = input('Please input your choice (exit-0):') if choice == '0': break elif not (choice[0].isalpha() and choice[1:].isdigit()): print('Please input right position!') continue else: try: y = choice[0].upper() x = int(choice[1:]) - 1 if 0 <= dic[y] <= 9 and 0 <= x <= 9: if checkerboard[dic[y]][x] != '-': print('Chess piece already exists in this position!') else: if color[0] == 'M': checkerboard[dic[y]][x] = 'M' if check([dic[y], x], color[0]): print('Congratulations! Player 1 wins!') mark = False else: checkerboard[dic[y]][x] = 'O' if check([dic[y], x], color[0]): print('Congratulations! Player 2 wins!') mark = False show() color.reverse() else: print('Position out of range!') except: print('Please input right position!') 以下是游戏运行后的示例输出:
Congs!Player 1 win! 1 2 3 4 5 6 7 8 9 10A - M - - - - - - - -B - - M O - M - - -C - - M M O O - - -D - - O M M M - - -E - - - O O M - - -F - - - - O - O - -G - - - - - - - - -H - - - - - - - - -I - - - - - - - - -J - - - - - - - - -
以上就是完整的五子棋开发指南。希望对开发控制台版五子棋有所帮助!
转载地址:http://khtg.baihongyu.com/