-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckerBoard.java
More file actions
141 lines (123 loc) · 3.8 KB
/
CheckerBoard.java
File metadata and controls
141 lines (123 loc) · 3.8 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
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
/*
* 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 aws52bcheckers;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
/**
*
* @author DovaReborn
*/
public class CheckerBoard {
private int numRows;
private int numCols;
private double boardWidth;
private double boardHeight;
private Color lightColor;
private Color darkColor;
private double rectWidth;
private double rectHeight;
private double squareEdge;
private AnchorPane board;
//my current implementation doesn't use this
public CheckerBoard(int numRows, int numCols, double boardWidth, double boardHeight){
this(numRows, numCols, boardWidth, boardHeight, Color.RED, Color.BLACK);
}
public CheckerBoard(int numRows, int numCols, double boardWidth, double boardHeight, Color lightColor, Color darkColor){
this.numRows = numRows;
this.numCols = numCols;
this.boardWidth = boardWidth;
this.boardHeight = boardHeight;
this.lightColor = lightColor;
this.darkColor = darkColor;
rectWidth = boardWidth/numCols;
rectHeight = boardHeight/numRows;
if(rectWidth<rectHeight){
squareEdge = rectWidth;
}else{
squareEdge = rectHeight;
}
board = null;
}
public final AnchorPane build(){
//verify all data is valid
if(getNumRows()==-1||getNumCols()==-1||getHeight()==-1||getWidth()==-1||getRectangleWidth()==-1||getRectangleHeight()==-1){
return null;
}
AnchorPane newPane = new AnchorPane();
//A source I found said to change all 3... Probably didn't have to
newPane.setMinSize(boardWidth, boardHeight);
newPane.setPrefSize(boardWidth, boardHeight);
newPane.setMaxSize(boardWidth, boardHeight);
//for each row...
for(int i = 0; i<numRows; i++){
//make x boxes
for(int j = 0; j<numCols; j++){
Rectangle newRect = new Rectangle((double)squareEdge*j, (double)squareEdge*i, squareEdge, squareEdge);
//this makes every-other block light/dark
if(((i%2)+(j%2))%2 == 0){
newRect.setFill(darkColor);
}else{
newRect.setFill(lightColor);
}
newPane.getChildren().add(newRect);
}
}
return newPane;
}
/***************************************************
Each getter implements some form of "error checking"
***************************************************/
public AnchorPane getBoard(){
if(board == null){
board = build();
return board;
}
return board;
}
public int getNumRows(){
if(numRows > 0){
return numRows;
}
return -1;
}
public int getNumCols(){
if(numCols > 0){
return numCols;
}
return -1;
}
public double getWidth(){
if(boardWidth >= 0){
return boardWidth;
}
return -1;
}
public double getHeight(){
if(boardHeight >= 0){
return boardHeight;
}
return -1;
}
public Color getLightColor(){
return lightColor;
}
public Color getDarkColor(){
return darkColor;
}
public double getRectangleWidth(){
if(rectWidth >= 0){
return rectWidth;
}
return -1;
}
public double getRectangleHeight(){
if(rectHeight >= 0){
return rectHeight;
}
return -1;
}
}