Merge branch 'main' of codeberg.org:adrien-lsh/ft_transcendence

This commit is contained in:
2024-04-23 15:41:23 +02:00
10 changed files with 250 additions and 206 deletions

View File

@ -45,6 +45,8 @@ class TicTacToeWebSocket(WebsocketConsumer):
self.member.send(self.member.sign)
if (self.game._everbody_is_here() and self.game.model.started == False):
if (self.game.time != -1):
self.game.broadcast("opponent_joined")
self.game.broadcast("game_start")
self.game.model.start()
@ -54,7 +56,12 @@ class TicTacToeWebSocket(WebsocketConsumer):
if (data.get("targetMorpion") is not None and data.get("targetCase") is not None):
if (self.game.add(data, self.member) == False):
return
self.game.broadcast("", data, [self.member])
if (data.get("catchup") is not None and self.game.model.finished == False and self.game.model.finished == True):
self.member.send("catchup", {"Morpion": self.game._map, "turn": self.game.turn})
if (self.game.checkWin() != False):
print(self.game.checkWin())
self.game.broadcast("game_end", {"winning_sign": self.member.sign})
self.game.broadcast("game_move", data, [self.member])
pass
def disconnect(self, event):

View File

@ -18,6 +18,10 @@ class TicTacToeGame(AGame):
self.players: list[TicTacToePlayer] = [TicTacToePlayer(player, None, self, ["x", "o"][i]) for i, player in enumerate(players)]
self.time = -1
self.turn = 'x'
self._map = [[-1 for _ in range(9)] for _ in range(9)]
def _everbody_is_here(self):
@ -44,14 +48,15 @@ class TicTacToeGame(AGame):
if (self.checkMove(newmove, player)):
self._map[newmove.get("targetMorpion")][newmove.get("targetCase")] = newmove.get("sign")
player.currentMorpion = int(newmove.get("targetMorpion"))
player.currentMorpion = int(newmove.get("targetCase"))
self.turn = newmove.get("sign")
return True
return False
def checkMove(self, newmove, player):
print(int(newmove.get("targetMorpion")), player.currentMorpion)
if (int(newmove.get("targetMorpion")) != player.currentMorpion):
if (int(newmove.get("targetMorpion")) != player.currentMorpion or newmove.get("sign") != self.turn):
return False
if (self._map[newmove.get("targetMorpion")][newmove.get("targetCase")] != -1):
@ -62,16 +67,16 @@ class TicTacToeGame(AGame):
def checkWin(self):
for tab in self._map:
for i in range(3):
if tab[i] == tab[i + 3] == tab[i + 6]:
if tab[i] != -1 and tab[i] == tab[i + 3] and tab[i + 3] == tab[i + 6]:
return tab[i]
for i in range(0, 9, 3):
if tab[i] == tab[i + 1] == tab[i + 2]:
if tab[i] != -1 and tab[i] == tab[i + 1] and tab[i + 1] == tab[i + 2]:
return tab[i]
if tab[0] == tab[4] == tab[8]:
if tab[0] != -1 and tab[0] == tab[4] and tab[4] == tab[8]:
return tab[0]
if tab[6] == tab[4] == tab[2]:
if tab[6] != -1 and tab[6] == tab[4] and tab[4] == tab[2]:
return tab[6]
return None
return False
def _spectator_join(self, user_id: int, socket: WebsocketConsumer):