ultimate_tictactoe/grid.js

263 lines
6.4 KiB
JavaScript
Raw Permalink Normal View History

2019-03-06 12:11:43 +01:00
class Grid
{
2019-03-21 12:13:46 +01:00
constructor(n, parent, tile_width, tile_height, ground_color)
2019-03-06 12:11:43 +01:00
{
this.n = n;
2019-03-21 12:13:46 +01:00
this.grid_container_div = null;
2019-03-06 12:11:43 +01:00
this.tile_width = tile_width;
this.tile_height = tile_height;
this.ground_color = ground_color;
this.won_player = null;
2019-03-10 13:30:18 +01:00
this.n_complete_subgrids = 0;
2019-03-21 12:13:46 +01:00
this.parent = parent;
2019-03-06 12:11:43 +01:00
this.subgrids = []
console.log("create grid of size " + this.n);
this.create();
}
create()
{
2019-03-21 12:13:46 +01:00
this.grid_container_div = document.createElement("div");
this.grid_container_div.className = "grid-container";
this.parent.appendChild(this.grid_container_div);
2019-03-06 12:11:43 +01:00
var x,y;
for (y = 0; y < this.n; y++)
{
var div_row = this.grid_container_div.appendChild(document.createElement("div"));
var row = [];
for (x = 0; x < this.n; x++)
{
var div_subgrid = div_row.appendChild(document.createElement("div"));
div_subgrid.className = "subgrid-container";
row.push(new Subgrid(this.n, x,y,div_subgrid,this.tile_width, this.tile_height, this.ground_color));
row[x].register_click_callback((i,j,k,l) => this.click_listener(i,j,k,l));
}
this.subgrids.push(row);
}
}
register_click_callback(func)
{
this.click_callback = func;
}
click_listener(sub_x, sub_y, x,y)
{
2019-03-07 19:14:29 +01:00
this.check_win(sub_x, sub_y, x, y);
2019-03-10 13:30:18 +01:00
this.check_complete(sub_x, sub_y, x, y);
2019-03-21 12:13:46 +01:00
if (this.click_callback != null)
{
this.click_callback(sub_x, sub_y, x, y);
}
2019-03-06 12:11:43 +01:00
}
player_change_listener(player)
{
var x,y;
for (y = 0; y < this.n; y++)
{
for (x = 0; x < this.n; x++)
{
this.subgrids[y][x].player_change_listener(player);
}
}
}
2019-03-10 13:30:18 +01:00
is_complete()
{
return !(this.n_complete_subgrids < this.n * this.n);
}
2019-03-07 19:14:29 +01:00
is_won()
{
return this.won_player != null;
}
get_won_player()
{
return this.won_player;
}
2019-03-10 13:30:18 +01:00
check_complete(sub_x, sub_y, x, y)
{
2019-03-22 08:59:41 +01:00
if (this.subgrids[sub_y][sub_x].is_won() || this.subgrids[sub_y][sub_x].is_draw())
2019-03-10 13:30:18 +01:00
{
this.n_complete_subgrids++;
}
}
2019-03-07 19:14:29 +01:00
check_win(sub_x, sub_y, x,y)
{
// check whether the game is won
console.log("check for win");
if (this.won_player == null)
{
var i;
var subgrid = this.subgrids[sub_y][sub_x];
var player = subgrid.cells[y][x].get_activated_player();
// check column
var is_col = true;
for (i = 0; i < this.n; i++)
{
if (!this.subgrids[i][sub_x].is_won() || player.get_id() != this.subgrids[i][sub_x].get_won_player().get_id())
{
is_col = false;
break;
}
}
// check row
var is_row = true;
for (i = 0; i < this.n; i++)
{
if (!this.subgrids[sub_y][i].is_won() || player.get_id() != this.subgrids[sub_y][i].get_won_player().get_id())
{
is_row = false;
break;
}
}
// check diag:
// main diag
var is_main_diag = false;
if (sub_x == sub_y)
{
is_main_diag = true;
for (i = 0; i < this.n; i++)
{
if (!this.subgrids[i][i].is_won() || player.get_id() != this.subgrids[i][i].get_won_player().get_id())
{
is_main_diag = false;
break;
}
}
}
// secundary diag
var is_sec_diag = false;
if (sub_x + sub_y == this.n - 1)
{
is_sec_diag = true;
for (i = 0; i < this.n; i++)
{
if (!this.subgrids[i][this.n - i - 1].is_won() || player.get_id() != this.subgrids[i][this.n - i - 1].get_won_player().get_id())
{
is_sec_diag = false;
break;
}
}
}
if (is_row || is_col || is_main_diag || is_sec_diag)
{
this.won_player = player;
console.log("game over");
//this.grid_container_div.style.backgroundColor = player.get_color();
}
}
}
2019-03-06 12:11:43 +01:00
activate_all()
{
var x,y;
for (y = 0; y < this.n; y++)
{
for (x = 0; x < this.n; x++)
{
this.subgrids[y][x].activate_all();
}
}
}
deactivate_all()
{
var x,y;
for (y = 0; y < this.n; y++)
{
for (x = 0; x < this.n; x++)
{
this.subgrids[y][x].deactivate_all();
}
}
2019-03-07 19:14:29 +01:00
this.won_player = null;
2019-03-10 13:30:18 +01:00
this.n_complete_subgrids = 0;
2019-03-06 12:11:43 +01:00
}
2019-03-07 10:38:08 +01:00
block_all()
2019-03-06 12:11:43 +01:00
{
var x,y;
for (y = 0; y < this.n; y++)
{
for (x = 0; x < this.n; x++)
{
this.subgrids[y][x].block();
}
}
}
2019-03-07 10:38:08 +01:00
unblock_all()
2019-03-06 12:11:43 +01:00
{
var x,y;
for (y = 0; y < this.n; y++)
{
for (x = 0; x < this.n; x++)
{
this.subgrids[y][x].unblock();
}
}
}
2019-03-21 12:13:46 +01:00
unblock_all_non_completed()
{
var x,y;
for (y = 0; y < this.n; y++)
{
for (x = 0; x < this.n; x++)
{
if (this.subgrids[y][x].is_won() || this.subgrids[y][x].is_draw())
{
continue;
}
this.subgrids[y][x].unblock();
}
}
}
2019-03-07 10:38:08 +01:00
block(x,y)
{
this.subgrids[y][x].block();
}
unblock(x,y)
{
this.subgrids[y][x].unblock();
}
2019-03-06 12:11:43 +01:00
on_screen_orientation_change(tile_w,tile_h)
{
this.tile_width = tile_w;
this.tile_h = tile_h;
var x,y;
for (y = 0; y < this.n; y++)
{
for (x = 0; x < this.n; x++)
{
this.subgrids[y][x].on_screen_orientation_change(tile_w, tile_h);
}
}
}
}