void Awake() { playerNumber = -1; gpManager = GameObject.Find("GameplayManager").GetComponent<GameplayManager>(); field = GetComponent<PlayingField>(); hand = new List<GameObject>(); vAxisInUse = false; hAxisInUse = false; showInfoFlag = true; selectionIndex = 0; selectionItems = new List<GameObject>(); }
// Use this for initialization void Start() { field = GetComponent<PlayingField>(); }
public GetRow_int() { _field = new PlayingField(FieldDefinition); }
public GetColumn_int() { _field = new PlayingField(FieldDefinition); }
public GetBigSquare_int() { _field = new PlayingField(FieldDefinition); }
public Constructor() { _field = new PlayingField(FieldDefinition); }
// Use this for initialization void Start() { playingField = FindObjectOfType <PlayingField>(); Debug.Log(playingField? "Found Playing field" : "Did not find playing field"); }
public PlayingField getPlayingField(int level) { List <List <Square> > tempSquareList = new List <List <Square> >(); Player player = null; Worker worker = null; string[] lines = System.IO.File.ReadAllLines($@"../../Levels/level{level}.txt"); for (int y = 0; y < lines.Length; y++) { var row = new List <Square>(); for (int x = 0; x < lines[y].Length; x++) { switch (lines[y][x]) { case '#': row.Add(new Wall()); break; case '.': row.Add(new Floor()); break; case 'o': var chest = new Floor(); chest.Content = new Chest(chest); row.Add(chest); break; case 'x': row.Add(new Destination()); break; case '@': var floor = new Floor(); player = new Player(floor); floor.Content = player; row.Add(floor); break; case '~': row.Add(new Pitfall()); break; case '$': var floor2 = new Floor(); worker = new Worker(floor2); floor2.Content = worker; row.Add(floor2); break; case ' ': var empty = new Empty(); row.Add(empty); break; } } tempSquareList.Add(row); } PlayingField playingField = new PlayingField() { Player = player, Worker = worker }; for (int i = 0; i < tempSquareList.Count; i++) { for (int j = 0; j < tempSquareList[i].Count; j++) { // TOP if (i > 0) { tempSquareList[i][j].Top = tempSquareList[i - 1][j]; } // Bottom if (i < tempSquareList.Count - 1) { tempSquareList[i][j].Bottom = tempSquareList[i + 1][j]; } // Left if (j > 0) { tempSquareList[i][j].Left = tempSquareList[i][j - 1]; } // Right if (j < tempSquareList[i].Count - 1) { tempSquareList[i][j].Right = tempSquareList[i][j + 1]; } playingField.Squares.Add(tempSquareList[i][j]); } } return(playingField); }
//private static readonly LimitedConcurrencyLevelTaskScheduler LIMITED_TASK_SCHED = new LimitedConcurrencyLevelTaskScheduler(8); //private static readonly ParallelOptions PARALLEL_OPTIONS = new ParallelOptions { MaxDegreeOfParallelism = 2, TaskScheduler = LIMITED_TASK_SCHED }; public Player(PlayingField field) { this.Field = field; }
static List <Turn> FindOptimalTurn(PlayingField field) { List <Turn> turns = new List <Turn>(1); List <Card> neededOutputCards = new List <Card>() { new Card((byte)(field[4].Value + 1), SuitEnum.RED), new Card((byte)(field[5].Value + 1), SuitEnum.BLACK), new Card((byte)(field[6].Value + 1), SuitEnum.GREEN) }; neededOutputCards.Sort(); foreach (Card neededOutputCard in neededOutputCards) { #region [TO OUTPUT FROM BUFFER] for (byte col = 0; col < PlayingField.COLUMNS_BUFFER; col++) { Card c = field[col]; if (c == neededOutputCard) { Turn turn = new Turn { FromColumn = col, FromTop = true, ToTop = true }; switch (c.Suit) { case SuitEnum.RED: turn.ToColumn = 4; break; case SuitEnum.BLACK: turn.ToColumn = 5; break; case SuitEnum.GREEN: turn.ToColumn = 6; break; } if (field.IsTurnAllowed(turn)) { turns.Add(turn); break; } } } #endregion if (turns.Count > 0) { break; } #region [TO OUTPUT FROM FIELD] for (byte col = 0; col < PlayingField.COLUMNS_FIELD; col++) { int row = field.GetColumnLength(col) - 1; if (row == -1) { continue; //<- No cards in this stack } Card c = field[col, row]; if (c == neededOutputCard || c.Suit == SuitEnum.ROSE) //<- If it is a card, which can go to the output { Turn turn = new Turn { FromColumn = col, FromRow = (byte)row, FromTop = false, ToTop = true }; switch (c.Suit) { case SuitEnum.ROSE: turn.ToColumn = 3; break; case SuitEnum.RED: turn.ToColumn = 4; break; case SuitEnum.BLACK: turn.ToColumn = 5; break; case SuitEnum.GREEN: turn.ToColumn = 6; break; } if (field.IsTurnAllowed(turn)) { turns.Add(turn); break; } } } #endregion if (turns.Count > 0) { break; } } if (turns.Count == 0) { #region [MERGE DRAGONS] SuitEnum[] mergableDragons = field.FindMergableDragons(); foreach (SuitEnum suit in mergableDragons) { turns.Add(new Turn { MergeDragons = suit }); break; } #endregion } return(turns); }
static List <Turn> FindOtherTurns(PlayingField field) { List <Turn> turns = new List <Turn>(); int[] rows = new int[PlayingField.COLUMNS_FIELD]; for (byte col = 0; col < PlayingField.COLUMNS_FIELD; col++) { rows[col] = field.GetColumnLength(col); } #region [FROM FIELD TO FIELD / BUFFER] foreach (Card refCard in MOVEMENT_PRIORITY) { bool transferedToEmpty = false; //<- We only want to transfer to an empty slot once, since the outcomes are the same for (byte colFrom = 0; colFrom < PlayingField.COLUMNS_FIELD; colFrom++) { for (byte rowFrom = 0; rowFrom < rows[colFrom]; rowFrom++) { Card c = field[colFrom, rowFrom]; if (c != refCard || !field.IsMovable(colFrom, rowFrom)) { continue; } Turn t = new Turn { FromColumn = colFrom, FromRow = rowFrom, FromTop = false, ToTop = false }; #region [TO FIELD] for (byte colTo = 0; colTo < PlayingField.COLUMNS_FIELD; colTo++) { if (colFrom == colTo) { continue; } t.ToColumn = colTo; if (field.IsTurnAllowed(t)) { if (c.Value == 0 || (rowFrom > 0 && field[colFrom, rowFrom - 1].Value == 0)) { turns.Add(t); } else if (rows[colTo] == 0) { if (!transferedToEmpty) { turns.Add(t); transferedToEmpty = true; } } else { turns.Add(t); } } } #endregion #region [TO BUFFER] t.ToTop = true; for (byte colTo = 0; colTo < PlayingField.COLUMNS_BUFFER; colTo++) { if (field[colTo].Suit != SuitEnum.EMPTY) { continue; } t.ToColumn = colTo; if (field.IsTurnAllowed(t)) { turns.Add(t); break; //<- It is enough if it works once } } #endregion if (c.Value > 0) { break; //<- There is only one of each number card } } } } #endregion #region [FROM BUFFER TO FIELD] for (byte colFrom = 0; colFrom < PlayingField.COLUMNS_BUFFER; colFrom++) { bool transferedToEmpty = false; //<- We only want to transfer to an empty slot once, since the outcomes are the same Turn t = new Turn { FromColumn = colFrom, FromTop = true, ToTop = false }; for (byte colTo = 0; colTo < PlayingField.COLUMNS_FIELD; colTo++) { t.ToColumn = colTo; if (field.IsTurnAllowed(t)) { if (field[colFrom].Value > 0) { if (rows[colTo] == 0) { if (!transferedToEmpty) { turns.Add(t); //<- Only enqueue, if we haven't yet put the card onto an empty slot transferedToEmpty = true; } } else { turns.Add(t); } } else { turns.Add(t); break; //<- Dragons can only be put in empty columns. Since we found one, we don't need to look for more since they have the same outcome } } } } #endregion return(turns); }
/// <summary> /// Tries to find a <see cref="Turn"/> stack cards together without breaking existing stacks or exposing dragons /// </summary> /// <param name="field"></param> /// <returns></returns> static List <Turn> FindStackingTurn(PlayingField field) { List <Turn> turns = new List <Turn>(1); Card[] neededOutputCards = { new Card((byte)(field[4].Value + 1), SuitEnum.RED), new Card((byte)(field[5].Value + 1), SuitEnum.BLACK), new Card((byte)(field[6].Value + 1), SuitEnum.GREEN) }; int[] rows = new int[PlayingField.COLUMNS_FIELD]; for (byte col = 0; col < PlayingField.COLUMNS_FIELD; col++) { rows[col] = field.GetColumnLength(col); } #region [TRY STACKING WITHOUT EXPOSING DRAGONS] byte foundTurnValue = 0; for (byte refValue = 9; refValue > foundTurnValue; refValue--) //<- Try to move the highest cards possible { for (byte colFrom = 0; colFrom < PlayingField.COLUMNS_FIELD; colFrom++) { for (byte rowFrom = 0; rowFrom < rows[colFrom]; rowFrom++) { if (field[colFrom, rowFrom].Value != refValue || !field.IsMovable(colFrom, rowFrom)) { continue; } bool routedToEmpty = false; Turn t = new Turn { FromColumn = colFrom, FromRow = rowFrom, FromTop = false, ToTop = false }; for (byte colTo = 0; colTo < PlayingField.COLUMNS_FIELD; colTo++) //<- Search for an output stack { if (colTo == colFrom) { continue; } t.ToColumn = colTo; if (rowFrom == 0) //<- If the card is on the bottom { if (rows[colTo] > 0 && field.IsTurnAllowed(t)) //<- AND if the card should be moved to a stack, that is not empty { turns.Add(t); foundTurnValue = refValue; } } else { Card cardBehind = field[colFrom, rowFrom - 1]; bool allowedToMove = cardBehind.Value > 0; //<- Make sure we are not exposing a dragon allowedToMove = allowedToMove && !(rows[colTo] == 0 && routedToEmpty); //<- We don't want to route to an empty spot twice with the same card allowedToMove = allowedToMove && (!field.IsMovable(colFrom, rowFrom - 1) || neededOutputCards.Contains(cardBehind)); //<- Make sure we are not breaking stacks. The card behind the proposed one may not be movable OR the card is needed on an output stack. allowedToMove = allowedToMove && field.IsTurnAllowed(t); //<- Make sure the turn is allowed at all if (allowedToMove) { turns.Add(t); foundTurnValue = refValue; routedToEmpty |= rows[colTo] == 0; //<- Set the routed to empty flag } } t.ToColumn = colTo; } } } } #endregion return(turns); }
public void WhenNewFieldIsCreatedOpenCellsCounterShouldBeZero() { PlayingField sampleField = new PlayingField(3, 4, 1); Assert.AreEqual(0, sampleField.OpenCellsCounter); }
private PlayingField CreateSut(IMineField mineField) { var field = new PlayingField(mineField); return(field); }
/// <summary> /// Method which takes care of anything the user inputs on the console during a minesweeper game. /// </summary> /// <param name="playingField">Method gets the current playing field.</param> public void HandleInput(PlayingField playingField) { int row; int col; int fieldRows = playingField.Field.GetLength(0); int fieldCows = playingField.Field.GetLength(1); bool isValid = false; while (!isValid) { Console.Write(EnterCordinates); string inputCommand = Console.ReadLine(); this.InputCoordinates = inputCommand.Split(' '); if (this.InputCoordinates.Length == 1) { switch (this.InputCoordinates[0]) { case "restart": Game.Instance().RestartGame(); isValid = true; break; case "exit": Game.Instance().ChangeToGameOver(); isValid = true; break; case "undo": List <Coordinates> lastTurnCells = Game.Instance().Memento.GetLastCells(); if (lastTurnCells == null) { Console.Beep(700, 400); Console.Beep(700, 400); } else { int howManyFieldsOpenedLast = 0; foreach (var cell in lastTurnCells) { if (playingField.Field[cell.Row, cell.Col].Status == CellStatus.Closed) { playingField.Field[cell.Row, cell.Col].Status = CellStatus.Flagged; Game.Instance().NumberOfFlags += 1; } else if (playingField.Field[cell.Row, cell.Col].Status == CellStatus.Opened) { playingField.Field[cell.Row, cell.Col].Status = CellStatus.Closed; howManyFieldsOpenedLast++; } else { playingField.Field[cell.Row, cell.Col].Status = CellStatus.Closed; Game.Instance().NumberOfFlags -= 1; } } Game.Instance().Memento.RemoveCells(); playingField.ReduceScore(howManyFieldsOpenedLast); } isValid = true; break; default: Console.WriteLine(InvalidCoordinatesText); break; } } else { bool isValidIntRow = int.TryParse(this.InputCoordinates[0], out row) && row < fieldRows && row >= 0; bool isValidIntCol = int.TryParse(this.InputCoordinates[1], out col) && col < fieldCows && col >= 0; if (isValidIntRow && isValidIntCol && this.InputCoordinates.Length > 2) { if (this.InputCoordinates[2].ToLower() == "f") { playingField.SetFlag(row, col); break; } else if (this.InputCoordinates[2].ToLower() == "r") { playingField.RemoveFlag(row, col); break; } else { Console.WriteLine(InvalidCoordinatesText); } } else if (isValidIntRow && isValidIntCol) { playingField.OpenCell(row, col); break; } else { Console.WriteLine(InvalidCoordinatesText); } } } }