我们可以使用Python编写一个简单的Tic-tac-toe游戏,其中包括普通模式和复杂模式。普通模式是一个3x3的网格,并且只需要连续三个数来赢得游戏。复杂模式是5x5的网格,并且需要连续四个数来赢得游戏。
以下是一个Tic-tac-toe游戏的代码示例:
# import necessary libraries
import numpy as np
# define game board
board_size = 3
board = np.zeros((board_size, board_size), dtype=int)
# define players
player_1 = 1
player_2 = 2
# set game mode
normal_mode = True
# define winning states
winning_states_3x3 = [np.array([1, 1, 1]), np.array([2, 2, 2])]
winning_states_5x5 = [np.array([1, 1, 1, 1]), np.array([2, 2, 2, 2])]
# function to check winning state
def check_win(player):
winning_states = winning_states_3x3 if normal_mode else winning_states_5x5
# check row
for row in range(board_size):
if np.array_equal(board[row], winning_states[player-1]):
return True
# check column
for col in range(board_size):
if np.array_equal(board[:,col], winning_states[player-1]):
return True
# check diagonal
if np.array_equal(np.diag(board), winning_states[player-1]):
return True
if np.array_equal(np.diag(np.fliplr(board)), winning_states[player-1]):
return True
return False
# function to check if board is full
def board_full():
return not np.any(board == 0)
# function to print board
def print_board():
for row in board:
print(row)
# start game
current_player = player_1
while not board_full():
# print board