Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. """Connect Four"""
  2.  
  3. import discord
  4. from discord.ext import commands
  5. from .utils import checks
  6.  
  7.  
  8. # pieces = {
  9. # "0": "⚪", # :white_circle:
  10. # "1": "🔴", # :red_circle:
  11. # "2": "🔵", # :large_blue_circle:
  12. # }
  13.  
  14.  
  15. class Session:
  16. """Active Session of Connect Four"""
  17. def __init__(self, p1, p2, chan):
  18. self.p1 = p1 # These will be discord.Member objects of players
  19. self.p2 = p2 # `p1` being ctx.author and `p2 being the ping
  20. self.chan = chan
  21. self.board = [[0 for x in range(7)] for y in range(7)]
  22. self.turn = 0
  23. self.msg = None
  24.  
  25. @property
  26. def current_player(self):
  27. if self.turn % 2 == 1:
  28. return self.p1
  29. else:
  30. return self.p2
  31.  
  32. def play(self, player, row):
  33. self.board[row][self.board[-1]] = player
  34. self.board[row][-1] += 1
  35. self.turn += 1
  36. return self.check()
  37.  
  38. def check(self):
  39. return False
  40.  
  41. @property
  42. def get_board(self):
  43. emojis = {
  44. "0": "⚪", # :white_circle:
  45. str(self.p1.id): "🔴", # :red_circle:
  46. str(self.p2.id): "🔵", # :large_blue_circle:
  47. }
  48.  
  49. board = []
  50. for row in self.board:
  51. board.append([emojis[str(i)] for i in row[:-1]][::-1])
  52.  
  53. return board
  54.  
  55.  
  56. class ConnectFour:
  57. """Connect Four"""
  58. def __init__(self, bot):
  59. self.bot = bot
  60. self.sessions = {}
  61.  
  62. def session(self, ctx):
  63. return self.sessions.get(ctx.channel.id, None)
  64.  
  65. async def invalid_session(self, ctx):
  66. await ctx.send("No active game in this channel.")
  67.  
  68. async def send_board(self, ctx):
  69. session = self.session(ctx)
  70. if session.msg:
  71. try:
  72. await session.msg.delete()
  73. except Exception as e:
  74. await ctx.send(f"{type(e)}: {e}")
  75.  
  76. board = session.get_board()
  77. parsed_board = "\n".join(["".join() for x in range(6)])
  78. em = discord.Embed(title=f"{session.p1.name} vs. {session.p2.name}",
  79. description=f":one::two::three::four::five::six::seven:\n{parsed_board}",
  80. color=session.current_player.color)
  81. session.msg = await ctx.send(embed=em) # Parse this out
  82.  
  83. @commands.group()
  84. async def c4(self, ctx):
  85. if not ctx.invoked_subcommand:
  86. await self.bot.formatter.format_help_for(ctx, ctx.command)
  87.  
  88. @c4.command(name="start", aliases=["play"])
  89. async def _start(self, ctx, user: discord.Member=None):
  90. if user:
  91. await ctx.send(f"Ping! Confirmed user: {user.name} (Currently not implemented)")
  92. elif False: # Disabling code until it's written
  93. pass
  94. else:
  95. await self.bot.formatter.format_help_for(ctx, ctx.command, "You need another player to start.")
  96.  
  97. @c4.command(name="quit", aliases=["end"])
  98. async def _quit(self, ctx):
  99. session = self.sessions.pop(ctx.channel.id, None)
  100. if session:
  101. await ctx.send("Game has ended.")
  102. else:
  103. await self.invalid_session(ctx)
  104.  
  105. @checks.sudo()
  106. @c4.command(name="kill", aliases=["killall"])
  107. async def _kill(self, ctx):
  108. sessions = len(self.sessions.keys())
  109. self.sessions = {}
  110. await ctx.send(f"All running games have been terminated. (Total: {sessions})")
  111.  
  112. @c4.command(name="move")
  113. async def _move(self, ctx, row: int):
  114. row -= 1
  115. session = self.session(ctx)
  116. if session:
  117. if ctx.author == session.current_player:
  118. if row in range(7):
  119. if session.board[row][-1] < 6:
  120. session.play(ctx.author.id, row)
  121. session.msg = await self.send_board(ctx)
  122. else:
  123. pass # Row full. Select another
  124. else:
  125. pass # Invalid row index. Select another
  126. else:
  127. await self.invalid_session(ctx)
  128.  
  129.  
  130. def setup(bot):
  131. bot.add_cog(ConnectFour(bot))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement