-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
93 lines (81 loc) · 1.26 KB
/
Board.cpp
File metadata and controls
93 lines (81 loc) · 1.26 KB
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
#include <iostream>
#include "Board.h"
#include <time.h>
using namespace std;
Board::Board(int newSize)
{
size = newSize;
Create();
}
Board::~Board()
{
delete blocks;
}
void Board::Create()
{
blocks = new Block[size*size];
for (int i = 0; i < size*size; i++)
{
blocks[i].setValue(i);
}
}
void Board::setSize()
{
int newSize;
do
{
cout << "What size do you want?" << endl;
cin >> newSize;
} while (newSize < 3 || newSize > 10);
size = newSize;
}
void Board::Print()
{
for (int i = 0; i < (size*size); i++)
{
blocks[i].Print();
cout << "\t|\t";
if ((i+1) % size == 0)
cout << endl;
}
}
void Board::Scramble()
{
srand(time(NULL));
do
{
for (int i = 0; i < size * 1000; i++)
{
int from = rand() % (size*size - 1) + 1;
int to = rand() % (size*size - 1) + 1;
Swap(from, to);
}
} while (CheckIfSolved());
}
bool Board::CheckIfSolved()
{
for (int i = 0; i < size*size; i++)
{
//cout << i << " " << blocks[i].getValue() << endl << endl;
if (blocks[i].getValue() != i)
return false;
}
return true;
}
void Board::Swap(int from, int to)
{
Block temp = blocks[to];
blocks[to] = blocks[from];
blocks[from] = temp;
}
int Board::getSize()
{
return size;
}
void Board::Reset()
{
delete[] blocks;
setSize();
Create();
Scramble();
}