-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
106 lines (100 loc) · 2.92 KB
/
Game.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game15
{
class Game
{
const int size =4;
int[,] map = new int [size,size];
int space_x, space_y;
static Random rnd = new Random();
public int count = 0;
//public Game ()//(int size)
//{
//size = 4;
//if (size < 2) size = 2;
//if (size > 5) size = 5;
//this.size = size;
//map = new int[size, size];
//}
public void Start ()
{
for (int x = 0; x < size; x++)
for (int y = 0; y < size; y++)
map[x, y] = CoordsToPosition(x, y) + 1;
space_x = size - 1;
space_y = size - 1;
map[space_x, space_y] = 0;
}
public void Shift(int position)
{
int x, y;
PositionToCoords(position, out x, out y);
if (Math.Abs(space_x - x) + Math.Abs(space_y - y) != 1)
return;
map[space_x, space_y] = map[x, y];
map[x, y] = 0;
space_x = x;
space_y = y;
}
public void ShiftRandom()
{
int a = rnd.Next(0, 4);
int x = space_x;
int y = space_y;
switch(a)
{
case 0: x--; break;
case 1: x++; break;
case 2: y--; break;
case 3: y++; break;
}
Shift(CoordsToPosition(x, y));
}
public bool CheckNumbers()
{
if (!(space_x == size - 1 && space_y == size - 1))
return false;
for (int x = 0; x < size; x++)
for (int y = 0; y < size; y++)
if (!(x == size - 1 && y == size - 1))
if (map[x, y] != CoordsToPosition(x, y) + 1)
return false;
return true;
}
public int GetNumber (int position)
{
int x, y;
PositionToCoords(position, out x, out y);
if (x < 0 || x >= size) return 0;
if (y < 0 || y >= size) return 0;
return map[x, y];
}
private int CoordsToPosition (int x, int y)
{
if (x < 0) x = 0;
if (x > size - 1) x = size - 1;
if (y < 0) y = 0;
if (y > size - 1) y = size - 1;
return y * size + x;
}
private void PositionToCoords(int position, out int x, out int y)
{
if (position < 0) position = 0;
if (position > size*size - 1) position = size*size - 1;
x = position % size;
y = position / size;
}
public int CountSteps()
{
return count++;
}
public void RefreshCount()
{
count = 0;
}
}
}