亲宝软件园·资讯

展开

python实现双人五子棋(终端版)

Miku丨无形 人气:0
这篇文章主要为大家详细介绍了python实现终端版的双人五子棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

五子棋还是有很多大佬发的,今天我也发一篇萌新写的小五子棋
这个是终端版的,如果有问题请大家指出,我去修改

代码如下:

screen = []
screen_change=0

def into():#初始空白棋盘
 for i in range(screen_change):
 list_width=[]
 for j in range(screen_change):
  list_width.append(' '+' |')
 screen.append(list_width)

def user_black():
 while True:
 coordinate = input("黑方请输入你想填下子的坐标 例1,2或者5,5")
 coordinate=coordinate.split(',')
 width_num = int(coordinate[0]) - 1
 high_num = int(coordinate[1]) - 1
 if screen[width_num][high_num][1]!=' ':
  print('你输入的地方已经有棋子了,请重新输入')
  continue
 screen[width_num][high_num] = ' ' + '@' + ' |'
 break
 screen_print()

def user_white():
 while True:
 coordinate = input("白方请输入你想填下子的坐标 例1,2或者5,5")
 coordinate = coordinate.split(',')
 width_num = int(coordinate[0]) - 1
 high_num = int(coordinate[1]) - 1
 if screen[width_num][high_num][1] != ' ':
  print('你输入的地方已经有棋子了,请重新输入')
  continue
 screen[width_num][high_num] = ' ' + '#' + ' |'
 break
 screen_print()

def eeferee():#判断输赢
 #判断行
 for i in range(screen_change):
 for j in range(screen_change-4):
  if screen[i][j][1]==screen[i][j+1][1]==screen[i][j+2][1]==screen[i][j+3][1]==screen[i][j+4][1] and screen[i][j][1]!=' ':
  return False
 for i in range(screen_change - 4):
 for j in range(screen_change):
  if screen[i][j][1]==screen[i+1][j][1]==screen[i+2][j][1]==screen[i+3][j][1]==screen[i+4][j][1] and screen[i][j][1]!=' ':
  return False
 for i in range(screen_change - 4):
 for j in range(screen_change - 4):
  if screen[i][j][1]==screen[i+1][j+1][1]==screen[i+2][j+2][1]==screen[i+3][j+3][1]==screen[i+4][j+4][1] and screen[i][j][1]!=' ':
  return False
 for k in range(4,screen_change):
  if k>=4:
  if screen[i][k][1] == screen[i+1][k-1][1] == screen[i+2][k-2][1] == screen[i+3][k-3][1] == screen[i+4][k-4][1] and screen[i][k][1] != ' ':
   return False
 return True

def screen_print():#打印棋盘
 print('——' * (screen_change*3+1))
 for i in range(screen_change):
 print('|',end=' ')
 for j in range(screen_change):
  print(screen[i][j],end=' ')
 print()
 print('——'*(screen_change*3+1))

def main():
 global screen_change
 screen_change = int(input('请输入你设定的棋盘大小:'))
 into()
 screen_print()
 while eeferee():
 user_black()
 if not eeferee():
  break
 user_white()
 print('游戏结束')

if __name__=='__main__':
 main()

效果图如下:

加载全部内容

相关教程
猜你喜欢
用户评论