编写五子棋的完整python代码控制台版ai_python实现的控制台五子棋

1.[代码][Python]代码

import os

import pdb

'''

@author 阿肥(是我的猫)

@date 2014年6月16日

@title 双人五子棋

@modified

2014年6月16日 最初版本

'''

class five:

def __init__(self,maxx,maxy):

self.maxx=maxx

self.maxy=maxy

self.qipan=[]

for i in range(maxx):

self.qipan.append([])

for j in range(maxy):

self.qipan[i].append(0)

def start(self):

'''

初始化测试

'''

#self.qipan[1][1]=self.qipan[2][1]=self.qipan[3][1]=self.qipan[4][1]=self.qipan[5][1]=1

who=False

os.system('cls')

self.printqp()

while True:

t=input('Please input(x,y),now is'+('〇' if who else '乂')+':')

t=t.split(',')

if len(t)==2:

x=int(t[0])

y=int(t[1])

if self.qipan[x][y]==0:

self.qipan[x][y]=1 if who else 2

os.system('cls')

self.printqp()

ans=self.isWin(x,y)

if ans:

print(('〇'if who else '乂')+'Win')

break

who=not who

os.system('pause')

def isWin(self,xPoint,yPoint):#判赢

pdb.set_trace

flag=False

t=self.qipan[xPoint][yPoint]

x=xPoint

y=yPoint

#横向

count=0

x=xPoint

y=yPoint

while (x>0 and t==self.qipan[x][y]):

count+=1

x-=1

x=xPoint

y=yPoint

while (x

count+=1

x+=1

if (count>5):return True

#纵向

count=0

x=xPoint

y=yPoint

while (y>0 and t==self.qipan[x][y]):

count+=1

y-=1

y=yPoint

while (y

count+=1

y+=1

if (count>5): return True

#/

count=0

x=xPoint

y=yPoint

while (x>0 and y

count+=1

x+=1

y-=1

x=xPoint

y=yPoint

while (x0 and t==self.qipan[x][y]):

count+=1

x-=1

y+=1

if (count>5):return True

#\

count=0

x=xPoint

y=yPoint

while (x>0 and y>0 and t==self.qipan[x][y]):

count+=1

x+=1

y-=1

x=xPoint

y=yPoint

while (x

count+=1

x-=1

y+=1

if (count>5): return True

return False

def printqp(self):#打印棋盘

print(' 〇一二三四五六七八九')

for i in range(self.maxx):

print(i,end='')

for j in range(self.maxy):

if self.qipan[i][j]==0:

print('十',end='')

elif self.qipan[i][j]==1:

print('〇',end='')

elif self.qipan[i][j]==2:

print('乂',end='')

print('\n')

if __name__=='__main__':

t=five(10,10)

#pdb.set_trace()

t.start()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值