I am trying to build a simple game of Tic Tac Toe with an AI that can only win or draw. I have taken the brute force approach to building this AI as the minmax approach is fairly complicated seen as I am new to javascript. The AI seems to work but the problem I have is that it makes too many moves when it is the AI's turn, so sometimes the AI will take two squares when I want it to take only one and sometimes it won't make any moves at all.
Code:
$(document).ready(function() {
var userChoice;
var computerChoice;
var dialogBox = $("#dialogBox");
var s1 = $("#top-left");
var s2 = $("#top");
var s3 = $("#top-right");
var s4 = $("#left");
var s5 = $("#middle");
var s6 = $("#right");
var s7 = $("#bottom-left");
var s8 = $("#bottom");
var s9 = $("#bottom-right");
var turn = 1; // turn 1=computer turn 2=user
var countMoves = 9;
renderDialog();
dialogChoice(); // user chooses X or O
move();
// make dialog box
function renderDialog() {
var winW = window.innerWidth;
var centerDialog = (winW / 2) - (600 * 0.5); // to center
dialogBox.css("display", "block")
dialogBox.css("left", centerDialog + "px");
dialogBox.css("top", "100px")
}
// make dialog interactive
function dialogChoice() {
var cross = $('#cross');
var circle = $('#circle');
cross.on("click", function() {
dialogBox.css("display", "none");
userChoice = "X";
computerChoice = "O";
computerRandom();
});
circle.on("click", function() {
dialogBox.css("display", "none");
userChoice = "O";
computerChoice = "X";
computerRandom();
});
}
// make function to allow user to click on square to move
function move() {
$("#top-left").on("click", function() {
if ($("#top-left").html() === '') {
$("#top-left").text(userChoice);
turn = 1;
userTurn();
};
});
$("#top").on("click", function() {
if ($("#top").html() === '') {
$("#top").text(userChoice);
turn = 1;
userTurn();
};
});
$("#top-right").on("click", function() {
if ($("#top-right").html() === '') {
$("#top-right").text(userChoice);
turn = 1;
userTurn();
}
});
$("#left").on("click", function() {
if ($("#left").html() === '') {
$("#left").text(userChoice);
turn = 1;
userTurn();
};
});
$("#middle").on("click", function() {
if ($("#middle").html() === '') {
$("#middle").text(userChoice);
turn = 1;
userTurn();
};
});
$("#right").on("click", function() {
if ($("#right").html() === '') {
$("#right").text(userChoice);
turn = 1;
userTurn();
};
});
$("#bottom-left").on("click", function() {
if ($("#bottom-left").html() === '') {
$("#bottom-left").text(userChoice);
turn = 1;
userTurn();
};
});
$("#bottom").on("click", function() {
if ($("#bottom").html() === '') {
$("#bottom").text(userChoice);
turn = 1;
userTurn();
};
});
$("#bottom-right").on("click", function() {
if ($("#bottom-right").html() === '') {
$("#bottom-right").text(userChoice);
turn = 1;
userTurn();
};
});
};
// switch turns on every round aswell as check for gameover tests.
function userTurn() {
computerAI();
computerRandom();
countMoves--;
console.log(turn);
if (isWinner() || draw()) {
countMoves = 9;
restartGame();
}
};
//check for winner
function isWinner() {
var result = false;
// first row
if (s1.text() == "X" && s2.text() == "X" && s3.text() == "X" || s1.text() == "O" && s2.text() == "O" && s3.text() == "O") {
s1.css("background", "green");
s2.css("background", "green");
s3.css("background", "green");
result = true;
return result;
}
//seconds row
if (s4.text() == "X" && s5.text() == "X" && s6.text() == "X" || s4.text() == "O" && s5.text() == "O" && s6.text() == "O") {
s4.css("background", "green");
s5.css("background", "green");
s6.css("background", "green");
result = true;
return result;
}
//third row
if (s7.text() == "X" && s8.text() == "X" && s9.text() == "X" || s7.text() == "O" && s8.text() == "O" && s9.text() == "O") {
s7.css("background", "green");
s8.css("background", "green");
s9.css("background", "green");
result = true;
return result;
}
// first column
if (s1.text() == "X" && s4.text() == "X" && s7.text() == "X" || s1.text() == "O" && s4.text() == "O" && s7.text() == "O") {
s1.css("background", "green");
s4.css("background", "green");
s7.css("background", "green");
result = true
return result;
}
//second column
if (s2.text() == "X" && s5.text() == "X" && s8.text() == "X" || s2.text() == "O" && s5.text() == "O" && s8.text() == "O") {
s2.css("background", "green");
s5.css("background", "green");
s8.css("background", "green");
result = true
return result;
}
//third column
if (s3.text() == "X" && s6.text() == "X" && s9.text() == "X" || s3.text() == "O" && s6.text() == "O" && s9.text() == "O") {
s3.css("background", "green");
s6.css("background", "green");
s9.css("background", "green");
result = true
return result;
}
// first diagonal
if (s1.text() == "X" && s5.text() == "X" && s9.text() == "X" || s1.text() == "O" && s5.text() == "O" && s9.text() == "O") {
s1.css("background", "green");
s5.css("background", "green");
s9.css("background", "green");
result = true
return result;
}
//second diagonal
if (s3.text() == "X" && s5.text() == "X" && s7.text() == "X" || s3.text() == "O" && s5.text() == "O" && s7.text() == "O") {
s3.css("background", "green");
s5.css("background", "green");
s7.css("background", "green");
result = true
return result;
}
};
function draw() {
var result = false;
if (countMoves == 0) {
result = true;
}
return result;
}
function restartGame() {
setTimeout(function() {
$(".col-xs-4").text('');
$(".col-xs-4").css("background", "red");
if (s5.text() == '') {
s5.text(computerChoice)
};
}, 2000)
}
function computerAI() {
countMoves--;
if (turn == 1) {
computerWin()
};
if (turn == 1) {
computerBlock()
};
if (turn == 1) {
computerFork()
};
if (turn == 1) {
blockFork()
}
if (turn == 1) {
computerRandom();
}
};
// computer will block opponent move
function computerBlock() {
// first Row1
if (s1.text() == userChoice && s2.text() == userChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
//first Row2
if (s3.text() == userChoice && s2.text() == userChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
//first Row3
if (s1.text() == userChoice && s3.text() == userChoice && s2.text() == '') {
s2.text(computerChoice);
turn = 2;
};
//second Row
if (s4.text() == userChoice && s5.text() == userChoice && s6.text() == '') {
s6.text(computerChoice);
turn = 2;
};
//second Row2
if (s6.text() == userChoice && s5.text() == userChoice && s4.text() == '') {
s4.text(computerChoice);
turn = 2;
};
//second Row3
if (s6.text() == userChoice && s4.text() == userChoice && s5.text() == '') {
s5.text(computerChoice);
turn = 2;
};
//third row
if (s7.text() == userChoice && s8.text() == userChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//third row2
if (s9.text() == userChoice && s8.text() == userChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
//third Row3
if (s9.text() == userChoice && s7.text() == userChoice && s8.text() == '') {
s8.text(computerChoice);
turn = 2;
};
//first column
if (s1.text() == userChoice && s3.text() == userChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
//first column2
if (s7.text() == userChoice && s4.text() == userChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
//first column3
if (s1.text() == userChoice && s7.text() == userChoice && s4.text() == '') {
s4.text(computerChoice);
turn = 2;
};
//second column
if (s2.text() == userChoice && s5.text() == userChoice && s8.text() == '') {
s8.text(computerChoice);
turn = 2;
};
//second column2
if (s8.text() == userChoice && s5.text() == userChoice && s2.text() == '') {
s2.text(computerChoice);
turn = 2;
};
//second column3
if (s2.text() == userChoice && s8.text() == userChoice && s5.text() == '') {
s5.text(computerChoice);
turn = 2;
};
//third column
if (s3.text() == userChoice && s6.text() == userChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//third column2
if (s9.text() == userChoice && s6.text() == userChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
//third column3
if (s3.text() == userChoice && s9.text() == userChoice && s6.text() == '') {
s6.text(computerChoice);
turn = 2;
};
// first diagonal
if (s1.text() == userChoice && s5.text() == userChoice) {
s9.text(computerChoice);
turn = 2;
};
//first diagonal 2
if (s9.text() == userChoice && s5.text() == userChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
// first diagonal 3
if (s1.text() == userChoice && s9.text() == userChoice && s5.text() == '') {
s5.text(computerChoice);
turn = 2;
};
//second diagonal
if (s3.text() == userChoice && s5.text() == userChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
//second diagonal2
if (s7.text() == userChoice && s5.text() == userChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
//second diagonal 3
if (s7.text() == userChoice && s3.text() == userChoice && s5.text() == '') {
s5.text(computerChoice);
turn = 2;
};
};
// computer will make move to win
function computerWin() {
// take third for win
// first Row1
if (s1.text() == computerChoice && s2.text() == computerChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
//first Row2
if (s3.text() == computerChoice && s2.text() == computerChoice) {
s1.text(computerChoice);
turn = 2;
};
//second Row
if (s4.text() == computerChoice && s5.text() == computerChoice && s6.text() == '') {
s6.text(computerChoice);
turn = 2;
};
//second Row2
if (s6.text() == computerChoice && s5.text() == computerChoice && s4.text() == '') {
s4.text(computerChoice);
turn = 2;
};
//third row
if (s7.text() == computerChoice && s8.text() == computerChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//third row2
if (s9.text() == computerChoice && s8.text() == computerChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
//first column
if (s1.text() == computerChoice && s3.text() == computerChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
//first column2
if (s7.text() == computerChoice && s4.text() == computerChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
//second column
if (s2.text() == computerChoice && s5.text() == computerChoice && s8.text() == '') {
s8.text(computerChoice);
turn = 2;
};
//second column2
if (s8.text() == computerChoice && s5.text() == computerChoice && s2.text() == '') {
s2.text(computerChoice);
turn = 2;
};
//third column
if (s3.text() == computerChoice && s6.text() == computerChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//third column2
if (s9.text() == computerChoice && s6.text() == computerChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
// first diagonal
if (s1.text() == computerChoice && s5.text() == computerChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//first diagonal2
if (s9.text() == computerChoice && s5.text() == computerChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
//second diagonal
if (s3.text() == computerChoice && s5.text() == computerChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
//second diagonal2
if (s7.text() == computerChoice && s5.text() == computerChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
};
// computer make move to create a fork
function computerFork() {
// fork1
if (s1.text() == computerChoice && s5.text() == computerChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
//fork2
if (s3.text() == computerChoice && s5.text() == computerChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
//fork3
if (s7.text() == computerChoice && s5.text() == computerChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//fork4
if (s9.text() == computerChoice && s5.text() == computerChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
};
function blockFork() {
// fork1
if (s1.text() == userChoice && s5.text() == userChoice && s3.text() == '') {
s3.text(computerChoice);
turn = 2;
};
//fork2
if (s3.text() == userChoice && s5.text() == userChoice && s1.text() == '') {
s1.text(computerChoice);
turn = 2;
};
//fork3
if (s7.text() == userChoice && s5.text() == userChoice && s9.text() == '') {
s9.text(computerChoice);
turn = 2;
};
//fork4
if (s9.text() == userChoice && s5.text() == userChoice && s7.text() == '') {
s7.text(computerChoice);
turn = 2;
};
};
function computerRandom() {
var squareArr = [s1, s2, s3, s4, s5, s6, s7, s8, s9];
var randomNum = Math.floor(Math.random() * 8);
if (squareArr[randomNum].text() == '') {
squareArr[randomNum].text(computerChoice);
squareArr.splice(randomNum, 1);
turn = 2;
}
};
});
example:http://codepen.io/aliz16/full/yJgQQd/
Check you functions that play for the computer.
Let's take computerWin for an example. What is stopping the function from making multiple marks? Several Conditions could be true. Delegating the turn doesn't stop the checks.
P.S. not all lines are correct either.
Related
I just wrote this piece of code that does the thing it's supposed to do, although it's really messy and pretty repetitive and I'm wondering how can I make it much shorter and concise.
if(id==1 && player == "playerOne"){
Object.assign(playerOne, fighters[0])
}else if(id==1 && player =="playerTwo"){
Object.assign(playerTwo, fighters[0])
}
if(id==2 && player == "playerOne"){
Object.assign(playerOne, fighters[1])
}else if(id==2 && player =="playerTwo"){
Object.assign(playerTwo, fighters[1])
}
if(id==3 && player == "playerOne"){
Object.assign(playerOne, fighters[2])
}else if(id==3 && player =="playerTwo"){
Object.assign(playerTwo, fighters[2])
}
if(id==4 && player == "playerOne"){
Object.assign(playerOne, fighters[3])
}else if(id==4 && player =="playerTwo"){
Object.assign(playerTwo, fighters[3])
}
if(id==5 && player == "playerOne"){
Object.assign(playerOne, fighters[4])
}else if(id==5 && player =="playerTwo"){
Object.assign(playerTwo, fighters[4])
}
if(id==6 && player == "playerOne"){
Object.assign(playerOne, fighters[5])
}else if(id==6 && player =="playerTwo"){
Object.assign(playerTwo, fighters[5])
}
if(id==7 && player == "playerOne"){
Object.assign(playerOne, fighters[6])
}else if(id==7 && player =="playerTwo"){
Object.assign(playerTwo, fighters[6])
}
if(id==8 && player == "playerOne"){
Object.assign(playerOne, fighters[7])
}else if(id==8 && player =="playerTwo"){
Object.assign(playerTwo, fighters[7])
}
if(id==9 && player == "playerOne"){
Object.assign(playerOne, fighters[8])
}else if(id==9 && player =="playerTwo"){
Object.assign(playerTwo, fighters[8])
}
Thank you in advance!
Assuming
a) there are no more than two players
b) you don't care that this code handles id < 1 and id > 9
It looks to me like you could reduce this to a single line.
Object.assign(player == "playerOne" ? playerOne : playerTwo, fighters[id - 1])
What if you rewrite it to be something along the lines of
if (player == "playerOne") {
p = playerOne
} else if (player =="playerTwo") {
p = playerTwo
}
Object.assign(p, fighters[id - 1])
A for loop might be helpful:
for (let i = 1; i < 10; i++) {
if (id == i && player == "playerOne") {
Object.assign(playerOne, fighters[id - 1]);
} else if (id == i && player == "playerTwo") {
Object.assign(playerTwo, fighters[id - 1]);
}
}
Based on the assumptions in your example, that id must be between 1 and 9 and player can be anything, but must be playerOne or playerTwo:
for (var i = 1; i < 10; i++) {
if (id == i) {
if(player == "playerOne"){
Object.assign(playerOne, fighters[i - 1]);
} else if (player == "playerTwo") {
Object.assign(playerTwo, fighters[i - 1]);
}
}
}
I'm coding a basic rock paper scissors game.
the 2 functions are working fine. the issue is the winner variable is always undefined..
I don't know what to do to correct this. I want it to say who won, computer or the human(user).
function game(x, y) {
var inputC = computerPlay();
var inputH = humanPlay();
var winner;
if (inputH == 'paper' && inputC == 'scissors') {
console.log('Computer wins with Scissors ');
if (inputH == 'scissors' && inputC == 'rock') {
console.log('Computer wins with rock');
if (inputC == 'paper' && inputH == 'rock') {
console.log('Computer wins with paper');
}
}
winner = "computer";
} else if (inputC == 'paper' && inputH == 'scissors') {
console.log('Human wins with Scissors ');
if (inputC == 'scissors' && inputH == 'rock') {
console.log('Human wins with rock');
if (inputH == 'paper' && inputC == 'rock') {
console.log('Human wins with paper');
}
}
winner = "human";
}
document.getElementById("text1").innerHTML = winner;
console.log("result is: " + winner + " wins");
}
I'm sure its something minor but my god I'm all out of ideas.
would like this
function game() {
var inputC = computerPlay();
var inputH = humanPlay();
var winner = "human";
if (inputH === inputC) {
winner = "nobody";
} else if ((inputH === 'paper' && inputC === 'scissors') ||
(inputH === 'scissors' && inputC === 'rock') ||
(inputH === 'rock' && inputC === 'paper')
) {
console.log('Computer wins with ' + inputC);
winner = "computer";
} else {
console.log('Human wins with ' + inputH);
}
document.getElementById("text1").innerHTML = winner;
console.log("result is: " + winner + " wins");
}
it was undefined because it didn't go to either one of condition.
Another alternative answer from me
function game(x, y) {
var inputC = computerPlay();
var inputH = humanPlay();
var isComputerWin =
(inputC == 'scissors' && inputH == 'paper') ||
(inputC == 'rock' && inputH == 'scissors') ||
(inputC == 'paper' && inputH == 'rock');
var winner = isComputerWin ? 'computer' : 'human';
var winnerWith = isComputerWin ? inputC : inputH;
console.log(winner + " wins with " + winnerWith);
document.getElementById("text1").innerHTML = winner;
console.log("result is: " + winner + " wins");
}
So I am trying to create this Rock, Paper, Scissors program to run on a telnet server. Except It's not allowing me to enter the words "Rock","Paper", or "Scissors".
First CPUhand is showing up as undefined and is not setting to one of the options in my if statements.
Second whenever I enter a single character in the command prompt, It gives me my "Invalid, try again!" else statement and skips to the next line.
Is anyone able to figure out why my if statements for CPUhand are not working or why I can't enter more than a single character in command prompt?
Screenshot1 Screenshot2
"use strict";
const
net = require('net'),
server = net.createServer(function(connection) { //create server
let randomNum = randomNumber(1, 3);
let CPUhand
if(randomNum == 1){
CPUhand == "Rock";
} else if(randomNum == 2){
CPUhand == "Paper";
} else if(randomNum == 3){
CPUhand == "Scissors";
}
connection.write("Enter: Rock, Paper, or Scissors!\r\n");
connection.on('data', function(chunk) { //collect data from user
let USERhand = chunk.toString();
if(CPUhand === "Rock" && USERhand === "Scissors" || USERhand === "Rock" && CPUhand === "Scissors"){
connection.write("Rock beats Scissors!\r\n");
}
else if(CPUhand === "Paper" && USERhand === "Rock" || USERhand === "Paper" && CPUhand === "Rock"){
connection.write("Paper beats Rock!\r\n");
}
else if(CPUhand === "Scissors" && USERhand === "Paper" || USERhand === "Scissors" && CPUhand === "Paper"){
connection.write("Scissors beats Paper!\r\n");
}
else if(CPUhand === USERhand){
connection.write("Draw!\r\n");
}
else{
connection.write("Invalid! Try Again! \r\n");
}
});
}); server.listen(5432); //bind port
function randomNumber(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
1) Typo in block:
if(randomNum == 1){
CPUhand == "Rock";
} else ...
Should be:
if(randomNum == 1){
CPUhand = "Rock";
} else ...
2) In user input you have also line break symbol:
let USERhand = chunk.toString();
Replace with:
let USERhand = chunk.toString().trim();
var boxId = ['boxMid','boxLeft','boxRight','boxTopMid','boxTopRight','boxTopLeft','boxLow Mid','boxLowLeft','boxLowRight'];
var turn = 0;
var pOne = "X";
var pTwo = "O";
var blankBox = "";
var boxMid = document.getElementById('boxMid').innerhtml;
var boxLeft = document.getElementById('boxLeft').innerhtml;
var boxRight = document.getElementById('boxRight').innerhtml;
var boxTopMid = document.getElementById('boxTopMid').innerhtml;
var boxTopLeft = document.getElementById('boxTopLeft').innerhtml;
var boxTopRight = document.getElementById('boxTopRight').innerhtml;
var boxLowMid = document.getElementById('boxLowMid').innerhtml;
var boxLowLeft = document.getElementById('boxLowLeft').innerhtml;
var boxLowRight = document.getElementById('boxLowRight').innerhtml;
var i = 0;
My player win function is not working and I've had several others look at it, so I figured I would get an outside perspective.
function xWins() {
if(boxId[0,1,2] == 'X'||
boxTopLeft == "X" && boxTopMid == "X" && boxTopRight == "X"||
boxLowMid == "X" && boxLowLeft == "X" && boxLowRight == "X"||
boxMid == "X" && boxTopMid == "X" && boxLowMid == "X"||
boxLeft == "X" && boxTopLeft == "X" && boxLowLeft == "X"||
boxRight == "X" && boxTopRight == "X" && boxLowRight == "X"||
boxMid == "X" && boxTopLeft == "X" && boxLowRight == "X"||
boxMid == "X" && boxLowLeft == "X" && boxTopRight == "X" ) {
alert ('Player One Wins!');
turn = 1;
}
}
function oWins(){
if(boxId[0,1,2] == "O"||
boxTopLeft == "O" && boxTopMid == "O" && boxTopRight == "O"||
boxLowMid == "O" && boxLowLeft == "O" && boxLowRight == "O"||
boxMid == "O" && boxTopMid == "O" && boxLowMid == "O"||
boxLeft == "O" && boxTopLeft == "O" && boxLowLeft == "O"||
boxRight == "O" && boxTopRight == "O" && boxLowRight == "O"||
boxMid == "O" && boxTopLeft == "O" && boxLowRight == "O"||
boxMid == "O" && boxLowLeft == "O" && boxTopRight == "O" ) {
alert ('Player Two Wins!');
turn = 0;
}
}
function test(bx){
if(document.getElementById(boxId[bx]).innerHTML == ""){
if (turn<1) {
document.getElementById(boxId[bx]).innerHTML = pOne;
document.onclick (turn++);
oWins();
xWins();
}
if (turn == 1) {
document.getElementById(boxId[bx]).innerHTML = pTwo;
document.onclick (turn--);
oWins();
xWins();
}
}
}
function reset() {
for(i=0;i<boxId.length;i++)
document.getElementsByClassName('boxy')[i].innerHTML = blankBox;
}
As far as I know, everything else works except for my scoreboard, but I'll figure that out when I get there. Please keep in mind that I am a high school student that has less than 2 months of real experience coding, so I probably did some things that could've been a lot easier, and more DRY.
Cause of Bug: You are telling it that oWins() by calling the oWins function, where it runs through your cascasde of OR conditionals to which one of them is matching. So you have 2 problems. 1, that oWins() is always testing true via your OR statements, and 2, that xWins() is being called after the oWins() function that is evaluating to true each time. Stringing together OR conditionals like that is problematic and often difficult to debug.
if (turn == 1) {
document.getElementById(boxId[bx]).innerHTML = pTwo;
document.onclick (turn--);
oWins();
xWins();
}
Im doing a project for class, it's a paper rock scissor type game. I have the game working fine, I'm now simply trying to make it so that you play for a best of three scenario.
I coded it (at least I tried to) so that when you win, playerwin gets a point, and when you lose, compwin gets a point. When one of you has two points, you get an alert saying you won or lost.
I ran through stack trying to find out how to do this on my own, since this is new to me. Tried my best, came up short. Im not getting any actual errors, its just not doing what I want. I thought that parseInt-'ing the variable would make it a number, but it's coming up as NaN, so its clearly not. What am I doing wrong?
Jsfiddle - http://jsfiddle.net/Lwj2zny5/
Html -
<body>
<div id="wrapper">
<h1>Lizard, paper, scissors, spock, rock</h1>
<div id="images">
<img class="game-image" src="Images/lizard.jpg" width="150" height="150" alt="" data-value="1"/>
<img class="game-image" src="Images/paper.jpg" width="150" height="150" alt="" data-value="2"/>
<img class="game-image" src="Images/scissors.jpg" width="150" height="150" alt="" data-value="3"/>
<img class="game-image" src="Images/spock.jpg" width="150" height="150" alt="" data-value="4"/>
<img class="game-image" src="Images/rock.jpg" width="150" height="150" alt="" data-value="5"/>
</div>
<div id="win">
</div>
<div id="score">
</div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="unit2.js"></script>
</body>
JS -
$(function() {
$(document).on('click', '.game-image', function(event) {
var value = $(event.target).data('value');
var win;
var playerwin = parseInt($.trim($(this).html()));
var compwin = parseInt($.trim($(this).html()));
var comproll = 1 + Math.floor(Math.random() * 5);
//$('#comproll').html('Result: '+comproll)
if (comproll === 1) {
comp = "Lizard";
} else if (comproll === 2) {
comp = "paper";
} else if (comproll === 3) {
comp = "scissors";
} else if (comproll === 4) {
comp = "Spock";
} else if (comproll === 5) {
comp = "Rock";
}
if (value === comproll) {
win = "This ends in a tie, computer chose " + comp + " also.";
} else if (value === 1) {
if (comproll === 2 || comproll === 4) {
win = "You win, comp choose " + comp + ".";
$(this).html(++playerwin);
} else if (comproll === 3 || comproll === 5) {
win = "You lose, comp choose " + comp + ".";
$(this).html(++compwin);
}
} else if (value === 2) {
if (comproll === 4|| comproll === 5) {
win = "You win, comp choose " + comp + ".";
$(this).html(++playerwin);
} else if (comproll === 1 ||comproll === 3) {
win = "You lose, comp choose " + comp + ".";
$(this).html(++compwin);
}
} else if (value === 3) {
if (comproll === 1 || comproll === 2) {
win = "You win, comp choose " + comp + ".";
$(this).html(++playerwin);
} else if (comproll === 4 || comproll === 5) {
win = "You lose, comp choose " + comp + ".";
$(this).html(++compwin);
}
} else if (value === 4) {
if (comproll === 3 || comproll === 5) {
win = "You win, comp choose " + comp + ".";
$(this).html(++playerwin);
} else if (comproll === 1 || comproll === 2) {
win = "You lose, comp choose " + comp + ".";
$(this).html(++compwin);
}
} else if (value === 5) {
if (comproll === 1 || comproll === 3) {
win = "You win, comp choose " + comp + ".";
$(this).html(++playerwin);
} else if (comproll === 2 || comproll === 4) {
win = "You lose, comp choose " + comp + ".";
$(this).html(++compwin);
}
}
$('#score').text(playerwin, compwin);
if (playerwin == 2) {
alert("You Won");
} else if (compwin == 2) {
alert("You lose");
}
$('#win').text(win);
}); //closes play function
}); // closes function
Yes i know this is bulky code, but what we're learning atm is if/else statements so thats what im using.
Thanks ahead of time!
$(document).on('click', '.game-image', function(event) {
...
var playerwin = parseInt($.trim($(this).html()));
...
$(this) refers to the clicked image. Images do not contain .html()
$(function() {
var compwin = 0;
var playerwin = 0;
$(document).on('click', '.game-image', function(event) {
var value = $(event.target).data('value');
var win;
var comproll = 1 + Math.floor(Math.random() * 5);
then
$("#win").text(win);
$('#score').text("Player = " + playerwin + ", Computer = " + compwin);
if (playerwin == 2) {
alert("You Won");
compwin = 0;
playerwin = 0;
$('#score').text("");
$("#win").text("");
} else if (compwin == 2) {
alert("You lose");
compwin = 0;
playerwin = 0;
$('#score').text("");
$("#win").text("");
}
As others have pointed out, you are getting the scores from image html instead of having variables tracking it. I created a jsfiddle trying to understand your code. But I cleaned it up too much and looks little bit different.
http://jsfiddle.net/sheth/uzagumu7/22/
I created short functions trying to make the code cleaner.
var playerScore = 0;
var computerScore = 0;
var choices = ["Lizard", "Paper", "Scissors", "Spock", "Rock"];
var computerRoll = function () {
return 1 + Math.floor(Math.random() * 5);
}
var resultOfGame = function (playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
return 0; // tie
} else if (
((playerChoice === 1) && ((computerChoice === 2) || (computerChoice === 4))) ||
((playerChoice === 2) && ((computerChoice === 4) || (computerChoice === 5))) ||
((playerChoice === 3) && ((computerChoice === 1) || (computerChoice === 2))) ||
((playerChoice === 4) && ((computerChoice === 3) || (computerChoice === 5))) ||
((playerChoice === 5) && ((computerChoice === 1) || (computerChoice === 3))))
{
// player wins
return 1;
} else {
return -1;
}
}
Newer version of jsfiddle has image with usemap. Also removed the alert.
http://jsfiddle.net/sheth/uzagumu7/78/