fix: python client not reconginizng end conditions
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
FROM rust:1.91.1 as build
|
FROM rust:1.91.1 AS build
|
||||||
|
|
||||||
RUN rustup target add x86_64-unknown-linux-musl && \
|
RUN rustup target add x86_64-unknown-linux-musl && \
|
||||||
apt update && \
|
apt update && \
|
||||||
|
|||||||
@@ -2,25 +2,27 @@ import asyncio
|
|||||||
import websockets
|
import websockets
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
class Slot(Enum):
|
class Slot(Enum):
|
||||||
NONE = 0
|
NONE = 0
|
||||||
RED = 1
|
RED = 1
|
||||||
BLUE = 2
|
BLUE = 2
|
||||||
|
|
||||||
|
|
||||||
def calculate_move(opponent_move, board, our_color, opponent_color):
|
def calculate_move(opponent_move, board, our_color, opponent_color):
|
||||||
if opponent_move is not None:
|
if opponent_move is not None:
|
||||||
print(f"Opponent played column {opponent_move}")
|
print(f"Opponent played column {opponent_move}")
|
||||||
# TODO: Implement your move calculation logic here instead
|
# TODO: Implement your move calculation logic here instead
|
||||||
# Use the board variable to see and set the current state of the board
|
# Use the board variable to see and set the current state of the board
|
||||||
return int(input("Column: "))
|
return int(input("Column: "))
|
||||||
|
|
||||||
async def gameloop (socket):
|
|
||||||
|
async def gameloop(socket):
|
||||||
board = [[Slot.NONE] * 5 for _ in range(6)]
|
board = [[Slot.NONE] * 5 for _ in range(6)]
|
||||||
our_color = Slot.NONE
|
our_color = Slot.NONE
|
||||||
opponent_color = Slot.NONE
|
opponent_color = Slot.NONE
|
||||||
while True: # While game is active, continually anticipate messages
|
while True: # While game is active, continually anticipate messages
|
||||||
message = (await socket.recv()).split(':') # Receive message from server
|
message = (await socket.recv()).split(':') # Receive message from server
|
||||||
print(f"Received message: {message}")
|
|
||||||
|
|
||||||
match message[0]:
|
match message[0]:
|
||||||
case 'CONNECT':
|
case 'CONNECT':
|
||||||
@@ -28,25 +30,25 @@ async def gameloop (socket):
|
|||||||
|
|
||||||
case 'GAME':
|
case 'GAME':
|
||||||
if message[1] == 'START':
|
if message[1] == 'START':
|
||||||
if message[2] == '1':
|
if message[2] == '1':
|
||||||
our_color = Slot.RED
|
our_color = Slot.RED
|
||||||
opponent_color = Slot.BLUE
|
opponent_color = Slot.BLUE
|
||||||
col = calculate_move(None, board, our_color, opponent_color) # calculate_move is some arbitrary function you have created to figure out the next move
|
col = calculate_move(None, board, our_color,
|
||||||
await socket.send(f'PLAY:{col}') # Send your move to the sever
|
opponent_color) # calculate_move is some arbitrary function you have created to figure out the next move
|
||||||
else:
|
await socket.send(f'PLAY:{col}') # Send your move to the sever
|
||||||
our_color = Slot.BLUE
|
else:
|
||||||
opponent_color = Slot.RED
|
our_color = Slot.BLUE
|
||||||
|
opponent_color = Slot.RED
|
||||||
|
if (message[1] == 'WINS') | (message[1] == 'LOSS') | (message[1] == 'DRAW') | (message[1] == 'TERMINATED'): # Game has ended
|
||||||
|
print(message[0])
|
||||||
|
board = [[Slot.NONE] * 5 for _ in range(6)]
|
||||||
|
our_color = None
|
||||||
|
opponent_color = None
|
||||||
|
await socket.send('READY')
|
||||||
|
|
||||||
case 'OPPONENT': # Opponent has gone; calculate next move
|
case 'OPPONENT': # Opponent has gone; calculate next move
|
||||||
col = calculate_move(message[1], board, our_color, opponent_color) # Give your function your opponent's move
|
col = calculate_move(message[1], board, our_color, opponent_color) # Give your function your opponent's move
|
||||||
await socket.send(f'PLAY:{col}') # Send your move to the sever
|
await socket.send(f'PLAY:{col}') # Send your move to the sever
|
||||||
|
|
||||||
case 'WIN' | 'LOSS' | 'DRAW' | 'TERMINATED': # Game has ended
|
|
||||||
print(message[0])
|
|
||||||
board = [[Slot.NONE] * 5 for _ in range(6)]
|
|
||||||
our_color = None
|
|
||||||
opponent_color = None
|
|
||||||
await socket.send('READY')
|
|
||||||
|
|
||||||
case 'KICK':
|
case 'KICK':
|
||||||
print("You have been kicked from the game")
|
print("You have been kicked from the game")
|
||||||
@@ -58,11 +60,13 @@ async def gameloop (socket):
|
|||||||
|
|
||||||
await socket.close()
|
await socket.close()
|
||||||
|
|
||||||
|
|
||||||
async def join_server(username):
|
async def join_server(username):
|
||||||
async with websockets.connect(f'wss://connect4.abunchofknowitalls.com') as socket: # Establish websocket connection
|
async with websockets.connect(f'wss://connect4.abunchofknowitalls.com') as socket: # Establish websocket connection
|
||||||
await socket.send(f'CONNECT:{username}')
|
await socket.send(f'CONNECT:{username}')
|
||||||
await gameloop(socket)
|
await gameloop(socket)
|
||||||
|
|
||||||
if __name__ == '__main__': # Program entrypoint
|
|
||||||
username = input("Enter username: ")
|
if __name__ == '__main__': # Program entrypoint
|
||||||
asyncio.run(join_server(username))
|
username = input("Enter username: ")
|
||||||
|
asyncio.run(join_server(username))
|
||||||
|
|||||||
Reference in New Issue
Block a user