1.采用列表
2.玩家先出,电脑其后
3.电脑落子规则:(1)直接落子成功(2)阻止玩家赢(3)随机落子
import copy
import random
def showchessboard(chessboardnext):
print((' | ' + ' | ' + ' |'))
print(str(chessboardnext[0]) + ' | ' + str(chessboardnext[1]) + ' | ' + str(
chessboardnext[2]) + ' |')
print((' | ' + ' | ' + ' |'))
print('———————————————————————————————————————')
print((' | ' + ' | ' + ' |'))
print(str(chessboardnext[3]) + ' | ' + str(chessboardnext[4]) + ' | ' + str(
chessboardnext[5]) + ' |')
print((' | ' + ' | ' + ' |'))
print('———————————————————————————————————————')
print((' | ' + ' | ' + ' |'))
print(str(chessboardnext[6]) + ' | ' + str(chessboardnext[7]) + ' | ' + str(
chessboardnext[8]) + ' |')
print((' | ' + ' | ' + ' |'))
#判空
def isNuLL(chessboard,location):
return chessboard[location]!='x'and chessboard[location]!='o'
#玩家落子
def playernow(chessboard):
location=int(input("Please input the location you choose"))
if isNuLL(chessboard,location):
chessboard[location] = 'x'
else:
print('The location is not NULL,please choose another one')
playernow(chessboard)
#电脑落子:直接落子获胜 阻止玩家落子 随机落子
#获取棋盘可落子位置
def locations(chessboard):
locs=[]
for i in range(0,9):
if chessboard[i]!='x' and chessboard[i]!='o':
locs.append(i)
return locs
#落子是否可以直接赢
def isgetwin(chessboard):
locs = locations(chessboard)
for i in locs:
chessboard[i]='o'
if isWin(chessboard,'o')==1:
print("The location computer choosed is " + str(i))
return 1
else:
chessboard[i]=i
return 0
#是否需要阻止玩家
def isstop(chessboard):
locs = locations(chessboard)
for j in locs:
chessboardnow[j] = 'x'
if isWin(chessboardnow, 'x') == 1:
print("The location computer choosed is " + str(j))
chessboardnow[j] = 'o'
return 1
else:
chessboardnow[j] = j
return 0
#电脑落子
def computernow(chessboard):
locs = locations(chessboard)
if isgetwin(chessboard)==0:
if isstop(chessboard)==0:
x = random.choice(locs)
print("The location computer choosed is " + str(x))
chessboard[x] = 'o'
#判赢
def isWin(chessboard,chess):
if (chessboard[0]==chessboard[1]==chessboard[2]==chess) or (chessboard[3]==chessboard[4]==chessboard[5]==chess) or (chessboard[6]==chessboard[7]==chessboard[8]==chess) or (chessboard[0]==chessboard[3]==chessboard[6]==chess) or(chessboard[1]==chessboard[4]==chessboard[7]==chess) or (chessboard[2]==chessboard[5]==chessboard[8]==chess) or (chessboard[0]==chessboard[4]==chessboard[8]==chess) or (chessboard[2]==chessboard[4]==chessboard[6]==chess):
# if chess=='x':
# print('Player Win')
# else:
# print('Computer Win')
return 1
else:
return 0
if __name__=='__main__':
# 棋盘初始化
chessboardnow = [0, 1, 2, 3, 4, 5, 6, 7, 8]
chessboardnext = copy.deepcopy(chessboardnow)
showchessboard(chessboardnext)
#先落子的最多下5次
times=0
lcs=locations(chessboardnow)
#玩家先落子
while times<5:
playernow(chessboardnow)
times+=1
chessboardnext = copy.deepcopy(chessboardnow)
showchessboard(chessboardnext)
if isWin(chessboardnow,'x')==1:
print('Game Over,Player Win!')
break
elif times<5:
computernow(chessboardnow)
chessboardnext = copy.deepcopy(chessboardnow)
showchessboard(chessboardnext)
if isWin(chessboardnow,'o')==1:
print('Game Over,Computer Win')
break
else:
times=5
if times==5 and isWin(chessboardnow,'x')!=1 :
print('游戏结束,平局')
# if lcs==[] and isWin(chessboardnow,'x')!=1 :
# print('游戏结束,平局')