-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
223 lines (178 loc) · 5.83 KB
/
app.ts
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
/// <reference path="jquery.d.ts" />
enum CardStatus {
none,
question,
view,
}
class ScreenDraw {
private _numberClass : String = "number-card";
private _count : number = 0;
public ctrlStartBtn(view: boolean) {
if (view) {
$(".start").removeAttr("disabled");
} else {
$(".start").attr("disabled", "disabled");
this._count = 0;
}
}
public ctrlCard(status: CardStatus, digit: number, numStr?: String) {
if (!numStr) {
numStr = "";
for (var i = 0; i < digit; i++) {
switch (status) {
case CardStatus.none:
numStr += "X";
break;
case CardStatus.question:
numStr += "?";
break;
}
}
}
var $number = $('.' + this._numberClass);
if ($number.length != 0) {
$number.each(function (index: number, elem: Element) {
$(elem).text(numStr.charAt(index));
});
} else {
for (var i=0; i < numStr.length; i++) {
var text = numStr.charAt(i);
var klass = this._numberClass;
var $num = $('<div class="' + klass + '">' + text + "</div>");
$("#card").append($num);
}
}
}
public viewResult(inputText: string, hit: number, blow: number) {
this._count++;
var resultView = $(".hidden").children(".resultView").clone(true);
resultView.find(".count").text(this._count);
resultView.find(".input-text").text(inputText);
resultView.find(".hit").text(hit);
resultView.find(".blow").text(blow);
$("#result").append(resultView);
}
}
class HitBlow {
private _digit: number = 0;
private _answer: String = "";
constructor(digit: number = 3) {
this._digit = digit;
this._digit = (digit > 10) ? 10 : this._digit;
}
get answer() : String {
return this._answer;
}
get digit() : number {
return this._digit;
}
public start() {
var val: String = "";
var numAttr: number[] = [];
for (var i = 0; i < 10; i++) {
numAttr.push(i)
}
for (var i = 0; i< this._digit; i++) {
var num = Math.floor(Math.random() * numAttr.length);
var rand = numAttr.splice(num, 1);
val += rand.toString();
}
this._answer = val;
}
public digitCheck(inputValue : String) : boolean {
if (inputValue.length != this._digit) {
return false; // 桁数が他りない
}
return true;
}
public inputCheck(inputValue : String) : number[] {
var ret: number[] = [0,0];
var hitCheck = new HitCheck();
ret[0] = hitCheck.check(this._answer, inputValue);
var blowCheck = new BlowCheck();
ret[1] = blowCheck.check(this._answer, inputValue);
return ret;
}
}
interface ICheck {
check(...result: String[]): number
}
class HitCheck implements ICheck {
public check(...result: String[]) : number {
var ret: number = 0;
if (result.length < 2) {
return 0;
}
var answer : String = result[0];
var input : String = result[1];
for (var i = 0; i < answer.length; i++) {
if (answer.charAt(i) == input.charAt(i)) {
ret++;
}
}
return ret;
}
}
class BlowCheck implements ICheck {
public check(...result: String[]) : number {
var ret: number = 0;
if (result.length < 2) {
return 0;
}
var answer : String = result[0];
var input : String = result[1];
for (var i = 0; i < result[0].length; i++) {
if (input[1].indexOf(answer.charAt(i)) != -1) {
if (answer.charAt(i) != input.charAt(i)) {
ret++;
}
}
}
return ret;
}
}
$(function () {
var digit = 3;
var screenDraw = new ScreenDraw();
var hitBlow = new HitBlow(digit);
$("#textarea").hide();
$(".retire").hide();
digit = hitBlow.digit;
screenDraw.ctrlCard(CardStatus.none, digit);
$(".start").click(function () {
screenDraw.ctrlStartBtn(false);
hitBlow.start();
screenDraw.ctrlCard(CardStatus.question, digit);
$("#result").empty();
$("#textarea").show();
$(".retire").show();
$("#inputValue").val("");
console.log(hitBlow.answer);
});
$(".input").click(function () {
var inputText = $("#inputValue").val();
if (!hitBlow.digitCheck(inputText)) {
alert("入力桁数が違います");
} else {
var ret = hitBlow.inputCheck(inputText);
var hit = ret[0];
var blow = ret[1];
screenDraw.viewResult(inputText, hit, blow);
if (hit == digit) {
screenDraw.ctrlCard(CardStatus.view, digit, inputText);
alert("正解");
$("#textarea").hide();
$(".retire").hide();
screenDraw.ctrlStartBtn(true);
}
}
});
$(".retire").click(function () {
alert("正解はl「" + hitBlow.answer + "」です");
$("#textarea").hide();
$(".retire").hide();
digit = hitBlow.digit;
screenDraw.ctrlCard(CardStatus.none,digit);
screenDraw.ctrlStartBtn(true);
});
});