Inheritance: IGameState
Exemplo n.º 1
0
        public MapScreen(GameState initial)
        {
            latestGameState = initial;

            for (int i = 0; i < board.Length; i++)
            {
                board[i] = new GUITile[Board.GetRowLength(i)];

                for (int j = 0; j < board[i].Length; j++)
                {
                    bool omit = OmittedTile(GetTerrainIndex(i, j));
                    GUITile tile = new GUITile(j, i, latestGameState.Board.GetTile(i, j), omit);
                    AddDrawableComponent(tile);
                    board[i][j] = tile;
                }
            }

            //Entire Screen size: GraphicsAdapter.DefaultAdapter.CurrentDisplayMode

            const int logWidth = 350;

            //int gameH = graphics.Viewport.Height;
            //int gameW = graphics.Viewport.Width;
            const int screenWidth = 1280;
            const int screenHeight = 720;

            //Debug.WriteLine(string.Format("width {0}, height {1}", gameW, gameH));

            gamelog = new GUILogList<GUIBufferTextBlock>(new Vector2((screenWidth-logWidth)/TXAGame.SCALE, 1/TXAGame.SCALE),screenHeight-2, logWidth-1);

            //AddButton(new TXAButton(new Vector2(750 / TXAGame.SCALE, 50 / TXAGame.SCALE), "Debug Log"), InsertText);

            AddDrawableComponent(gamelog);

            robber = new GUIRobber(GetRobberPos());

            AddDrawableComponent(robber);

            latestGameState.GetLatestEvents(int.MaxValue).Skip(gamelog.Count).ForEach(a => InsertLogEvent(a.ToString()));
            //lastLogPoll = DateTime.Now;

            //Test Roads and pieces
            UpdateGameState(initial);
        }
Exemplo n.º 2
0
        public void UpdateGameState(GameState state)
        {
            Console.WriteLine("UpdateGameState called");

            latestGameState = state;

            #region Roads
            Dictionary<Edge, int> allRoads = latestGameState.Board.GetAllRoads();

            foreach (KeyValuePair<Edge, int> road in allRoads)
            {
                int tile1 = road.Key.FirstTile;
                int tile2 = road.Key.SecondTile;

                if (roads.Exists(r => r.Tile1 == tile1 && r.Tile2 == tile2))
                {
                    continue;
                }

                Edge t1Coord = GetTerrainCoords(tile1);
                Edge t2Coord = GetTerrainCoords(tile2);

                Vector2 diffVector = board[t2Coord.FirstTile][t2Coord.SecondTile].Position / TXAGame.SCALE -
                                     board[t1Coord.FirstTile][t1Coord.SecondTile].Position / TXAGame.SCALE;

                Vector2 placeVector = (board[t1Coord.FirstTile][t1Coord.SecondTile].Position/TXAGame.SCALE)+(diffVector/2);

                float rotation = 0;

                const float value = (float) (Math.PI/3);

                if (diffVector.X < 0)
                {
                    rotation = value*2;
                }
                else if (diffVector.X < diffVector.Y)
                {
                    rotation = value;
                }

                GUIRoad newRoad = new GUIRoad(placeVector,rotation,road.Value, tile1, tile2);
                newRoad.Visible = true;

                AddDrawableComponent(newRoad);

            }
            #endregion

            #region Pieces
            Dictionary<Intersection, Piece> piecelist = state.Board.GetAllPieces();

            foreach (KeyValuePair<Intersection, Piece> piece in piecelist)
            {
                int t1 = piece.Key.FirstTile;
                int t2 = piece.Key.SecondTile;
                int t3 = piece.Key.ThirdTile;

                GUIPiece alreadyPiece = pieces.FirstOrDefault(e => e.Tile1 == t1 && e.Tile2 == t2 && e.Tile3 == t3);

                if (alreadyPiece != null)
                {
                    if (alreadyPiece.Type == Token.Settlement && piece.Value.Token == Token.City)
                    {
                        alreadyPiece.Type = Token.City;
                    }
                    continue;
                }

                Vector2 diffVector = t1 + 1 == t2
                                         ? new Vector2(GUITile.TileWidth()/2, GUITile.TileHeight()/4)
                                         : new Vector2(0, GUITile.TileHeight()/2);

                Edge t1C = GetTerrainCoords(t1);

                Vector2 placePos = board[t1C.FirstTile][t1C.SecondTile].Position/TXAGame.SCALE + diffVector;

                GUIPiece newPiece = new GUIPiece(placePos, piece.Value.Player, piece.Value.Token, t1, t2, t3);
                newPiece.Visible = true;

                pieces.Add(newPiece);

                AddDrawableComponent(newPiece);
            }
            #endregion

            #region Robber
            robber.UpdateRobberPosition(GetRobberPos());
            #endregion

            #region GameLog

            latestGameState.GetLatestEvents(int.MaxValue).Skip(gamelog.Count).ForEach(a => InsertLogEvent(a.ToString()));
            //Console.WriteLine(events.Count);
            //events.ForEach(a => InsertLogEvent(a.ToString()));
            //lastLogPoll = DateTime.Now;
            #endregion
        }
