-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_player.js
185 lines (176 loc) · 4.67 KB
/
single_player.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
// Support variables declaration
let sys=[];
let sysDig=[[],[],[],[]];
let sysInd=0;
let sysNum=0;
// Provides status of the game
let turn=1;
let now="left";
let userQuery=[];
// Picks a number to play the game
function preCompute(){
for (let i=1023; i<=9876; i++){
let temp=i;
let d=[];
let alright=true;
for (let j=0; j<4 && alright; j++){
let hold=temp%10;
temp-=hold;
temp/=10;
for (let k=0; k<d.length; k++){
if (d[k]==hold){
alright=false;
break;
}
}
d.push(hold);
}
if (alright){
sys.push(i);
for (let j=0; j<4; j++){
sysDig[j].push(d[j]);
}
}
}
sysInd=Math.floor(Math.random()*10000000);
sysInd%=sys.length;
sysNum=sys[sysInd];
}
function forfeit(){
let chk=document.querySelector("#userBox");
if (chk!==null) chk.remove();
failure();
}
function initiate(){
document.querySelector("div.ready").remove();
document.querySelector("body").innerHTML += `
<div class="contain" style="padding: 40px; 0px;"></div>
<div class="ready">
<input type="button" value="Forfeit" id="begin" onclick="forfeit()">
</div>
`;
preCompute();
document.querySelector("div.contain").innerHTML = `
<div class="sidebyside ${now}" id="userBox">
<section class="resp p ${now}">
<form onsubmit="return false;">
<input type="reset" id="userReset" style="position: fixed; left: -200px; display: none;">
<label for="userquery" id="labelQuery"><h2>Guess ${turn}:</h2></label><br>
<input type="number" id="userquery" placeholder="Guess wise"><br>
<input type="submit" id="submitButtonUser" value="Confirm" onclick="userResp()">
</form>
</section>
</div>
`;
document.querySelector("#userquery").focus();
}
function winner (){
document.querySelector("div.ready").innerHTML = `
<div id="toss">
<h1>Congrats!</h1>
<h2>You took ${turn} turns</h2>
<input type="button" class="inpBtn" value="Play Again!" onclick="reload()">
</div>
`;
document.querySelector("body").innerHTML += `
<footer>
© Designed & Powered by<br><span>Suphal Athlur</span>
</footer>
`;
document.querySelector("#toss .inpBtn").focus();
}
function failure (){
let chk=document.querySelector("#userBox");
if (chk!==null) chk.remove();
document.querySelector("div.ready").innerHTML = `
<div id="toss">
<h1>Oops!</h1>
<h2>You gave up in ${turn} turn(s)</h2>
<input type="button" class="inpBtn" value="Play Again!" onclick="reload()">
</div>
`;
document.querySelector("body").innerHTML += `
<footer>
© Designed & Powered by<br><span>Suphal Athlur</span>
</footer>
`;
document.querySelector("#toss .inpBtn").focus();
}
function reload(){
document.querySelector("#toss .inpBtn").focus();
}
function userResp (){
// Read query from user
let t=document.querySelector("#userquery").value;
if (t===""){
alert("Fill all required fields.");
return;
}
let userNum=(Number)(t);
let notokay=false;
let temp=userNum;
let userDig=[];
if (userNum<1000 || userNum>10000) notokay=true;
else{
for (let j=0; j<4 && !notokay; j++){
let hold=temp%10;
temp-=hold;
temp/=10;
for (let k=0; k<userDig.length; k++){
if (userDig[k]==hold){
notokay=true;
break;
}
}
userDig.push(hold);
}
}
if (notokay){
alert ("Invalid Number. Enter 4 digit number with distinct digits and without leading zeroes.");
document.querySelector("#userReset").click();
return;
}
userQuery.push(userNum);
// Generate the query result
let retCows=0;
let retBulls=0;
for (let i=0; i<4; i++){
for (let j=0; j<4; j++){
if (sysDig[j][sysInd]==userDig[i]){
if (i==j) retBulls++;
else retCows++;
}
}
}
// Display the query result
document.querySelector("#userBox").remove();
document.querySelector("body div.contain").innerHTML += `
<div class="sidebyside ${now}">
<section class="resp p ${now}"><a>
<h2><span>Guess ${turn}:</span> ${userNum}</h2><br>
<div class="bullrep">Bulls: ${retBulls}</div>
<div class="cowrep">Cows: ${retCows}</div>
</a></section>
</div>
`;
if (now==="right"){
document.querySelector("body div.contain").innerHTML += "<br>";
now="left";
}
else now="right";
if (retBulls==4) winner();
turn++;
document.querySelector("body div.contain").innerHTML += `
<div class="sidebyside ${now}" id="userBox">
<section class="resp p ${now}">
<form onsubmit="return false;">
<input type="reset" id="userReset" style="position: fixed; left: -200px; display: none;">
<label for="userquery" id="labelQuery"><h2>Guess ${turn}:</h2></label><br>
<input type="number" id="userquery" required><br>
<input type="submit" id="submitButtonUser" value="Confirm" onclick="userResp()">
</form>
</section>
</div>
`;
document.querySelector("#userquery").focus();
}