Skip to content

Commit

Permalink
show signs on result
Browse files Browse the repository at this point in the history
  • Loading branch information
jbakse committed Oct 7, 2022
1 parent 5d4970b commit 75c4194
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 55 deletions.
118 changes: 63 additions & 55 deletions public/examples/rock_paper_scissors/index.js
@@ -1,37 +1,42 @@
// Play Rock Paper Scissors online

const signs = ["rock", "paper", "scissors"];
const colors = { player1: "#f33", player2: "#66f", observer: "gray" };
const buttons = [];

let shared;

/**
* here are the properties in `shared`
*
* shared.leftSigns
* randomized order of the signs
* shared.player1Signs - randomized order of the signs
* e.g. ["rock", "paper", "scissors"]
*
* shared.rightSigns
* randomized order of the signs
* shared.player2Signs - randomized order of the signs
* e.g. ["rock", "paper", "scissors]
*
* shared.leftSign
* currently selected sign of player1
* shared.player1Sign - currently selected sign of player1
* "rock" || "paper" || "scissors" || undefined
*
* shared.rightSign
* currently selected sign of player2 || undefined
* shared.player2Sign - currently selected sign of player2 || undefined
* "rock" || "paper" || "scissors" || undefined
*
* shared.gameState
* shared.gameState - high level switch of what stage of the game we are in
* "play" || "result"
*
* shred.winner
* shared.winner - the last winner, only valid if gameState === "result"
* "player1" || "player2" || "draw"
*/

let my;
let guests;
let my, guests;
/**
* here are the properties in `my` and guests[] objects
*
* x - the x position of the cursor
* y - the y position of the cursor
* show - whether to show the cursor
* role - "player1" || "player2" || "observer"
*
*/

