-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle.js
224 lines (219 loc) · 8.18 KB
/
Puzzle.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//Ehsan Kiani Far
const GameState = Object.freeze({
START: Symbol("start"),
SELECT: Symbol("select"),
BEGIN: Symbol("begin"),
PLAYING: Symbol("playing"),
END: Symbol("end")
});
const NOT_USED_NUMBER=255;
export default class Puzzle{
constructor(){
//The core matrix that game made from it
this.aCore = [ [1,2,3,4,5,6,7,8,9],
[2,3,4,5,6,7,8,9,1],
[3,4,5,6,7,8,9,1,2],
[4,5,6,7,8,9,1,2,3],
[5,6,7,8,9,1,2,3,4],
[6,7,8,9,1,2,3,4,5],
[7,8,9,1,2,3,4,5,6],
[8,9,1,2,3,4,5,6,7],
[9,1,2,3,4,5,6,7,8]];
//the current game state
this.stateCur = GameState.START;
//default missing numbers list
this.aMissing = [1,3,5,7,9,2,4,6,8];
}
//generate a text that can be viewed by user
generateOutput(){
let sResult = "";
for(let i=0; i<this.aCore.length; i++){
for(let j=0; j<this.aCore[i].length; j++){
if(this.aCore[i][j]==this.aMissing[i]){
sResult+="?  ";
}
else{
sResult+=this.aCore[i][j]+"  ";
}
//best practice is like that but only if give me marks
//sResult+=aCore[i][j]==aMissing[i]? "? ": sResult+=aCore[i][j]+" ";
}
if(this.aMissing[i]<10 && this.aMissing[i]>0){
sResult+="«";
}
//go to next line
sResult+="\n";
}
return sResult;
}
//check user input
checkInput(nUserInput){
if(nUserInput<10 && nUserInput>0){
for(let i=0; i<this.aMissing.length; i++){
if (this.aMissing[i]==nUserInput){
//correct
if(i==this.aMissing.length-1){
//win
this.aMissing[i]=NOT_USED_NUMBER;
return "win";
}
else{
//continue
this.aMissing[i]=NOT_USED_NUMBER;
return "correct";
}
}
else if(this.aMissing[i]!=NOT_USED_NUMBER){
//lose
this.aMissing[i]=NOT_USED_NUMBER;
return "lose";
}
}
}
else{
//wrong input
return "wrong";
}
}
//this method shuffle tha matrix based on 2 random number in X and Y axis
shuffleMatrix(){
//Deep cloning main matrix
let aTempCore=JSON.parse(JSON.stringify(this.aCore));
let nRandomX=Math.floor(Math.random()*9);
let nRandomY=Math.floor(Math.random()*9);
for(let i=0;i<aTempCore.length; i++){
for(let j=0; j<aTempCore[i].length; j++){
this.aCore[nRandomX][nRandomY]=aTempCore[i][j];
nRandomY++;
if(nRandomY==aTempCore[i].length){
nRandomY=0;
}
}
nRandomX++;
if(nRandomX==aTempCore.length){
nRandomX=0;
}
}
}
//this method randomize the missing numbers;
randomize(){
for(let i=0 ; i<this.aMissing.length ; i++){
this.aMissing[i]=Math.ceil(Math.random()*9);
}
}
takeTurn(sInput){
sInput=sInput.toLowerCase();
//very beginning of the game
if(this.stateCur==GameState.START){
this.stateCur=GameState.SELECT;
return "Please select game level\n[Easy] [Medium] [Hard] [Default]";
}
//select level
else if(this.stateCur==GameState.SELECT){
switch(sInput){
case "easy":
this.stateCur=GameState.BEGIN;
break;
case "medium":
this.stateCur=GameState.BEGIN;
this.shuffleMatrix();
break;
case "hard":
this.stateCur=GameState.BEGIN;
this.shuffleMatrix();
this.randomize();
break;
case "default":
this.stateCur=GameState.BEGIN;
break;
default:
return "You should select between easy, medium, hard, and default";
break;
}
return "Please send [start] to begin the game or send [help] to get help";
}
//before playing the game
else if(this.stateCur==GameState.BEGIN){
switch(sInput){
case "start":
this.stateCur=GameState.PLAYING;
return (this.generateOutput()+"Insert a number between 1 and 9 to replace first ?. It should be unique in row and column");
break;
case "help":
return "You should insert a number to fill the ? and it should be unique in the row and column";
break;
default :
return "Wrong input";
break;
}
}
//during play
else if(this.stateCur==GameState.PLAYING){
if(sInput=="help"){
return "Just check what number should be instead of the first question mark!"
}
else if(sInput=="answer"){
for(let i=0; i<this.aMissing.length; i++){
if(this.aMissing[i]!= NOT_USED_NUMBER){
return "Your answer is: " +this.aMissing[i];
}
}
}
else if(sInput=="exit"){
this.stateCur=GameState.END;
return "Thank you for playing. if you want to start again insert [again]"
}
switch (this.checkInput(sInput)){
case "win":
//user win
this.stateCur=GameState.END;
return (this.generateOutput()+"You finished the game successfully.\n to repeat the game insert [again]");
break;
case "lose":
//user lose
this.stateCur=GameState.END;
return (this.generateOutput()+"Sorry! You lost the game.\n to repeat the game insert [again]");
break;
case "correct":
//keep going
return (this.generateOutput()+"Correct! Keep going.");
break;
case "wrong":
//wrong user input
return "Please insert a number between 1 and 9";
break;
}
}
//End of the game
else if(this.stateCur==GameState.END){
//Reinitialize tha matrix
this.aCore = [ [1,2,3,4,5,6,7,8,9],
[2,3,4,5,6,7,8,9,1],
[3,4,5,6,7,8,9,1,2],
[4,5,6,7,8,9,1,2,3],
[5,6,7,8,9,1,2,3,4],
[6,7,8,9,1,2,3,4,5],
[7,8,9,1,2,3,4,5,6],
[8,9,1,2,3,4,5,6,7],
[9,1,2,3,4,5,6,7,8]];
//reinitialize the missing numbers
this.aMissing = [1,3,5,7,9,2,4,6,8];
switch(sInput){
case "again":
//play again
this.stateCur=GameState.START;
return "Now you are at the beginning of the game. insert anything to start.";
break;
case "help":
return "You finished a game! now you have the option to play again or stay here forever";
break;
case "about":
return "My name is Ehsan Kiani Far and this is may school project";
break;
default:
return "Wrong input";
break;
}
}
}
}