python简单网格五子棋_python控制台简单实现五子棋

#棋盘

#落子

#规则

import random

class chess:

def __init__(self):

print('#---------------棋盘----------------#')

self.grid = []

self.si = []

for i in range(8):

self.grid.append([' □ ']*8)

for j in range(8):

print(''.join(self.grid[j]),'\n') #''.join(grid[j]))

def getPos(self,x,y):

print('#----------------落子----------------#')

self.x = x

self.y = y

if self.grid[self.x][self.y]== ' ○ ' or self.grid[self.x][self.y]== ' ● ': # 判断棋子是否重复

print('\t\t\t棋子有重复')

else:

self.l = 0

self.grid[self.x][self.y] = ' ● '

if self.x==7 or self.y==0 :

self.grid[random.randint(self.x-2,self.x-1)][random.randint(self.y-2,self.y-1)] = ' ○ '

else:

self.grid[self.x][self.y] = ' ● '

self.grid[self.x + 1][ self.y + 1] =' ○ '

self.si.append([self.x, self.y])

for i in range(len(self.si)):

if self.si[i-1][0] == self.si[i][0] or self.si[i-1][1] == self.si[i][1] or self.si[i][1]==self.si[i][0] and self.si[i-1][1]==self.si[i-1][0] and (self.grid[self.x-1][self.y] != ' □ ' or self.grid[self.x+1][self.y+1] != ' □ '

or self.grid[self.x-1][self.y-1] != ' □ '

or self.grid[self.x+1][self.y] != ' □ '

or self.grid[self.x-1][self.y] != ' □ '

or self.grid[self.x][self.y-1] != ' □ ' or self.grid[self.x][self.y+1] != ' □ '

or self.grid[self.x][self.y-1] != ' □ '): # 判断玩家是否排成一条线

self.l += 1

for j in range(8):

print(''.join(self.grid[j])+'\n') #''.join(grid[j]))

def rulers(self):

recode = []

recode.append([self.x,self.y])

print(self.si)

for i in range(len(recode)):

for j in range(len(recode[i])):

if self.l==5: # 标记,当为5时结束游戏

print('游戏结束')

print('玩家胜利')

self.t = False

return self.t

elif len(self.si)>6: # 判断平局,因为条件简单,所以不会出现电脑赢的情况

print('游戏结束')

print('平局')

self.t = False

return self.t

else:

self.t = True

return self.t

play = chess()

t = True

while t: # 循环实现棋盘出现

x,y = input("\t请输入x,y坐标用','分割:\n").split(',')

play.getPos(int(x),int(y))

t = play.rulers()

这只是简单练习

小白一枚,AI训练方式还未学习,后面会持续出更

继续努力

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值