人工智能_奥赛罗棋
B y QQ:231469242
欢迎共同爱好者交流,并改进代码 奉承开源精神,公开源码。
我赢了电脑(低级人工智能),computerMove()算法需要不断改进
测试中注意:走棋格式64 或55等等两个连接数字,但必须要吃对方子,否则电脑会提示无效走棋
黑白棋,又叫反棋(Reversi)、奥赛罗棋(Othello)、苹果棋或翻转棋。黑白棋在西方和日本很流行。游戏通过相互翻转对方的棋子,最后以棋盘上谁的棋子多来判断胜负。它的游戏规则简单,因此上手很容易,但是它的变化又非常复杂。有一种说法是:只需要几分钟学会它,却需要一生的时间去精通它。
黑白棋是19世纪末英国人发明的。直到上个世纪70年代日本人长谷川五郎将其进行发展和推广,借用莎士比亚名剧奥赛罗(othello)为这个游戏重新命名(日语“オセロ”),也就是现在大家玩的黑白棋。为何借用莎士比亚名剧呢?是因为奥赛罗是莎士比亚一个名剧的男主角。他是一个黑人,妻子是白人,因受小人挑拨,怀疑妻子不忠一直情海翻波,最终亲手把妻子杀死。后来真相大白,奥赛罗懊悔不已,自杀而死。黑白棋就是借用这个黑人白人斗争的故事而命名。
游戏规则
黑白棋的棋盘是一个有8*8方格的棋盘。下棋时将棋下在空格中间,而不是像围棋一样下在交叉点上。开始时在棋盘正中有两白两黑四个棋子交叉放置,黑棋总是先下子。 下子的方法
把自己颜色的棋子放在棋盘的空格上,而当自己放下的棋子在横、竖、斜八个方向内有一个自己的棋子,则被夹在中间的全部翻转会成为自己的棋子。并且,只有在可以翻转棋子的地方才可以下子。 棋规
1.棋局开始时黑棋位于e4和d5,白棋位于d4和e5,如图1-3所示。 2.黑方先行,双方交替下棋。
3.一步合法的棋步包括:在一个空格新落下一个棋子,并且翻转对手一个或多个棋子。
4.新落下的棋子与棋盘上已有的同色棋子间,对方被夹住的所有棋子都要翻转过来。可以是横着夹,竖着夹,或是斜着夹。夹住的位置上必须全部是对手的棋子,不能有空格。
5.一步棋可以在数个方向上翻棋,任何被夹住的棋子都必须被翻转过来,棋手无权选择不去翻某个棋子。
6.除非至少翻转了对手的一个棋子,否则就不能落子。如果一方没有合法棋步,也就是说不管他下到哪里,都不能至少翻转对手的一个棋子,那他这一轮只能弃权,而由他的对手继续落子直到他有合法棋步可下。
7.如果一方至少有一步合法棋步可下,他就必须落子,不得弃权。 8.棋局持续下去,直到棋盘填满或者双方都无合法棋步可下。 胜负规则 规则
如果玩家在棋盘上没有地方可以下子,则该玩家对手可以连下。双方都没有棋子可以下时棋局结束,以棋子数目来计算胜负,棋子多的一方获胜。
在棋盘还没有下满时,如果一方的棋子已经被对方吃光,则棋局也结束。将对手棋子吃光的一方获胜。
翻转棋类似于棋盘游戏“奥赛罗 (Othello)”,是一种得分会戏剧性变化并且需
要长时间思考的策略性游戏。
翻转棋的棋盘上有 64 个可以放置黑白棋子的方格(类似于国际象棋和跳棋)。游戏的目标是使棋盘上自己颜色的棋子数超过对手的棋子数。 该游戏非常复杂,其名称就暗示着结果的好坏可能会迅速变化。
当游戏双方都不能再按规则落子时,游戏就结束了。通常,游戏结束时棋盘上会摆满了棋子。结束时谁的棋子最多谁就是赢家。 玩法 每个“翻转棋”游戏开始时,棋盘上已经交叉放好了四颗棋子。其中两颗是黑棋,另两颗是白棋。黑棋总是先走。
当您的棋子在某一直线方向包围了对手的棋子时,就可以翻转这些棋子的颜色,使它们成为您方的颜色。例如,如果您执黑棋,并且看到在一排白棋的某一端是一颗黑棋,那么当您将一颗黑棋放在这一排的另一端时,所有的白棋都将翻转并变为黑棋!
所有的直线方向均有效:水平、垂直和斜线方向。 走棋的唯一规则是只能走包围并翻转对手的棋子。每一回合都必须至少翻转一颗对手的棋子。
按规则不能再走棋时,这一回合弃权。计算机会自动将控制权交给对方。 得分
“翻转棋”的计分方法非常简单。游戏过程中随时都显示黑棋数与白棋数。结束时棋数最多的一方就是赢家。 游戏进行过程中,每一回合的得分都可能会发生急剧的变化。然而那正是游戏的乐趣所在! 计分方法
现行的规定是:双方分先下偶数局数的棋(如4局),胜1分,负0分,和0.5分,分数多的取胜。假如分数一样,就以棋子数目来计算胜负。那么棋子数目怎么判断呢?如果双方将棋盘下满,当然好判断。如果棋局结束,黑棋有34个子,白棋有30个子,那么黑棋胜34-30=4个子。但是如果棋盘没下满呢,如黑棋有34个子,白棋有27个子。此时有两种计分方法:日本的计分法是,剩余的空格全部给胜方,那么黑棋胜34+3-27=10个子。欧洲的计分法是,剩余的空格双方各得一半,那么黑棋胜(34+1.5)-(27+1.5)=7个子。
赛事: 5 中国
重庆黑白棋公开赛 广州黑白棋公开赛 上海黑白棋公开赛 北京黑白棋公开赛 天津黑白棋公开赛
香港黑白棋公开赛(HKOC) 日本
オセロ名人戦
全日本选手権大会 グランドオセロ大会 全日本小学校别団体戦 全日本マスターオープン 美国
美国黑白棋公开赛(USOC) 新加坡
新加坡黑白棋公开赛(SOC) 世界
黑白棋世界赛(WOC)
8相关程序
WZebra
黑白棋程序“斑马”(WZebra)是Zebra的Windows版本,除了可以下棋外,还提供了打谱、复盘、棋局析、自我学习等功能,也可以加载Thor棋谱文件,进行针对性训练。在标准比赛时间(2x15分钟)内,其搜索深度可达中局18至27步、终局24至31步。WZebra是自由软件,提供了中文版本,并提供中文帮助。 S.A.I.O.
Saio是System Artificial Intelligence Othello缩写,是黑白棋程式之一。并不开放下载。
代码需要不断改进
#improved by Toby #QQ :231469242 #Reversi
import random import sys
def getNewBoard():
#creates a brand new,blank board data structure board=[]
for i in range(8):
board.append([' ']*8)
return board '''
>>> getNewBoard()
[[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']] >>> '''
def drawBoard(board):
#This function prints out the board that it was passed.returns None HLINE=' +---+---+---+---+---+---+---+---+' VLINE=' | | | | | | | | |'
print (' 1 2 3 4 5 6 7 8') print(HLINE)
for y in range(8): print (VLINE)
print (y+1,end=' ') for x in range(8):
print ('| %s'%(board[x][y]),end=' ') #display '|' and 3 spaces
print('|') print(VLINE) print(HLINE) '''
>>> drawBoard(mainBoard)
1 2 3 4 5 6 7 8 +---+---+---+---+---+---+---+---+ | | | | | | | | | 1 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 2 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 3 | | | | | | | | |
| | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 4 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 5 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 6 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 7 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 8 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ >>>
def drawBoard(board):
#This function prints out the board that it was passed.returns None HLINE=' +---+---+---+---+---+---+---+---+' VLINE=' | | | | | | | | |'
print (' 1 2 3 4 5 6 7 8') print(HLINE)
for y in range(8): print (VLINE)
print (y+1,end='') print(VLINE) print(VLINE) print(HLINE) >>> drawBoard(mainBoard)
1 2 3 4 5 6 7 8 +---+---+---+---+---+---+---+---+ | | | | | | | | | 1 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+
| | | | | | | | | 2 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 3 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 4 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 5 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 6 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 7 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 8 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ >>> '''
def resetBoard(board):
#blank out the board it is passed,except for the original starting position
for x in range(8): for y in range(8): board[x][y]=' '
#starting pieces: board[3][3]='X' board[3][4]='O'
board[4][3]='O' board[4][4]='X' '''
>>> resetBoard(mainBoard) >>> drawBoard(mainBoard)
1 2 3 4 5 6 7 8 +---+---+---+---+---+---+---+---+ | | | | | | | | | 1 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 2 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 3 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 4 | | | | X | O | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 5 | | | | O | X | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 6 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 7 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 8 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ >>> '''
def playAgain():
print ('Do you want to play again?(yes or no)') return input().lower().startswith('y') ''' >>>
>>> playAgain()
Do you want to play again?(yes or no) y True
>>> playAgain()
Do you want to play again?(yes or no) n False
>>> playAgain()
Do you want to play again?(yes or no) f False >>> '''
def whoGoesFirst():
print('Do you want to play first?') if input().lower().startswith('y'): return 'player' else:
return 'computer'
'''
>>> whoGoesFirst()
Do you want to play first? y
'player'
>>> whoGoesFirst()
Do you want to play first? n
'computer' >>> '''
def isOnBoard(x,y):
#return True if the coordinates are located on the board return x>=0 and x<=7 and y>=0 and y<=7 '''
>>> isOnBoard(2,1) True
>>> isOnBoard(9,9) False >>> '''
def enterPlayerTile():
#let's the player type which they want to be
#returns a list with the player's tile as the first item,and the computer's tile as the second tile=''
while not(tile=='X' or tile=='O'): print('Do you want to be X or O?') tile=input().upper()
#the first element in the tuple is the player's tile,the second is the computer's tile if tile=='X':
return ['X','O'] else:
return ['O','X'] '''
>>> enterPlayerTile() Do you want to be X or O? x
['X', 'O'] >>> '''
def getBoardCopy(board):
#Make a duplicate of the board list and return the duplicate dupeBoard=getNewBoard()
for x in range(8):
for y in range(8):
dupeBoard[x][y]=board[x][y] return dupeBoard '''
>>> resetBoard(mainBoard) >>> drawBoard(mainBoard)
1 2 3 4 5 6 7 8 +---+---+---+---+---+---+---+---+ | | | | | | | | | 1 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 2 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 3 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 4 | | | | X | O | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 5 | | | | O | X | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 6 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 7 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 8 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+
>>> getBoardCopy(mainBoard)
[[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', 'X', 'O', ' ', ' ', ' '],
[' ', ' ', ' ', 'O', 'X', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']]
>>> drawBoard(getBoardCopy(mainBoard)) 1 2 3 4 5 6 7 8 +---+---+---+---+---+---+---+---+ | | | | | | | | | 1 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 2 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 3 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 4 | | | | X | O | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 5 | | | | O | X | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 6 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 7 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 8 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+
>>> '''
def getScoreOfBoard(board):
#determine the score by counting the tiles.returns a dictionary with keys 'X'and 'O' xscore=0 oscore=0
for x in range(8): for y in range(8):
if board[x][y]=='X': xscore+=1
if board[x][y]=='O': oscore+=1
return {'X':xscore,'O':oscore} '''
>>> resetBoard(mainBoard)
>>> getScoreOfBoard(mainBoard) {'X': 2, 'O': 2} >>> '''
def isOnCorner(x,y):
return (x==0 and y==0)or(x==7 and y==0)or(x==0 and y==7)or(x==7 and y==7) ''' >>>
>>> isOnCorner(7,7) True
>>> isOnCorner(0,0) True
>>> isOnCorner(3,4) False >>> '''
def showPoints(playerTile,computerTile): #print out the current score
scores=getScoreOfBoard(mainBoard) print('You have %s points.The computer has %s points.' %(scores[playerTile],scores[computerTile]))
'''
mainBoard=getNewBoard() resetBoard(mainBoard)
playerTile,computerTile=enterPlayerTile() >>>
Do you want to be X or O? x
>>> showPoints(playerTile,computerTile)
You have 2 points.The computer has 2 points. >>> '''
def isValidMove(board,tile,xstart,ystart):
#returns False if the player's move on space xstart,ystart is invalid #if it is a valid move,returns a list of spaces that would become the player'
#s if they made a move here.
if board[xstart][ystart]!=' ' or not isOnBoard(xstart,ystart): #board[xstart][ystart] must be an empty space and on the board return False
board[xstart][ystart]=tile #temporarily set the tile on the board,it will be emptied later
if tile=='X': otherTile='O' else:
otherTile='X'
tilesToFlip=[] for xdirection,ydirection in [[0,1],[1,1],[1,0],[1,-1],[0,-1],[-1,-1],[-1,0],[-1,1]]: x,y=xstart,ystart
x+=xdirection #first step in the direction y+=ydirection #first step in the direction
if isOnBoard(x,y) and board[x][y]==otherTile:
#there is a piece belonging to the other player next to our piece.
x+=xdirection y+=ydirection
if not isOnBoard(x,y):
continue #reach the bottom line,back to for loop while board[x][y]==otherTile: #proceeding if the next tile in the same direction is otherTile
#the while loop end when board[x][y] is empty or Tile x+=xdirection y+=ydirection
if not isOnBoard(x,y):#break out of while loop,then continue in for loop
break
if not isOnBoard(x,y): continue
if board[x][y]==tile:
#there are pieces to flip over,go in the reverse direction until we reach the original
#space,noting all the tiles along the way while True:
x-=xdirection y-=ydirection
if x==xstart and y==ystart: #append([x,y]) until reach the original space break
tilesToFlip.append([x,y])
board[xstart][ystart]=' ' #restore the empty space
if len(tilesToFlip)==0: #if no tiles were flipped,this is not a valid move
return False return tilesToFlip '''
Do you want to be X or O? x
1 2 3 4 5 6 7 8 +---+---+---+---+---+---+---+---+ | | | | | | | | | 1 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | |
2 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 3 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 4 | | | | X | O | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 5 | | | | O | X | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 6 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 7 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 8 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+
>>> isValidMove(mainBoard,'X',4,6) #mainBoard[4][2] correspond to the coordinate [5][3] False
>>> isValidMove(mainBoard,'X',5,3) [[4, 3]]
>>> isValidMove(mainBoard,'X',3,5) [[3, 4]]
>>> isValidMove(mainBoard,'O',4,3) False '''
def getValidMoves(board,tile):
#returns a list of [x,y] lists of valid moves for the given player on the given board validMoves=[]
for x in range(8):
for y in range(8):
if isValidMove(board,tile,x,y)!=False: validMoves.append([x,y])
return validMoves ''' >>>
Do you want to be X or O? x
1 2 3 4 5 6 7 8 +---+---+---+---+---+---+---+---+ | | | | | | | | | 1 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 2 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 3 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 4 | | | | X | O | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 5 | | | | O | X | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 6 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 7 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 8 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+
>>> getValidMoves(mainBoard,'X') [[2, 4], [3, 5], [4, 2], [5, 3]] '''
def getBoardWithValidMoves(board,tile):
#returns a new board with . marking the valid moves the given player can make
dupeBoard=getBoardCopy(board)
for x,y in getValidMoves(dupeBoard,tile): dupeBoard[x][y]='.' return dupeBoard '''
>>> getBoardWithValidMoves(mainBoard,'X') [[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', '.', ' ', ' ', ' '], [' ', ' ', ' ', 'X', 'O', '.', ' ', ' '], [' ', ' ', '.', 'O', 'X', ' ', ' ', ' '], [' ', ' ', ' ', '.', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']]
1 2 3 4 5 6 7 8 +---+---+---+---+---+---+---+---+ | | | | | | | | | 1 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 2 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 3 | | | | | . | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 4 | | | | X | O | . | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | |
5 | | | . | O | X | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 6 | | | | . | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 7 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 8 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ '''
def makeMove(board,tile,xstart,ystart):
#place the tile on the board at xstart,ystart,and flip any of the opponent's piece
#return False if this is an invalid move,True if it is valid tilesToFlip=isValidMove(board,tile,xstart,ystart)
if tilesToFlip==False: return False
board[xstart][ystart]=tile for x,y in tilesToFlip: board[x][y]=tile return True '''
1 2 3 4 5 6 7 8
+---+---+---+---+---+---+---+---+ | | | | | | | | | 1 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 2 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 3 | | | | | . | | | |
| | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 4 | | | | X | O | . | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 5 | | | . | O | X | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 6 | | | | . | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 7 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 8 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ >>> makeMove(mainBoard,'X',4,2) True
>>> drawBoard(mainBoard)
1 2 3 4 5 6 7 8 +---+---+---+---+---+---+---+---+ | | | | | | | | | 1 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 2 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 3 | | | | | X | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 4 | | | | X | X | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | |
5 | | | | O | X | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 6 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 7 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | 8 | | | | | | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+ >>> '''
def getPlayerMove(board,playerTile): #let the player type in their move
#returns the move as [x,y] (or returns the strings 'hints' or 'quit') DIGITS1TO8='1 2 3 4 5 6 7 8'.split() while True:
print ('Enter your move,or type quit to end the game, or hints turn off/on hints.')
move=input().lower() if move=='quit': return 'quit' if move=='hints': return 'hints'
if len(move)==2 and move[0] in DIGITS1TO8 and move[1] in DIGITS1TO8:
x=int(move[0])-1 y=int(move[1])-1
if isValidMove(board,playerTile,x,y)==False: continue else: break else:
print ('That is not a valid move.Type the x digit(1-8),then the y digit(1-8).')
print ('for example,81 will be the top-right corner.') return [x,y] '''
>>> getPlayerMove(mainBoard,'X')
Enter your move,or type quit to end the game, or hints turn off/on hints. 88
Enter your move,or type quit to end the game, or hints turn off/on hints. 8 8
That is not a valid move.Type the x digit(1-8),then the y digit(1-8). for example,81 will be the top-right corner.
Enter your move,or type quit to end the game, or hints turn off/on hints. 53 [4, 2] >>> '''
def getComputerMove(board,computerTile):
#move and return that move as a [x,y] list
possibleMoves=getValidMoves(board,computerTile)
#randomize the order of the possible moves,in order to not guess random.shuffle(possibleMoves)
#always go for a corner if available for x,y in possibleMoves: if isOnCorner(x,y): return [x,y]
#go through all the possible moves and remember the best scoring move bestScore=-1
for x,y in possibleMoves:
dupeBoard=getBoardCopy(board)
makeMove(dupeBoard,computerTile,x,y)
score=getScoreOfBoard(dupeBoard)[computerTile] if score>bestScore: bestMove=[x,y] bestScore=score return bestMove '''
>>> getComputerMove(mainBoard,'O') [2, 3] >>>
'''
def getRandomMove(board,tile): #return a random move
return random.choice(getValidMoves(board,tile))
def isOnSide(x,y):
return x==0 or x==7 or y==0 or y==7
def getCornerSideBestMove(board,tile):
#return a corner move,side move,or the best move possibleMoves=getValidMoves(board,tile)
#randomize the order of the possible moves random.shuffle(possibleMoves)
#always go for corner if possible for x,y in possibleMoves: if isOnCorner(x,y): return [x,y]
#if there is no corner,return a side move for x,y in possibleMoves: if isOnSide(x,y): return [x,y]
return getComputerMove(board,tile)
def getSideBestMove(board,tile):
#return a side move,corner move ,or the best move possibleMoves=getValidMoves(board,tile)
#randomize the order of the possible moves random.shuffle(possibleMoves)
#return a side move if possible for x,y in possibleMoves: if isOnSide(x,y): return [x,y]
def getWorstMove(board,tile):
#return the move that flips the least number of tiles possibleMoves=getValidMoves(board,tile)
#randomize the order of the possible moves random.shuffle(possibleMoves)
#go through all the possible moves and remember the least scores move worstScore=64
for x,y in possibleMoves:
dupeBoard=getBoardCopy(board) makeMove(dupeBoard,tile,x,y)
score=getScoreOfBoard(dupeBoard)[tile] if score #return a corner,a space, or the move that flips the least number of tiles possibleMoves=getValidMoves(board,tile) #randomize the order of the possible moves random.shuffle(possibleMoves) #always go for corner if available for x,y in possibleMoves: if isOnCorner(x,y): return [x,y] return getWorstMove(board,tile) while True: #reset the board and game mainBoard=getNewBoard() resetBoard(mainBoard) playerTile,computerTile=enterPlayerTile() showHints=False turn=whoGoesFirst() print('The'+turn+'will go first') while True: if turn=='player': #player's turn if showHints: validMovesBoard=getBoardWithValidMoves(mainBoard,playerTile) drawBoard(validMovesBoard) else: drawBoard(mainBoard) showPoints(playerTile,computerTile) move=getPlayerMove(mainBoard,playerTile) if move=='quit': print('Thanks for playing') elif move=='hints': showHints=not showHints continue else: makeMove(mainBoard,playerTile,move[0],move[1]) if getValidMoves(mainBoard,computerTile)==[]: break else: turn='computer' else: #computer's turn drawBoard(mainBoard) showPoints(playerTile,computerTile) input('Press Enter to see the computer\\'smove.') x,y=getComputerMove(mainBoard,computerTile) makeMove(mainBoard,computerTile,x,y) if getValidMoves(mainBoard,playerTile)==[]: break else: turn='player' #display the final score drawBoard(mainBoard) scores=getScoreOfBoard(mainBoard) print('X scored %s points. O scored %s points.'%(scores['X'],scores['O'])) if scores[playerTile]>scores[computerTile]: print('You beat the computer by %s points! congratulations!'%(scores[playerTile]-scores[computerTile])) elif scores[computerTile] print ('The game was a tie!') if not playAgain(): break 因篇幅问题不能全部显示,请点此查看更多更全内容