-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckersBaseGame
124 lines (106 loc) · 2.18 KB
/
CheckersBaseGame
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
int colorCont = 0;
int[] hues = new int[32];
ArrayList<Piece> pieces = new ArrayList<>();
Piece picked;
void setup(){
size(800, 800);
noStroke();
colorMode(HSB);
pieces.add(new Piece(0, 0));
}
void draw(){
drawBoard();
displayPieces();
movePicked();
}
void movePicked(){
if(picked!=null){
picked.snap(mouseX, mouseY);
}
}
void mousePressed(){
setPicked();
}
void mouseReleased(){
if(picked != null){
picked.snap(mouseX, mouseY);
picked = null;
}
}
void setPicked(){
for(Piece p : pieces){
int px = (p.c*100)+50;
int py = (p.r*100)+50;
// if dist to center of piece is less than its radius
if ((Math.pow((mouseX-px), 2) + Math.pow((mouseY-py), 2)) < Math.pow(p.radius, 2)){
picked = p;
println("picked in row: " + p.r + " col: " + p.c);
}
}
}
void displayPieces(){
for(Piece p : pieces) p.display();
}
void setColor(int i, int j){
if((i+j)%2 == 1){
fill(0);
}
else{
fill(colorCont, 225, 200);
colorCont = colorCont + 8;
}
}
void drawBoard(){
background(0);
for(int i = 0; i<8; i++){
for(int j = 0; j<8; j++){
setColor(i, j);
rect(i*100, j*100, 100, 100, 20);
}
}
colorCont = 0;
}
class Piece{
int c;
int r;
int radius;
Piece(){
c = 0;
r = 0;
radius = 50;
}
Piece(int a, int b){
this.c = a;
this.r = b;
radius = 50;
}
void display(){
fill(255);
ellipse((c*100)+50, (r*100)+50, 2*radius, 2*radius);
}
// TODO: fix snap-to-grid
void snap(float x, float y){
float row = y/100;
int rowInt = (int)row;
float col = x/100;
int colInt = (int)col;
if(row-rowInt >= 0.8){
r = rowInt+1;
}
else{
r = rowInt;
}
if(col-colInt > 0.8){
c = colInt+1;
}
else{
c = colInt;
}
if(row > 7){
r = 7;
}
if(col > 7){
c = 7;
}
}
}