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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
public class BoardView extends View {
private final int LINES_WEIGHT;
private final int PIECES_WEIGHT;
private final byte CELLS_PER_DIM;
private final byte BORDER_LINES_PER_DIM;
private float cell_dim;
private float pieces_dim;
private boolean min_dim_is_width = true;
private int total_board_dim;
// x as first dim
private byte [][] players_ids;
private int cursor_grid_matrix_x;
private int cursor_grid_matrix_y;
/*
* 0 for none player
* > 0 for player 1
* < 0 for player 2
*/
private byte current_player;
private boolean end_game;
private final TicTacToeActivity context;
public BoardView(Context context) {
super(context);
this.context = (TicTacToeActivity) context;
LINES_WEIGHT = getResources().getInteger(R.id.board_lines_weight);
PIECES_WEIGHT = getResources().getInteger(R.id.board_pieces_weight);
CELLS_PER_DIM = (byte) getResources().getInteger(R.id.board_cells_per_dim);
BORDER_LINES_PER_DIM = (byte) getResources().getInteger(R.id.board_border_lines_per_dim);
players_ids = new byte[CELLS_PER_DIM][CELLS_PER_DIM];
end_game = false;
setFocusable(true);
setFocusableInTouchMode(true);
newGame();
}
public byte[] getSerialisationBytes() {
byte serialisationValues [] = new byte[10];
for (int x = 0; x < 3; x++){
for (int y = 0; y < 3; y++){
serialisationValues[3*x + y] = players_ids[x][y];
}
}
serialisationValues[9] = current_player;
return serialisationValues;
}
public void setValuesFromUnSerialisarion(byte [] unserialisationValues){
for (int x = 0; x < 3; x++){
for (int y = 0; y < 3; y++){
players_ids[x][y] = unserialisationValues[3*x + y];
}
}
current_player = unserialisationValues[9];
}
public void setBoardValueAtXY(int x, int y, byte value){
players_ids[x][y] = value;
}
public void newGame(){
current_player = 1; // set to player 1
cursor_grid_matrix_x = 0;
cursor_grid_matrix_y = 0;
for (byte cellMatrixLine = 0; cellMatrixLine < CELLS_PER_DIM; cellMatrixLine++){
for (byte cellMatrixColumn = 0; cellMatrixColumn < CELLS_PER_DIM; cellMatrixColumn++){
players_ids[cellMatrixColumn][cellMatrixLine] = 0; // set to none player
}
}
}
public void tryToPlayIn(int gridMatrixX, int gridMatrixY){
if (players_ids[gridMatrixX][gridMatrixY] == 0){
players_ids[gridMatrixX][gridMatrixY] = current_player;
invalidate(getCursorRect());
byte winner_id = winner();
if (winner_id != 0){
String winner_text = "";
String key_input_asking = getResources().getString(R.string.endgame_key_input_asking);
if (winner_id > 0){
winner_text = getResources().getString(R.string.circles_win);
}
else {
winner_text = getResources().getString(R.string.crosses_win);
}
Toast.makeText(
context,
winner_text+"\n"+key_input_asking,
Toast.LENGTH_LONG
).show();
end_game = true;
return;
}
if (board_filled()){
String no_winner_text = getResources().getString(R.string.no_winner_text);
String key_input_asking = getResources().getString(R.string.endgame_key_input_asking);
Toast.makeText(
context,
no_winner_text+"\n"+key_input_asking,
Toast.LENGTH_LONG
).show();
end_game = true;
return;
}
current_player *= -1; // change player
}
}
public void onSizeChanged(int width, int height, int oldWidth, int oldHeight){
int min_dim = -1;
if (width < height){
min_dim = width;
min_dim_is_width = true;
}
else {
min_dim = height;
min_dim_is_width = false;
}
cell_dim = (min_dim - LINES_WEIGHT * BORDER_LINES_PER_DIM) / CELLS_PER_DIM;
pieces_dim = .8f * cell_dim;
total_board_dim = (int) (BORDER_LINES_PER_DIM * LINES_WEIGHT + CELLS_PER_DIM * cell_dim);
super.onSizeChanged(width, height, oldWidth, oldHeight);
}
public void onDraw(Canvas canvas){
paintBackground(canvas);
drawGridBorders(canvas);
paintDroppingZone(canvas);
drawPieces(canvas);
drawCursor(canvas);
}
private void drawPieces(Canvas canvas) {
for (byte cellMatrixLine = 0; cellMatrixLine < CELLS_PER_DIM; cellMatrixLine++){
for (byte cellMatrixColumn = 0; cellMatrixColumn < CELLS_PER_DIM; cellMatrixColumn++){
byte currentPlayerId = players_ids[cellMatrixColumn][cellMatrixLine];
if (currentPlayerId > 0){
drawCirclePiece(canvas, cellMatrixColumn, cellMatrixLine);
}
else if(currentPlayerId < 0){
drawCrossPiece(canvas, cellMatrixColumn, cellMatrixLine);
}
}
}
}
private void drawCursor(Canvas canvas){
Paint cursorColor = new Paint();
cursorColor.setStyle(Style.FILL);
cursorColor.setColor(getResources().getColor(R.color.board_cursor_color));
Rect cellRect = getCursorRect();
canvas.drawRect(cellRect, cursorColor);
}
private Rect getCursorRect(){
return new Rect(
(int) (LINES_WEIGHT + cursor_grid_matrix_x * (cell_dim + LINES_WEIGHT)),
(int) (LINES_WEIGHT + cursor_grid_matrix_y * (cell_dim + LINES_WEIGHT)),
(int) (LINES_WEIGHT + cursor_grid_matrix_x * (cell_dim + LINES_WEIGHT) + cell_dim -1),
(int) (LINES_WEIGHT + cursor_grid_matrix_y * (cell_dim + LINES_WEIGHT) + cell_dim -1)
);
}
private void paintDroppingZone(Canvas canvas) {
Paint bottom_zone_color = new Paint();
bottom_zone_color.setColor(getResources().getColor(R.color.board_dropping_zone_color));
bottom_zone_color.setStyle(Style.FILL);
Rect dropping_zone_rect;
if (min_dim_is_width){
dropping_zone_rect = new Rect(
0,
total_board_dim,
getWidth(),
getHeight()
);
}
else {
dropping_zone_rect = new Rect(
total_board_dim,
0,
getWidth(),
getHeight()
);
}
canvas.drawRect(
dropping_zone_rect,
bottom_zone_color
);
}
private void drawGridBorders(Canvas canvas) {
Paint line_color = new Paint();
line_color.setColor(getResources().getColor(R.color.board_lines_color));
// Drawing lines
for(int line = 0; line < BORDER_LINES_PER_DIM; line++){
for (int lineNumber = 0; lineNumber < LINES_WEIGHT; lineNumber++) {
canvas.drawLine(
0,
line * (cell_dim + LINES_WEIGHT)+lineNumber,
total_board_dim,
line * (cell_dim + LINES_WEIGHT)+lineNumber,
line_color
);
}
}
// Drawing columns
for(int column = 0; column < BORDER_LINES_PER_DIM; column++){
for (int lineNumber = 0; lineNumber < LINES_WEIGHT; lineNumber++) {
canvas.drawLine(
column * (cell_dim + LINES_WEIGHT)+lineNumber,
0,
column * (cell_dim + LINES_WEIGHT)+lineNumber,
total_board_dim,
line_color
);
}
}
}
private void paintBackground(Canvas canvas) {
Paint background_color = new Paint();
background_color.setColor(getResources().getColor(R.color.board_background));
background_color.setStyle(Style.FILL);
canvas.drawRect(0,0,getWidth(),getHeight(), background_color);
}
private void drawCrossPiece(Canvas canvas, int gridMatrixX, int gridMatrixY){
Paint crossColor = new Paint();
crossColor.setStyle(Style.STROKE);
crossColor.setColor(getResources().getColor(R.color.board_crosses_color));
final float CELL_X = LINES_WEIGHT + gridMatrixX * (cell_dim + LINES_WEIGHT);
final float CELL_Y = LINES_WEIGHT + gridMatrixY * (cell_dim + LINES_WEIGHT);
final float CELL_PADING = (cell_dim - pieces_dim) / 2;
for (int lineIndex = 0; lineIndex < PIECES_WEIGHT; lineIndex++){
float lineStart_X = CELL_X + CELL_PADING + lineIndex;
float lineEnd_X = lineStart_X + pieces_dim - PIECES_WEIGHT;
float lineStart_Y = CELL_Y + CELL_PADING;
float lineEnd_Y = CELL_Y + cell_dim - CELL_PADING - 1;
canvas.drawLine(lineStart_X, lineStart_Y, lineEnd_X, lineEnd_Y, crossColor);
lineStart_X = CELL_X + cell_dim -1 - CELL_PADING - lineIndex;
lineEnd_X = lineStart_X - pieces_dim + PIECES_WEIGHT;
canvas.drawLine(lineStart_X, lineStart_Y, lineEnd_X, lineEnd_Y, crossColor);
}
}
private void drawCirclePiece(Canvas canvas, int gridMatrixX, int gridMatrixY){
Paint circleColor = new Paint();
circleColor.setStyle(Style.STROKE);
circleColor.setColor(getResources().getColor(R.color.board_circles_color));
final float CELL_X = LINES_WEIGHT + gridMatrixX * (cell_dim + LINES_WEIGHT);
final float CELL_Y = LINES_WEIGHT + gridMatrixY * (cell_dim + LINES_WEIGHT);
final float CELL_PADING = (cell_dim - pieces_dim) / 2;
final float CELL_CENTER_X = CELL_X + cell_dim / 2;
final float CELL_CENTER_Y = CELL_Y + cell_dim / 2;
for (int lineIndex = 0; lineIndex < PIECES_WEIGHT; lineIndex++){
float radius = cell_dim/2 - CELL_PADING - lineIndex;
canvas.drawCircle(CELL_CENTER_X, CELL_CENTER_Y, radius, circleColor);
}
}
public boolean onKeyDown(int keyCode, KeyEvent event){
if ( ! end_game ) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
context.finish();
break;
case KeyEvent.KEYCODE_DPAD_UP:
invalidate(getCursorRect());
cursorTopOrToColumnEnd();
invalidate(getCursorRect());
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
invalidate(getCursorRect());
cursorDownOrToColumnStart();
invalidate(getCursorRect());
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
invalidate(getCursorRect());
cursorLeftOrToRowEnd();
invalidate(getCursorRect());
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
invalidate(getCursorRect());
cursorRightOrToRowStart();
invalidate(getCursorRect());
break;
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_DPAD_CENTER:
tryToPlayIn(cursor_grid_matrix_x, cursor_grid_matrix_y);
break;
default:
return super.onKeyDown(keyCode, event);
}
}
else {
context.finish();
}
return true;
}
private void cursorLeftOrToRowEnd(){
cursor_grid_matrix_x -= 1;
if (cursor_grid_matrix_x < 0){
cursor_grid_matrix_x = CELLS_PER_DIM - 1;
}
}
private void cursorRightOrToRowStart(){
cursor_grid_matrix_x += 1;
if (cursor_grid_matrix_x >= CELLS_PER_DIM){
cursor_grid_matrix_x = 0;
}
}
private void cursorTopOrToColumnEnd(){
cursor_grid_matrix_y -= 1;
if (cursor_grid_matrix_y < 0){
cursor_grid_matrix_y = CELLS_PER_DIM - 1;
}
}
private void cursorDownOrToColumnStart(){
cursor_grid_matrix_y += 1;
if (cursor_grid_matrix_y >= CELLS_PER_DIM){
cursor_grid_matrix_y = 0;
}
}
public boolean onTouchEvent(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN){
if ( ! end_game ) {
float x = event.getX();
float y = event.getY();
float nearest_left_border_start = x - x
% (LINES_WEIGHT + cell_dim);
float nearest_left_border_end = nearest_left_border_start
+ LINES_WEIGHT - 1;
float nearest_top_border_start = y - y
% (LINES_WEIGHT + cell_dim);
float nearest_top_border_end = nearest_top_border_start
+ LINES_WEIGHT - 1;
// is pointed area in the board ?
if (x >= 0 && x < total_board_dim && y >= 0
&& y < total_board_dim) {
// is pointed area NOT in a border line ?
if (x > nearest_left_border_end
&& y > nearest_top_border_end) {
int matrixX = (int) (nearest_left_border_start / (LINES_WEIGHT + cell_dim));
int matrixY = (int) (nearest_top_border_start / (LINES_WEIGHT + cell_dim));
// set cursor to pointed cell
cursor_grid_matrix_x = matrixX;
cursor_grid_matrix_y = matrixY;
tryToPlayIn(matrixX, matrixY);
} else {
Toast.makeText(
context,
getResources().getString(
R.string.on_border_line_click),
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(
context,
getResources().getString(
R.string.out_of_board_click),
Toast.LENGTH_SHORT).show();
}
}
else {
context.finish();
}
}
return true;
}
private byte winner(){
byte the_winner;
the_winner = lineWinner();
if (the_winner != 0){
return the_winner;
}
the_winner = columnWinner();
if (the_winner != 0){
return the_winner;
}
return diagonals_winner();
}
private byte lineWinner(){
byte the_winner = 0; // set to none
byte winner_combos = 1;
for (int line = 0; line < CELLS_PER_DIM; line++){
if ( players_ids[0][line] == 0 ){
continue;
}
winner_combos = 1;
the_winner = players_ids[0][line];
for(int column = 1; column < CELLS_PER_DIM; column++){
if (players_ids[column][line] * the_winner <= 0){ // different player or none player ?
break;
}
winner_combos++;
}
if (winner_combos == CELLS_PER_DIM){
return the_winner;
}
}
return 0;
}
private byte columnWinner(){
byte the_winner = 0; // set to none
byte winner_combos = 1;
for (int column = 0; column < CELLS_PER_DIM; column++){
if ( players_ids[column][0] == 0 ){
continue;
}
winner_combos = 1;
the_winner = players_ids[column][0];
for(int line = 1; line < CELLS_PER_DIM; line++){
if (players_ids[column][line] * the_winner <= 0){ // different player or none player ?
break;
}
winner_combos++;
}
if (winner_combos == CELLS_PER_DIM){
return the_winner;
}
}
return 0;
}
private byte diagonals_winner(){
byte the_winner;
the_winner = players_ids[0][0];
if (the_winner != 0){
int winner_combos = 1;
for (int progression = 1; progression < CELLS_PER_DIM; progression++){
if (players_ids[progression][progression] * the_winner <= 0){
break;
}
winner_combos++;
}
if (winner_combos == CELLS_PER_DIM){
return the_winner;
}
}
the_winner = players_ids[CELLS_PER_DIM - 1][0];
if (the_winner != 0){
int winner_combos = 1;
for (int progression = 1; progression < CELLS_PER_DIM; progression++){
if (players_ids[CELLS_PER_DIM - 1 - progression][progression] * the_winner <= 0){
break;
}
winner_combos++;
}
if (winner_combos == CELLS_PER_DIM){
return the_winner;
}
}
return 0;
}
private boolean board_filled(){
for (int line_id = 0; line_id < CELLS_PER_DIM; line_id++){
for (int column_id = 0; column_id < CELLS_PER_DIM; column_id++){
if (players_ids[column_id][line_id] == 0){
return false;
}
}
}
return true;
}
} |
Partager