-
Notifications
You must be signed in to change notification settings - Fork 0
/
portfolio.js
157 lines (139 loc) · 5.18 KB
/
portfolio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//The Clicking Game!!!!
// // sets background color when color is clicked
$("#redBtn").on("click", function () {
console.log("test");
$("body").addClass("bgCol1");
});
$("#blueBtn").on("click", function () {
console.log("test");
$("body").addClass("bgCol2");
});
$("#yellowBtn").on("click", function () {
console.log("test");
$("body").addClass("bgCol3");
});
//player 1 & 2 score counter
var p1 = 0;
$(".p1Btn").on("click",function () {
p1++;
console.log(p1);
$(".display").text(p1);
outcome();
score();
});
//
//
var p2 = 0;
$(".p2Btn").on("click",function () {
p2++;
console.log(p2);
$(".display2").text(p2);
outcome();
score();
});
// function used to keep up with winner
function outcome() {
if (p1 === 15)
{
alert("Player 1 WINS!!!")
}
else if (p2 === 15)
{
alert("Player 2 WINS!!!")
}
}
function score() {
if (p1 > p2)
{
$("#winning").text("Player 1!!!")
}
else if (p1 < p2)
{
$("#winning").text("Player 2!!!")
}
else if (p1 === p2)
{
$("#winning").text("TIED!!!")
}
}
// function used for keyboard and mouse option
$("#mouse").on("click", function () {
$("#keyboard").removeAttr("disabled","");
$("#mouse").attr("disabled","");
});
$("#keyboard").on("click", function () {
$("#mouse").removeAttr("disabled","");
$("#keyboard").attr("disabled","");
//adds the ability to use keys P 7 W
document.addEventListener("keydown", function (e) {
if(e.code === "KeyW")
{
p1++;
$(".display").text(p1);
outcome();
score();
}
else if(e.code === "KeyP")
{
p2++;
$(".display2").text(p2);
outcome();
score();
}
})
});
// Tic Tac Toe!!!!var gameSpaces = document.querySelectorAll("td");
var gameSpaces = document.querySelectorAll("td");
var player1 = 1;
function clickedSquare(e) {
console.log("hjh");
if (player1 === 1 && e.target.innerHTML ==="")
{
e.target.innerHTML = "X";
player1 = 2;
}
else if(e.target.innerHTML === "")
{
e.target.innerHTML = "O";
player1 = 1;
}
checkForWinner()
}
for (var i = 0; i < gameSpaces.length; i++) {
gameSpaces[i].addEventListener('click', clickedSquare);
}
//function to check for winner
function checkForWinner() {
console.log('Checking for winner');
if((gameSpaces[0].innerText=== gameSpaces[1].innerText &&gameSpaces[1].innerText === gameSpaces[2].innerText && gameSpaces[2].innerText==="X")
||(gameSpaces[3].innerText=== gameSpaces[4].innerText &&gameSpaces[4].innerText === gameSpaces[5].innerText && gameSpaces[5].innerText==="X")
||(gameSpaces[6].innerText=== gameSpaces[7].innerText &&gameSpaces[7].innerText === gameSpaces[8].innerText && gameSpaces[8].innerText==="X")
||(gameSpaces[0].innerText=== gameSpaces[3].innerText &&gameSpaces[3].innerText === gameSpaces[6].innerText && gameSpaces[6].innerText==="X")
||(gameSpaces[1].innerText=== gameSpaces[4].innerText &&gameSpaces[4].innerText === gameSpaces[7].innerText && gameSpaces[7].innerText==="X")
||(gameSpaces[2].innerText=== gameSpaces[5].innerText &&gameSpaces[5].innerText === gameSpaces[8].innerText && gameSpaces[8].innerText==="X")
||(gameSpaces[0].innerText=== gameSpaces[4].innerText &&gameSpaces[4].innerText === gameSpaces[8].innerText && gameSpaces[8].innerText==="X")
||(gameSpaces[2].innerText=== gameSpaces[4].innerText &&gameSpaces[4].innerText === gameSpaces[6].innerText && gameSpaces[6].innerText==="X"))
{
alert("X wins");
return false;
}
else if((gameSpaces[0].innerText=== gameSpaces[1].innerText &&gameSpaces[1].innerText === gameSpaces[2].innerText && gameSpaces[2].innerText==="O")
||(gameSpaces[3].innerText=== gameSpaces[4].innerText &&gameSpaces[4].innerText === gameSpaces[5].innerText && gameSpaces[5].innerText==="O")
||(gameSpaces[6].innerText=== gameSpaces[7].innerText &&gameSpaces[7].innerText === gameSpaces[8].innerText && gameSpaces[8].innerText==="O")
||(gameSpaces[0].innerText=== gameSpaces[3].innerText &&gameSpaces[3].innerText === gameSpaces[6].innerText && gameSpaces[6].innerText==="O")
||(gameSpaces[1].innerText=== gameSpaces[4].innerText &&gameSpaces[4].innerText === gameSpaces[7].innerText && gameSpaces[7].innerText==="O")
||(gameSpaces[2].innerText=== gameSpaces[5].innerText &&gameSpaces[5].innerText === gameSpaces[8].innerText && gameSpaces[8].innerText==="O")
||(gameSpaces[0].innerText=== gameSpaces[4].innerText &&gameSpaces[4].innerText === gameSpaces[8].innerText && gameSpaces[8].innerText==="O")
||(gameSpaces[2].innerText=== gameSpaces[4].innerText &&gameSpaces[4].innerText === gameSpaces[6].innerText && gameSpaces[6].innerText==="O"))
{
alert("O wins");
return false;
}
}
// Add a New Game button that when clicked, will clear the game area and start a new game.
var btn = document.getElementById("reset");
btn.onclick=function(e){
console.log('button clicked');
for (var i = 0; i < gameSpaces.length; i++)
gameSpaces[i].innerHTML="";
};