xxxxxxxxxx
83
/* I'm trying to finish a rock, paper, scissor game. My current sketch shows two players, one on the left and one on the right. When mouse is clicked I use random() to generate both players' move -- rock is represented by ellipse; paper is represented by rectangle; and scissor is represented by triangle. Help me use booleans and conditionals to determine whether the outcome is player1 wins, player 2 wins, or tie, and dislpay the outcome for every round on the screen like the example GIF. */
let player1;
let player2;
let tie = false;
let player1wins = false;
let player2wins = false;
function setup() {
createCanvas(400, 400);
rectMode(CENTER);
textSize(16);
textAlign(CENTER);
noStroke();
player1 = round(random(1,3));
player2 = round(random(1,3));
}
function draw() {
background(220);
// player1
text("Player 1", width/4, height/2 - 50);
if (player1 == 1){ // rock
ellipse(width / 4, height / 2, 50, 50);
}
else if (player1 == 2){ // paper
rect(width / 4, height / 2, 50, 50);
}
else if (player1 == 3){ // scissor
triangle(width/4, height/2 - 25, width/4-25, height/2+25, width/4+25, height/2+25);
}
// player2
text("Player 2", width/1.3, height/2 - 50);
if (player2 == 1){ // rock
ellipse(width / 1.3, height / 2, 50, 50);
}
else if (player2 == 2){ // paper
rect(width / 1.3, height / 2, 50, 50);
}
else if (player2 == 3){ // scissor
triangle(width/1.3, height/2 - 25, width/1.3-25, height/2+25, width/1.3+25, height/2+25);
}
if(player1 == 1 && player2 == 1 || player1 == 2 && player1 == 2 || player1 == 3 && player2 == 3){
tie = true;
player2win = false;
} else if (player1 == 1 && player2 == 2 || player1 == 2 && player2 == 3 || player1 == 3 && player1 == 1){
tie = false;
player2win = true;
} else if (player1 == 1 && player2 == 3 || player1 == 2 && player2 == 1 || player1 == 3 && player2 == 2){
player1win = true;
tie = false
player2win = false;
}
//text at the bottom
if (tie){
text("tie!", 200, 300);
} else if (player2win){
text("player 2 wins", 200, 300);
} else if (player1win){
text("player 1 wins", 200, 300);
}
}
function mousePressed(){
player1 = round(random(1,3));
player2 = round(random(1,3));
}