function preload() {
// connect to the party server
Expand Down Expand Up @@ -121,10 +126,10 @@ function setup() {
}

function startRound() {
shared.leftSigns = shuffle(signs);
shared.rightSigns = shuffle(signs);
shared.leftSign = undefined;
shared.rightSign = undefined;
shared.player1Signs = shuffle(signs);
shared.player2Signs = shuffle(signs);
shared.player1Sign = undefined;
shared.player2Sign = undefined;
shared.gameState = "play";
}

Expand All @@ -148,21 +153,24 @@ function draw() {
background("black");
buttons.forEach((b) => b.draw());
guests.forEach(drawGuest);
if (shared.gameState === "result") drawResult();
}

if (shared.gameState === "result") {
fill("white");
textAlign(CENTER, CENTER);
textSize(32);
if (shared.winner === "player1") {
text("Player 1\nWins!", width / 2, height / 2);
}
if (shared.winner === "player2") {
text("Player 2\nWins!", width / 2, height / 2);
}
if (shared.winner === "draw") {
text("Draw!", width / 2, height / 2);
}
function drawResult() {
push();
fill("white");
textAlign(CENTER, CENTER);
textSize(32);
if (shared.winner === "player1") {
text("Player 1\nWins!", width / 2, height / 2);
}
if (shared.winner === "player2") {
text("Player 2\nWins!", width / 2, height / 2);
}
if (shared.winner === "draw") {
text("Draw!", width / 2, height / 2);
}
pop();
}

function mousePressed() {
Expand All @@ -172,46 +180,46 @@ function mousePressed() {
function stepHost() {
if (!partyIsHost()) return;
if (shared.gameState === "play") {
if (shared.leftSign !== undefined && shared.rightSign !== undefined) {
if (shared.player1Sign !== undefined && shared.player2Sign !== undefined) {
shared.gameState = "result";
shared.winner = getWinner(shared.leftSign, shared.rightSign);
shared.winner = getWinner(shared.player1Sign, shared.player2Sign);
setTimeout(startRound, 3000);
}
}
}

function getWinner(leftSign, rightSign) {
if (leftSign === rightSign) return "draw";
if (leftSign === "rock" && rightSign === "scissors") return "player1";
if (leftSign === "rock" && rightSign === "paper") return "player2";
if (leftSign === "paper" && rightSign === "rock") return "player1";
if (leftSign === "paper" && rightSign === "scissors") return "player2";
if (leftSign === "scissors" && rightSign === "paper") return "player1";
if (leftSign === "scissors" && rightSign === "rock") return "player2";
function getWinner(player1Sign, player2Sign) {
if (player1Sign === player2Sign) return "draw";
if (player1Sign === "rock" && player2Sign === "scissors") return "player1";
if (player1Sign === "rock" && player2Sign === "paper") return "player2";
if (player1Sign === "paper" && player2Sign === "rock") return "player1";
if (player1Sign === "paper" && player2Sign === "scissors") return "player2";
if (player1Sign === "scissors" && player2Sign === "paper") return "player1";
if (player1Sign === "scissors" && player2Sign === "rock") return "player2";
}

function syncUI() {
// sync the randomized sign order
buttons[0].sign = shared.leftSigns[0];
buttons[1].sign = shared.leftSigns[1];
buttons[2].sign = shared.leftSigns[2];
buttons[3].sign = shared.rightSigns[0];
buttons[4].sign = shared.rightSigns[1];
buttons[5].sign = shared.rightSigns[2];
buttons[0].sign = shared.player1Signs[0];
buttons[1].sign = shared.player1Signs[1];
buttons[2].sign = shared.player1Signs[2];
buttons[3].sign = shared.player2Signs[0];
buttons[4].sign = shared.player2Signs[1];
buttons[5].sign = shared.player2Signs[2];

// hide buttons when selection made
buttons[0].visible =
shared.leftSign === undefined || shared.leftSign === buttons[0].sign;
shared.player1Sign === undefined || shared.player1Sign === buttons[0].sign;
buttons[1].visible =
shared.leftSign === undefined || shared.leftSign === buttons[1].sign;
shared.player1Sign === undefined || shared.player1Sign === buttons[1].sign;
buttons[2].visible =
shared.leftSign === undefined || shared.leftSign === buttons[2].sign;
shared.player1Sign === undefined || shared.player1Sign === buttons[2].sign;
buttons[3].visible =
shared.rightSign === undefined || shared.rightSign === buttons[3].sign;
shared.player2Sign === undefined || shared.player2Sign === buttons[3].sign;
buttons[4].visible =
shared.rightSign === undefined || shared.rightSign === buttons[4].sign;
shared.player2Sign === undefined || shared.player2Sign === buttons[4].sign;
buttons[5].visible =
shared.rightSign === undefined || shared.rightSign === buttons[5].sign;
shared.player2Sign === undefined || shared.player2Sign === buttons[5].sign;
}

class Button {
Expand All @@ -238,7 +246,7 @@ class Button {
fill(colors[this.controller]);
textAlign(CENTER, CENTER);
textSize(18);
if (secret) {
if (secret && shared.gameState === "play") {
text("?", 0, 0);
} else {
text(this.sign, 0, 0);
Expand All @@ -251,10 +259,10 @@ class Button {

console.log("mousePressed", this.controller, my.role, this.sign);
if (this.controller === "player1") {
shared.leftSign = this.sign;
shared.player1Sign = this.sign;
}
if (this.controller === "player2") {
shared.rightSign = this.sign;
shared.player2Sign = this.sign;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions public/index.html
Expand Up @@ -101,6 +101,7 @@ <h3>Demos</h3>
<li><a href="examples/pong">pong</a></li>
<li><a href="examples/cover_up">cover_up</a></li>
<li><a href="examples/tic_tac_toe">tic_tac_toe</a></li>
<li><a href="examples/rock_paper_scissors">rock_paper_scissors</a></li>
</ul>
</section>

Expand Down

0 comments on commit 75c4194

Please sign in to comment.