-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8puzzle.cs
73 lines (64 loc) · 1.73 KB
/
8puzzle.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
}
class Board
{
//Array of tiles
Tile[] tileBoard;
//Are tiles in correct positions?
public Boolean isCorrect()
{
for (int i = 0; i < 9; i++)
{
if (i != tileBoard[i].getNumber())
return false;
}
return true;
}
//Where is tile on the board?
public int getLocation(int tileNumber)
{
for (int i = 0; i <= 9; i++)
{
if (tileBoard[i].getNumber() == tileNumber)
return i;
}
return -1;
}
//What tiles are you next to?
//ugly, but it works
public int[] getNeighbors(int tileNumber)
{
int location = getLocation(tileNumber);
int[] neighbors = new int[4];
int counter = 0;
if (location + 3 < 9)
neighbors[counter++] = location + 3;
if (location - 3 >= 0)
neighbors[counter++] = location - 3;
if ((location + 1) / 3 == (location) / 3)
neighbors[counter++] = location + 1;
if ((location - 1) / 3 == (location) / 3)
neighbors[counter++] = location - 1;
return neighbors;
}
}
//Hold tile information, can add more functionality to it later (images in addition to numbers)
class Tile
{
int number;
public int getNumber()
{
return number;
}
}
}