morpion: add: Frontend checks, QOL changes and winning conditions
This commit is contained in:
parent
8c8847cdd8
commit
91cdc96972
@ -18,12 +18,12 @@ class TicTacToe
|
|||||||
this.canvas = canvas
|
this.canvas = canvas
|
||||||
this.context = this.canvas.getContext("2d");
|
this.context = this.canvas.getContext("2d");
|
||||||
this.sign;
|
this.sign;
|
||||||
|
this.currentMorpion = 4;
|
||||||
this.turn;
|
this.turn;
|
||||||
}
|
}
|
||||||
|
|
||||||
async init()
|
async init()
|
||||||
{
|
{
|
||||||
console.log(this.game_id);
|
|
||||||
await this.game.join();
|
await this.game.join();
|
||||||
this.canvas.addEventListener("mousedown", (event, morpion = this) => this.onClick(event, morpion));
|
this.canvas.addEventListener("mousedown", (event, morpion = this) => this.onClick(event, morpion));
|
||||||
}
|
}
|
||||||
@ -37,22 +37,46 @@ class TicTacToe
|
|||||||
async onReceive(messageData)
|
async onReceive(messageData)
|
||||||
{
|
{
|
||||||
console.log(messageData)
|
console.log(messageData)
|
||||||
if (messageData.detail == "x" || messageData.detail == "o")
|
switch (messageData.detail)
|
||||||
{
|
{
|
||||||
|
case 'x':
|
||||||
|
case 'o':
|
||||||
this.sign = messageData.detail;
|
this.sign = messageData.detail;
|
||||||
this.turn = messageData.detail == "x";
|
this.turn = messageData.detail == "x";
|
||||||
}
|
if (this.turn)
|
||||||
else if (messageData.detail == "game_start")
|
this.setOutline(4, false);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'game_start':
|
||||||
this.game.started = true;
|
this.game.started = true;
|
||||||
else if (messageData.targetMorpion && messageData.targetCase)
|
this.game.finished = false;
|
||||||
{
|
break;
|
||||||
|
|
||||||
|
case 'game_move':
|
||||||
this.map[messageData.targetMorpion][messageData.targetCase] = (this.sign == "x") ? 1 : 0;
|
this.map[messageData.targetMorpion][messageData.targetCase] = (this.sign == "x") ? 1 : 0;
|
||||||
this.printSign(messageData.targetMorpion, messageData.targetCase, (this.sign == "x") ? "o" : "x");
|
this.printSign(messageData.targetMorpion, messageData.targetCase, (this.sign == "x") ? "o" : "x");
|
||||||
if (this.checkWin() != -1)
|
this.setOutline(this.currentMorpion, false);
|
||||||
printWin();
|
break;
|
||||||
|
|
||||||
|
case 'game_end':
|
||||||
|
this.game.finished = true;
|
||||||
|
this.canvas.removeEventListener("mousedown", (event, morpion = this) => this.onClick(event, morpion));
|
||||||
|
this.printWin(messageData.winning_sign);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printWin(winning_sign)
|
||||||
|
{
|
||||||
|
this.context.beginPath();
|
||||||
|
this.context.fillStyle = "white";
|
||||||
|
this.context.fillRect(this.width / 2 - 200, this.height - this.gap + 10, 400, 80);
|
||||||
|
this.context.closePath();
|
||||||
|
this.context.beginPath();
|
||||||
|
this.context.fillStyle = (winning_sign == "o") ? "red" : "green";
|
||||||
|
this.context.fillText((winning_sign == "o") ? "Winner is : O" : "Winner is : X", this.width / 2 - 30, this.height - this.gap / 2, 140);
|
||||||
|
this.context.closePath();
|
||||||
|
}
|
||||||
checkWin()
|
checkWin()
|
||||||
{
|
{
|
||||||
for (let i = 0; i < 9; i++)
|
for (let i = 0; i < 9; i++)
|
||||||
@ -85,6 +109,10 @@ class TicTacToe
|
|||||||
let y = event.offsetY;
|
let y = event.offsetY;
|
||||||
let targetMorpion, targetCase;
|
let targetMorpion, targetCase;
|
||||||
|
|
||||||
|
if (this.game.finished)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
targetMorpion = morpion.findPlace(x, this) + morpion.findPlace(y, this) * 3;
|
targetMorpion = morpion.findPlace(x, this) + morpion.findPlace(y, this) * 3;
|
||||||
if (morpion.findPlace(x, this) < 0 || morpion.findPlace(y, this) < 0)
|
if (morpion.findPlace(x, this) < 0 || morpion.findPlace(y, this) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
@ -92,8 +120,8 @@ class TicTacToe
|
|||||||
|
|
||||||
if (morpion.checkCase(targetMorpion, targetCase))
|
if (morpion.checkCase(targetMorpion, targetCase))
|
||||||
{
|
{
|
||||||
|
morpion.setOutline(this.currentMorpion, true);
|
||||||
morpion.sendCase(targetMorpion, targetCase);
|
morpion.sendCase(targetMorpion, targetCase);
|
||||||
morpion.setOutline();
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
morpion.incorrectCase();
|
morpion.incorrectCase();
|
||||||
@ -101,19 +129,20 @@ class TicTacToe
|
|||||||
|
|
||||||
checkCase(targetMorpion, targetCase)
|
checkCase(targetMorpion, targetCase)
|
||||||
{
|
{
|
||||||
return (this.map[targetMorpion][targetCase] == -1 && this.turn == true);
|
return (this.map[targetMorpion][targetCase] == -1 && this.turn == true && targetMorpion == this.currentMorpion);
|
||||||
}
|
}
|
||||||
|
|
||||||
incorrectCase()
|
incorrectCase()
|
||||||
{
|
{
|
||||||
console.log("bozo");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sendCase(targetMorpion, targetCase)
|
sendCase(targetMorpion, targetCase)
|
||||||
{
|
{
|
||||||
this.map[targetMorpion][targetCase] = (this.sign == "x") ? 0 : 1;
|
this.map[targetMorpion][targetCase] = (this.sign == "x") ? 0 : 1;
|
||||||
|
this.currentMorpion = targetCase;
|
||||||
this.printSign(targetMorpion, targetCase, this.sign);
|
this.printSign(targetMorpion, targetCase, this.sign);
|
||||||
console.log(this.game.send, targetMorpion, targetCase)
|
console.log(targetMorpion, targetCase)
|
||||||
this.game.send(JSON.stringify({"targetMorpion" : targetMorpion, "targetCase" : targetCase, "sign" : this.sign}));
|
this.game.send(JSON.stringify({"targetMorpion" : targetMorpion, "targetCase" : targetCase, "sign" : this.sign}));
|
||||||
console.log(this.turn);
|
console.log(this.turn);
|
||||||
this.turn = !this.turn;
|
this.turn = !this.turn;
|
||||||
@ -172,21 +201,25 @@ class TicTacToe
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
setOutline()
|
setOutline(targetMorpion, clear)
|
||||||
{
|
{
|
||||||
if (this.turn)
|
let targetX = (this.gap + targetMorpion % 3 * this.rectsize * 3);
|
||||||
|
let targetY = (this.gap + Math.floor(targetMorpion / 3) * this.rectsize * 3);
|
||||||
|
if (this.game.finished)
|
||||||
|
return;
|
||||||
|
if (!clear)
|
||||||
{
|
{
|
||||||
this.context.beginPath();
|
this.context.beginPath();
|
||||||
this.context.strokeStyle = (this.sign == "x") ? "green" : "red";
|
this.context.strokeStyle = (this.sign == "x") ? "green" : "red";
|
||||||
this.context.roundRect(0, 0, this.canvas.width, this.canvas.height, 25);
|
this.context.rect(targetX, targetY, this.rectsize * 3, this.rectsize * 3)
|
||||||
this.context.stroke();
|
this.context.stroke();
|
||||||
this.context.closePath();
|
this.context.closePath();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.context.beginPath();
|
this.context.beginPath();
|
||||||
this.context.strokeStyle = "#1a1a1d";
|
this.context.strokeStyle = `rgb(230 230 230)`;
|
||||||
this.context.roundRect(0, 0, this.canvas.width, this.canvas.height, 25);
|
this.context.rect(targetX, targetY, this.rectsize * 3, this.rectsize * 3)
|
||||||
this.context.stroke();
|
this.context.stroke();
|
||||||
this.context.closePath();
|
this.context.closePath();
|
||||||
}
|
}
|
||||||
@ -244,34 +277,6 @@ class TicTacToe
|
|||||||
}
|
}
|
||||||
this.context.closePath();
|
this.context.closePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
selectCase(x, y)
|
|
||||||
{
|
|
||||||
case_morpion = Math.floor(x / this.rectsize) + Math.floor((y / this.rectsize)) * 3
|
|
||||||
case_square = Math.floor(x / Math.floor(this.rectsize / 3)) % this.rectsize + Math.floor(y / this.rectsize) % this.rectsize
|
|
||||||
// ask server if case_morpion == playing_case && case_square == empty
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
setOutline()
|
|
||||||
{
|
|
||||||
if (this.turn)
|
|
||||||
{
|
|
||||||
this.context.beginPath();
|
|
||||||
this.context.strokeStyle = (this.sign == "x") ? "green" : "red";
|
|
||||||
this.context.roundRect(0, 0, this.canvas.width, this.canvas.height, 25);
|
|
||||||
this.context.stroke();
|
|
||||||
this.context.closePath();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
this.context.beginPath();
|
|
||||||
this.context.strokeStyle = "#1a1a1d";
|
|
||||||
this.context.roundRect(0, 0, this.canvas.width, this.canvas.height, 25);
|
|
||||||
this.context.stroke();
|
|
||||||
this.context.closePath();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { TicTacToe };
|
export { TicTacToe };
|
BIN
frontend/static/js/sound/tictactoe/play-move.mp3
Normal file
BIN
frontend/static/js/sound/tictactoe/play-move.mp3
Normal file
Binary file not shown.
@ -19,7 +19,6 @@ export class TicTacToeOnlineView extends AbstractView
|
|||||||
this.Morpion = new TicTacToe(this.height, this.width, 60, 60, document.getElementById("Morpion"), this.game_id);
|
this.Morpion = new TicTacToe(this.height, this.width, 60, 60, document.getElementById("Morpion"), this.game_id);
|
||||||
this.Morpion.DrawSuperMorpion();
|
this.Morpion.DrawSuperMorpion();
|
||||||
await this.Morpion.init();
|
await this.Morpion.init();
|
||||||
this.Morpion.setOutline();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async leavePage()
|
async leavePage()
|
||||||
|
@ -45,6 +45,8 @@ class TicTacToeWebSocket(WebsocketConsumer):
|
|||||||
self.member.send(self.member.sign)
|
self.member.send(self.member.sign)
|
||||||
|
|
||||||
if (self.game._everbody_is_here() and self.game.model.started == False):
|
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.broadcast("game_start")
|
||||||
self.game.model.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 (data.get("targetMorpion") is not None and data.get("targetCase") is not None):
|
||||||
if (self.game.add(data, self.member) == False):
|
if (self.game.add(data, self.member) == False):
|
||||||
return
|
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
|
pass
|
||||||
|
|
||||||
def disconnect(self, event):
|
def disconnect(self, event):
|
||||||
|
@ -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.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)]
|
self._map = [[-1 for _ in range(9)] for _ in range(9)]
|
||||||
|
|
||||||
def _everbody_is_here(self):
|
def _everbody_is_here(self):
|
||||||
@ -44,14 +48,15 @@ class TicTacToeGame(AGame):
|
|||||||
|
|
||||||
if (self.checkMove(newmove, player)):
|
if (self.checkMove(newmove, player)):
|
||||||
self._map[newmove.get("targetMorpion")][newmove.get("targetCase")] = newmove.get("sign")
|
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 True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def checkMove(self, newmove, player):
|
def checkMove(self, newmove, player):
|
||||||
print(int(newmove.get("targetMorpion")), player.currentMorpion)
|
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
|
return False
|
||||||
|
|
||||||
if (self._map[newmove.get("targetMorpion")][newmove.get("targetCase")] != -1):
|
if (self._map[newmove.get("targetMorpion")][newmove.get("targetCase")] != -1):
|
||||||
@ -62,16 +67,16 @@ class TicTacToeGame(AGame):
|
|||||||
def checkWin(self):
|
def checkWin(self):
|
||||||
for tab in self._map:
|
for tab in self._map:
|
||||||
for i in range(3):
|
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]
|
return tab[i]
|
||||||
for i in range(0, 9, 3):
|
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]
|
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]
|
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 tab[6]
|
||||||
return None
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _spectator_join(self, user_id: int, socket: WebsocketConsumer):
|
def _spectator_join(self, user_id: int, socket: WebsocketConsumer):
|
||||||
|
Loading…
Reference in New Issue
Block a user