Advertisement
Guest User

Untitled

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