Python实现井字棋小游戏
(编辑:jimmy 日期: 2024/11/19 浏览:3 次 )
本文实例为大家分享了Python实现井字棋小游戏的具体代码,供大家参考,具体内容如下
import os def print_board(board): print(board['TL'] + '|' + board['TM'] + '|' + board['TR']) print('-+-+-') print(board['ML'] + '|' + board['MM'] + '|' + board['MR']) print('-+-+-') print(board['BL'] + '|' + board['BM'] + '|' + board['BR']) def main(): init_board = { 'TL': ' ', 'TM': ' ', 'TR': ' ', 'ML': ' ', 'MM': ' ', 'MR': ' ', 'BL': ' ', 'BM': ' ', 'BR': ' ' } begin = True while begin: curr_board = init_board.copy() begin = False turn = 'x' counter = 0 os.system('clear') print_board(curr_board) while counter < 9: move = input('轮到%s走棋, 请输入位置: ' % turn) if curr_board[move] == ' ': counter += 1 curr_board[move] = turn if turn == 'x': turn = 'o' else: turn = 'x' os.system('clear') print_board(curr_board) choice = input('再玩一局"text-align: center">以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇:python:批量统计xml中各类目标的数量案例