local version done

This commit is contained in:
Jonas Weinz 2019-03-07 19:14:29 +01:00
parent 8f18813827
commit aaa6dfc020
5 changed files with 168 additions and 38 deletions

View File

@ -10,6 +10,7 @@ class GameManager
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.is_local_player_a = false;;
this.grid.player_change_listener(this.dummy_player);
@ -27,11 +28,19 @@ class GameManager
click_listener(sub_x, sub_y, x,y)
{
// TODO: dummy
console.log("click");
var subgrid = this.grid.subgrids[sub_y][sub_x];
var tile = subgrid.cells[y][x];
if (this.game_mode == "local")
{
// check whether the game is over:
if (grid.is_won())
{
this.status_change_listener("" + grid.get_won_player().get_name() + " has won.")
this.end_game();
}
else
{
this.toggle_local_player();
}
}
}
register_game_mode_change_listener(func)
@ -39,6 +48,11 @@ class GameManager
this.game_mode_change_listener = func;
}
register_status_change_listener(func)
{
this.status_change_listener = func;
}
set_game_mode(mode)
{
this.game_mode = mode;
@ -51,6 +65,20 @@ class GameManager
this.set_game_mode("local");
this.grid.deactivate_all();
this.grid.unblock_all();
this.is_local_player_a = false;
this.toggle_local_player();
}
toggle_local_player()
{
this.is_local_player_a = !this.is_local_player_a;
var next_player = this.is_local_player_a ? this.local_player_a : this.local_player_b;
this.status_change_listener("" + "it's " + next_player.get_name() + "s turn...");
this.grid.player_change_listener(next_player);
}
end_game()
@ -58,9 +86,4 @@ class GameManager
this.set_game_mode("none");
this.grid.block_all();
}
on_local_game_move(sub_x, sub_y, x, y)
{
//checking whether one
}
}

86
grid.js
View File

@ -46,6 +46,7 @@ class Grid
click_listener(sub_x, sub_y, x,y)
{
this.check_win(sub_x, sub_y, x, y);
this.click_callback(sub_x, sub_y, x,y);
}
@ -61,6 +62,89 @@ class Grid
}
}
is_won()
{
return this.won_player != null;
}
get_won_player()
{
return this.won_player;
}
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();
}
}
}
activate_all()
{
var x,y;
@ -83,6 +167,8 @@ class Grid
this.subgrids[y][x].deactivate_all();
}
}
this.won_player = null;
}
block_all()

View File

@ -51,7 +51,7 @@ class Sidebar
// status area:
this.info_container.appendChild(this.create_label("status"));
this.status_text = this.create_label("...");
this.status_text = this.create_label("select gamemode");
this.info_container.appendChild(this.status_text);
this.info_container.style.display = "none";
@ -64,6 +64,7 @@ class Sidebar
{
// TODO
this.game_manager.register_game_mode_change_listener((c) => this.game_mode_change_listener(c));
this.game_manager.register_status_change_listener((c) => this.status_change_listener(c));
this.b_local.addEventListener("click", () => this.game_manager.start_local_game());
this.b_end_game.addEventListener("click", () => this.game_manager.end_game());
@ -110,7 +111,7 @@ class Sidebar
{
this.activate_create_game();
this.deactivate_control();
this.deactivate_info();
this.activate_info();
return
}
if (gamemode == "local")
@ -121,6 +122,11 @@ class Sidebar
}
}
status_change_listener(statustext)
{
this.status_text.innerHTML = statustext;
}
}

View File

@ -56,8 +56,45 @@ class Subgrid
this.click_callback = func;
}
click_listener(x,y)
{
this.check_win(x,y);
this.click_callback(this.sub_x, this.sub_y, x, y);
}
player_change_listener(player)
{
var x,y;
for (y = 0; y < this.n; y++)
{
for (x = 0; x < this.n; x++)
{
this.cells[y][x].player_change_listener(player);
}
}
}
unlock_request_listener(x,y)
{
// TODO: unused so far
//return this.unlock_request_callback(x,y);
return false;
}
is_won()
{
return this.won_player != null;
}
get_won_player()
{
return this.won_player;
}
check_win(x,y)
{
// check whether this subfield is won
@ -126,28 +163,6 @@ class Subgrid
this.subgrid_container_div.style.backgroundColor = player.get_color();
}
}
this.click_callback(this.sub_x, this.sub_y, x, y);
}
player_change_listener(player)
{
var x,y;
for (y = 0; y < this.n; y++)
{
for (x = 0; x < this.n; x++)
{
this.cells[y][x].player_change_listener(player);
}
}
}
unlock_request_listener(x,y)
{
// TODO: unused so far
//return this.unlock_request_callback(x,y);
return false;
}
activate_all()
@ -172,6 +187,7 @@ class Subgrid
this.cells[y][x].deactivate();
}
}
this.won_player = null;
this.subgrid_container_div.style.backgroundColor = ""; // reset to css color
}

View File

@ -142,10 +142,9 @@ class Tile
}
}
var active_before = this.is_activated;
this.activate();
if (!active_before)
if (!this.is_activated)
{
this.activate();
this.click_callback(this.x, this.y);
}