今天上午在《Automate the boring stuff with python》里看到了一个为井字棋盘建模的实例,由于书上只是简单地将棋盘实现,只能一个人傻傻地画圈画叉,因而一时兴起,写了一个十分粗糙的智(zhi)能(zhang)井字棋游戏模拟器。
# _*_ coding utf-8 _*_
# designer:はな
# time:2021/6/28 8:55
# name:智能的井字棋(其实并不智能).py
import random
def printtheboard(theboard):
print(theboard['1'] + '|' + theboard['2'] + '|' + theboard['3'])
print('-+-+-')
print(theboard['4'] + '|' + theboard['5'] + '|' + theboard['6'])
print('-+-+-')
print(theboard['7'] + '|' + theboard['8'] + '|' + theboard['9'])
print("棋盘格局如下:")
print("1|2|3\n"
"4|5|6\n"
"7|8|9")
print("下棋时请输入对应位置的数字序号")
#判断赢家
def winnercheck(theboard):
winner = ''
if theboard['1'] == theboard['2'] and theboard['2'] == theboard['3'] and theboard['1'] != ' ':
winner = theboard['1']
elif theboard['1'] == theboard['4'] and theboard['4'] == theboard['7'] and theboard['1'] != ' ':
winner = theboard['1']
elif theboard['1'] == theboard['5'] and theboard['5'] == theboard['9'] and theboard['1'] != ' ':
winner = theboard['1']
elif theboard['2'] == theboard['5'] and theboard['5'] == theboard['8'] and theboard['2'] != ' ':
winner = theboard['2']
elif theboard['3'] == theboard['6'] and theboard['6'] == theboard['9'] and theboard['3'] != ' ':
winner = theboard['3']
elif theboard['4'] == theboard['5'] and theboard['5'] == theboard['6'] and theboard['4'] != ' ':
winner = theboard['4']
elif theboard['7'] == theboard['8'] and theboard['8'] == theboard['9'] and theboard['7'] != ' ':
winner = theboard['7']
elif theboard['3'] == theboard['5'] and theboard['5'] == theboard['7'] and theboard['3'] != ' ':
winner = theboard['3']
return winner
def chess_algorithm(theboard, turn, me):
position_computer = ''
# 先判断有无马上要赢的步骤:
if ((theboard['1'] == theboard['2'] and theboard['1'] == me) or
(theboard['6'] == theboard['9'] and theboard['6'] == me) or
(theboard['7'] == theboard['5'] and theboard['5'] == me)) and \
theboard['3'] == ' ':
position_computer = '3'
elif ((theboard['3'] == theboard['2'] and theboard['2'] == me) or
(theboard['5'] == theboard['9'] and theboard['5'] == me) or
(theboard['7'] == theboard['4'] and theboard['4'] == me)) and \
theboard['1'] == ' ':
position_computer = '1'
elif ((theboard['1'] == theboard['4'] and theboard['1'] == me) or
(theboard['3'] == theboard['5'] and theboard['3'] == me) or
(theboard['8'] == theboard['9'] and theboard['8'] == me)) and \
theboard['7'] == ' ':
position_computer = '7'
elif ((theboard['1'] == theboard['5'] and theboard['1'] == me) or
(theboard['3'] == theboard['6'] and theboard['6'] == me) or
(theboard['7'] == theboard['8'] and theboard['7'] == me)) and \
theboard['9'] == ' ':
position_computer = '9'
elif ((theboard['1'] == theboard['3'] and theboard['1'] == me) or
(theboard['5'] == theboard['8'] and theboard['5'] == me)) and \
theboard['2'] == ' ':
position_computer = '2'
elif ((theboard['1'] == theboard['7'] and theboard['1'] == me) or
(theboard['5'] == theboard['6'] and theboard['5'] == me)) and \
theboard['4'] == ' ':
position_computer = '4'
elif ((theboard['9'] == theboard['3'] and theboard['3'] == me) or
(theboard['5'] == theboard['4'] and theboard['5'] == me)) and \
theboard['6'] == ' ':
position_computer = '6'
elif ((theboard['7'] == theboard['9'] and theboard['7'] == me) or
(theboard['5'] == theboard['2'] and theboard['5'] == me)) and \
theboard['8'] == ' ':
position_computer = '8'
elif ((theboard['1'] == theboard['9'] and theboard['1'] == me) or
(theboard['3'] == theboard['7'] and theboard['3'] == me) or
(theboard['2'] == theboard['8'] and theboard['2'] == me) or
(theboard['4'] == theboard['6'] and theboard['4'] == me)) and \
theboard['5'] == ' ':
position_computer = '5'
# 再判断有无马上要输得步骤:
elif ((theboard['1'] == theboard['2'] and theboard['1'] == turn) or
(theboard['6'] == theboard['9'] and theboard['6'] == turn) or
(theboard['7'] == theboard['5'] and theboard['5'] == turn)) and \
theboard['3'] == ' ':
position_computer = '3'
elif ((theboard['3'] == theboard['2'] and theboard['2'] == turn) or
(theboard['5'] == theboard['9'] and theboard['5'] == turn) or
(theboard['7'] == theboard['4'] and theboard['4'] == turn)) and \
theboard['1'] == ' ':
position_computer = '1'
elif ((theboard['1'] == theboard['4'] and theboard['1'] == turn) or
(theboard['3'] == theboard['5'] and theboard['3'] == turn) or
(theboard['8'] == theboard['9'] and theboard['8'] == turn)) and \
theboard['7'] == ' ':
position_computer = '7'
elif ((theboard['1'] == theboard['5'] and theboard['1'] == turn) or
(theboard['3'] == theboard['6'] and theboard['6'] == turn) or
(theboard['7'] == theboard['8'] and theboard['7'] == turn)) and \
theboard['9'] == ' ':
position_computer = '9'
elif ((theboard['1'] == theboard['3'] and theboard['1'] == turn) or
(theboard['5'] == theboard['8'] and theboard['5'] == turn)) and \
theboard['2'] == ' ':
position_computer = '2'
elif ((theboard['1'] == theboard['7'] and theboard['1'] == turn) or
(theboard['5'] == theboard['6'] and theboard['5'] == turn)) and \
theboard['4'] == ' ':
position_computer = '4'
elif ((theboard['9'] == theboard['3'] and theboard['3'] == turn) or
(theboard['5'] == theboard['4'] and theboard['5'] == turn)) and \
theboard['6'] == ' ':
position_computer = '6'
elif ((theboard['7'] == theboard['9'] and theboard['7'] == turn) or
(theboard['5'] == theboard['2'] and theboard['5'] == turn)) and \
theboard['8'] == ' ':
position_computer = '8'
elif ((theboard['1'] == theboard['9'] and theboard['1'] == turn) or
(theboard['3'] == theboard['7'] and theboard['3'] == turn) or
(theboard['2'] == theboard['8'] and theboard['2'] == turn) or
(theboard['4'] == theboard['6'] and theboard['4'] == turn)) and \
theboard['5'] == ' ':
position_computer = '5'
# 若没有,则可以采取进攻:
else:
available_list = []
opposite_list = []
for i in range(1, 10):
if theboard[f'{i}'] == ' ':
available_list.append(str(i))
if theboard[f'{i}'] == turn:
opposite_list.append(str(i))
if '5' in available_list:
position_computer = '5'
else:
position_computer = random.choice(available_list)
return position_computer
#选择先手
def loop1(theboard):
turn = 'X'
winner = ''
for i in range(9):
print("现在是" + turn + "下棋")
if turn == 'X':
while True:
position = input("请输入要下的位置:")
if position == '' or int(position) not in range(1, 10):
print("请输入一个正确的位置")
continue
elif theboard[position] != ' ':
print("这个位置已经下过了,请重新选一个位置下:")
continue
else:
theboard[position] = 'X'
break
else:
theboard[chess_algorithm(theboard, 'X', 'O')] = 'O'
printtheboard(theboard)
winner = winnercheck(theboard)
if winner != '':
break
if turn == 'X':
turn = 'O'
else:
turn = 'X'
return winner
#选择后手:
def loop2(theboard):
turn = 'O'
winner = ''
print("现在是X下棋")
theboard[str(random.randint(1,10))] = 'X'
printtheboard(theboard)
for i in range(8):
print("现在是" + turn + "下棋")
if turn == 'O':
while True:
position = input("请输入要下的位置:")
if position == '' or int(position) not in range(1, 10):
print("请输入一个正确的位置")
continue
elif theboard[position] != ' ':
print("这个位置已经下过了,请重新选一个位置下:")
continue
else:
theboard[position] = 'O'
break
else:
theboard[chess_algorithm(theboard, 'O', 'X')] = turn
printtheboard(theboard)
winner = winnercheck(theboard)
if winner != '':
break
if turn == 'X':
turn = 'O'
else:
turn = 'X'
return winner
while True:
theboard = {'1': ' ', '2': ' ', '3': ' ',
'4': ' ', '5': ' ', '6': ' ',
'7': ' ', '8': ' ', '9': ' ', }
winner = ''
order = input("请选择您是先手(X)还是后手(O):")
while True:
if order in ['先手', 'xianshou', 'x', 'X']:
winner = loop1(theboard)
break
elif order in ['后手', 'houshou', 'o', 'O']:
winner = loop2(theboard)
break
else:
order = input("请输入正确内容:")
print("游戏结束")
if winner != '':
print(winner + "方胜!")
else:
print("平局!")
again = input("需要再玩一次吗?(Y/N):")
if again in ['y', 'Y']:
continue
elif again in ['n', 'N']:
print("好的,感谢参与,欢迎再次进行游戏!")
break
else:
print("不好意思,您输入有误,那就再玩一次吧!")
实际的游戏情形是这样的:
不过话又说回来,这还是我第一次真正自己用程序语言创作一个游戏。虽然有些简陋,但之后应该还会对它进行改进吧 。