-
Notifications
You must be signed in to change notification settings - Fork 0
/
8QueensProblem code
150 lines (126 loc) · 4.09 KB
/
8QueensProblem code
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aiproject2;
import static java.lang.System.arraycopy;
/**
*
* @author Admin
*/
public class AIProject2 {
/**
* @param args the command line arguments
*/
public static int runs = 100;
public static void main(String[] args) {
int[] queens = new int[8];
for(int i = 0; i < 8; i++){
queens[i] = (int)(Math.random() * 8);
}
System.out.println();
System.out.println("Numerical representation of board:");
display(queens);
System.out.println("Visual representation of board:");
displayBoard(queens);
System.out.println("Possible attacks: " + numOfAttacks(queens));
System.out.println();
System.out.println();
int[] result = HillClimbing(queens);
while(numOfAttacks(result) > 0 && runs > 0){
result = HillClimbing(result);
runs--;
}
System.out.println("Numerical representation of result:");
display(result);
System.out.println("Visual representation of result:");
displayBoard(result);
System.out.println("Possible attacks: " + numOfAttacks(result));
System.out.println();
}
public static int[] HillClimbing(int[] queens){
int[] current = new int[8];
int[] child = new int[8];
current = queens;
int[] next = {0,0,0,0,0,0,0,0};
while(1 == 1){
int min = 28;
for(int i = 0; i < 64; i++){
child = createChild(current,i);
int attacks = numOfAttacks(child);
if(attacks < min){
min = attacks;
arraycopy(child,0,next,0,8);
}
}
if(min >= numOfAttacks(current)){
return current;
}
arraycopy(next,0,current,0,8);
}
}
public static int numOfAttacks(int[] arr){
int row = 0;
int major = 0;
int minor = 0;
int[] diagonal = {0,1,2,3,4,5,6,7};
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
if(arr[i] == arr[j] && i != j){
row++;
}
}
}
int[] tempMajor = new int[8];
int[] tempMinor = new int[8];
arraycopy(arr,0,tempMajor,0,8);
arraycopy(arr,0,tempMinor,0,8);
for(int i = 0; i < 8; i++){
tempMajor[i] -= diagonal[i];
tempMinor[i] += diagonal[i];
}
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
if(tempMajor[i] == tempMajor[j] && i != j){
major++;
}
if(tempMinor[i] == tempMinor[j] && i != j){
minor++;
}
}
}
return row + major + minor;
}
public static int[] createChild(int[] arr, int instance){
int[] child = new int[8];
child = arr;
int col = instance / 8;
if(child[col] == 7){
child[col] = 0;
}
else{
child[col] = ++child[col];
}
return child;
}
public static void display(int[] arr){
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void displayBoard(int[] arr){
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
if(arr[j] == i){
System.out.printf("%3s", "Q ");
}
else{
System.out.printf("%3s", "[]");
}
}
System.out.println();
}
}
}