-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
203 lines (156 loc) · 5.64 KB
/
index.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var player1 = {
playerName: "Player1",
playerMarbles: 10,
playerNumber: 0,
ready: false
};
var player2 = {
playerName: "Player2",
playerMarbles: 10,
playerNumber: 0,
ready: false
};
var titleText = ".titleText";
// Step1 : Set Name
$(".setName").click(function (e) {
e.preventDefault();
if($(".p1Name").val() == "") {
alert("You cannot play without setting both names");
}
else if($(".p2Name").val() == "") {
alert("You cannot play without setting both names");
}
else {
$(".rollBtn").css("display", "block");
$(".setName").css("display", "none");
$(".p1Name, .p2Name").attr("readonly", "readonly");
$(".p1Name, .p2Name").css("background-color", "transparent").css("color", "white");
player1.playerName = $(".p1Name").val();
player2.playerName = $(".p2Name").val();
}
});
// Step 2: Can roll
$(".rollBtn").click(function (e) {
player1.playerNumber = $("#Player1").val();
player2.playerNumber = $("#Player2").val();
e.preventDefault();
CheckNumbers();
});
// Step 3: Check numbers if there is not more then 20 and less then 1
function CheckNumbers() {
if(player1.playerNumber > 20 || player1.playerNumber < 1) {
console.log($("input").val());
$("#Player1").css("background-color", "red");
$("#Player1").val("");
setTimeout(function() {
alert(player1.playerName + " Choose between 1-10");
$("#Player1").css("background-color", "#94FDFF");
},100);
}
else if (player2.playerNumber > 20 || player2.playerNumber < 1) {
console.log($("input").val());
$("#Player2").css("background-color", "red");
$("#Player2").val("");
setTimeout(function() {
alert(player2.playerName + " Choose between 1-10");
$("#Player2").css("background-color", "#94FDFF");
},100);
}
else {
CheckMarbles();
}
}
function CheckMarbles() {
if(player1.playerMarbles >= player1.playerNumber) {
player1.ready = true;
}
else {
alert(player1.playerName + ` Dont have enough marbles!`)
}
if(player2.playerMarbles >= player2.playerNumber) {
player2.ready = true;
}
else {
alert(player2.playerName + " Dont have enough marbles!")
}
if (player1.ready && player2.ready) {
console.log("Ready and Roll");
player1.playerMarbles -= player1.playerNumber;
player2.playerMarbles -= player2.playerNumber;
$(".p1Marbles").text(player1.playerMarbles);
$(".p2Marbles").text(player2.playerMarbles);
RollMarbles();
$(".rollBtn").css("display", "none");
}
}
function RollMarbles() {
$(titleText).text("Rolling 🎲");
setTimeout(function() {
var randomNumber = Math.floor(Math.random() * 2);
var player1Result = player1.playerNumber % 2;
var player2Result = player2.playerNumber % 2;
console.log(`Results ${randomNumber}, P1: ${player1Result}, P2 ${player2Result}`);
CheckResults(randomNumber, player1Result, player2Result);
}, 1500)
setTimeout(function() {
player1.ready = false;
player2.ready = false;
$(".rollBtn").css("display", "block");
},2000)
}
// 0 = Odd / 1 = Even
// p1 (4) % 2 = 0 = Even
// p2 (3) % 2 = 1 = Odd
function CheckResults(result, p1Result, p2Result) {
console.log(`${p1Result} + ${p2Result}`);
if(p1Result == p2Result) {
$(titleText).text("- DRAW -");
player1.playerMarbles = parseInt(player1.playerMarbles) + parseInt(player1.playerNumber);
player2.playerMarbles = parseInt(player2.playerMarbles) + parseInt(player2.playerNumber);
}
else {
var oddWinner;
var evenWinner;
if(result == 0) {
if(p1Result == 1) {
console.log("p1 Odd Win");
oddWinner = player1.playerName;
player1.playerMarbles = parseInt(player1.playerMarbles) + parseInt(player1.playerNumber) + parseInt(player2.playerNumber);
}
if(p2Result == 1) {
console.log("p2 Odd Win");
oddWinner = player2.playerName;
player2.playerMarbles = parseInt(player2.playerMarbles) + parseInt(player2.playerNumber) + parseInt(player1.playerNumber);
}
$(titleText).text(". Odd ." + "\n Winner is " + oddWinner);
}
else if (result == 1) {
if(p1Result == 0) {
console.log("p1 Even Win");
evenWinner = player1.playerName;
player1.playerMarbles = parseInt(player1.playerMarbles) + parseInt(player1.playerNumber) + parseInt(player2.playerNumber);
}
if(p2Result == 0) {
console.log("p2 Even Win");
evenWinner = player2.playerName;
player2.playerMarbles = parseInt(player2.playerMarbles) + parseInt(player2.playerNumber) + parseInt(player1.playerNumber);
}
$(titleText).text(": Even :" + "\n Winner is " + evenWinner);
}
}
CheckWinner();
}
function CheckWinner() {
if(player1.playerMarbles == 20) {
$(titleText).text("GAME END \n Winner is " + player1.playerName);
$(".rollBtn").css("display", "none");
}
if(player2.playerMarbles == 20) {
$(titleText).text("GAME END \n Winner is " + player2.playerName);
$(".rollBtn").css("display", "none");
}
$(".p1Marbles").text(player1.playerMarbles);
$(".p2Marbles").text(player2.playerMarbles);
$("#Player1").val("");
$("#Player2").val("");
}