ultimate_tictactoe/game_manager.js

91 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-03-06 12:11:43 +01:00
class GameManager
{
2019-03-07 13:07:51 +01:00
constructor(grid, n)
2019-03-06 12:11:43 +01:00
{
this.grid = grid;
2019-03-07 13:07:51 +01:00
this.n = n;
2019-03-06 12:11:43 +01:00
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");
2019-03-07 19:14:29 +01:00
this.is_local_player_a = false;;
2019-03-06 12:11:43 +01:00
this.grid.player_change_listener(this.dummy_player);
2019-03-07 10:38:08 +01:00
// possible modes:
// -- none
// -- local
// -- remote //TODO
this.game_mode = "none";
2019-03-07 13:07:51 +01:00
this.grid.register_click_callback((i,j,k,l) => this.click_listener(i,j,k,l));
}
click_listener(sub_x, sub_y, x,y)
{
2019-03-07 19:14:29 +01:00
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();
2019-03-08 10:40:16 +01:00
this.grid.block_all()
this.grid.subgrids[y][x].unblock();
2019-03-07 19:14:29 +01:00
}
}
2019-03-07 10:38:08 +01:00
}
register_game_mode_change_listener(func)
{
this.game_mode_change_listener = func;
}
2019-03-07 19:14:29 +01:00
register_status_change_listener(func)
{
this.status_change_listener = func;
}
2019-03-07 10:38:08 +01:00
set_game_mode(mode)
{
this.game_mode = mode;
this.game_mode_change_listener(mode);
console.log("changed gamemode to " + mode);
}
start_local_game()
{
this.set_game_mode("local");
2019-03-07 13:07:51 +01:00
this.grid.deactivate_all();
this.grid.unblock_all();
2019-03-07 19:14:29 +01:00
this.is_local_player_a = false;
this.toggle_local_player();
2019-03-07 10:38:08 +01:00
}
2019-03-07 19:14:29 +01:00
toggle_local_player()
2019-03-07 10:38:08 +01:00
{
2019-03-07 19:14:29 +01:00
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);
}
2019-03-07 19:14:29 +01:00
end_game()
{
2019-03-07 19:14:29 +01:00
this.set_game_mode("none");
this.grid.block_all();
2019-03-06 12:11:43 +01:00
}
}