用python编写一个字符界面的五子棋人机对战程序,游戏难度中等
时间: 2024-05-01 17:24:06 浏览: 99
Python五子棋人机对战程序
好的,下面是代码实现:
```python
import os
# 初始化棋盘
def init_board():
board = []
for i in range(0, 15):
row = ["+" for i in range(0, 15)]
board.append(row)
return board
# 显示棋盘
def display_board(board):
os.system("clear")
print(" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15")
for i in range(0, 15):
print(chr(i+ord("a")), end=" ")
for j in range(0, 15):
print(board[i][j], end=" ")
print()
# 判断是否下在了棋盘内
def is_valid_location(board, row, col):
if row < 0 or row >= len(board) or col < 0 or col >= len(board[0]):
return False
if board[row][col] != "+":
return False
return True
# 下棋
def make_move(board, row, col, player):
if is_valid_location(board, row, col):
board[row][col] = player
return True
return False
# 判断是否获胜
def check_win(board, player):
for i in range(0, 15):
for j in range(0, 11):
if board[i][j:j+5] == [player]*5:
return True
for i in range(0, 11):
for j in range(0, 15):
if [board[i+k][j] for k in range(0, 5)] == [player]*5:
return True
for i in range(0, 11):
for j in range(0, 11):
if [board[i+k][j+k] for k in range(0, 5)] == [player]*5:
return True
for i in range(0, 11):
for j in range(4, 15):
if [board[i+k][j-k] for k in range(0, 5)] == [player]*5:
return True
return False
# 人机对战
def play():
board = init_board()
display_board(board)
player = "X"
while True:
if player == "X":
row = input("请落子(如a1):")
row = ord(row[0]) - ord("a")
col = int(row[1:]) - 1
else:
row, col = get_computer_move(board)
if make_move(board, row, col, player):
display_board(board)
if check_win(board, player):
print("恭喜", player, "获胜!")
break
if player == "X":
player = "O"
else:
player = "X"
# 计算AI的落子位置
def get_computer_move(board):
for i in range(0, 15):
for j in range(0, 15):
if board[i][j] == "+":
if check_win_by_location(board, i, j, "O"):
return i, j
for i in range(0, 15):
for j in range(0, 15):
if board[i][j] == "+":
if check_win_by_location(board, i, j, "X"):
return i, j
for i in range(0, 15):
for j in range(0, 15):
if board[i][j] == "+":
if check_live_three_by_location(board, i, j, "O"):
return i, j
for i in range(0, 15):
for j in range(0, 15):
if board[i][j] == "+":
if check_live_three_by_location(board, i, j, "X"):
return i, j
for i in range(0, 15):
for j in range(0, 15):
if board[i][j] == "+":
if check_live_two_by_location(board, i, j, "O"):
return i, j
for i in range(0, 15):
for j in range(0, 15):
if board[i][j] == "+":
if check_live_two_by_location(board, i, j, "X"):
return i, j
while True:
row, col = random.randint(0, 14), random.randint(0, 14)
if board[row][col] == "+":
return row, col
# 判断在某个位置能否获胜
def check_win_by_location(board, row, col, player):
board[row][col] = player
win = check_win(board, player)
board[row][col] = "+"
return win
# 判断在某个位置是否形成活三棋型
def check_live_three_by_location(board, row, col, player):
board[row][col] = player
live_three = False
for i in range(0, 15):
for j in range(0, 15):
if board[i][j] == "+":
board[i][j] = "O" if player == "X" else "X"
if check_win_by_location(board, i, j, player):
live_three = True
board[i][j] = "+"
board[row][col] = "+"
return live_three
# 判断在某个位置是否形成活二棋型
def check_live_two_by_location(board, row, col, player):
board[row][col] = player
live_two = False
for i in range(0, 15):
for j in range(0, 15):
if board[i][j] == "+":
board[i][j] = "O" if player == "X" else "X"
if check_live_three_by_location(board, i, j, player):
live_two = True
board[i][j] = "+"
board[row][col] = "+"
return live_two
play()
```
这个程序使用了一些基本的算法来实现AI的落子位置计算,包括:
1. 判断在某个位置能否获胜
2. 判断在某个位置是否形成活三棋型
3. 判断在某个位置是否形成活二棋型
这些算法都是基于五子棋的规则和常识来实现的,当然也可以使用更加高级的算法来实现,比如蒙特卡罗树搜索等。
阅读全文