-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
159 lines (154 loc) · 3.89 KB
/
board.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
155
156
157
158
159
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <random>
#include <fstream>
#include <string>
#include "board.h"
#include "util.h"
// This creates a chess board of rows by col
ChessBoard::ChessBoard (int row, int col, int white, int black) {
solved = false;
rows = row;
cols = col;
wQueens = white;
bQueens = black;
vector<Queen> whiteQueens;
vector<Queen> blackQueens;
vectBoard.resize (rows, std::vector<int> (cols, 0));
ChessBoard::setUp();
ChessBoard::fillBoard();
}
/* This function calculates and sets the fitness of the board state
* The fitness is better the fewer number of threats
*/
int ChessBoard::findFitness() {
fitness = 0;
//incriment fitness if any queen shares a square witrh another
for (int i = 0; i < bQueens; i++ ) {
for (int j = i+1; j < bQueens; j++) {
if ((blackQueens.at(i).row == blackQueens.at(j).row) && (blackQueens.at(j).col == blackQueens.at(j).col))
fitness++;
}
}
for (int i = 0; i < wQueens; i++ ) {
for (int j = i+1; j < wQueens; j++) {
if ((whiteQueens.at(i).row == whiteQueens.at(j).row) && (whiteQueens.at(j).col == whiteQueens.at(j).col))
fitness++;
}
}
//Incriment fitness if any queen is threatened by a queen on the other team
for (auto &i : whiteQueens) {
for (auto &j : blackQueens) {
if (i.row == j.row)
fitness++;
if (i.col == j.col)
fitness++;
if (abs(i.row - j.row) == abs(i.col - j.col))
fitness++;
}
}
if (fitness == 0)
solved = true;
return fitness;
}
void ChessBoard::fillBoard() {
//std::cout << "Filling Board with 0's\n";
for (int i = 0; i < rows; i++) {
for (int j =0; j < cols; j++) {
vectBoard[i][j] = 0;
}
}
//std::cout << "Setting W pos\n";
int i = 0;
for (auto &w : whiteQueens) {
//std::cout << "Queen "<< i << std::endl;
//std::cout << w.row << " " << w.col << endl;
i++;
vectBoard[w.row][w.col] = 1;
}
//std::cout << "Setting B pos\n";
i = 0;
for (auto &b : blackQueens) {
//std::cout << "Queen " << i << std::endl;
//std::cout << b.row << " " << b.col << endl;
i++;
vectBoard[b.row][b.col] = -1;
}
}
/* This function places the desired number of white and black queens in the board */
void ChessBoard::setUp () {
for (int i = 0; i < rows; i++) {
if (i < wQueens) {
whiteQueens.push_back(Queen());
whiteQueens.back().row = i;
whiteQueens.back().col = rand() % cols;
} else {
blackQueens.push_back(Queen());
blackQueens.back().row = i;
blackQueens.back().col = rand() % cols;
}
}
}
/* This method prints the chess board to the terminal */
void ChessBoard::display () {
//cls();
std::cout << "\n\t ";
for (int i = 0; i < cols; i++)
std::cout << i << " ";
std::cout << std::endl;
for (int i = 0; i < rows; i++) {
std::cout << "\t" << i << " |";
for (int j = 0; j < cols; j++) {
std::cout << " ";
if (vectBoard[i][j] == 1)
std::cout << "W";
if (vectBoard[i][j] == -1)
std::cout << "B";
if (vectBoard[i][j] == 0)
std::cout << " ";
std::cout << " |";
}
std::cout << endl;
}
std::cout << "\t ";
for (int i = 0; i < cols; i++)
std::cout << "--- ";
std::cout << endl;
}
void ChessBoard::printToFile (string file) {
using namespace std;
ofstream ofile;
ofile.open(file);
ofile << "\n\t ";
for (int i = 0; i < cols; i++)
ofile << i << " ";
ofile << std::endl;
for (int i = 0; i < rows; i++) {
ofile << "\t" << i << " |";
for (int j = 0; j < cols; j++) {
ofile << " ";
if (vectBoard[i][j] == 1)
ofile << "W";
if (vectBoard[i][j] == -1)
ofile << "B";
if (vectBoard[i][j] == 0)
ofile << " ";
ofile << " |";
}
ofile << endl;
}
ofile << "\t ";
for (int i = 0; i < cols; i++)
ofile << "--- ";
ofile << endl;
ofile << "\tSolved: ";
if (solved)
ofile << "Yes\n";
if (!solved)
ofile << "No\n";
}
/* This function prints out the position of a queen in the row col format */
void Queen::displayPosition () {
std::cout << " " << row << " " << col << std::endl;
}