-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlide.cs
407 lines (405 loc) · 10 KB
/
Slide.cs
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace Slide15
{
public class Data : System.IComparable
{
public int data{get;set;}
public string name{get;set;}
public int CompareTo(object obj)
{
return this.data.CompareTo(((Data)obj).data);
}
}
public class Slide15
{
public static int[] map;
public static int x,y,w,h;
public static bool isExit;
public static bool isClear;
public static string PlayerName="";
public static Data[][] Data;
public static void Menu()
{
int selected=3;
isExit=false;
isClear=false;
while(true)
{
Console.SetCursorPosition(0,0);
switch(selected)
{
case 3:
Console.ForegroundColor=ConsoleColor.Black;
Console.BackgroundColor=ConsoleColor.White;
Console.Write("Level 1\n");
Console.ResetColor();
Console.Write("Level 2\n");
Console.Write("Level 3\n");
Console.Write("Quit\n");
break;
case 4:
Console.Write("Level 1\n");
Console.ForegroundColor=ConsoleColor.Black;
Console.BackgroundColor=ConsoleColor.White;
Console.Write("Level 2\n");
Console.ResetColor();
Console.Write("Level 3\n");
Console.Write("Quit\n");
break;
case 5:
Console.Write("Level 1\n");
Console.Write("Level 2\n");
Console.ForegroundColor=ConsoleColor.Black;
Console.BackgroundColor=ConsoleColor.White;
Console.Write("Level 3\n");
Console.ResetColor();
Console.Write("Quit\n");
break;
case 6:
Console.Write("Level 1\n");
Console.Write("Level 2\n");
Console.Write("Level 3\n");
Console.ForegroundColor=ConsoleColor.Black;
Console.BackgroundColor=ConsoleColor.White;
Console.Write("Quit\n");
Console.ResetColor();
break;
}
ConsoleKeyInfo c = Console.ReadKey(true);
switch(c.Key)
{
case ConsoleKey.DownArrow:
if(selected!=6)
selected++;
break;
case ConsoleKey.UpArrow:
if(selected!=3)
selected--;
break;
case ConsoleKey.Enter:
if(selected==6)
{
Console.CursorVisible=true;
Environment.Exit(0);
}
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
init(selected,selected);
stopwatch.Start();
while(!isExit)
{
ConsoleKeyInfo kb = Console.ReadKey(true);
switch(kb.Key)
{
case ConsoleKey.UpArrow:
swap(3);
Check();
break;
case ConsoleKey.DownArrow:
swap(4);
Check();
break;
case ConsoleKey.LeftArrow:
swap(1);
Check();
break;
case ConsoleKey.RightArrow:
swap(2);
Check();
break;
case ConsoleKey.Escape:
isExit=true;
break;
}
}
stopwatch.Stop();
Console.Clear();
if(isClear)
{
Console.SetCursorPosition(Console.WindowWidth/2 -10,Console.WindowHeight/2);
Console.Write("Clear!!");
Console.ReadKey();
Console.Clear();
{
int tmplength = Data[selected-3].Length;
Array.Resize(ref Data[selected-3],Data[selected-3].Length+1);
Data[selected-3][tmplength]=new Data();
Data[selected-3][tmplength].data=stopwatch.Elapsed.Hours*3600+stopwatch.Elapsed.Minutes*60+stopwatch.Elapsed.Seconds;
Data[selected-3][tmplength].name=PlayerName;
Array.Sort(Data[selected-3]);
Console.SetCursorPosition(Console.WindowWidth/2-5,Console.WindowHeight/2-7);
Console.Write("現在のタイム: {0}秒",stopwatch.Elapsed.Hours*3600+stopwatch.Elapsed.Minutes*60+stopwatch.Elapsed.Seconds);
}
}
for(int i=0;i<Data[selected-3].Length-1 || i<5;i++)
{
if(0<=i && i<= Data[selected-3].Length-1)
{
Console.SetCursorPosition(Console.WindowWidth/2-5,Console.WindowHeight/2-5+2*i);
Console.Write("{0}位: {1}秒 ({2})",i+1,Data[selected-3][i].data,Data[selected-3][i].name);
using (var streamWriter = new StreamWriter("Data", false, Encoding.UTF8))
{
var xmlSerializer1 = new XmlSerializer(typeof(Data[][]));
xmlSerializer1.Serialize(streamWriter, Data);
}
}
}
stopwatch.Reset();
break;
}
}
}
public static void Main()
{
string title = Console.Title;
Console.Title = "";
Console.Clear();
try{
var xmlSerializer2 = new XmlSerializer(typeof(Data[][]));
var xmlSettings = new System.Xml.XmlReaderSettings()
{
CheckCharacters = false, // (2)
};
using (var streamReader = new StreamReader("Data", Encoding.UTF8))
using (var xmlReader
= System.Xml.XmlReader.Create(streamReader, xmlSettings))
{
Data = (Data[][])xmlSerializer2.Deserialize(xmlReader); // (3)
}
}catch{
Data=new Data[3][]{
new Data[0],
new Data[0],
new Data[0]
};
}
Console.WriteLine("ランキングに使うので、名前を入力してください。");
PlayerName=Console.ReadLine();
Console.Clear();
Console.CursorVisible = false;
Menu();
Console.CursorVisible=true;
}
public static void init(int sw,int sh)
{
x=sw-1;
y=sh-1;
w=sw;
h=sh;
Console.Clear();
int seed = Environment.TickCount;
map=new int[w*h];
for(int i=0;i<(w*h-1);i++)
{
map[i]=i+1;
}
map[w*h-1]=0;
Console.SetCursorPosition(0,h+2);
Console.Write("空白を矢印キーで動かして、\n");
Console.SetCursorPosition(0,h+3);
Draw();
Console.SetCursorPosition(0,h*2+3);
Console.Write("の形にしてください。\n\nMenuに戻る時はESCを押してください。");
isExit=true;
while(isExit)
{
for(int i=0;i<1000;i++)
{
Random r = new Random(seed ++);
int a = r.Next(5);
swap(a);
}
Console.SetCursorPosition(0,0);
Draw();
Check();
}
}
public static void Check()
{
for(int i=0;i<(w*h-1);i++)
{
if(map[i]!=i+1)
{
isExit=false;
isClear=false;
return;
}
}
isExit=true;
isClear=true;
return;
}
public static void Draw()
{
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
{
switch(map[w*i+j])
{
case 1:
Console.BackgroundColor=ConsoleColor.Cyan;
Console.Write("1");
break;
case 2:
Console.BackgroundColor=ConsoleColor.Blue;
Console.Write("2");
break;
case 3:
Console.BackgroundColor=ConsoleColor.Magenta;
Console.Write("3");
break;
case 4:
Console.BackgroundColor=ConsoleColor.Gray;
Console.ForegroundColor=ConsoleColor.Black;
Console.Write("4");
break;
case 5:
Console.BackgroundColor=ConsoleColor.DarkYellow;
Console.Write("5");
break;
case 6:
Console.BackgroundColor=ConsoleColor.Blue;
Console.Write("6");
break;
case 7:
Console.BackgroundColor=ConsoleColor.Red;
Console.Write("7");
break;
case 8:
Console.BackgroundColor=ConsoleColor.DarkYellow;
Console.Write("8");
break;
case 9:
Console.BackgroundColor=ConsoleColor.Gray;
Console.ForegroundColor=ConsoleColor.Black;
Console.Write("9");
break;
case 10:
Console.BackgroundColor=ConsoleColor.Magenta;
Console.Write("10");
break;
case 11:
Console.BackgroundColor=ConsoleColor.Cyan;
Console.Write("11");
break;
case 12:
Console.BackgroundColor=ConsoleColor.DarkRed;
Console.Write("12");
break;
case 13:
Console.BackgroundColor=ConsoleColor.DarkBlue;
Console.Write("13");
break;
case 14:
Console.BackgroundColor=ConsoleColor.DarkYellow;
Console.Write("14");
break;
case 15:
Console.BackgroundColor=ConsoleColor.DarkGray;
Console.Write("15");
break;
case 16:
Console.BackgroundColor=ConsoleColor.Blue;
Console.Write("16");
break;
case 17:
Console.BackgroundColor=ConsoleColor.DarkRed;
Console.Write("17");
break;
case 18:
Console.BackgroundColor=ConsoleColor.DarkYellow;
Console.Write("18");
break;
case 19:
Console.BackgroundColor=ConsoleColor.DarkMagenta;
Console.Write("19");
break;
case 20:
Console.BackgroundColor=ConsoleColor.Red;
Console.Write("20");
break;
case 21:
Console.BackgroundColor=ConsoleColor.Yellow;
Console.ForegroundColor=ConsoleColor.Black;
Console.Write("21");
break;
case 22:
Console.BackgroundColor=ConsoleColor.DarkGray;
Console.Write("22");
break;
case 23:
Console.BackgroundColor=ConsoleColor.DarkMagenta;
Console.Write("23");
break;
case 24:
Console.BackgroundColor=ConsoleColor.DarkBlue;
Console.Write("24");
break;
case 25:
Console.BackgroundColor=ConsoleColor.DarkRed;
Console.Write("25");
break;
case 0:
Console.BackgroundColor=ConsoleColor.Black;
Console.Write(" ");
Console.ResetColor();
break;
}
Console.ForegroundColor=ConsoleColor.White;
}
Console.Write("\n");
}
Console.ResetColor();
}
public static void swap(int houkou)
{
switch(houkou)
{
case 1://<-
if(x<(w-1))
{
map[w*y+x]=map[w*y+x+1];
map[w*y+x+1]=0;
x++;
Console.SetCursorPosition(0,0);
Draw();
}
break;
case 2://->
if(x>0)
{
map[w*y+x]=map[w*y+x-1];
map[w*y+x-1]=0;
x--;
Console.SetCursorPosition(0,0);
Draw();
}
break;
case 3://↑
if(y<(h-1))
{
map[w*y+x]=map[w*y+x+w];
map[w*y+x+w]=0;
y++;
Console.SetCursorPosition(0,0);
Draw();
}
break;
case 4://↓
if(y>0)
{
map[w*y+x]=map[w*y+x-w];
map[w*y+x-w]=0;
y--;
Console.SetCursorPosition(0,0);
Draw();
}
break;
}
}
}
}