python制作简单五子棋游戏
作者:Eric· Charles 时间:2021-09-13 06:49:47
本文实例为大家分享了python五子棋游戏的具体代码,供大家参考,具体内容如下
#五子棋
‘''
矩阵做棋盘 16*16 “+”
打印棋盘 for for
游戏是否结束
开始下棋 while 游戏是否结束:
黑白交替 player=0 p%2==0 ==1 p+=1
下棋动作一样 但是棋子不一样
‘''
代码
#创建棋盘的程序
def initBoard():
global board #调用全局的board
board=[None]*16
for i in range(len(board)):
board[i]=["+ "]*16
#打印棋盘的程序
def printBoard():
global board
for i in range(len(board)):
for j in range(len(board[i])):
print(board[i][j],end=" ")
print("")
#开始下棋的程序
def startGame():
global board
player=0
while isGameContinue():
if player%2==0:
#黑方下棋
print("==>黑方下棋")
if not playChess("●"):
continue
else:
#白方下棋
print("==>白方下棋")
if not playChess("○"):
continue
player+=1
def playChess(chess):
#获取位置
x=int(input("==> X="))-1
y=int(input("==> Y="))-1
if board[x][y]=="+ ":
board[x][y]=chess
printBoard()
return True #落子成功
else:
print("==> 已有棋子 请重新落子\a")
printBoard()
return False#落子失败
def isGameContinue():
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j]!="+ ":
#横向
if j<=11:
if board[i][j]==board[i][j+1]==board[i][j+2]==board[i][j+3]==board[i][j+4]:
whoWin(i,j)
return False
#竖向
if i<=11:
if board[i][j]==board[i+1][j]==board[i+2][j]==board[i+3][j]==board[i+4][j]:
whoWin(i,j)
return False
#反斜
if i<=11 and j<=11:
if board[i][j]==board[i+1][j+1]==board[i+2][j+2]==board[i+3][j+3]==board[i+4][j+4]:
whoWin(i,j)
return False
#正斜
if i>=4 and j<=11:
if board[i][j]==board[i-1][j+1]==board[i-2][j+2]==board[i-3][j+3]==board[i-4][j+4]:
whoWin(i,j)
return False
return True
def whoWin(i,j):
if board[i][j]=="●":
print("黑方胜!")
else:
print("白方胜!")
for i in range(10):
print("\a")
board=[]
initBoard()
printBoard()
startGame()
来源:https://blog.csdn.net/qq_42197548/article/details/85073198
标签:python,五子棋
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中常用操作字符串的函数与方法总结
2023-07-25 12:09:23
ubuntu 18.04 安装opencv3.4.5的教程(图解)
2022-10-29 16:36:01
![](https://img.aspxhome.com/file/2023/6/90566_0s.jpg)
Python对list列表结构中的值进行去重的方法总结
2021-10-18 04:09:04
关于大批量数据高效插入方法
2010-07-31 19:07:00
将滚动条(scrollbar)保持在最底部的方法
2008-02-21 10:05:00
WxPython界面利用pubsub如何实现多线程控制
2021-01-28 08:10:03
![](https://img.aspxhome.com/file/2023/7/77877_0s.png)
python pandas中的agg函数用法
2023-07-20 09:40:08
![](https://img.aspxhome.com/file/2023/8/61178_0s.png)
Pandas 同元素多列去重的实例
2023-02-09 21:03:32
用面包屑来简化多层的tabs
2011-06-16 20:32:54
Asp模板制作方法详解
2007-10-11 19:05:00
使用php shell命令合并图片的代码
2023-06-21 22:34:35
详解python多线程、锁、event事件机制的简单使用
2022-03-16 19:48:25
Python中那些 Pythonic的写法详解
2023-09-14 20:01:59
Centos 6.4 安装Python 2.7 python-pip的详细步骤
2023-12-01 10:21:03
Python version 2.7 required, which was not found in the registry
2021-06-02 14:57:13
![](https://img.aspxhome.com/file/2023/9/67929_0s.png)
Golang 的defer执行规则说明
2023-07-14 08:31:10
python计数排序和基数排序算法实例
2023-11-01 01:23:26
ASP程序直接连接MYSQL数据库
2008-10-03 12:13:00
帮你六步改善SQL Server安全规划全攻略
2009-01-20 15:07:00
讲解Python3中NumPy数组寻找特定元素下标的两种方法
2023-07-17 09:58:42
![](https://img.aspxhome.com/file/2023/0/65540_0s.png)