просто працює
unknown
plain_text
2 years ago
3.7 kB
1
Indexable
Never
import numpy as np import telebot from telebot import types bot = telebot.TeleBot('6169116994:AAG_Km_Lda-ZMlzQ8rzD6WcQ--o5FxwMnJE') ROWS = 6 COLS = 7 PLAYER_1 = 1 PLAYER_2 = 2 player_colors = { PLAYER_1: '🔴', PLAYER_2: '🟡' } board = np.zeros((ROWS, COLS), dtype=int) def print_board(): print(np.flip(board, 0)) def is_valid_move(col): return board[ROWS-1][col] == 0 def drop_token(col, player): for row in range(ROWS): if board[row][col] == 0: board[row][col] = player break def check_win(player): for row in range(ROWS): for col in range(COLS - 3): if board[row][col] == player and board[row][col+1] == player and board[row][col+2] == player and board[row][col+3] == player: return True for row in range(ROWS - 3): for col in range(COLS): if board[row][col] == player and board[row+1][col] == player and board[row+2][col] == player and board[row+3][col] == player: return True for row in range(ROWS - 3): for col in range(COLS - 3): if board[row][col] == player and board[row+1][col+1] == player and board[row+2][col+2] == player and board[row+3][col+3] == player: return True for row in range(3, ROWS): for col in range(COLS - 3): if board[row][col] == player and board[row-1][col+1] == player and board[row-2][col+2] == player and board[row-3][col+3] == player: return True return False def main(): game_over = False turn = PLAYER_1 while not game_over: print_board() col = int(input(f"Player {turn}, choose a column (0-{COLS-1}): ")) if is_valid_move(col): drop_token(col, turn) if check_win(turn): print_board() print(f"Player {turn} wins!") game_over = True else: if turn == PLAYER_1: turn = PLAYER_2 else: turn = PLAYER_1 else: print("That move is not valid. Please try again.") #main() # bot @bot.message_handler(commands=['start']) def start(message): menu = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True) menu.add('/help', '/play', '/playbot', '/settings') hello = f'Wassup {message.from_user.first_name} use the menu below to play or get help.' bot.send_message(message.chat.id, hello, reply_markup=menu) @bot.message_handler(commands=['help']) def help(message): help_text = "Here are the rules of the game:\n\n" \ "1. The game is played on a 6x7 board.\n" \ "2. Players take turns dropping their tokens into a column.\n" \ "3. The token will fall to the lowest available spot in the column.\n" \ "4. The first player to connect four of their tokens horizontally, vertically, or diagonally wins!\n\n" \ "\n\n"\ "Here are the available commands:\n" \ "/start - Start the game.\n" \ "/play - Play against another player.\n" \ "/playbot - Play against the computer.\n" \ "/help - Display this message.\n" \ "/settings - setting the color of the tokens and the first player.\n" bot.send_message(message.chat.id, help_text) @bot.message_handler(commands=['play']) def play(message): pass @bot.message_handler(commands=['playbot']) def playbot(message): pass bot.polling(non_stop=True)