python制作五子棋_python制作简单五子棋游戏

本文实例为大家分享了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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

好的,以下是一个简单五子棋游戏Python代码: ```python import numpy as np # 初始化棋盘 def create_board(): board = np.zeros((15,15)) return board # 判断落子是否合法 def is_valid_move(board, row, col): if board[row][col] == 0: return True else: return False # 落子 def place_piece(board, row, col, piece): board[row][col] = piece # 判断是否有五子连珠 def check_win(board, piece): # 横向 for r in range(15): for c in range(11): if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece and board[r][c+4] == piece: return True # 纵向 for r in range(11): for c in range(15): if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece and board[r+4][c] == piece: return True # 斜向 for r in range(11): for c in range(11): if board[r][c] == piece and board[r+1][c+1] == piece and board[r+2][c+2] == piece and board[r+3][c+3] == piece and board[r+4][c+4] == piece: return True for r in range(11): for c in range(4, 15): if board[r][c] == piece and board[r+1][c-1] == piece and board[r+2][c-2] == piece and board[r+3][c-3] == piece and board[r+4][c-4] == piece: return True return False # 打印棋盘 def print_board(board): print(np.flip(board, 0)) # 主程序 def play_game(): board = create_board() game_over = False turn = 1 while not game_over: if turn == 1: row = int(input("Player 1, please enter row (0-14): ")) col = int(input("Player 1, please enter column (0-14): ")) if is_valid_move(board, row, col): place_piece(board, row, col, 1) if check_win(board, 1): print("Player 1 wins!") game_over = True turn = 2 else: print("Invalid move! Please try again.") else: row = int(input("Player 2, please enter row (0-14): ")) col = int(input("Player 2, please enter column (0-14): ")) if is_valid_move(board, row, col): place_piece(board, row, col, 2) if check_win(board, 2): print("Player 2 wins!") game_over = True turn = 1 else: print("Invalid move! Please try again.") print_board(board) play_game() ``` 这个程序使用了numpy库来创建和操作二维数组,实现了五子棋的基本功能。你可以在控制台中输入行和列的坐标来落子,程序会自动判断是否合法并检查是否有五子连珠。如果有玩家获胜,程序会输出相应的提示信息并结束游戏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值