Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.

Commit a4afdcc

Browse files
implemented logic for resigning and drawing, and for verifying messages as sensible
1 parent ac0ec7a commit a4afdcc

1 file changed

Lines changed: 59 additions & 13 deletions

File tree

src/capy_app/frontend/cogs/games/multiplayerchess_cog.py

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ async def multichess(self, interaction: discord.Interaction, opponent: discord.U
3737
["♜", "♞", "♝", "♛", "♚", "♝", "♞", "♜"],
3838
]
3939
players = [interaction.user, opponent]
40-
symbols = ["♔", "♕", "♖", "♗", "♘", "♙", "♚", "♛", "♜", "♝", "♞", "♟"]
4140
turn = 0
41+
draw_proposed = False
4242

4343
# print out the board
44+
# returns NONE
4445
def print_board(board):
4546
print("+---+---+---+---+---+---+---+---+")
4647
for i in range(len(board)):
@@ -51,15 +52,28 @@ def print_board(board):
5152
print("+---+---+---+---+---+---+---+---+")
5253

5354
# make the move specified by the user
55+
# assumes the move given is valid
56+
# returns NONE
5457
def make_move(board, piece, start, end):
55-
return
58+
board[start[0]][start[1]] = ""
59+
board[end[0]][end[1]] = piece
5660

5761
# parse the chess notation to obtain piece, start location, end location
62+
# returns (False, False, False) if given string is NOT in valid notation form,
63+
# otherwise returns piece, start location, end location
5864
def parse_notation(notation):
5965
return
6066

67+
# returns true if given king color is in check, false otherwise
68+
def is_in_check(board, color):
69+
# loop thru board looking for opponent's pieces, see what squares they attack
70+
return
71+
6172
# returns true if move legal, else false
62-
def is_move_legal(piece, start, end):
73+
def is_move_legal(board, piece, start, end):
74+
# check to be sure piece, start, end are note false
75+
if piece == False:
76+
return False
6377
# check bounds
6478

6579
# check if piece can move this way
@@ -69,13 +83,20 @@ def is_move_legal(piece, start, end):
6983
# check for being in check after move
7084
# should cover pins too
7185

86+
# castling rules
87+
# castling out of check
88+
# castling thru check
89+
# has king or rook moved?
90+
7291
return
7392

7493
# check for checkmate
94+
# returns true if checkmate has been played, false otherwise
7595
def check_win(board):
7696
return
7797

7898
# check for stalemate or repetition
99+
# returns true if stalemate or repetition found, false otherwise
79100
def check_draw(board):
80101
return
81102

@@ -86,28 +107,53 @@ def check_draw(board):
86107

87108
def check(msg: discord.Message):
88109
return (
89-
msg.author == players[turn]
90-
and msg.channel == interaction.channel
91-
and msg.content.isdigit()
92-
# check notation validity
93-
and 1 <= int(msg.content) <= 9
94-
and board[int(msg.content) - 1] == " "
110+
# checking to make sure the message is valid
111+
msg.author == players[turn] # correct player sent the msg
112+
and msg.channel == interaction.channel # channel is correct
113+
and ( # if notation, check notation validity
114+
msg.content == "draw?"
115+
or msg.content == "accept"
116+
or msg.content == "decline"
117+
or msg.content == "resign"
118+
or parse_notation(msg.content)[0] != False
119+
)
120+
and is_move_legal(board, parse_notation(msg.content)) # move must be legal
95121
)
96122

97123
while True:
98124
try:
99125
move_msg = await self.bot.wait_for("message", check=check, timeout=60.0)
100-
move = int(move_msg.content) - 1
101126

102-
if check_win(board):
127+
# check for draw offer
128+
if move_msg.content == "draw?":
129+
draw_proposed = True
130+
await interaction.followup.send(
131+
f'{print_board()}\n{players[turn%2].mention} proposes a draw! Type "accept" to accept the draw or "decline" to decline it.'
132+
)
133+
return
134+
# check for draw decline
135+
if move_msg.content == "decline" and draw_proposed:
136+
draw_proposed = False
137+
await interaction.followup.send(
138+
f"{print_board()}\n{players[turn%2].mention} declines to draw."
139+
)
140+
return
141+
142+
# check for checkmates or resignations
143+
if check_win(board) or move_msg.content == "resign":
103144
await interaction.followup.send(
104-
f"{print_board()}\n{players[turn].mention} wins! 🎉"
145+
f"{print_board()}\n{players[turn%2].mention} wins! 🎉"
105146
)
106147
return
107148

149+
# check for draw
150+
if check_draw(board) or (draw_proposed and move_msg.content == "accept"):
151+
await interaction.followup.send(f"{print_board()}\nIt's a draw!")
152+
return
153+
108154
turn += 1
109155
await interaction.followup.send(
110-
f"{print_board()}\n{players[turn].mention}, it's your turn!"
156+
f"{print_board()}\n{players[turn%2].mention}, it's your turn!"
111157
)
112158
except asyncio.TimeoutError:
113159
await interaction.followup.send("⌛ Game timed out!")

0 commit comments

Comments
 (0)