/// <summary> /// Initializes the board. /// </summary> /// <param name="gameParams">Game parameters.</param> private void InitializeGame(GameParameters gameParams) { PrivateExecutor = new GameExecutor(); gameParams.TileSet = TileSetType.PASSIVE; // Force this rule! PrivateExecutor.CurrentGameState = new GameState(); PrivateExecutor.StartGame(gameParams); }
private static void Main() { while (true) { var gameExecutor = new GameExecutor(); gameExecutor.Play(new GuessGame()); } }
public static void Main() { IReader reader = new Reader(); IWriter writer = new Writer(); GameExecutor gameExecutor = new GameExecutor(); Engine engine = new Engine(writer, reader, gameExecutor); engine.Run(); }
public void SetSimulation(GameExecutor simulation) { simulation.FollowerPlacedEvent += Simulation_FollowerPlacedEvent; simulation.FollowerRemovedEvent += Simulation_FollowerRemovedEvent; simulation.GameEndedEvent += Simulation_GameEndedEvent; simulation.GameStartedEvent += Simulation_GameStartedEvent; simulation.MoveEndedEvent += Simulation_MoveEndedEvent; simulation.MovePassedEvent += Simulation_MovePassedEvent; simulation.MoveStartedEvent += Simulation_MoveStartedEvent; simulation.TilePlacedEvent += Simulation_TilePlacedEvent; }
/// <summary> /// Expects 2 arguments, paths to the players' dlls. If they are not specified, runs the game locally. /// </summary> /// <param name="args"></param> private static void Main(string[] args) { if (args.Length != 2) { var runner = new GameRunner(); runner.RunGameLocally(); return; } var gameExecutor = new GameExecutor(); gameExecutor.RunGame(args[0], args[1]); }
/// <summary> /// Default constructor. /// </summary> public GameServer(SimulationArguments args) { Executor = new GameExecutor(); Executor.GameStartedEvent += Executor_GameStartedEvent; Executor.MoveStartedEvent += Executor_MoveStartedEvent; Executor.TilePlacedEvent += Executor_TilePlacedEvent; Executor.FollowerPlacedEvent += Executor_FollowerPlacedEvent; Executor.FollowerRemovedEvent += Executor_FollowerRemovedEvent; Executor.MovePassedEvent += Executor_MovePassedEvent; Executor.GameEndedEvent += Executor_GameEndedEvent; Socket = new ServerSocket(); Socket.ClientConnected += Socket_ClientConnected; Socket.ClientDisconnected += Socket_ClientDisconnected; Socket.MessageReceived += Socket_MessageReceived; PlayersInGame = new Dictionary <PlayerColor, ClientHandlerSocket>(); PlayersNames = new Dictionary <PlayerColor, string>(); MessageHandlerThread = new Thread(new ThreadStart(MessageHandlingLoop)); HasMessageResetEvent = new ManualResetEventSlim(); CurrentState = ServerState.BEFORE_LOBBY; // Make the settings based on the arguments _Port = args.Port; _PlayerLimit = args.PlayerCount; _EnabledColors = new List <PlayerColor>(args.EnabledColors); // Save file must be set first, because game loader will repeat the game events if (args.SaveGame) { SetSaveFile(args.SaveFileName); } if (args.LoadGame) { _LoadedResponses = LoadGame(args, args.LoadFileName); args.Parameters = Executor.CurrentGameState.Params.Copy(); _Loaded = true; } }
public List<ServerResponse> LoadGame(SimulationArguments args, GameExecutor executor, string filename) { var serializer = new XmlSerializer(typeof(ServerResponse)); List<ServerResponse> responses = new List<ServerResponse>(); using (var reader = new StreamReader(filename)) { var msgs = reader.ReadToEnd() .Split(new string[] { "</Response>" }, StringSplitOptions.RemoveEmptyEntries) .Select(x => x + "</Response>"); bool any = false; bool hasParams = false; foreach (var msg in msgs) { try { var response = (ServerResponse)serializer.Deserialize(new StringReader(msg)); responses.Add(response); switch (response.Type) { case ServerResponseType.GAME_START: if (any) throw new Exception(); args.Parameters = response.Parameters; args.EnabledColors = response.Parameters.PlayerOrder.ToList(); args.PlayerCount = args.EnabledColors.Count; args.Parameters.TileSet = TileSetType.STANDARD_PASSIVE; hasParams = true; executor.StartGame(args.Parameters); break; case ServerResponseType.MOVE_START: // nothing, tileset is given break; case ServerResponseType.TILE_PLACEMENT: executor.PlaceTile(response.Color, response.Scheme, response.Coords, response.Orientation); break; case ServerResponseType.FOLLOWER_PLACEMENT: executor.PlaceFollower(response.Color, response.Coords, response.RegionId); break; case ServerResponseType.FOLLOWER_REMOVEMENT: executor.RemoveFollower(response.Color, response.Coords); break; case ServerResponseType.NO_FOLLOWER_PLACEMENT: executor.NoFollowerPlacement(response.Color); break; case ServerResponseType.GAME_RESUME: case ServerResponseType.GAME_PAUSE: case ServerResponseType.GAME_HISTORY: // Skip this break; default: throw new Exception(); } any = true; } catch { throw new ArgumentException("Save file is corrupted."); } } if (!hasParams) throw new ArgumentException("Save file contains no game setting infomration."); executor.ChangeTileSetType(TileSetType.STANDARD); } return responses; }
public void Execute() { var gameExecutor = new GameExecutor(); gameExecutor.Play(new Game.GuessGame.GuessGame()); }
/// <summary> /// Asks player for move. /// </summary> /// <param name="tile">Tile to be placed.</param> /// <param name="tilePlacement">Tile placement part of the move.</param> /// <param name="followerPlacement">Follower placement part of the move.</param> public void GetMove(TileScheme tile, out PlayerRequest tilePlacement, out PlayerRequest followerPlacement) { var grid = _Executor.CurrentGameState.Grid; List <Tuple <Coords, TileOrientation> > tilePlacementPossibilities = new List <Tuple <Coords, TileOrientation> >(); foreach (var baseCoords in grid.Keys) { // Check all possible placements = places neighbouring already placed tiles foreach (var neighOr in new TileOrienationEnumerator()) { var coords = baseCoords.GetNeighbouringCoords(neighOr); // If tile is already placed, skip if (grid.ContainsKey(coords)) { continue; } // Check all orientations of the tile foreach (var orientation in new TileOrienationEnumerator()) { // If cannot be placed, skip try { GridExplorer.CheckTilePlacement(grid, tile, coords, orientation, false); tilePlacementPossibilities.Add(new Tuple <Coords, TileOrientation>(coords, orientation)); } catch (GameException) { } } } } // Select one possibility var selected = tilePlacementPossibilities[_Random.Next(tilePlacementPossibilities.Count)]; tilePlacement = new PlayerRequest() { Type = PlayerRequestType.TILE_PLACEMENT, Color = Color, Coords = selected.Item1, Orientation = selected.Item2, Scheme = tile }; // Make fake executor GameExecutor executor = new GameExecutor(); executor.CurrentGameState = _Executor.CurrentGameState.Copy(); executor.ChangeTileSetType(TileSetType.PASSIVE); executor.PlaceTile(Color, tile, selected.Item1, selected.Item2); var newState = executor.CurrentGameState; followerPlacement = new PlayerRequest() { Type = PlayerRequestType.NO_FOLLOWER_PLACEMENT, Color = Color }; // Compute best move for followers int bestFollowerMoveValue = -1; // If can place if (newState.PlacedFollowers.Where(p => p.Color == Color).Count() < newState.Params.FollowerAmount) { for (int i = 0; i < tile.RegionCount; i++) { if (!GridExplorer.IsRegionOccupied(newState.Grid, selected.Item1, i)) { int newScore = GridExplorer.GetPointsForFollower(newState.Grid, selected.Item1, i, false); if (newScore > bestFollowerMoveValue) { bestFollowerMoveValue = newScore; followerPlacement.Type = PlayerRequestType.FOLLOWER_PLACEMENT; followerPlacement.Coords = selected.Item1; followerPlacement.RegionId = i; } } } } // For all own placed folowers foreach (var fp in newState.PlacedFollowers.Where(p => p.Color == Color)) { int newScore = -1; switch (newState.Grid[fp.TileCoords].Scheme.GetRegionType(fp.RegionId)) { case RegionType.MOUNTAIN: if (GridExplorer.IsRegionClosed(newState.Grid, fp.TileCoords, fp.RegionId)) { newScore = 2; // If placing is for at least 2, it is good! (else get out of here) } else if (GridExplorer.GetPointsForFollower(newState.Grid, fp.TileCoords, fp.RegionId, false) < 2) { newScore = 1; // If there are too few points, get out of here } else { newScore = -1; // Else you can wait with this follower } break; case RegionType.SEA: case RegionType.GRASSLAND: if (GridExplorer.IsRegionClosed(newState.Grid, fp.TileCoords, fp.RegionId)) { newScore = 3; // If placing is for at least 3, it is good! (else get out of here) } else if (GridExplorer.GetPointsForFollower(newState.Grid, fp.TileCoords, fp.RegionId, false) < 2) { newScore = 1; // If there are too few points, get out of here } else { newScore = -1; } break; } if (newScore > bestFollowerMoveValue) { bestFollowerMoveValue = newScore; followerPlacement.Type = PlayerRequestType.FOLLOWER_REMOVEMENT; followerPlacement.Coords = fp.TileCoords; } } }