/// <summary> /// Gets the location of a specific item. /// </summary> /// <returns>The item location. NULL if the item cannot be found.</returns> /// <param name="grid">Grid.</param> /// <param name="item">Item.</param> public static GridLocation GetItemLocation(this GameFieldGrid grid, GameFieldItem item) { if(item == null) { throw new ArgumentNullException ("item", "Item is required!"); } int index = 0; foreach(var searchItem in grid) { if(searchItem.Equals(item)) { int row = index / grid.Columns; int col = index % grid.Columns; return new GridLocation (row, col); } index++; } return null; }
private static void PopulateGameState(GameState gameState, Tuple<string, string> data) { switch(data.Item1) { case "localplayerscore": gameState.LocalPlayerScore = XmlConvert.ToInt32 (data.Item2); break; case "opponentplayerscore": gameState.OpponentPlayerScore = XmlConvert.ToInt32 (data.Item2); break; case "currentplayer": gameState.CurrentPlayer = (PlayerType)Enum.Parse (typeof(PlayerType), data.Item2, true); break; case "cards": var cardData = data.Item2.Split (new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); foreach(string cardName in cardData) { var card = (TreasureType)Enum.Parse (typeof(TreasureType), cardName, true); gameState.Cards.Push (card); } break; case "field": var colData = data.Item2.Split (new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); // First number is the row index. int rowIndex = XmlConvert.ToInt32 (colData [0]); int colIndex = 0; for (int i = 1; i < colData.Length; i++) { var itemInfo = colData[i].Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); var item = new GameFieldItem (); item.Id = XmlConvert.ToInt32 (itemInfo[0]); item.Pyramid = (PyramidType)Enum.Parse (typeof(PyramidType), itemInfo [1], true); item.Treasure = (TreasureType)Enum.Parse (typeof(TreasureType), itemInfo [2], true); gameState.GameField.SetItem (rowIndex, colIndex, item); colIndex++; } break; } }
public void SetUp() { Util.RegisterServices (); this.gameState = new GameState () { GameField = new GameFieldGrid(), CurrentPlayer = PlayerType.Local, Cards = new Stack<TreasureType>(), LocalPlayerScore = 10, OpponentPlayerScore = 20 }; // Push two cards for testing. this.gameState.Cards.Push (TreasureType.Candle); this.gameState.Cards.Push (TreasureType.Computer); int id = 0; var pyramid = PyramidType.None; var treasure = TreasureType.None; for(int row = 0; row < GameFieldGrid.ROWS; row++) { for(int col = 0; col < GameFieldGrid.COLUMNS; col++) { // Hide four treasures. if(row == 2 && col == 2) { treasure = TreasureType.Candle; } else if(row == 3 && col == 3) { treasure = TreasureType.Computer; } if(row == 4 && col == 4) { treasure = TreasureType.Dog; } if(row == 5 && col == 5) { treasure = TreasureType.Glasses; } var item = new GameFieldItem() { Id = id++, Pyramid = pyramid, Treasure = treasure }; this.gameState.GameField.SetItem (row, col, item); // Alternate pyramids. if(pyramid == PyramidType.None) { pyramid = PyramidType.PyramidA; } else if(pyramid == PyramidType.PyramidA) { pyramid = PyramidType.PyramidB; } else if(pyramid == PyramidType.PyramidB) { pyramid = PyramidType.PyramidA; } } } }
public void InitGameState(GameState gameState) { var platformServices = ServiceContainer.Resolve<IPlatformServices> (); int id = 1; var pyramidType = PyramidType.PyramidA; // Distribute the treasures randomly on the board. var randomizedTreasures = DefaultGameInteractionService.GetRandomizedTreasures (); // Map from index of the treasure to the treasure. var treasureIndexes = new Dictionary<int, TreasureType>(); int row = 0; int col = 0; foreach(var treasure in randomizedTreasures) { // Get a random location within the 2x2 square. int randomRowAdd = platformServices.GetRandomNumber (0, 2); int randomColAdd = platformServices.GetRandomNumber (0, 2); // Get the index of the location. int treasureIndex = (row + randomRowAdd) * gameState.GameField.Columns + col + randomColAdd; // Remember the index and the treasure. treasureIndexes.Add (treasureIndex, treasure); // Treasures are hidden in 2x2 squares. Exactly one treasure per square. Move to next 2x2 square. col += 2; if(col >= gameState.GameField.Columns) { row += 2; col = 0; } } // Find a random spot for the initial empty item location. This may not be on top of a treasure. int emptyItemLocationIndex; do { emptyItemLocationIndex = platformServices.GetRandomNumber (0, gameState.GameField.Rows * gameState.GameField.Columns); } while(treasureIndexes.ContainsKey (emptyItemLocationIndex)); // Populate the game field. int index = 0; for(row = 0; row < gameState.GameField.Rows; row++) { for(col = 0; col < gameState.GameField.Columns; col++) { var item = new GameFieldItem () { Id = id++, Pyramid = pyramidType, Treasure = TreasureType.None }; // If we come across the initial empty location, remove the pyramid. if(index == emptyItemLocationIndex) { item.Pyramid = PyramidType.None; } // If there's a treasure, hide it. if(treasureIndexes.ContainsKey(index)) { item.Treasure = treasureIndexes[index]; } gameState.GameField.SetItem (row, col, item); // Alternate pyramids to make the field look nicer. pyramidType = pyramidType == PyramidType.PyramidA ? PyramidType.PyramidB : PyramidType.PyramidA; index++; } } // Shuffle the stack of cards to draw from. Each treasure is there three times. // First 12 score 1 each, second 12 score 2 each, third 12 score 3 each. var randomizedCards = DefaultGameInteractionService.GetRandomizedTreasures (3); gameState.Cards = new Stack<TreasureType> (randomizedCards); }