人工智能奥赛罗棋.doc_第1页
人工智能奥赛罗棋.doc_第2页
人工智能奥赛罗棋.doc_第3页
人工智能奥赛罗棋.doc_第4页
人工智能奥赛罗棋.doc_第5页
已阅读5页,还剩22页未读 继续免费阅读

VIP免费下载

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

人工智能_奥赛罗棋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#Reversiimport randomimport sysdef 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%(boardxy),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): boardxy= #starting pieces: board33=X board34=O board43=O board44=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)yTrue playAgain()Do you want to play again?(yes or no)nFalse playAgain()Do you want to play again?(yes or no)fFalse 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?yplayer whoGoesFirst()Do you want to play first?ncomputer def isOnBoard(x,y): #return True if the coordinates are located on the board return x=0 and x=0 and y isOnBoard(2,1)True isOnBoard(9,9)False def enterPlayerTile(): #lets the player type which they want to be #returns a list with the players tile as the first item,and the computers 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 players tile,the second is the computers tile if tile=X: return X,O else: return O,X enterPlayerTile()Do you want to be X or O?xX, 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): dupeBoardxy=boardxy 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 Xand O xscore=0 oscore=0 for x in range(8): for y in range(8): if boardxy=X: xscore+=1 if boardxy=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. %(scoresplayerTile,scorescomputerTile) 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 players 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 boardxstartystart!= or not isOnBoard(xstart,ystart): #boardxstartystart must be an empty space and on the board return False boardxstartystart=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 boardxy=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 boardxy=otherTile: #proceeding if the next tile in the same direction is otherTile #the while loop end when boardxy 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 boardxy=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) boardxstartystart= #restore the empty space if len(tilesToFlip)=0: #if no tiles were flipped,this is not a valid move return False return tilesToFlipDo 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) #mainBoard42 correspond to the coordinate 53False isValidMove(mainBoard,X,5,3)4, 3 isValidMove(mainBoard,X,3,5)3, 4 isValidMove(ma

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论