Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Connect Four"""
- import discord
- from discord.ext import commands
- from .utils import checks
- # pieces = {
- # "0": "⚪", # :white_circle:
- # "1": "🔴", # :red_circle:
- # "2": "🔵", # :large_blue_circle:
- # }
- class Session:
- """Active Session of Connect Four"""
- def __init__(self, p1, p2, chan):
- self.p1 = p1 # These will be discord.Member objects of players
- self.p2 = p2 # `p1` being ctx.author and `p2 being the ping
- self.chan = chan
- self.board = [[0 for x in range(7)] for y in range(7)]
- self.turn = 0
- self.msg = None
- @property
- def current_player(self):
- if self.turn % 2 == 1:
- return self.p1
- else:
- return self.p2
- def play(self, player, row):
- self.board[row][self.board[-1]] = player
- self.board[row][-1] += 1
- self.turn += 1
- return self.check()
- def check(self):
- return False
- @property
- def get_board(self):
- emojis = {
- "0": "⚪", # :white_circle:
- str(self.p1.id): "🔴", # :red_circle:
- str(self.p2.id): "🔵", # :large_blue_circle:
- }
- board = []
- for row in self.board:
- board.append([emojis[str(i)] for i in row[:-1]][::-1])
- return board
- class ConnectFour:
- """Connect Four"""
- def __init__(self, bot):
- self.bot = bot
- self.sessions = {}
- def session(self, ctx):
- return self.sessions.get(ctx.channel.id, None)
- async def invalid_session(self, ctx):
- await ctx.send("No active game in this channel.")
- async def send_board(self, ctx):
- session = self.session(ctx)
- if session.msg:
- try:
- await session.msg.delete()
- except Exception as e:
- await ctx.send(f"{type(e)}: {e}")
- board = session.get_board()
- parsed_board = "\n".join(["".join() for x in range(6)])
- em = discord.Embed(title=f"{session.p1.name} vs. {session.p2.name}",
- description=f":one::two::three::four::five::six::seven:\n{parsed_board}",
- color=session.current_player.color)
- session.msg = await ctx.send(embed=em) # Parse this out
- @commands.group()
- async def c4(self, ctx):
- if not ctx.invoked_subcommand:
- await self.bot.formatter.format_help_for(ctx, ctx.command)
- @c4.command(name="start", aliases=["play"])
- async def _start(self, ctx, user: discord.Member=None):
- if user:
- await ctx.send(f"Ping! Confirmed user: {user.name} (Currently not implemented)")
- elif False: # Disabling code until it's written
- pass
- else:
- await self.bot.formatter.format_help_for(ctx, ctx.command, "You need another player to start.")
- @c4.command(name="quit", aliases=["end"])
- async def _quit(self, ctx):
- session = self.sessions.pop(ctx.channel.id, None)
- if session:
- await ctx.send("Game has ended.")
- else:
- await self.invalid_session(ctx)
- @checks.sudo()
- @c4.command(name="kill", aliases=["killall"])
- async def _kill(self, ctx):
- sessions = len(self.sessions.keys())
- self.sessions = {}
- await ctx.send(f"All running games have been terminated. (Total: {sessions})")
- @c4.command(name="move")
- async def _move(self, ctx, row: int):
- row -= 1
- session = self.session(ctx)
- if session:
- if ctx.author == session.current_player:
- if row in range(7):
- if session.board[row][-1] < 6:
- session.play(ctx.author.id, row)
- session.msg = await self.send_board(ctx)
- else:
- pass # Row full. Select another
- else:
- pass # Invalid row index. Select another
- else:
- await self.invalid_session(ctx)
- def setup(bot):
- bot.add_cog(ConnectFour(bot))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement