用Python写五子棋
时间: 2023-05-26 13:02:07 浏览: 100
Python写的五子棋
很抱歉,我是一个语言模型AI,无法编写实际的代码,但是我可以提供一份五子棋的Python实现代码供你参考:
```python
import os
import sys
# 五子棋类
class Gobang:
# 初始化
def __init__(self):
self.board_size = 15 # 棋盘大小
self.board = [] # 棋盘
for i in range(self.board_size):
self.board.append(["+" for j in range(self.board_size)])
self.chess = "x" # 刚开始下棋的颜色
self.win_flag = False # 是否已分出胜负
# 打印棋盘
def print_board(self):
os.system("cls")
print(" " * 2, end="")
for j in range(self.board_size):
print(chr(ord("A") + j), end=" ")
print()
for i in range(self.board_size):
print("{:>2} ".format(i + 1), end="")
for j in range(self.board_size):
print(self.board[i][j], end=" ")
print()
print()
# 判断棋子是否在范围内
def is_valid(self, x, y):
if x < 0 or x >= self.board_size or y < 0 or y >= self.board_size:
return False
return True
# 判断位置是否已经有棋子了
def is_empty(self, x, y):
if self.board[x][y] == "+":
return True
return False
# 落子
def drop_chess(self, x, y):
self.board[x][y] = self.chess
# 切换下棋的颜色
def toggle_chess(self):
if self.chess == "x":
self.chess = "o"
else:
self.chess = "x"
# 判断横向是否有五颗相同的棋子
def check_horizonal(self, x, y):
count = 1 # 统计相同的棋子数量
i, j = x, y - 1
while self.is_valid(i, j) and not self.is_empty(i, j) and self.board[i][j] == self.chess:
count += 1
j -= 1
i, j = x, y + 1
while self.is_valid(i, j) and not self.is_empty(i, j) and self.board[i][j] == self.chess:
count += 1
j += 1
return count >= 5
# 判断纵向是否有五颗相同的棋子
def check_vertical(self, x, y):
count = 1
i, j = x - 1, y
while self.is_valid(i, j) and not self.is_empty(i, j) and self.board[i][j] == self.chess:
count += 1
i -= 1
i, j = x + 1, y
while self.is_valid(i, j) and not self.is_empty(i, j) and self.board[i][j] == self.chess:
count += 1
i += 1
return count >= 5
# 判断左上到右下是否有五颗相同的棋子
def check_diag1(self, x, y):
count = 1
i, j = x - 1, y - 1
while self.is_valid(i, j) and not self.is_empty(i, j) and self.board[i][j] == self.chess:
count += 1
i -= 1
j -= 1
i, j = x + 1, y + 1
while self.is_valid(i, j) and not self.is_empty(i, j) and self.board[i][j] == self.chess:
count += 1
i += 1
j += 1
return count >= 5
# 判断右上到左下是否有五颗相同的棋子
def check_diag2(self, x, y):
count = 1
i, j = x - 1, y + 1
while self.is_valid(i, j) and not self.is_empty(i, j) and self.board[i][j] == self.chess:
count += 1
i -= 1
j += 1
i, j = x + 1, y - 1
while self.is_valid(i, j) and not self.is_empty(i, j) and self.board[i][j] == self.chess:
count += 1
i += 1
j -= 1
return count >= 5
# 判断是否已经分出胜负
def check_win(self, x, y):
if (
self.check_horizonal(x, y)
or self.check_vertical(x, y)
or self.check_diag1(x, y)
or self.check_diag2(x, y)
):
return True
return False
# 游戏循环
def game_loop(self):
while not self.win_flag:
self.print_board()
chess_pos = input("{} to move (e.g. A1): ".format(self.chess)).strip().upper()
if len(chess_pos) != 2:
continue
x = ord(chess_pos[0]) - ord("A")
y = int(chess_pos[1:]) - 1
if not self.is_valid(x, y) or not self.is_empty(x, y):
continue
self.drop_chess(x, y)
if self.check_win(x, y):
self.win_flag = True
print("{} wins!".format(self.chess))
self.toggle_chess()
# 主函数
def main():
gobang = Gobang()
gobang.game_loop()
if __name__ == "__main__":
main()
```
这份代码实现了以下功能:
- 初始化一个15x15的棋盘;
- 下棋的初始颜色是黑色("x");
- 双方轮流输入棋子的位置坐标,可以输入类似于"A1"这样的形式,输入无效的坐标会被忽略;
- 每次落子后会检查是否已经分出胜负,如果已经有一方胜利,则游戏结束。
阅读全文