public void TestDetonate2WithAllMinesInsideFieldDetonates9Cells() { int n = 5; IGameboard board = this.GenerateGameboard(n); board[2, 2] = new Mine(MineRadius.MineRadiusTwo); board.Detonate(new Position(2, 2)); bool nineCellsAreDetonated = true; for (int i = 1; i <= 3; i++) { for (int j = 1; j < 3; j++) { if (!board[i, j].Exploded) { nineCellsAreDetonated = false; break; } } if (!nineCellsAreDetonated) { break; } } Assert.IsTrue(nineCellsAreDetonated); }
public void TestDetonate2WithAllMinesInsideFieldDoesNotDetonateUnnecessaryCells() { int n = 5; IGameboard board = this.GenerateGameboard(n); board[2, 2] = new Mine(MineRadius.MineRadiusTwo); board.Detonate(new Position(2, 2)); bool moreThanNineCellsAreDetonated = false; int i = 0; int j = 0; for (; i < n; i++) { for (; j < n; j++) { if (!((1 <= i) && (i <= 3) && (1 <= j) && (j <= 3))) { if (board[i, j].Exploded) { moreThanNineCellsAreDetonated = true; break; } } } if (moreThanNineCellsAreDetonated) { break; } } Assert.IsFalse(moreThanNineCellsAreDetonated, String.Format("Cell at {0}, {1} was detonated", i, j)); }
void CreateGameboard() { IGameboard gameboard = ServiceLocator.Instance.GetService <IGameboard>(); IGameboardVisualFactory gameboardVisualFactory = ServiceLocator.Instance.GetService <IGameboardVisualFactory>(); gameboardVisualFactory.CreateVisualForGameboard(gameboard); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { List <Player> tempPlayers = new List <Player>(); tempPlayers.Add(new Player { Index = 0, IsDead = false, IsHuman = true, Name = "player1", Points = 0 }); tempPlayers.Add(new Player { Index = 1, IsDead = true, IsHuman = true, Name = "player2", Points = 0 }); tempPlayers.Add(new Player { Index = 2, IsDead = false, IsHuman = true, Name = "player3", Points = 0 }); StateGlobals.Players = tempPlayers; PhaseHandler.CurrentPlayer = StateGlobals.Players[0]; // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); gameboard = ServiceContainer.Container.Resolve <IGameboard>(); spellboard = ServiceContainer.Container.Resolve <ISpellboard>(); gameboard.GenerateEmptyGameboard(); spellboard.GenerateEmptySpellboard(); gameEventHandler = ServiceContainer.Container.Resolve <IGameEventHandler>(); infoStringHandler = ServiceContainer.Container.Resolve <IInfoStringHandler>(); SpellsGenerator sg = new SpellsGenerator(); sg.GenerateSpells(); }
/// <summary> /// /// </summary> public override void Initialize() { base.Initialize(); m_Gameboard = FindXKParent <Gameboard>(); m_Gameboard.IsValid("GameboardComp.Gameboard"); }
public void TestDetonate1With2MinesInsideFieldDoesNotDetonateUnnecessaryCells() { int n = 5; IGameboard board = this.GenerateGameboard(n); board[0, 0] = new Mine(MineRadius.MineRadiusOne); board.Detonate(new Position(0, 0)); int i = 0; int j = 0; bool moreThanOneCellIsDetonated = false; for (; i < n; i++) { for (; j < n; j++) { if (i != 0 && j != 0 && i != 1 && j != 1 && !board[i, j].Exploded) { moreThanOneCellIsDetonated = true; break; } } if (moreThanOneCellIsDetonated) { break; } } Assert.IsFalse(moreThanOneCellIsDetonated, String.Format("Cell at {0}, {1} was detonated", i, j)); }
public int MakeMove(IGameboard gameboard) { var mySymbol = gameboard.ActiveSymbol; var rootNode = new MinimaxNode(gameboard, -1); return Minimax(rootNode, true, -1, mySymbol).Move; }
private void ChoosePlayers_Click(object sender, RoutedEventArgs e) { _playerRepo = new PlayerRepo(); _playerList = _playerRepo.ListOfPlayers(1); _gameboard = new Gameboard(_playerList, _dice); UpdateScreen(); }
public void TestDetonate3WithAllMinesInsideFieldDetonates13Cells() { int n = 7; IGameboard board = this.GenerateGameboard(n); Position position = new Position(3, 3); board[3, 3] = new Mine(MineRadius.MineRadiusThree); board.Detonate(position); bool cellsAreDetonated = true; for (int i = 2; i <= 4; i++) { for (int j = 2; j <= 4; j++) { if (!board[i, j].Exploded) { cellsAreDetonated = false; break; } } if (!cellsAreDetonated) { break; } } cellsAreDetonated = cellsAreDetonated && board[1, 3].Exploded == board[3, 5].Exploded && board[3, 5].Exploded == board[5, 3].Exploded && board[5, 3].Exploded == board[3, 1].Exploded && board[3, 1].Exploded == true; Assert.IsTrue(cellsAreDetonated); }
public Game(IGameboard gameBoard, IPlayer player) { _gameBoard = gameBoard; boardSize = _gameBoard.GetBoardSize(); _player = player; gameOver = false; }
public void TestDetonate5WithAllMinesInsideFieldDetonates25Cells() { int n = 9; IGameboard board = this.GenerateGameboard(n); Position position = new Position(4, 4); board[4, 4] = new Mine(MineRadius.MineRadiusFive); board.Detonate(position); bool cellsAreDetonated = true; for (int i = 2; i <= 6; i++) { for (int j = 2; j <= 6; j++) { if (!board[i, j].Exploded) { cellsAreDetonated = false; break; } } if (!cellsAreDetonated) { break; } } Assert.IsTrue(cellsAreDetonated); }
public override void Detonate(Position position, IGameboard gameboard) { int x = position.X; int y = position.Y; for (int i = x - 1; i <= x + 1; i++) { for (int j = y - 1; j <= y + 1; j++) { if (this.IsInsideField(gameboard, i, j)) { this.ExplodeOneMine(gameboard, i, j); } } } if (this.IsInsideField(gameboard, x - 2, y)) { this.ExplodeOneMine(gameboard, x - 2, y); } if (this.IsInsideField(gameboard, x + 2, y)) { this.ExplodeOneMine(gameboard, x + 2, y); } if (this.IsInsideField(gameboard, x, y - 2)) { this.ExplodeOneMine(gameboard, x, y - 2); } if (this.IsInsideField(gameboard, x, y + 2)) { this.ExplodeOneMine(gameboard, x, y + 2); } }
private void GenerateMinesInField(IGameboard gameboard, int minesCount) { int size = gameboard.Size; Array mineRadiusValues = Enum.GetValues(typeof(MineRadius)); int mineRadiusMaxIndex = mineRadiusValues.Length - 1; List <Position> usedPositions = new List <Position>(); while (usedPositions.Count < minesCount) { int cellX = this.rand.GetRandom(0, size - 1); int cellY = this.rand.GetRandom(0, size - 1); Position position = new Position(cellX, cellY); if (usedPositions.Contains(position)) { continue; } usedPositions.Add(position); int cellType = this.rand.GetRandom(0, mineRadiusMaxIndex); MineRadius randomRadius = (MineRadius)mineRadiusValues.GetValue(cellType); ICell currentCell = new Mine(randomRadius); gameboard[cellX, cellY] = currentCell; } }
public void TestDetonate5WithAllMinesInsideFieldDoesNotDetonateUnnecessaryCells() { int n = 9; IGameboard board = this.GenerateGameboard(n); Position position = new Position(4, 4); board[4, 4] = new Mine(MineRadius.MineRadiusFive); board.Detonate(position); bool cellsAreDetonated = false; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (!((2 <= i) && (i <= 6) && (2 <= j) && (j <= 6))) { if (board[i, j].Exploded) { cellsAreDetonated = true; break; } } } if (cellsAreDetonated) { break; } } Assert.IsFalse(cellsAreDetonated); }
public IGameboardVisual CreateVisualForGameboard(IGameboard gameboard) { DummyGameboardVisual visual = new DummyGameboardVisual(); visual.SetGameboard(gameboard); return(visual); }
// TODO: gameboards are never cleared from the storage... Memory issues possible // Sufficient for this demo, would need to be solved in case this is to be published public void RegisterBoard(IGameboard gameboard) { if (boards.ContainsKey(gameboard.GameId)) { throw new GameboardAlreadyExistsException(); } boards.Add(gameboard.GameId, gameboard); }
void Init(Building building) { this.building = building; this.gameboard = ServiceLocator.Instance.GetService <IGameboard>(); this.CreateVisual(); }
protected virtual void ExplodeOneMine(IGameboard gameboard, int x, int y) { if (this.ShouldDecreaseMines(gameboard, x, y)) { gameboard.MinesCount--; } gameboard[x, y].Explode(); }
public Game(IGameboard gameboard, ICommandPathReducer commandReducer, ICommandToCoordinateMapper commandToCoordinateMapper, IResultFormatter resultFormatter) { _gameboard = gameboard; _commandReducer = commandReducer; _commandToCoordinateMapper = commandToCoordinateMapper; _resultFormatter = resultFormatter; }
private bool CheckTilesTriplet(IGameboard board, int tileA, int tileB, int tileC) { if (!string.IsNullOrEmpty(board.Tiles[tileA]) && board.Tiles[tileA] == board.Tiles[tileB] && board.Tiles[tileB] == board.Tiles[tileC]) { SetWinner(board, board.Tiles[tileA]); SetWinningTiles(board, tileA, tileB, tileC); return true; } return false; }
private void CheckDraw(IGameboard board) { if (board.EndState == EndStates.NotFinished) { if (AreAllTilesAreFilled(board)) { board.EndState = EndStates.Draw; } } }
public void TestDetonate1With2MinesInsideFieldDetonates2Cells() { IGameboard board = this.GenerateGameboard(5); board[0, 0] = new Mine(MineRadius.MineRadiusOne); board.Detonate(new Position(0, 0)); bool twoCellsAreDetonated = board[0, 0].Exploded && board[1, 1].Exploded; Assert.IsTrue(twoCellsAreDetonated); }
public void TestGameboardOfDimention5AndPercentageOfMines20Has5Mines() { int size = 5; var fieldGenerator = new GameboardGenerator(RandomGenerator.Instance); IGameboard gameboard = fieldGenerator.Generate(size, 0.2); int numberOfMines = this.GetNumberOfMines(gameboard); bool numberOfMinesIs5 = numberOfMines == 5; Assert.IsTrue(numberOfMinesIs5, String.Format("Number of mines is {0}. Should be 5.", numberOfMines)); }
public void TestGameboardOfDimention10AndPercentageOfMines30Has30Mines() { int size = 10; var fieldGenerator = new GameboardGenerator(RandomGenerator.Instance); IGameboard gameboard = fieldGenerator.Generate(size, 0.3); int numberOfMines = this.GetNumberOfMines(gameboard); bool numberOfMinesIs30 = numberOfMines == 30; Assert.IsTrue(numberOfMinesIs30, String.Format("Number of mines is {0}. Should be 30.", numberOfMines)); }
private void GenerateEmptyField(IGameboard gameboard) { int size = gameboard.Size; for (int row = 0; row < size; row++) { for (int col = 0; col < size; col++) { gameboard[row, col] = new EmptyCell(); } } }
private bool IsBoardComplete(IGameboard board) { for (int i = 0; i < 9; i++) { if (string.IsNullOrEmpty(board.Tiles[i])) { return false; } } return true; }
/// <inheritdoc/> protected override void OnInitialized() { base.OnInitialized(); if (this.Session.SetGameBoardName(this.GameboardName, this.Session.GameboardName, this.Session.Gameboards.DefaultGameboardName)) { this.StateHasChanged(); } this.gameboard = this.Session.Gameboards.GetGameboard(this.Session.GameboardName); }
private bool AreAllTilesAreFilled(IGameboard board) { for (int i = 0; i < 9; i++) { if (string.IsNullOrEmpty(board.Tiles[i])) { return false; } } return true; }
public GameEngine(IGameController gameController, IGameboardGenerator fieldGenerator, IDetonationPatternFactory detonationFactory) { this.gameController = gameController; int size = this.gameController.GetPlaygroundSizeFromUser(); double minesPercentage = this.DetermineMinesPercentage(); this.board = fieldGenerator.Generate(size, minesPercentage); this.board.SetDetonationFactory(detonationFactory); this.blownMines = 0; }
private IList<int> GetFreeTiles(IGameboard gameboard) { var result = new List<int>(); for (int i = 0; i < gameboard.Tiles.Length; i++) { if (gameboard.Tiles[i] == string.Empty) { result.Add(i); } } return result; }
/// <summary> /// Returns true if the move is valid. /// </summary> /// <param name="field">The field to check on</param> /// <param name="x">The X position on the field</param> /// <param name="y">The Y position on the field</param> internal static bool IsValidMove(IGameboard gameboard, int x, int y) { bool isInsideField = IsInsideField(gameboard, x, y); if (isInsideField) { if (gameboard[x, y].IsMine && !gameboard[x, y].Exploded) { return true; } } return false; }
public override void Detonate(Position position, IGameboard gameboard) { for (int i = position.X - 1; i <= position.X + 1; i++) { for (int j = position.Y - 1; j <= position.Y + 1; j++) { if (this.IsInsideField(gameboard, i, j)) { this.ExplodeOneMine(gameboard, i, j); } } } }
public IGameboardVisual CreateVisualForGameboard(IGameboard gameboard) { GameObject prefab = gameboard.Config.prefab; Assert.IsNotNull(prefab); GameObject go = Object.Instantiate(prefab); GameboardVisual visual = go.GetComponent <GameboardVisual>(); visual.SetGameboard(gameboard); return(visual); }
public override void Detonate(Position position, IGameboard gameboard) { for (int i = position.X - 2; i <= position.X + 2; i++) { for (int j = position.Y - 2; j <= position.Y + 2; j++) { if (this.IsInsideField(gameboard, i, j)) { this.ExplodeOneMine(gameboard, i, j); } } } }
public override void Detonate(Position position, IGameboard gameboard) { base.Detonate(position, gameboard); for (int i = position.X - 1; i <= position.X + 1; i += 2) { for (int j = position.Y - 1; j <= position.Y + 1; j += 2) { if (this.IsInsideField(gameboard, i, j)) { this.ExplodeOneMine(gameboard, i, j); } } } }
/// <summary> /// Returns true if the move is valid. /// </summary> /// <param name="field">The field to check on</param> /// <param name="x">The X position on the field</param> /// <param name="y">The Y position on the field</param> internal static bool IsValidMove(IGameboard gameboard, int x, int y) { bool isInsideField = IsInsideField(gameboard, x, y); if (isInsideField) { if (gameboard[x, y].IsMine && !gameboard[x, y].Exploded) { return(true); } } return(false); }
public void TestDetonate1WithAllMinesInsideFieldDetonates5Cells() { IGameboard board = this.GenerateGameboard(5); board[1, 1] = new Mine(MineRadius.MineRadiusOne); board.Detonate(new Position(1, 1)); bool patternOneDetonatesFiveCells = board[1, 1].Exploded && board[0, 0].Exploded && board[0, 2].Exploded && board[2, 2].Exploded && board[2, 0].Exploded; Assert.IsTrue(patternOneDetonatesFiveCells); }
public void TestDetonate2With4MinesInsideFieldDetonates4Cells() { int n = 5; IGameboard board = this.GenerateGameboard(n); board[4, 0] = new Mine(MineRadius.MineRadiusTwo); board.Detonate(new Position(4, 0)); bool fourCellsAreDetonated = board[3, 0].Exploded && board[3, 1].Exploded && board[4, 0].Exploded && board[4, 1].Exploded; Assert.IsTrue(fourCellsAreDetonated); }
private int GetNumberOfMines(IGameboard gameboard) { int count = 0; for (int i = 0; i < gameboard.Size; i++) { for (int j = 0; j < gameboard.Size; j++) { if (gameboard[i, j].IsMine) { count++; } } } return count; }
void InitialiseServices() { IBuildingConfigurationService buildingConfigurationService = ServiceLocator.Instance.GetService <IBuildingConfigurationService>(); BuildingLibrary library = Resources.Load <BuildingLibrary>(BuildingLibrary.RESOURCE_LOCATION); buildingConfigurationService.UpdateConfiguration(library); IGameboard gameboard = ServiceLocator.Instance.GetService <IGameboard>(); GameboardConfiguration gameboardConfig = Resources.Load <GameboardConfiguration>(GameboardConfiguration.RESOURCE_LOCATION); gameboard.Initialise(gameboardConfig); IBuildingService buildingService = ServiceLocator.Instance.GetService <IBuildingService>(); buildingService.Load(); }
/// <summary> /// A Factory method. Returns a detonator depending on the mine radius. /// </summary> /// <param name="position">The position to be detonated</param> /// <param name="field">The field to which the position belongs</param> public DetonationPattern GetDetonationPattern(Position position, IGameboard gameboard) { DetonationPattern detonationPattern = null; Mine mine = (Mine)gameboard[position.X, position.Y]; switch (mine.Radius) { case MineRadius.MineRadiusOne: { detonationPattern = new RadiusOneDetonator(); break; } case MineRadius.MineRadiusTwo: { detonationPattern = new RadiusTwoDetonator(); break; } case MineRadius.MineRadiusThree: { detonationPattern = new RadiusThreeDetonator(); break; } case MineRadius.MineRadiusFour: { detonationPattern = new RadiusFourDetonator(); break; } case MineRadius.MineRadiusFive: { detonationPattern = new RadiusFiveDetonator(); break; } default: { throw new InvalidOperationException("No detonation pattern exists!"); } } return(detonationPattern); }
public void RenderPlayground(IGameboard gameboard) { int size = gameboard.Size; StringBuilder result = new StringBuilder(); result.Append(" "); for (int i = 0; i < size; i++) { result.Append(i + " "); } result.AppendLine(); result.Append(" "); for (int i = 0; i < size * 2; i++) { result.Append("-"); } result.AppendLine(); for (int i = 0; i < size; i++) { result.Append(i + " |"); for (int j = 0; j < size; j++) { if (gameboard[i, j].Exploded) { result.Append(ExplodedCellSymbol + " "); } else if (gameboard[i, j].IsMine) { int mineType = (int)((Mine)gameboard[i, j]).Radius + 1; result.Append(mineType + " "); } else { result.Append(EmptyCellSymbol + " "); } } result.AppendLine(); } this.render.WriteLine(result.ToString()); }
/// <summary> /// A Factory method. Returns a detonator depending on the mine radius. /// </summary> /// <param name="position">The position to be detonated</param> /// <param name="field">The field to which the position belongs</param> public DetonationPattern GetDetonationPattern(Position position, IGameboard gameboard) { DetonationPattern detonationPattern = null; Mine mine = (Mine)gameboard[position.X, position.Y]; switch (mine.Radius) { case MineRadius.MineRadiusOne: { detonationPattern = new RadiusOneDetonator(); break; } case MineRadius.MineRadiusTwo: { detonationPattern = new RadiusTwoDetonator(); break; } case MineRadius.MineRadiusThree: { detonationPattern = new RadiusThreeDetonator(); break; } case MineRadius.MineRadiusFour: { detonationPattern = new RadiusFourDetonator(); break; } case MineRadius.MineRadiusFive: { detonationPattern = new RadiusFiveDetonator(); break; } default: { throw new InvalidOperationException("No detonation pattern exists!"); } } return detonationPattern; }
public void CheckGameEnd(IGameboard board) { bool finished = false; for (int i = 0; i < 3; i++) { if (CheckRow(i, board) || CheckColumn(i, board)) { finished = true; break; } } if (!finished) { CheckTopLeftBottomRightDiagonal(board); CheckBottomLeftTopRightDiagonal(board); CheckDraw(board); } }
public override void Detonate(Position position, IGameboard gameboard) { for (int i = position.X - 2; i <= position.X + 2; i++) { for (int j = position.Y - 2; j <= position.Y + 2; j++) { bool ur = i == position.X - 2 && j == position.Y - 2; bool ul = i == position.X - 2 && j == position.Y + 2; bool dr = i == position.X + 2 && j == position.Y - 2; bool dl = i == position.X + 2 && j == position.Y + 2; if (ur) { continue; } if (ul) { continue; } if (dr) { continue; } if (dl) { continue; } if (this.IsInsideField(gameboard, i, j)) { this.ExplodeOneMine(gameboard, i, j); } } } }
/// <summary> /// Get coordinates from user , untill find correct position for play. /// </summary> /// <param name="gameboard"></param> /// <returns>Valid position</returns> public Position GetNextPositionForPlayFromUser(IGameboard gameboard) { while (true) { this.messenger.AskForNewCoordinates(); string userInput = this.inputReader.GetUserInput(); while (!InputValidator.IsValidInputForNextPosition(userInput)) { this.messenger.MessageForWrongCoordinates(); this.messenger.AskForNewCoordinates(); userInput = this.inputReader.GetUserInput(); } Position validPosition = this.ExtractPositionFromString(userInput); if (InputValidator.IsValidMove(gameboard, validPosition.X, validPosition.Y)) { return validPosition; } else { this.messenger.MessageForInvalidMove(); } } }
private void GenerateMinesInField(IGameboard gameboard, int minesCount) { int size = gameboard.Size; Array mineRadiusValues = Enum.GetValues(typeof(MineRadius)); int mineRadiusMaxIndex = mineRadiusValues.Length - 1; List<Position> usedPositions = new List<Position>(); while (usedPositions.Count < minesCount) { int cellX = this.rand.GetRandom(0, size - 1); int cellY = this.rand.GetRandom(0, size - 1); Position position = new Position(cellX, cellY); if (usedPositions.Contains(position)) { continue; } usedPositions.Add(position); int cellType = this.rand.GetRandom(0, mineRadiusMaxIndex); MineRadius randomRadius = (MineRadius)mineRadiusValues.GetValue(cellType); ICell currentCell = new Mine(randomRadius); gameboard[cellX, cellY] = currentCell; } }
private bool IsWinner(string symbol, IGameboard board) { return IsTripletWinning(symbol, board, 0, 1, 2) || IsTripletWinning(symbol, board, 3, 4, 5) || IsTripletWinning(symbol, board, 6, 7, 8) || IsTripletWinning(symbol, board, 0, 3, 6) || IsTripletWinning(symbol, board, 1, 4, 7) || IsTripletWinning(symbol, board, 2, 5, 8) || IsTripletWinning(symbol, board, 0, 4, 8) || IsTripletWinning(symbol, board, 2, 4, 6); }
private void CreateHardGame() { board = backend.CreateHardGame(); gameStorage.Setup(g => g.GetGameboard(GAME_ID)).Returns(board); }
private void CheckTopLeftBottomRightDiagonal(IGameboard board) { CheckTilesTriplet(board, 0, 4, 8); }
public int MakeMove(IGameboard gameboard) { var freeTiles = GetFreeTiles(gameboard); var selected = rand.Next(0, freeTiles.Count); return freeTiles[selected]; }
private void SetWinner(IGameboard board, string winnerSymbol) { board.EndState = winnerSymbol == Constants.SYMBOL_O ? EndStates.PlayerOWon : EndStates.PlayerXWon; }
private void SetWinningTiles(IGameboard board, int tileA, int tileB, int tileC) { board.WinningTiles.Add(tileA); board.WinningTiles.Add(tileB); board.WinningTiles.Add(tileC); }
private void MarkTile(int index, string gameId) { board = backend.MarkTile(index, gameId); }
private void CheckBottomLeftTopRightDiagonal(IGameboard board) { CheckTilesTriplet(board, 2, 4, 6); }
private bool CheckRow(int row, IGameboard board) { return CheckTilesTriplet(board, row * 3, row * 3 + 1, row * 3 + 2); }
private bool CheckColumn(int column, IGameboard board) { return CheckTilesTriplet(board, column % 3, column % 3 + 3, column % 3 + 6); }