三连棋(python 3.2),连棋python3.2,python 3.2学p
三连棋(python 3.2),连棋python3.2,python 3.2学p
python 3.2
学python的第一段完整的代码,费了蛮多时间的。
代码比较简单,没有复杂的语法。
适合初学者。
注:落子得位置和小键盘一致。带简单的AI,一不小心是会被电脑下过的哦!!!
'''Created on 2011-5-5@author: Administrator'''import randomdef drawBroad(broad): print('-----------') print(' '+broad[7]+' | '+broad[8]+' | '+broad[9]) print('-----------') print(' '+broad[4]+' | '+broad[5]+' | '+broad[6]) print('-----------') print(' '+broad[1]+' | '+broad[2]+' | '+broad[3]) print('-----------')def chooseLetter(): print('before the game ,please choose the letter "X" or "O"("X will be first"): ') letter = input().strip().upper()[0] print("you choose :"+letter) if (letter == 'X'): return 'X' elif (letter == 'O'): return 'O' else: chooseLetter()#if has win return letter#else return Ndef checkWin(broad,letter): if ((broad[1] ==letter and broad[2] ==letter and broad[3] ==letter ) or (broad[4] ==letter and broad[5] ==letter and broad[6] ==letter ) or (broad[7] ==letter and broad[8] ==letter and broad[9] ==letter ) or (broad[1] ==letter and broad[4] ==letter and broad[7] ==letter ) or (broad[2] ==letter and broad[5] ==letter and broad[8] ==letter ) or (broad[3] ==letter and broad[6] ==letter and broad[9] ==letter ) or (broad[7] ==letter and broad[5] ==letter and broad[3] ==letter ) or (broad[1] ==letter and broad[5] ==letter and broad[9] ==letter ) ): return letter else: return 'N'def hasLetter(broad,location): return broad[location] != ' '# return freeListdef FreeList(broad): freeList=[] for i in range(1,10): if(not hasLetter(broad,i)): freeList.append(i) return freeListdef PlayerMove (broad,letter): if(FreeList(broad) == []): print("tie !!!") return 'T' playlocation = int(input("your turn:").strip()[0]) print("***in PlayerMove() ***") #if (playlocation in list(range(1,10))and not hasLetter(broad,playlocation)): if(playlocation in FreeList(broad)): broad[playlocation] = letter drawBroad(broad) if(checkWin(broad,letter)!= 'N'): print("you win") return letter else: PlayerMove(broad,letter)def NextPlay(letter): if(letter == 'X'): return 'O' else: return 'X'def ComputerMove(broad, computerLetter): #1,try to Computer win #2,try to break player win #3,random location if(FreeList(broad) == []): print("tie !!!") return 'T' print("***in ComputerMove() ***") if(WillWin(broad,computerLetter)!=0): broad[WillWin(broad,computerLetter)]=computerLetter print("computer win") return computerLetter elif(WillWin(broad,NextPlay(computerLetter)!=0)): broad[WillWin(broad,NextPlay(computerLetter))]=computerLetter else: broad[random.choice(FreeList(broad))]=computerLetter drawBroad(broad)# copy boarddef BoardTemp(board): boardTemp = {} for i in range(1,10): boardTemp[i] = board[i] return boardTemp# if will win return location# else return def WillWin(board, Letter): boardTemp=BoardTemp(board) for i in FreeList(board): boardTemp[i]=Letter if(checkWin(boardTemp,Letter) != 'N'): #drawBroad(boardTemp) return i else: boardTemp[i]=" " return 0def GameInit(): broad = {} for i in range(1,10): broad[i]=' ' drawBroad(broad) return broad def GameBody(board,letter): templetter=letter rs = '' while(True): rs = ComputerMove(board, templetter) if(rs is not None): break templetter = NextPlay(templetter) rs = PlayerMove(board,templetter) if(rs is not None): break templetter = NextPlay(templetter) return rsdef GameStart(): broad = GameInit() playerletter = chooseLetter() if(playerletter == 'X'): PlayerMove(broad,playerletter) rs = GameBody(broad,NextPlay(playerletter)) print("rs:"+rs) print(FreeList(broad))GameStart()#该片段来自于http://byrx.net
相关内容
- Inverse power method for solving the eigenvalue problem in Python,i
- python拆分中英文混合字符串 - 按中文拆分,python中英文
- python语言解决《Ruby太慢了》问题,,python 2.6ec
- Python 处理图像几何变换,python图像几何变换,import Ima
- python递归解决0-1背包问题,python递归0-1背包,#coding:utf
- 检查小米手机是否可以购买,检查小米手机购买,饥饿营
- 多线程,限制线程数运行,,多线程限制线程数,#encoding
- 判断图片分辨率并修改,判断图片分辨率,碰到许多图片
- 使用python测测你的系统最多能创建多少个线程,python线
- 一个简单的MySQLdb封装类(3.2),mysqldb封装类3.2,__author__
评论关闭