python五子棋含禁手代码
时间: 2023-12-08 18:36:17 浏览: 96
是一个使用Python编写的五子棋游戏,包含禁手规则的实现:
```python
import numpy as np
# 定义棋盘大小
BOARD_SIZE = 15
# 定义棋子类型
EMPTY = 0
BLACK = 1
WHITE = 2
# 定义禁手类型
NO_PROHIBITION = 0
TRIPLE_THREE = 1
FOUR_FOUR = 2
OVERLINE = 3
# 定义禁手状态
NO_PROHIBITION_STATE = 0
TRIPLE_THREE_STATE = 1
FOUR_FOUR_STATE = 2
OVERLINE_STATE = 3
# 定义禁手状态对应的提示信息
PROHIBITION_MSG = {
TRIPLE_THREE_STATE: "三三禁手!",
FOUR_FOUR_STATE: "四四禁手!",
OVERLINE_STATE: "长连禁手!"
}
# 定义棋盘
board = np.zeros((BOARD_SIZE, BOARD_SIZE), dtype=int)
# 定义当前下棋方
current_player = BLACK
# 定义禁手状态
prohibition_state = NO_PROHIBITION_STATE
# 判断是否在棋盘内
def is_in_board(x, y):
return x >= 0 and x < BOARD_SIZE and y >= 0 and y < BOARD_SIZE
# 判断是否为空位
def is_empty(x, y):
return board[x][y] == EMPTY
# 判断是否为当前下棋方
def is_current_player(x, y):
return board[x][y] == current_player
# 判断是否胜利
def is_win(x, y):
# 判断横向是否连成五子
count = 1
for i in range(1, 5):
if is_in_board(x, y+i) and board[x][y+i] == current_player:
count += 1
else:
break
for i in range(1, 5):
if is_in_board(x, y-i) and board[x][y-i] == current_player:
count += 1
else:
break
if count >= 5:
return True
# 判断纵向是否连成五子
count = 1
for i in range(1, 5):
if is_in_board(x+i, y) and board[x+i][y] == current_player:
count += 1
else:
break
for i in range(1, 5):
if is_in_board(x-i, y) and board[x-i][y] == current_player:
count += 1
else:
break
if count >= 5:
return True
# 判断左上到右下是否连成五子
count = 1
for i in range(1, 5):
if is_in_board(x+i, y+i) and board[x+i][y+i] == current_player:
count += 1
else:
break
for i in range(1, 5):
if is_in_board(x-i, y-i) and board[x-i][y-i] == current_player:
count += 1
else:
break
if count >= 5:
return True
# 判断左下到右上是否连成五子
count = 1
for i in range(1, 5):
if is_in_board(x+i, y-i) and board[x+i][y-i] == current_player:
count += 1
else:
break
for i in range(1, 5):
if is_in_board(x-i, y+i) and board[x-i][y+i] == current_player:
count += 1
else:
break
if count >= 5:
return True
return False
# 判断是否存在禁手
def has_prohibition(x, y):
global prohibition_state
# 判断三三禁手
count = 0
for i in range(-2, 3):
for j in range(-2, 3):
if is_in_board(x+i, y+j) and board[x+i][y+j] == current_player:
count += 1
if count >= 5:
if prohibition_state == NO_PROHIBITION_STATE:
prohibition_state = TRIPLE_THREE_STATE
return True
count = 0
# 判断四四禁手
count = 0
for i in range(-1, 2):
for j in range(-1, 2):
if i == 0 and j == 0:
continue
if is_in_board(x+i, y+j) and board[x+i][y+j] == current_player:
count += 1
if count >= 2:
for i in range(-1, 2):
for j in range(-1, 2):
if i == 0 and j == 0:
continue
if is_in_board(x+i, y+j) and is_empty(x+i, y+j):
board[x+i][y+j] = 3 - current_player
if has_prohibition(x+i, y+j):
board[x+i][y+j] = EMPTY
if prohibition_state == NO_PROHIBITION_STATE:
prohibition_state = FOUR_FOUR_STATE
return True
board[x+i][y+j] = EMPTY
count = 0
# 判断长连禁手
count = 1
for i in range(1, 5):
if is_in_board(x, y+i) and board[x][y+i] == current_player:
count += 1
else:
break
for i in range(1, 5):
if is_in_board(x, y-i) and board[x][y-i] == current_player:
count += 1
else:
break
if count >= 6:
if prohibition_state == NO_PROHIBITION_STATE:
prohibition_state = OVERLINE_STATE
return True
count = 1
for i in range(1, 5):
if is_in_board(x+i, y) and board[x+i][y] == current_player:
count += 1
else:
break
for i in range(1, 5):
if is_in_board(x-i, y) and board[x-i][y] == current_player:
count += 1
else:
break
if count >= 6:
if prohibition_state == NO_PROHIBITION_STATE:
prohibition_state = OVERLINE_STATE
return True
count = 1
for i in range(1, 5):
if is_in_board(x+i, y+i) and board[x+i][y+i] == current_player:
count += 1
else:
break
for i in range(1, 5):
if is_in_board(x-i, y-i) and board[x-i][y-i] == current_player:
count += 1
else:
break
if count >= 6:
if prohibition_state == NO_PROHIBITION_STATE:
prohibition_state = OVERLINE_STATE
return True
count = 1
for i in range(1, 5):
if is_in_board(x+i, y-i) and board[x+i][y-i] == current_player:
count += 1
else:
break
for i in range(1, 5):
if is_in_board(x-i, y+i) and board[x-i][y+i] == current_player:
count += 1
else:
break
if count >= 6:
if prohibition_state == NO_PROHIBITION_STATE:
prohibition_state = OVERLINE_STATE
return True
return False
# 下棋
def play(x, y):
global current_player
global prohibition_state
if is_in_board(x, y) and is_empty(x, y):
board[x][y] = current_player
if has_prohibition(x, y):
board[x][y] = EMPTY
print(PROHIBITION_MSG[prohibition_state])
prohibition_state = NO_PROHIBITION_STATE
return
if is_win(x, y):
print("黑方胜利!" if current_player == BLACK else "白方胜利!")
return
current_player = 3 - current_player
# 打印棋盘
def print_board():
print(" ", end="")
for i in range(BOARD_SIZE):
print(chr(ord('a')+i), end=" ")
print()
for i in range(BOARD_SIZE):
print("{:2d}".format(i+1), end="")
for j in range(BOARD_SIZE):
if board[i][j] == BLACK:
print("●", end=" ")
elif board[i][j] == WHITE:
print("○", end=" ")
else:
print("+", end=" ")
print()
# 游戏循环
while True:
print_board()
if current_player == BLACK:
print("黑方下棋:", end="")
else:
print("白方下棋:", end="")
move = input()
if move == "exit":
break
x, y = ord(move[0])-ord('a'), int(move[1:])-1
play(x, y)
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)