-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
102 lines (95 loc) · 1.88 KB
/
Copy pathboard.cpp
File metadata and controls
102 lines (95 loc) · 1.88 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
94
95
96
97
98
99
100
101
102
/*
* board.cpp
*
* Created on: Apr 25, 2021
* Author: albus
*/
#include "board.hpp"
#include <stdlib.h>
board::board(unsigned char queens[8]) {
for (unsigned int i = 0; i < 8; i++) {
this->queens[i] = queens[i];
this->score = 0;
}
}
board::board() {
for (unsigned int i = 0; i < 8; i++) {
int val = 0;
val = rand() % 8;
this->queens[i] = val;
}
this->score = 0;
}
board::~board() {
}
void board::calc_score() {
//for each queen in the board
this->score = 0;
for (unsigned int i = 0; i < 8; i++) {
int x = i, y = this->queens[i];
//get all intersecting cells in the forward diagonal
while (x < 8 && x >= 0 && y < 8 && y >= 0) {
if (y == int(this->queens[x]))
this->score--;
else
this->score++;
x++;
y--;
}
x = i, y = this->queens[i];
while (x < 8 && x >= 0 && y < 8 && y >= 0) {
if (y == int(this->queens[x]))
this->score--;
else
this->score++;
x--;
y++;
}
x = i, y = this->queens[i];
//get all intersecting cells in the backward diagonal
while (x < 8 && x >= 0 && y < 8 && y >= 0) {
if (y == int(this->queens[x]))
this->score--;
else
this->score++;
x++;
y++;
}
x = i, y = this->queens[i];
while (x < 8 && x >= 0 && y < 8 && y >= 0) {
if (y == int(this->queens[x]))
this->score--;
else
this->score++;
x--;
y--;
}
}
}
int board::get_score() {
return this->score;
}
void board::mutate() {
int pos_a = 0, pos_b = 0, holder = 0;
pos_a = rand() % 8;
pos_b = rand() % 8;
holder = this->queens[pos_a];
this->queens[pos_a] = this->queens[pos_b];
this->queens[pos_b] = holder;
this->score = 0;
}
bool board::validate() {
for (int i = 0; i < 8; i++) {
for (int j = i - 1; j >= 0; j--) {
if (this->queens[i] == this->queens[j])
return false;
}
}
return true;
}
void board::set_score(int score) {
this->score = score;
}
uint8_t* board::get_queens() {
return this->queens;
}