qq聊天界面实现五子棋对战
搞完了微信的接口,再来玩一下qq接口,从网上找了一个终端五子棋的代码,同qq-bot结合,输入put+位置即可进行对战。
#! usr/bin/python
#coding=utf-8
from qqbot import QQBot
class chessBoard(object) :
def __init__(self,row,column) :
self.row = row
self.column = column
self.board = [["." for x in range(column+1)] for x in range(row+1) ]
self.board[0][0] = " "
for i in range(1,column+1) :
self.board[0][i] = chr(ord("a")+(i-1))
for i in range(1,row+1) :
self.board[i][0] = chr(ord("A")+(i-1))
self.turn = 0
self.printBoard()
self.deep = 4
self.bestX = None
self.bestY = None
#print the board
def printBoard(self) :
board=""
for i in range(self.row+1) :
st = " "
for j in range(self.column+1) :
if self.board[i][j]=='.':
st += '#'
else:
st += self.board[i][j]
st += " "
board += st + "\n"
print(st)
return board
# start game!
def gameBegin(self) :
self.turn = 0
def runGame(self, place, turn):
print "run game!"
self.turn = turn
if self.turn == 0 :
self.putCheese(place)
else :
self.putCheese(place)
if self.Win(place) :
print("No "+str(self.turn+1)+" has WIN!")
return 1
return 0
# put the cheese onto the board
def putCheese(self,palce) :
xIndex = palce[0]
yIndex = palce[1]
if self.canGOHere(xIndex,yIndex) == False :
raise Exception("Can't go here!Try again.")
return
xIndex = ord(xIndex) - ord("A")
yIndex = ord(yIndex) - ord("a")
if self.turn == 0 :
self.board[xIndex+1][yIndex+1] = "X"
else :
self.board[xIndex+1][yIndex+1] = "O"
# if you can go here
def canGOHere(self,xIndex,yIndex) :
xIndex = ord(xIndex) - ord("A")
yIndex = ord(yIndex) - ord("a")
if xIndex >= self.row or yIndex >= self.column or xIndex < 0 or yIndex < 0 :
return False
if self.board[xIndex+1][yIndex+1] != "." :
return False
return True
def calc(self,count,bian1,bian2) :
ma = 0
ma = max(ma,count+bian1+bian2)
if count == 3 and bian1==1 and bian2 == 1 :
ma = max(ma,100)
if count == 4 and bian1==1 and bian2 == 1 :
ma = max(ma,1000)
if count == 4 and bian1 + bian2 == 1 :
ma = max(ma,100)
if count == 1 and bian1 + bian2 == 0 :
ma = max(ma,1)
if count == 1 and bian1 + bian2 == 1 :
ma = max(ma,2)
if count == 1 and bian1 + bian2 == 2 :
ma = max(ma,5)
if count == 2 and bian1 + bian2 == 2 :
ma = max(ma,10)
if count == 3 and bian1 + bian2 == 1 :
ma = max(ma,50)
return ma
def evulate(self,t) :
ma = 0
row = self.row
column = self.column
for i in range(row) :
for j in range(column) :
xIndex = i
yIndex = j
label = "X" if t == 0 else "O"
count,bian1,bian2 = self.rowCount(xIndex+1,yIndex+1,label)
ma = max(ma,self.calc(count,bian1,bian2))
count,bian1,bian2 = self.columnCount(xIndex+1,yIndex+1,label)
ma = max(ma,self.calc(count,bian1,bian2))
count,bian1,bian2 = self.leftDuijiao(xIndex+1,yIndex+1,label)
ma = max(ma,self.calc(count,bian1,bian2))
count,bian1,bian2 = self.rightDuijiao(xIndex+1,yIndex+1,label)
ma = max(ma,self.calc(count,bian1,bian2))
return ma
def AI(self) :
t = self.turn
alpha = -100000
beta = 100000
self.dfs(4,t,alpha,beta)
palce = ""
palce += chr(self.bestX-1+ord("A"))
palce += chr(self.bestY-1+ord("a"))
return palce
def dfs(self,deep,t,alpha,beta) :
if deep <= 0 :
return self.evulate(t)
socre = -0x7fffffff
row = self.row
column = self.column
flag = 0
bestX = 0
bestY = 0
for i in range(1,row+1) :
if flag==1 :
break
for j in range(1,column+1) :
if flag==1 :
break
u = 0
if (i - 1 >= 1 and self.board[i-1][j] == ".") :
u += 1
if i + 1 <= row and self.board[i+1][j] == "." :
u += 1
if j - 1 >= 1 and self.board[i][j-1] == "." :
u += 1
if j + 1 <= column and self.board[i][j+1] == "." :
u += 1
if i - 1 >= 1 and j - 1 >=1 and self.board[i-1][j-1] == "." :
u += 1
if i + 1 <= row and j+1<=column and self.board[i+1][j+1] == "." :
u += 1
if j - 1 >= 1 and i + 1 <=row and self.board[i+1][j-1] == "." :
u += 1
if j + 1 <= column and i-1>=1 and self.board[i-1][j+1] == "." :
u += 1
if u == 8 :
continue
if self.board[i][j] == "." :
self.board[i][j] = "X" if t == 0 else "O"
score = - self.dfs(deep - 1, t^1 ,-beta, -alpha)
self.board[i][j] = "."
if score > alpha :
alpha = score
bestX = i
bestY = j
if alpha >= beta:
flag = 1
if deep == self.deep :
self.bestX = bestX
self.bestY = bestY
return alpha
# if can win
def Win(self,palce) :
xIndex = palce[0]
yIndex = palce[1]
xIndex = ord(xIndex) - ord("A")
yIndex = ord(yIndex) - ord("a")
label = "X" if self.turn == 0 else "O"
row = self.row
column = self.column
#heng xiang
if self.rowCount(xIndex+1,yIndex+1,label)[0] >= 5 :
return True
#zong xiang
if self.columnCount(xIndex+1,yIndex+1,label)[0] >= 5 :
return True
#zuo dui jiao
if self.leftDuijiao(xIndex+1,yIndex+1,label)[0] >= 5 :
return True
# you dui jiao
if self.rightDuijiao(xIndex+1,yIndex+1,label)[0] >= 5 :
return True
return False
def rowCount(self,xIndex,yIndex,label) :
row = self.row
column = self.column
x = xIndex-1
count = 1
bian1 = 0
bian2 = 0
while x >= 1 and self.board[x][yIndex] == label :
count += 1
x -= 1
if x >= 1 and self.board[x][yIndex] == "." :
bian1 = 1
x = xIndex + 1
while x <= row and self.board[x][yIndex] == label :
count += 1
x += 1
if x <= row and self.board[x][yIndex] == "." :
bian2 = 1
return count,bian1,bian2
def columnCount(self,xIndex,yIndex,label) :
row = self.row
column = self.column
count = 1
y = yIndex - 1
bian1 = 0
bian2 = 0
while y >= 1 and self.board[xIndex][y] == label :
count += 1
y -= 1
if y >= 1 and self.board[xIndex][y] == "." :
bian1 = 1
y = yIndex + 1
while y <= column and self.board[xIndex][y] == label :
y += 1
count += 1
if y <= column and self.board[xIndex][y] == "." :
bian2 = 1
return count,bian1,bian2
def leftDuijiao(self,xIndex,yIndex,label) :
row = self.row
column = self.column
count = 1
x = xIndex - 1
y = yIndex - 1
bian1 = 0
bian2 = 0
while y >= 1 and x >= 1 and self.board[x][y] == label :
count += 1
x -= 1
y -= 1
if y >= 1 and x >= 1 and self.board[x][y] == "." :
bian1 = 1
x = xIndex + 1
y = yIndex +1
while y <= column and x <= row and self.board[x][y] == label :
count += 1
x += 1
y += 1
if y <= column and x <= row and self.board[x][y] == "." :
bian2 = 1
return count,bian1,bian2
def rightDuijiao(self,xIndex,yIndex,label) :
row = self.row
column = self.column
count = 1
x = xIndex - 1
y = yIndex + 1
bian1 = 0
bian2 = 0
while y <= column and x >= 1 and self.board[x][y] == label :
count += 1
x -= 1
y += 1
if y <= column and x >= 1 and self.board[x][y] == "." :
bian1 = 1
x = xIndex + 1
y = yIndex -1
while y >= 1 and x <= row and self.board[x][y] == label :
count += 1
x += 1
y -= 1
if y >= 1 and x <= row and self.board[x][y] == "." :
bian2 = 1
return count,bian1,bian2
myqqbot = QQBot()
@myqqbot.On('qqmessage')
def handler(bot, message):
if message.content == '开始游戏':
global p
bot.SendTo(message.contact, 'Start new game!!')
p = chessBoard(7,7)
p.gameBegin()
bot.SendTo(message.contact, p.printBoard())
if message.content.startswith("put"):
global p
global turn
place = message.content.split(" ")[1]
print place
try:
if p.runGame(place, turn) == 1:
print "OK1"
bot.SendTo(message.contact, p.printBoard())
bot.SendTo(message.contact, "You win!!!")
else:
print "OK2"
bot.SendTo(message.contact, p.printBoard())
turn = turn^1
except Exception, e:
print str(e)
p = chessBoard(7,7)
turn = 0;
myqqbot.LoginAndRun()