-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.cpp
87 lines (79 loc) · 1.29 KB
/
State.cpp
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
#include "State.h"
#include <string.h>
#include <stdlib.h>
#include <iostream>
State::State(char init_state[10], int blank_space)
{
strcpy(puzzle, init_state);
blank = blank_space;
}
void State::copy(State new_state)
{
strcpy(puzzle, new_state.puzzle);
blank = new_state.blank;
}
bool State::goal_test()
{
for (int i = 0; i < 9; i++)
{
if (puzzle[i] != '0' + i)
return 0;
}
return 1;
}
void State::move_right()
{
if (blank % 3 > 1 or blank < 0 or blank > 7)
{
return;
}
char aux = puzzle[blank];
puzzle[blank] = puzzle[blank + 1];
puzzle[blank + 1] = aux;
blank += 1;
}
void State::move_left()
{
if (blank % 3 < 1 or blank > 8)
{
return;
}
char aux = puzzle[blank];
puzzle[blank] = puzzle[blank - 1];
puzzle[blank - 1] = aux;
blank -= 1;
}
void State::move_up()
{
if (blank < 3 or blank > 8)
{
return;
}
char aux = puzzle[blank];
puzzle[blank] = puzzle[blank - 3];
puzzle[blank - 3] = aux;
blank -= 3;
}
void State::move_down()
{
if (blank < 0 or blank > 5)
{
return;
}
char aux = puzzle[blank];
puzzle[blank] = puzzle[blank + 3];
puzzle[blank + 3] = aux;
blank += 3;
}
void State::print_state()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
std::cout << puzzle[3 * i + j] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}