started implementing local game rules in game_manager

This commit is contained in:
Jonas Weinz 2019-03-07 17:51:19 +01:00
parent b327faba6c
commit 8f18813827
5 changed files with 107 additions and 4 deletions

View File

@ -8,6 +8,9 @@ class GameManager
this.dummy_player = new Player("test_player", "rgb(0,128,0)"); this.dummy_player = new Player("test_player", "rgb(0,128,0)");
this.local_player_a = new Player("red player", "rgb(128,0,0)");
this.local_player_b = new Player("green player", "rgb(0,128,0");
this.grid.player_change_listener(this.dummy_player); this.grid.player_change_listener(this.dummy_player);
// possible modes: // possible modes:
@ -26,6 +29,9 @@ class GameManager
{ {
// TODO: dummy // TODO: dummy
console.log("click"); console.log("click");
var subgrid = this.grid.subgrids[sub_y][sub_x];
var tile = subgrid.cells[y][x];
} }
register_game_mode_change_listener(func) register_game_mode_change_listener(func)
@ -43,13 +49,18 @@ class GameManager
start_local_game() start_local_game()
{ {
this.set_game_mode("local"); this.set_game_mode("local");
this.grid.unblock_all();
this.grid.deactivate_all(); this.grid.deactivate_all();
this.grid.unblock_all();
} }
end_game() end_game()
{ {
this.set_game_mode("none"); this.set_game_mode("none");
this.grid.unblock_all(); this.grid.block_all();
}
on_local_game_move(sub_x, sub_y, x, y)
{
//checking whether one
} }
} }

View File

@ -8,6 +8,8 @@ class Grid
this.tile_height = tile_height; this.tile_height = tile_height;
this.ground_color = ground_color; this.ground_color = ground_color;
this.won_player = null;
this.subgrids = [] this.subgrids = []
console.log("create grid of size " + this.n); console.log("create grid of size " + this.n);

View File

@ -4,6 +4,7 @@ class Player
{ {
this.name = name; this.name = name;
this.color = color; this.color = color;
this.id = name;
} }
set_name(name) set_name(name)
@ -18,7 +19,12 @@ class Player
get_name() get_name()
{ {
return this.name return this.name;
}
get_id()
{
return this.id;
} }
get_color() get_color()

View File

@ -8,6 +8,7 @@ class Subgrid
this.tile_width = tile_width; this.tile_width = tile_width;
this.cells = []; this.cells = [];
this.won_player = null;
this.sub_x = sub_x; this.sub_x = sub_x;
this.sub_y = sub_y; this.sub_y = sub_y;
@ -58,7 +59,75 @@ class Subgrid
click_listener(x,y) click_listener(x,y)
{ {
this.click_callback(this.sub_x, this.sub_y, x, y); // check whether this subfield is won
if (this.won_player == null)
{
var i;
var player = this.cells[y][x].get_activated_player();
// check column
var is_col = true;
for (i = 0; i < this.n; i++)
{
if (!this.cells[i][x].get_is_activated() || player.get_id() != this.cells[i][x].get_activated_player().get_id())
{
is_col = false;
break;
}
}
// check row
var is_row = true;
for (i = 0; i < this.n; i++)
{
if (!this.cells[y][i].get_is_activated() || player.get_id() != this.cells[y][i].get_activated_player().get_id())
{
is_row = false;
break;
}
}
// check diag:
// main diag
var is_main_diag = false;
if (x == y)
{
is_main_diag = true;
for (i = 0; i < this.n; i++)
{
if (!this.cells[i][i].get_is_activated() || player.get_id() != this.cells[i][i].get_activated_player().get_id())
{
is_main_diag = false;
break;
}
}
}
// secundary diag
var is_sec_diag = false;
if (x + y == this.n - 1)
{
is_sec_diag = true;
for (i = 0; i < this.n; i++)
{
if (!this.cells[i][this.n - i - 1].get_is_activated() || player.get_id() != this.cells[i][this.n - i - 1].get_activated_player().get_id())
{
is_sec_diag = false;
break;
}
}
}
if (is_row || is_col || is_main_diag || is_sec_diag)
{
this.won_player = player;
this.subgrid_container_div.style.backgroundColor = player.get_color();
}
}
this.click_callback(this.sub_x, this.sub_y, x, y);
} }
player_change_listener(player) player_change_listener(player)
@ -103,6 +172,7 @@ class Subgrid
this.cells[y][x].deactivate(); this.cells[y][x].deactivate();
} }
} }
this.subgrid_container_div.style.backgroundColor = ""; // reset to css color
} }
block() block()

14
tile.js
View File

@ -18,6 +18,8 @@ class Tile
this.div.style.transform = "translate( calc( " + x + "*" + w + "), calc(" + y + "*" + h + "))"; this.div.style.transform = "translate( calc( " + x + "*" + w + "), calc(" + y + "*" + h + "))";
this.player = null; this.player = null;
this.activated_player = null;
this.reset(); this.reset();
this.bind(); this.bind();
@ -61,6 +63,16 @@ class Tile
this.deactivate(); this.deactivate();
} }
get_is_activated()
{
return this.is_activated;
}
get_activated_player()
{
return this.activated_player;
}
lock() lock()
{ {
this.locked = true; this.locked = true;
@ -76,6 +88,7 @@ class Tile
deactivate() deactivate()
{ {
this.is_activated = false; this.is_activated = false;
this.activated_player = null;
this.elem.style.background = this.ground_color; this.elem.style.background = this.ground_color;
this.elem.style.opacity = 0.3; this.elem.style.opacity = 0.3;
this.elem.style.border = "none"; this.elem.style.border = "none";
@ -84,6 +97,7 @@ class Tile
activate() activate()
{ {
this.is_activated = true; this.is_activated = true;
this.activated_player = this.player;
this.elem.style.background = this.player.get_color(); this.elem.style.background = this.player.get_color();
this.elem.style.opacity = 1.0; this.elem.style.opacity = 1.0;
this.elem.style.border = "2px solid rgba(255,255,255,0.3)"; this.elem.style.border = "2px solid rgba(255,255,255,0.3)";