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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
package controller;
import adapter.ModelAdapter;
import javafx.event.*;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.canvas.*;
import javafx.scene.control.Button;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import model.*;
import observer.IObserver;
/**
* Created by loic on 22/09/16.
*/
public class MainWindowController implements IObserver {
@FXML Canvas boardCanvas;
@FXML Text score;
@FXML Button restartButton;
private ModelAdapter adapter;
private IModel model;
private int squareSize=100;
private int squarePadding=10;
private int[] boardPosition={30,0};
private int fontSize=60;
/**
* Load component required to the view
* @param adapter
* @param model
* @param scene
*/
public void loadComponent(ModelAdapter adapter, IModel model, Scene scene){
this.adapter=adapter;
this.model=model;
this.update(); // Draw board
// Catch keyboard keys
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
if(event.getCode().toString().equals("UP")){
goUp(null);
}
else if(event.getCode().toString().equals("DOWN")){
goDown(null);
}
else if(event.getCode().toString().equals("LEFT")){
goLeft(null);
}
else if(event.getCode().toString().equals("RIGHT")){
goRight(null);
}
}
});
}
@FXML protected void goUp(ActionEvent event) {
this.adapter.goUp();
}
@FXML protected void goDown(ActionEvent event) {
this.adapter.goDown();
}
@FXML protected void goLeft(ActionEvent event) {
this.adapter.goLeft();
}
@FXML protected void goRight(ActionEvent event) {
this.adapter.goRight();
}
@Override
public void update() {
this.draw();
}
@FXML private void restartGame(ActionEvent event){
IModel model=new Board(4,4);
ModelAdapter adapter=new ModelAdapter((Board) model);
adapter.addObserver(this);
adapter.addRandomNumber();
this.adapter=adapter;
this.model=model;
this.update();
}
/**
* Draw the grid
*/
private void draw(){
// Set score font and draw
this.score.setFont(new Font(40));
if(this.adapter.isLoosed()){
this.score.setText("Score : " + this.model.getScore() + "\n You loose !!!");
}
else{
this.score.setText("Score : " + this.model.getScore());
}
// Get board
int[][] board=this.model.getBoard();
// Draw board
GraphicsContext gc = boardCanvas.getGraphicsContext2D();
gc.clearRect(0,0,500,500);
gc.setFill(Color.rgb(187,173,160));
gc.fillRect(this.boardPosition[0],this.boardPosition[1], ((this.squareSize+squarePadding)*board.length)+squarePadding, ((this.squareSize+squarePadding)*board[0].length)+squarePadding);
// Draw cells
for(int i=0; i<board.length;i++) {
for (int j = 0; j < board[i].length; j++) {
// Get value
int value=board[i][j];
// Apply theme
switch (value){
case -1:
gc.setFill(Color.rgb(202,192,180));
break;
case 2:
gc.setFill(Color.rgb(236,226,217));
break;
case 4:
gc.setFill(Color.rgb(237,222,199));
break;
case 8:
gc.setFill(Color.rgb(241,175,115));
break;
case 16:
gc.setFill(Color.rgb(242,151,94));
break;
case 32:
gc.setFill(Color.rgb(242,151,94));
break;
case 64:
gc.setFill(Color.rgb(244,123,96));
break;
case 128:
gc.setFill(Color.rgb(233,206,115));
break;
case 256:
gc.setFill(Color.rgb(233,206,115));
break;
case 512:
gc.setFill(Color.rgb(237,197,84));
break;
case 1024:
gc.setFill(Color.rgb(234,197,57));
break;
case 2048:
gc.setFill(Color.rgb(233,193,43));
break;
default:
gc.setFill(Color.BLACK);
break;
}
// Build coordinates
int x=this.boardPosition[0] + (j*this.squareSize);
x+=(j+1)*squarePadding;
int y=this.boardPosition[1] + (i*this.squareSize);
y+=(i+1)*squarePadding;
// Draw cell
gc.fillRect(x,y, this.squareSize, this.squareSize);
// Build number font color
if(value>2048) {
gc.setFill(Color.WHITE);
}
else{
gc.setFill(Color.BLACK);
}
// Draw if value is greeter than 0
if(value>0){
// Ajuste font position
String strValue=""+value;
int localFontSize=fontSize;
if(strValue.length()==3){
localFontSize=localFontSize/2;
}else if(strValue.length()==4){
localFontSize=localFontSize/3;
}
else if(strValue.length()>4){
localFontSize=localFontSize/4;
}
gc.setFont(new Font(localFontSize));
// Write value
gc.fillText(strValue, x + (this.squareSize / 2) - ((localFontSize/4)+strValue.length()*localFontSize/5) , y + (this.squareSize / 2) + (localFontSize /3));
}
}
}
}
}
|