forked from GeeksforGeeks-VIT-Bhopal/GeekWeek-Local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
146 lines (122 loc) · 2.87 KB
/
sketch.js
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
var test;
var moving = false;
var tileSize = 100;
var movingPiece;
var whitesMove = true;
var moveCounter = 10;
var images = [];
var whiteAI = false;
var blackAI = true;
var depthPara;
var depthPlus;
var depthMinus;
var tempMaxDepth = 3;
function setup() {
createCanvas(800, 800);
htmlStuff();
for (var i = 1; i < 10; i++) {
images.push(loadImage("assets/2000px-Chess_Pieces_Sprite_0" + i + ".png"));
}
for (var i = 10; i < 13; i++) {
images.push(loadImage("assets/2000px-Chess_Pieces_Sprite_" + i + ".png"));
}
test = new Board();
}
function draw() {
background(100);
showGrid();
test.show();
runAIs();
}
function runAIs() {
maxDepth = tempMaxDepth;
if (!test.isDead() && !test.hasWon()) {
if (blackAI) {
if (!whitesMove) {
if (moveCounter < 0) {
test = maxFunAB(test, -400, 400, 0);
// test = maxFun(test, 0);
print(test);
whitesMove = true;
moveCounter = 10;
} else {
moveCounter--;
}
}
}
if (whiteAI) {
if (whitesMove) {
if (moveCounter < 0) {
test = minFunAB(test, -400, 400, 0);
// test = minFun(test, 0);
print("test", test);
whitesMove = false;
moveCounter = 10;
} else {
moveCounter--;
}
}
}
}
}
function showGrid() {
for (var i = 0; i < 8; i++) {
for (var j = 0; j < 8; j++) {
if ((i + j) % 2 == 1) {
fill(0);
} else {
fill(240);
}
noStroke();
rect(i * tileSize, j * tileSize, tileSize, tileSize);
}
}
}
function keyPressed() {
}
function mousePressed() {
var x = floor(mouseX / tileSize);
var y = floor(mouseY / tileSize);
if (!test.isDone()) {
if (!moving) {
movingPiece = test.getPieceAt(x, y);
if (movingPiece != null && movingPiece.white == whitesMove) {
movingPiece.movingThisPiece = true;
} else {
return;
}
} else {
if (movingPiece.canMove(x, y, test)) {
movingPiece.move(x, y, test);
movingPiece.movingThisPiece = false;
whitesMove = !whitesMove;
} else {
movingPiece.movingThisPiece = false;
}
}
moving = !moving;
}
}
//---------------------------------------------------------------------------------------------------------------------
function htmlStuff() {
createP(
""
)
depthPara = createDiv("Thinking " + maxDepth + " moves ahead");
depthMinus = createButton("-");
depthPlus = createButton('+');
depthPlus.mousePressed(plusDepth);
depthMinus.mousePressed(minusDepth);
}
function minusDepth() {
if (tempMaxDepth > 1) {
tempMaxDepth -= 1;
depthPara.html("Thinking " + tempMaxDepth + " moves ahead");
}
}
function plusDepth() {
if (tempMaxDepth < 5) {
tempMaxDepth += 1;
depthPara.html("Thinking " + tempMaxDepth + " moves ahead");
}
}