This repository has been archived by the owner on May 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QState.cpp
154 lines (124 loc) · 3.77 KB
/
QState.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
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
//--------------------------------------------------------------------------
// File: QState.cpp
//
// Created by: Joel S. McCance
// Creation date: Fri Feb 25 14:52:19 2011
// Last modified: Mon Feb 28 19:04:42 2011
//--------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "QState.h"
//--------------------------------------------------------------------------
// INITIALIZATION
//--------------------------------------------------------------------------
bool QState::_initialized = false;
//--------------------------------------------------------------------------
// CONSTRUCTORS
//--------------------------------------------------------------------------
QState::QState( )
{
_size = 0;
_score = 0;
}
//--------------------------------------------------------------------------
QState::QState(int size)
{
if (not _initialized)
{
// We use /dev/urandom in order to allow for running the program
// many, many times in a very short span without horrific
// collision issues.
std::ifstream urandom;
long seed;
urandom.open("/dev/urandom", std::ios::in | std::ios::binary);
urandom.read(reinterpret_cast<char*>(&seed), sizeof(seed));
srand48(seed);
_initialized = true;
urandom.close( );
}
_size = size;
for(unsigned int i = 0; i < _size; ++i)
{
_state.push_back(lrand48( ) % _size);
}
_score = countAttacks( );
}
//--------------------------------------------------------------------------
QState::QState(QState& parent, unsigned int row, unsigned int col)
{
_size = parent._size;
_state = parent._state;
_state[col] = row;
_score = countAttacks( );
}
//--------------------------------------------------------------------------
// PUBLIC METHODS
//--------------------------------------------------------------------------
int QState::getScore( )
{
return _score;
}
//--------------------------------------------------------------------------
void QState::generateSuccessors(std::vector<QState>& successors)
{
for (unsigned int col = 0; col < _size; ++col)
{
for (unsigned int row = 0; row < _size; ++row)
{
// Only generate successor if it is not identical to the
// parent.
if (_state[col] != row)
{
QState state(*this, row, col);
successors.push_back(state);
}
}
}
}
//--------------------------------------------------------------------------
void QState::printBoard( )
{
for (unsigned int row = 0; row < _size; ++row)
{
for (unsigned int col = 0; col < _size; ++col)
{
char ch = '-';
if (_state[col] == row)
{
ch = 'Q';
}
std::cout << ch << " ";
}
std::cout << std::endl;
}
}
//--------------------------------------------------------------------------
// PRIVATE METHODS
//--------------------------------------------------------------------------
int QState::countAttacks( )
{
int attackCount = 0;
for (unsigned int i = 0; i < _size - 1; ++i)
{
for (unsigned int j = i + 1; j < _size; ++j)
{
// Are the queens in the same row?
if (_state[i] == _state[j])
{
++attackCount;
}
else
{
// Are the queens on a shared diagonal?
int row_diff = i - j;
int col_diff = _state[i] - _state[j];
if (row_diff*row_diff == col_diff*col_diff)
{
++attackCount;
}
}
}
}
return attackCount;
}