Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Simon says in JavaScript?

Cookiecrumbles222
 Share



var playerChoice = 0;
var playerTotal = 0;
var computerChoice = 0;
var computerChoiceOut;
var playerList = [];
var computerList = [];
var playerChoiceOut;
var i;
var thing = 0;
var acount = 0;

window.onload = function() {    
 document.getElementById("info").style.visibility = "hidden";
 document.getElementById("score").style.visibility = "hidden";
 document.getElementById("things").style.visibility = "hidden";
}

function test(){
    
    for(i = 0; i < playerList.length; i++){
        if(playerList[i] == computerList[i]){
            thing++;
        }
    }
    if(thing == playerList.length){
           next();
        }
    if (thing != playerList.length){
        gameOver();
    }
    
}

function next(){
    document.getElementById("things").style.pointerEvents = "auto";
    comp();
    acount = 0;
    playerTotal++;
    document.getElementById("cscore").innerHTML = playerTotal;
}
/* If user click rock/paper/cuter */
function rockf(){
    playerChoice = 1;
    playerChoiceOut = "Rock";
    playerList.push(playerChoiceOut);
    acount++;
    if(acount == computerList.length){test(); document.getElementById("things").style.pointerEvents = "none";}
    
}
function paperf(){
    playerChoice = 2;
    playerChoiceOut = "Paper";
    playerList.push(playerChoiceOut);
    acount++;
    if(acount == computerList.length){test(); document.getElementById("things").style.pointerEvents = "none";}
}
function cuterf(){
    playerChoice = 3;
    playerChoiceOut = "Scissors";
    playerList.push(playerChoiceOut);
    acount++;
    if(acount == computerList.length){test(); document.getElementById("things").style.pointerEvents = "none";}
}

function comp(){
    computerChoice = Math.floor(Math.random() * 3) + 1;
    
    if(computerChoice == 1) {
       computerChoiceOut = "Rock";
       }
    if(computerChoice == 2) {
       computerChoiceOut = "Paper";
       }
    if(computerChoice == 3) {
       computerChoiceOut = "Scissors";
       }
    computerList.push(computerChoiceOut);
    document.getElementById("info").innerHTML = "<p>Computer Picks</p>" + computerChoiceOut;
}


function gameOver(){
    document.getElementById("rock").style.visibility = "hidden";
    document.getElementById("paper").style.visibility = "hidden";
    document.getElementById("cuter").style.visibility = "hidden";
    document.getElementById("score").style.visibility = "hidden";
    document.getElementById("info").innerHTML ="Your Final score was " + playerTotal;
    document.getElementById("things").style.visibility = "hidden";
    document.getElementById("top").innerHTML = "<h1>Game Over</h1>";
    document.getElementById("hi").style.visibility = "visible";
    document.getElementById("hi").innerHTML = "<p>Start Again</p>";
}


function startGame(){
    playerChoice = 0;
    playerTotal = 0;
    comp();
    document.getElementById("info").style.visibility = "visible";
    document.getElementById("score").style.visibility = "visible";
    document.getElementById("things").style.visibility = "visible";
    document.getElementById("top").innerHTML = "<h1>WELCOME TO RPS</h1>";
    document.getElementById("cscore").innerHTML = playerTotal;
    document.getElementById("hi").style.visibility = "hidden";
    document.getElementById("rock").style.visibility = "visible";
    document.getElementById("paper").style.visibility = "visible";
    document.getElementById("cuter").style.visibility = "visible";
    document.getElementById("rock").style.pointerEvents = "auto";
    document.getElementById("paper").style.pointerEvents = "auto";
    document.getElementById("cuter").style.pointerEvents = "auto";
    
}

hey guys im tring to make a simon say type game but with rock, paper, and scissors as the colors. I'm trying to make a sequence and then test if they match and if they do they get a point, and if they don't they lose, but I'm running into errors anyone know why? Thanks for any help!

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Cookiecrumbles222 said:

but I'm running into errors anyone know why?

can you tell us which errors?

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

What is the purpose of test()? It is the only function that calls gameOver(), which ends the game. As written, test() always ends the game after one round.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, badreg said:

What is the purpose of test()? It is the only function that calls gameOver(), which ends the game. As written, test() always ends the game after one round.

its to test the two arrays to see if they are the same if they are continue and if they are not, then game over, did i mess it up?

Link to comment
Share on other sites

Link to post
Share on other sites

I didn't read through your entire code to troubleshoot, but you can console.log the relevant variables to make sure that it's doing what you intend. There's nothing else that calls gameOver(), so it's either the test() function, or one or more of the variables that it is checking.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×