public Game CreateGame(Player player1, Player player2) { Game game = new Game { Player1 = player1, Player2 = player2, Board = new GameBoard() }; string group = Guid.NewGuid().ToString("d"); _games[group] = game; player1.isPlaying = true; player2.isPlaying = true; player1.Symbol = "RedSymbol"; player2.Symbol = "BlueSymbol"; player1.Group = group; player2.Group = group; Groups.Add(player1.Id, group); Groups.Add(player2.Id, group); return game; }
public Game FindGame(Player player, out Player opponent) { opponent = null; if (player.Group == null) return null; Game game; _games.TryGetValue(player.Group, out game); if (game != null) { opponent = (player.Id == game.Player1.Id) ? game.Player2 : game.Player1; return game; } return null; }
public Player CreatePlayer(string userName) { var player = new Player(userName); _players[userName] = player; return player; }
public Player GetOpponent(Game game, Player player) { return (game.Player1.Id == player.Id) ? game.Player2 : game.Player1; }
public Player GetNewOpponent(Player player) { return _players.Values.FirstOrDefault(u => !u.isPlaying && u.Id != player.Id); }
private bool StartGame(Player p1) { Player p2; var game = GameState.Instance.FindGame(p1, out p2); if (game != null) // Already player is playing a game { Clients.Group(p1.Group).buildBoard(game); return true; } p2 = GameState.Instance.GetNewOpponent(p1); if (p2 == null) // No Free Players available { Clients.Caller.waitingList(); return true; } game = GameState.Instance.CreateGame(p1, p2); game.WhosTurn = p1.Id; Clients.Caller.id2 = p2.Id; Clients.Group(p1.Group).buildBoard(game); return true; }