private String formatCellState(CellState cellState) { if (cellState.Equals(aliveState)) { return("1"); } else if (cellState.Equals(deadState)) { return("0"); } throw new SystemException("Internal error..Invalid Cell State for an Elementary Cell: " + cellState.ToString()); }
public void ShouldEqualWithSameCellState() { CellState initial = CellState.Alive; bool equals = initial.Equals(CellState.Alive); Assert.True(equals); }
public void ShouldNotEqualWithDifferentCellStates() { CellState initial = CellState.Alive; bool equals = initial.Equals(CellState.Dead); Assert.False(equals); }
public void ShouldNotEqualsNull() { CellState initial = CellState.Alive; bool equals = initial.Equals(null); Assert.False(equals); }
public int SumOfAliveCells() { int total = 0; CellState cell = CellState.Alive; for (int i = 0; i < gridWidth; i++) { for (int j = 0; j < gridHeight; j++) { if (cell.Equals('1')) { total += 1; } } } return(total); }
// Computer behaviour, CellValue private void DoComputerTurnCellValue() { // Create an array of int with the same dimensions as Gameboard, and set each element to 0 int[,] cellValues = new int[Gameboard.GetLength(0), Gameboard.GetLength(1)]; for (int iCell = 0; iCell < cellValues.GetLength(0); iCell++) { for (int jCell = 0; jCell < cellValues.GetLength(1); jCell++) { cellValues[iCell, jCell] = 0; } } // Value of cells if the row is completely empty of symbols int emptyValue = 1; // Value of cells if the row contains one ComputerSymbol int oneValue = 3; // Value of cells if the row contains two ComputerSymbols, is always larger than the maximum value a cell can reach with just emptyValue & oneValue, meaning this cell gets absolute priority int twoValue = ((emptyValue + oneValue) * PossibleRows.GetLength(0)) + 1; // Value of cells if the row contains two PlayerSymbols, almost the same as twoValue but the resulting cellValue should always be lower than twoValue (except when a single cell about to complete more than one row) int counterValue = (emptyValue + oneValue) * PossibleRows.GetLength(0); // Go over PossibleRows to see which rows contain only Empty cells or cells with ComputerSymbol, if so add a certain value, one of the above, to that cell in cellValues for (int i = 0; i < PossibleRows.GetLength(0); i++) { CellState cell0 = Gameboard[PossibleRows[i, 0].M, PossibleRows[i, 0].N]; CellState cell1 = Gameboard[PossibleRows[i, 1].M, PossibleRows[i, 1].N]; CellState cell2 = Gameboard[PossibleRows[i, 2].M, PossibleRows[i, 2].N]; // If none of the cells contain the PlayerSymbol if (!cell0.Equals(PlayerSymbol) && !cell1.Equals(PlayerSymbol) && !cell2.Equals(PlayerSymbol)) { // Add all values of the CellStates together and adjust for the current ComputerSymbol (1 if X, 2 if O), // Resulting in the number of times the ComputerSymbols is present in that row int totalRowValue = (int)cell0 + (int)cell1 + (int)cell2; totalRowValue = totalRowValue / (int)ComputerSymbol; switch (totalRowValue) { // If the row contains no ComputerSymbol, ie it is empty, add emptyValue to all cells of that row case (0): { cellValues[PossibleRows[i, 0].M, PossibleRows[i, 0].N] += emptyValue; cellValues[PossibleRows[i, 1].M, PossibleRows[i, 1].N] += emptyValue; cellValues[PossibleRows[i, 2].M, PossibleRows[i, 2].N] += emptyValue; break; } // If the row contains one ComputerSymbol, add oneValue to all the empty cells in that row case (1): { cellValues[PossibleRows[i, 0].M, PossibleRows[i, 0].N] += (cell0 == CellState.Empty) ? oneValue : 0; cellValues[PossibleRows[i, 1].M, PossibleRows[i, 1].N] += (cell1 == CellState.Empty) ? oneValue : 0; cellValues[PossibleRows[i, 2].M, PossibleRows[i, 2].N] += (cell2 == CellState.Empty) ? oneValue : 0; break; } // If the row contains two ComputerSymbols, add twoValue to the empty cell case (2): { cellValues[PossibleRows[i, 0].M, PossibleRows[i, 0].N] += (cell0 == CellState.Empty) ? twoValue : 0; cellValues[PossibleRows[i, 1].M, PossibleRows[i, 1].N] += (cell1 == CellState.Empty) ? twoValue : 0; cellValues[PossibleRows[i, 2].M, PossibleRows[i, 2].N] += (cell2 == CellState.Empty) ? twoValue : 0; break; } // Something went wrong, log a message default: { Debug.WriteLine(string.Format("TicTacToe: DoComputerTurn CellValue incorrectly calculates the number of ComputerSymbols in a row.")); break; } } } else { // Check if the row contains two PlayerSymbols, meaning the player is about to win if (((int)cell0 + (int)cell1 + (int)cell2) / (int)PlayerSymbol == 2) { // If the cell is not empty nor already equal to counterValue (meaning it has already been adjusted for the fact the player is about to win, preventing adding counterValue more then once, and thereby the possibility of the computer prioritising countering the player instead of winning itself) if (cell0.Equals(CellState.Empty) && cellValues[PossibleRows[i, 0].M, PossibleRows[i, 0].N] != counterValue) { cellValues[PossibleRows[i, 0].M, PossibleRows[i, 0].N] += counterValue; } else if (cell1.Equals(CellState.Empty) && cellValues[PossibleRows[i, 1].M, PossibleRows[i, 1].N] != counterValue) { cellValues[PossibleRows[i, 1].M, PossibleRows[i, 1].N] += counterValue; } else if (cell2.Equals(CellState.Empty) && cellValues[PossibleRows[i, 1].M, PossibleRows[i, 1].N] != counterValue) { cellValues[PossibleRows[i, 2].M, PossibleRows[i, 2].N] += counterValue; } else { Debug.WriteLine(string.Format("TicTacToe: CellValues detected a row with two PlayerSymbols, but could not find the empty cell")); } } } // Should we log the cellValue values every loop, so each time a row in PossibleRows was analysed bool logEveryLoop = false; if (logEveryLoop) { Debug.WriteLine(string.Format("TicTacToe: CellValues loop {0}", i.ToString())); Debug.WriteLine(string.Format("TicTacToe: CellValues {0}, {1}, {2}", cellValues[0, 0].ToString(), cellValues[0, 1].ToString(), cellValues[0, 2].ToString())); Debug.WriteLine(string.Format("TicTacToe: CellValues {0}, {1}, {2}", cellValues[1, 0].ToString(), cellValues[1, 1].ToString(), cellValues[1, 2].ToString())); Debug.WriteLine(string.Format("TicTacToe: CellValues {0}, {1}, {2}", cellValues[2, 0].ToString(), cellValues[2, 1].ToString(), cellValues[2, 2].ToString())); } } // Should we log the cellValue values when all rows of PossibleRows were looked at bool logFinalCellValues = true; if (logFinalCellValues) { Debug.WriteLine(string.Format("TicTacToe: final CellValues are;")); Debug.WriteLine(string.Format("TicTacToe: CellValues {0}, {1}, {2}", cellValues[0, 0].ToString(), cellValues[0, 1].ToString(), cellValues[0, 2].ToString())); Debug.WriteLine(string.Format("TicTacToe: CellValues {0}, {1}, {2}", cellValues[1, 0].ToString(), cellValues[1, 1].ToString(), cellValues[1, 2].ToString())); Debug.WriteLine(string.Format("TicTacToe: CellValues {0}, {1}, {2}", cellValues[2, 0].ToString(), cellValues[2, 1].ToString(), cellValues[2, 2].ToString())); } // Find the highest value in cellValues int highestCellValue = 0; foreach (int cellValue in cellValues) { if (cellValue > highestCellValue) { highestCellValue = cellValue; } } // Go over cellValues again, adding the coordinates of every cell that matches the highestCellValue to a List List <CellCoor> highestCells = new List <CellCoor>(); for (int iHighestValue = 0; iHighestValue < cellValues.GetLength(0); iHighestValue++) { for (int jHighestValue = 0; jHighestValue < cellValues.GetLength(1); jHighestValue++) { if (cellValues[iHighestValue, jHighestValue] == highestCellValue) { highestCells.Add(new CellCoor(iHighestValue, jHighestValue)); } } } // The cell to play CellCoor cellToPlay = new CellCoor(); // If highestCells contains more than one element look if any of the cells would hinder the player from continuing one of its rows if (highestCells.Count > 1) { Debug.WriteLine("CellValue looking for counterPlayer cells"); // New list of possible cells to play List <CellCoor> highestCells2 = new List <CellCoor>(); foreach (CellCoor highCell in highestCells) { for (int iCounterPlayer = 0; iCounterPlayer < PossibleRows.GetLength(0); iCounterPlayer++) { CellCoor antiCell0 = PossibleRows[iCounterPlayer, 0]; CellCoor antiCell1 = PossibleRows[iCounterPlayer, 1]; CellCoor antiCell2 = PossibleRows[iCounterPlayer, 2]; // If the current cell of highestCells is present in a row of PossibleRows if (highCell.Equals(antiCell0) || highCell.Equals(antiCell1) || highCell.Equals(antiCell2)) { // If the row contains a PlayerSymbol, add it to the new list if (Gameboard[antiCell0.M, antiCell0.N].Equals(PlayerSymbol) || Gameboard[antiCell1.M, antiCell1.N].Equals(PlayerSymbol) || Gameboard[antiCell2.M, antiCell2.N].Equals(PlayerSymbol)) { highestCells2.Add(highCell); Debug.WriteLine(string.Format("CellValue found a counterPlayer cell at ({0}, {1})", highCell.M, highCell.N)); } } } } // If any cells were found and added to the new list, set the origional list to the new if (highestCells2.Count > 0) { highestCells = new List <CellCoor>(highestCells2); } } // Pick a cell from highestCells, at random since all cells in the List are equally valuable cellToPlay = highestCells[Rand.Next(0, highestCells.Count)]; Debug.WriteLine(string.Format("TicTacToe: DoComputerTurnCellValue() wants to play ({0}, {1}), out of {2} choices", cellToPlay.M, cellToPlay.N, highestCells.Count)); // Play the selected cell UpdateGameboardCell(cellToPlay, ComputerSymbol); }
public int SumOfAliveCells() { int total = 0; CellState cell = CellState.Alive; for (int i = 0; i < gridWidth; i++) { for (int j = 0; j < gridHeight; j++) { if (cell.Equals('1')) { total += 1; } } } return(total); //grH = gridHeight; //grW = gridWidth; ////List<int>[] lst = new List<int>[CellState.Dead]; //int activeCells = (int)CellState.Alive; //int total = grH + grW; //List[] lst = new List[gridWidth, gridHeight]; //foreach (var item in LifeGrid) //{ //} //Console.WriteLine($"total : {total}"); //Dictionary<int, LifeGrid> customers = new Dictionary<int, LifeGrid>(); //foreach (var item in gridWidth) //{ // Console.WriteLine("active cellss:"); //} //int total = 0; //for (int i = 0; i < gridWidth; i++) //{ // if (i == customers.ce) // total += activeCells; //} //return total; //return activeCells; //foreach (var item in LifeGrid) //{ //} //foreach (CellState cells in CellState.GetValues(CellState.Dead)) //{ // if // total += (int)cells; //} // for (int i = 0; i < gridHeight; i++) // for (int j = 0; j < gridWidth; j++) // { // CurrentState[i, j] = CellState.Dead; // total += activeCells; // } // return total; //} //foreach (var state in currentState) //{ // output.Append(state == CellState.Alive ? "1" : "."); // x++; // if (x >= rowLength) // { // x = 0; // output.AppendLine(); // } }
private void Game_OnNotify(NotifyReason reason, CellState[] cells) { switch (reason) { // game setup case NotifyReason.PiecePlaced: if (cells == null || cells.Length != 1) { throw new Exception("Invalid cells sent to notify"); } AddOverlay(string.Format("Placed a {0}", cells[0].Piece.ToString().Replace("_", ""))); break; case NotifyReason.InvalidCellSelected: AddOverlay("Cannot place a piece on this cell, try again"); break; // choose an opponent's piece to win the battle case NotifyReason.PickOpponent: OHighlightAwayPieces = true; AddOverlay("Choose an opponent's piece. If you guess correctly you win the battle."); break; // battle case NotifyReason.BattleLost: case NotifyReason.BattleWon: case NotifyReason.BattleTied: if (cells == null || cells.Length != 2) { throw new Exception("Invalid cells sent to notify"); } // track human or opponenet move if (cells[0].Player == Player.Red) { OPlayerMoveFrom = cells[0]; } else { OOpponentMoveFrom = cells[0]; } // clear the battles if human if (cells[0].Player == Player.Red) { OBattles.Clear(); } // indicate that a cell should be marked for a battle OBattles.Add(new Tuple <bool, CellState>(reason == NotifyReason.BattleWon, cells[1])); // turn off highlighting if needed to guess OHighlightAwayPieces = false; // update the UI ForceVisible.Add(cells[1]); GetCellAndUpdate(cells[1].Row, cells[1].Col); // update, but do not change the current text AddOverlay(OPreviousText); break; // piece moved case NotifyReason.PieceMove: if (cells == null || cells.Length != 2) { throw new Exception("Invalid cells sent to notify"); } // track human or opponenet move if (cells[0].Player == Player.Red) { OPlayerMoveFrom = cells[0]; } else { OOpponentMoveFrom = cells[0]; } // clear the battles if human if (cells[0].Player == Player.Red) { // clear OBattles.Clear(); // update the force visible cells var update = ForceVisible.ToArray(); ForceVisible.Clear(); foreach (var cell in update) { GetCellAndUpdate(cell.Row, cell.Col); } } else { // check if one of the force visible cells has moved CellState update = default(CellState); foreach (var cell in ForceVisible) { if (cell.Row == cells[0].Row && cell.Col == cells[0].Col) { update = cell; } } if (!update.Equals(default(CellState))) { // found it, so delete and move it ForceVisible.Remove(update); ForceVisible.Add(cells[1]); } } // update, but do not change the current text AddOverlay(OPreviousText); break; // indicate that it is time to pick a piece to play case NotifyReason.YourTurn: // todo AddOverlay("It is your turn"); break; case NotifyReason.InvalidMove: AddOverlay("That move is invalid. Choose another piece."); break; // check for the win case NotifyReason.StaleMate: AddOverlay("Stale mate. No winner."); break; case NotifyReason.PlayerWins: AddOverlay("You win!"); break; case NotifyReason.OpponentWins: AddOverlay("Opponent wins."); break; case NotifyReason.GameOver: AddOverlay(OPreviousText); break; // nothing case NotifyReason.AllPiecesAreInPlay: case NotifyReason.CorrectlyGuessedPiece: case NotifyReason.IncorrectlyGuessedPiece: case NotifyReason.ChooseOpponent: case NotifyReason.PlayerPiecesSet: case NotifyReason.TheirTurn: break; default: throw new Exception("Inavlid Notify reason : " + reason); } }
public Boolean matches(CellState currentLeftCellState, CellState currentState, CellState currentRightCellState) { return(currentLeftCellState.Equals(leftCellState) && currentState.Equals(middleCellState) && currentRightCellState.Equals(rightCellState)); }