Exemplo n.º 3
0
 public void NewGameState(GameState state)
 {
     //assert CurrentScreen is always MapScreen
     MapScreen ms = screenManager.CurrentScreen as MapScreen;
     ms.UpdateGameState(state);
 }
Exemplo n.º 4
0
 public GUIControl(GameState st)
 {
     state = st;
 }
Exemplo n.º 5
0
        /// <summary>
        /// Let a player move the robber to a new location and draw a random card from a player with a building on the tile
        /// If the agent moves the robber to a water tile or the tile that it was already on, nothing will happen
        /// If the agent tries to draw a card from a unaffected player, no cards will be drawn
        /// </summary>
        /// <param name="player">The player that must move the robber</param>
        /// <param name="gameState">The gamestate to send to the player</param>
        private void MoveRobber(Player player, GameState gameState)
        {
            int robberPosition = player.Agent.MoveRobber(gameState);
            if (board.GetTile(robberPosition).Terrain == Terrain.Water || robberPosition == board.GetRobberLocation())
            {
                Console.WriteLine("IAgent " + player.Agent.GetType().Name + " moved robber illegally");
                throw new AgentActionException("Agent " + player.Agent.GetType().Name + " moved robber illegally", true);
            }
            board = board.MoveRobber(robberPosition);

            Log(new MoveRobberLogEvent(player.Id, robberPosition));
            //Draw a card from an opponent
            var opponents = new List<int>();
            foreach (var piece in board.GetPieces(robberPosition))
            {
                if (piece.Player == player.Id) continue;
                if (opponents.Contains(piece.Player)) continue;
                opponents.Add(piece.Player);
            }
            if (opponents.Count == 0) return; //No opponents to draw from
            int choice = player.Agent.ChoosePlayerToDrawFrom(CurrentGamestate(), opponents.ToArray());

            if (!opponents.Contains(choice))
            {
                Console.WriteLine("IAgent " + player.Agent.GetType().Name + " chose an illegal player to draw from");
                return;
            }

            if (players[choice].Resources.Count == 0) return; //Nothing to take

            Log(new StealCardLogEvent(player.Id, choice));

            //Move a card from one player to another
            var position = shuffleRandom.Next(players[choice].Resources.Count);
            var toMove = players[choice].Resources[position];
            players[choice].Resources.RemoveAt(position);
            player.Resources.Add(toMove);
        }
Exemplo n.º 6
0
 private GameState CurrentGamestate(int playerId)
 {
     GameState gs = new GameState(board, developmentCardStack, resourceBank, players, playerId, log, longestRoadId, largestArmyId);
     if (visual)
     {
         gui.NewGameState(gs);
     }
     return gs;
 }