forked from saipo/astrbot_plugin_dajiao
提交井字棋游戏
This commit is contained in:
parent
f5dc744507
commit
cbb078eb4a
72
TicTacToeGame.py
Normal file
72
TicTacToeGame.py
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
import random
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
class TicTacToeGame:
|
||||||
|
def __init__(self):
|
||||||
|
self.board = [" "] * 9
|
||||||
|
self.player_symbol = "X"
|
||||||
|
self.computer_symbol = "O"
|
||||||
|
self.current_player = "X" # X先走
|
||||||
|
|
||||||
|
def print_board(self) -> str:
|
||||||
|
"""返回棋盘字符串"""
|
||||||
|
board_str = (
|
||||||
|
f"\n {self.board[0]} | {self.board[1]} | {self.board[2]} \n"
|
||||||
|
"-----------\n"
|
||||||
|
f" {self.board[3]} | {self.board[4]} | {self.board[5]} \n"
|
||||||
|
"-----------\n"
|
||||||
|
f" {self.board[6]} | {self.board[7]} | {self.board[8]} \n"
|
||||||
|
)
|
||||||
|
return board_str
|
||||||
|
|
||||||
|
def check_winner(self) -> Optional[str]:
|
||||||
|
"""检查是否有玩家获胜"""
|
||||||
|
win_combinations = [
|
||||||
|
[0, 1, 2], [3, 4, 5], [6, 7, 8], # 横向
|
||||||
|
[0, 3, 6], [1, 4, 7], [2, 5, 8], # 纵向
|
||||||
|
[0, 4, 8], [2, 4, 6] # 对角线
|
||||||
|
]
|
||||||
|
|
||||||
|
for combo in win_combinations:
|
||||||
|
if self.board[combo[0]] == self.board[combo[1]] == self.board[combo[2]] != " ":
|
||||||
|
return self.board[combo[0]]
|
||||||
|
return None
|
||||||
|
|
||||||
|
def is_board_full(self) -> bool:
|
||||||
|
"""检查棋盘是否已满"""
|
||||||
|
return " " not in self.board
|
||||||
|
|
||||||
|
def computer_move(self) -> int:
|
||||||
|
"""电脑AI移动"""
|
||||||
|
for i in range(9):
|
||||||
|
if self.board[i] == " ":
|
||||||
|
self.board[i] = self.computer_symbol
|
||||||
|
if self.check_winner() == self.computer_symbol:
|
||||||
|
return i
|
||||||
|
self.board[i] = " "
|
||||||
|
|
||||||
|
for i in range(9):
|
||||||
|
if self.board[i] == " ":
|
||||||
|
self.board[i] = self.player_symbol
|
||||||
|
if self.check_winner() == self.player_symbol:
|
||||||
|
self.board[i] = self.computer_symbol
|
||||||
|
return i
|
||||||
|
self.board[i] = " "
|
||||||
|
|
||||||
|
move_order = [4, 0, 2, 6, 8, 1, 3, 5, 7]
|
||||||
|
for move in move_order:
|
||||||
|
if self.board[move] == " ":
|
||||||
|
return move
|
||||||
|
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def make_move(self, position: int) -> bool:
|
||||||
|
"""玩家移动"""
|
||||||
|
if 0 <= position <= 8 and self.board[position] == " ":
|
||||||
|
self.board[position] = self.current_player
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def switch_player(self):
|
||||||
|
"""切换当前玩家"""
|
||||||
|
self.current_player = "O" if self.current_player == "X" else "X"
|
97
main.py
97
main.py
@ -7,7 +7,9 @@ from .back import time_long, volume, isUserExist, insertUser, seconds_to_hms, ml
|
|||||||
import pymysql
|
import pymysql
|
||||||
from .Tool import get_tool_name
|
from .Tool import get_tool_name
|
||||||
import astrbot.api.message_components as Comp
|
import astrbot.api.message_components as Comp
|
||||||
|
from astrbot.session import session_waiter, SessionController
|
||||||
import aiohttp # 需要异步HTTP客户端
|
import aiohttp # 需要异步HTTP客户端
|
||||||
|
from TicTacToeGame import TicTacToeGame
|
||||||
|
|
||||||
@register("helloworld", "YourName", "一个简单的 Hello World 插件", "1.0.0")
|
@register("helloworld", "YourName", "一个简单的 Hello World 插件", "1.0.0")
|
||||||
class MyPlugin(Star):
|
class MyPlugin(Star):
|
||||||
@ -347,3 +349,98 @@ class MyPlugin(Star):
|
|||||||
async def bushutest(self, event: AstrMessageEvent):
|
async def bushutest(self, event: AstrMessageEvent):
|
||||||
yield event.plain_result("自动部署成功!!")
|
yield event.plain_result("自动部署成功!!")
|
||||||
|
|
||||||
|
@filter.command("井字棋")
|
||||||
|
async def tic_tac_toe_handler(self, event: AstrMessageEvent):
|
||||||
|
"""井字棋游戏处理器"""
|
||||||
|
try:
|
||||||
|
# 初始化游戏
|
||||||
|
game = TicTacToeGame()
|
||||||
|
|
||||||
|
# 发送初始说明和空棋盘
|
||||||
|
instructions = (
|
||||||
|
"欢迎来到井字棋游戏!\n"
|
||||||
|
"输入1-9的数字来选择位置:\n"
|
||||||
|
" 1 | 2 | 3 \n"
|
||||||
|
"-----------\n"
|
||||||
|
" 4 | 5 | 6 \n"
|
||||||
|
"-----------\n"
|
||||||
|
" 7 | 8 | 9 \n"
|
||||||
|
"\n当前棋盘:" + game.print_board()
|
||||||
|
)
|
||||||
|
yield event.plain_result(instructions)
|
||||||
|
|
||||||
|
@session_waiter(timeout=60, record_history_chains=False)
|
||||||
|
async def game_waiter(controller: SessionController, event: AstrMessageEvent):
|
||||||
|
nonlocal game
|
||||||
|
|
||||||
|
# 玩家回合
|
||||||
|
if game.current_player == game.player_symbol:
|
||||||
|
try:
|
||||||
|
move = int(event.message_str) - 1
|
||||||
|
if not game.make_move(move):
|
||||||
|
await event.send(event.make_result(chain=[Comp.Plain("无效移动,请重试。")]))
|
||||||
|
controller.keep(timeout=60, reset_timeout=True)
|
||||||
|
return
|
||||||
|
except ValueError:
|
||||||
|
await event.send(event.make_result(chain=[Comp.Plain("请输入1-9的数字。")]))
|
||||||
|
controller.keep(timeout=60, reset_timeout=True)
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
# 电脑回合
|
||||||
|
move = game.computer_move()
|
||||||
|
game.board[move] = game.current_player
|
||||||
|
|
||||||
|
# 检查游戏状态
|
||||||
|
winner = game.check_winner()
|
||||||
|
if winner:
|
||||||
|
result = event.make_result()
|
||||||
|
result.chain = [
|
||||||
|
Comp.Plain(f"{game.print_board()}\n"),
|
||||||
|
Comp.Plain("恭喜你赢了!" if winner == game.player_symbol else "电脑赢了!")
|
||||||
|
]
|
||||||
|
await event.send(result)
|
||||||
|
controller.stop()
|
||||||
|
return
|
||||||
|
|
||||||
|
if game.is_board_full():
|
||||||
|
result = event.make_result()
|
||||||
|
result.chain = [
|
||||||
|
Comp.Plain(f"{game.print_board()}\n"),
|
||||||
|
Comp.Plain("平局!")
|
||||||
|
]
|
||||||
|
await event.send(result)
|
||||||
|
controller.stop()
|
||||||
|
return
|
||||||
|
|
||||||
|
# 切换玩家并继续游戏
|
||||||
|
game.switch_player()
|
||||||
|
|
||||||
|
# 发送更新后的棋盘
|
||||||
|
result = event.make_result()
|
||||||
|
if game.current_player == game.player_symbol:
|
||||||
|
result.chain = [
|
||||||
|
Comp.Plain(f"{game.print_board()}\n"),
|
||||||
|
Comp.Plain("轮到你走了,请输入1-9:")
|
||||||
|
]
|
||||||
|
else:
|
||||||
|
result.chain = [
|
||||||
|
Comp.Plain(f"{game.print_board()}\n"),
|
||||||
|
Comp.Plain("电脑思考中...")
|
||||||
|
]
|
||||||
|
await event.send(result)
|
||||||
|
|
||||||
|
# 重置超时时间
|
||||||
|
controller.keep(timeout=60, reset_timeout=True)
|
||||||
|
|
||||||
|
try:
|
||||||
|
await game_waiter(event)
|
||||||
|
except TimeoutError:
|
||||||
|
yield event.plain_result("井字棋游戏超时结束!")
|
||||||
|
except Exception as e:
|
||||||
|
yield event.plain_result(f"游戏出错: {str(e)}")
|
||||||
|
finally:
|
||||||
|
event.stop_event()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"井字棋游戏错误: {str(e)}")
|
||||||
|
yield event.plain_result("游戏发生错误,请稍后再试。")
|
Loading…
x
Reference in New Issue
Block a user