python五子棋小游戏计时代码
时间: 2024-11-22 18:27:03 浏览: 5
python 五子棋小游戏源代码
在Python中创建一个简单的五子棋小游戏并添加计时功能,你可以使用`time`模块来记录游戏时间。以下是一个基本的框架,包括初始化棋盘、玩家轮流下棋、检查胜利条件以及计时:
```python
import random
import time
# 初始化棋盘
chess_board = [[' ' for _ in range(15)] for _ in range(15)]
player_x = 'X'
player_o = 'O'
def print_board():
for row in chess_board:
print(' '.join(row))
def check_win(board, player):
# 检查一行、一列和对角线是否有五个连续的棋子
for i in range(15):
if board[i] == [player] * 5:
return True
if (board[0][i], board[1][i], board[2][i],
board[7][i], board[8][i]) == [player] * 5:
return True
if (board[i][0], board[i][1], board[i][2],
board[i][7], board[i][8]) == [player] * 5:
return True
return False
def make_move(player, move):
x, y = move
chess_board[x][y] = player
def game_loop():
start_time = time.time()
while not check_win(chess_board, player_x) and not check_win(chess_board, player_o):
print_board()
print(f"Player {player_x}'s turn")
x, y = input("Enter your move (x, y): ").split(',')
x, y = int(x), int(y)
make_move(player_x, (x, y))
if check_win(chess_board, player_x):
break
player_x, player_o = player_o, player_x
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Game over! Player {player_x} wins in {elapsed_time:.2f} seconds.")
game_loop()
阅读全文