internal void ClearBorad() { LastSelectedCell = null; foreach (GameCell boardGameCell in BoardGameCells) { SetCellState(boardGameCell, Enums.eCellValue.Blank); } }
private void initializeBoardGameCells() { BoardGameCells = new GameCell[BoradSize, BoradSize]; for (int row = 0 ; row < BoradSize ; row++) { for (int column = 0 ; column < BoradSize ; column++) { BoardGameCells[row, column] = new GameCell(row, column); } } }
private void initializeBoardGameCells() { BoardGameCells = new GameCell[BoradSize, BoradSize]; for (int row = 0; row < BoradSize; row++) { for (int column = 0; column < BoradSize; column++) { BoardGameCells[row, column] = new GameCell(row, column); } } }
private bool rowEnded(GameCell i_GameCell, Enums.eCellValue i_WantedCellState) { bool result = true; for (int i = 0; i < BoradSize; i++) { if (i_GameCell != BoradGameCells[i, i_GameCell.ColumnIndex] && BoradGameCells[i, i_GameCell.ColumnIndex].Value != i_WantedCellState) { result = false; break; } } return(result); }
private GameCell playComputerMove() { bool continueSearching = true; List <GameCell> selectedUnWantedCells = new List <GameCell>(r_BoardGame.FreeCellsList.Count); GameCell selectedCell, preferredSelecteCell = null; bool thisCellEndsGameForComputer, thisCellEndsGameForPlayer; do { selectedCell = getRandomFreeCell(); thisCellEndsGameForComputer = isPlayerEndedGame(selectedCell, k_PlayerTwoValue); thisCellEndsGameForPlayer = isPlayerEndedGame(selectedCell, k_PlayerOneValue); //trying to get a cell that doesn't end the game for computer // and doesn't help the player if (thisCellEndsGameForComputer || thisCellEndsGameForPlayer) { selectedUnWantedCells.Add(selectedCell); r_BoardGame.FreeCellsList.Remove(selectedCell); //prefer to select a cell that helps the player //than one that end the game for computer if (thisCellEndsGameForPlayer) { preferredSelecteCell = selectedCell; } if (r_BoardGame.IsThereNoMoreFreeCells) { if (preferredSelecteCell != null) { selectedCell = preferredSelecteCell; } continueSearching = false; } } else { continueSearching = false; } if (!continueSearching) { r_BoardGame.FreeCellsList.AddRange(selectedUnWantedCells); } } while (continueSearching); setCellState(selectedCell); return(selectedCell); }
internal void SetCellState(GameCell i_GameCell, Enums.eCellValue i_TheState) { i_GameCell.Value = i_TheState; if (i_TheState == Enums.eCellValue.Blank) { if (!FreeCellsList.Contains(i_GameCell)) { FreeCellsList.Add(i_GameCell); } } else { LastSelectedCell = i_GameCell; FreeCellsList.Remove(i_GameCell); } }
private void computerPlayingLogic(ref bool io_RoundIsOver) { togglePlayers(); GameCell computerSelectedCell = playComputerMove(); bool isComputerLost = isPlayerEndedGame(computerSelectedCell, computerSelectedCell.Value); if (isComputerLost) { io_RoundIsOver = true; currentPlayerLose(); } else { if (r_BoardGame.IsThereNoMoreFreeCells) { setTie(out io_RoundIsOver); } else { togglePlayers(); } } }
private bool diagonalEnded(GameCell i_GameCell, Enums.eCellValue i_WantedCellState) { bool firstDiagonalIsTrue = true, secondDiagonal = true; if (i_GameCell.RowIndex == i_GameCell.ColumnIndex) { for (int i = 0; i < BoradSize; i++) { if (i_GameCell != BoradGameCells[i, i] && BoradGameCells[i, i].Value != i_WantedCellState) { firstDiagonalIsTrue = false; break; } } } else { firstDiagonalIsTrue = false; } if (i_GameCell.RowIndex + i_GameCell.ColumnIndex == BoradSize - 1) { for (int i = 0; i < BoradSize; i++) { if (i_GameCell != BoradGameCells[i, BoradSize - 1 - i] && BoradGameCells[i, BoradSize - 1 - i].Value != i_WantedCellState) { secondDiagonal = false; break; } } } else { secondDiagonal = false; } return(firstDiagonalIsTrue || secondDiagonal); }
private void setCellState(GameCell i_GameCell) { Enums.eCellValue cellState = CurrentPlayerTurn == Enums.ePlayer.PlayerOne ? k_PlayerOneValue : k_PlayerTwoValue; r_BoardGame.SetCellState(i_GameCell, cellState); }
private bool rowEnded(GameCell i_GameCell, Enums.eCellValue i_WantedCellState) { bool result = true; for (int i = 0 ; i < BoradSize ; i++) { if (i_GameCell != BoradGameCells[i, i_GameCell.ColumnIndex] && BoradGameCells[i, i_GameCell.ColumnIndex].Value != i_WantedCellState) { result = false; break; } } return result; }
private bool isPlayerEndedGame(GameCell i_GameCell, Enums.eCellValue i_WantedCellState) { return rowEnded(i_GameCell, i_WantedCellState) || columnEnded(i_GameCell, i_WantedCellState) || diagonalEnded(i_GameCell, i_WantedCellState); }
private bool diagonalEnded(GameCell i_GameCell, Enums.eCellValue i_WantedCellState) { bool firstDiagonalIsTrue = true, secondDiagonal = true; if (i_GameCell.RowIndex == i_GameCell.ColumnIndex) { for (int i = 0 ; i < BoradSize ; i++) { if (i_GameCell != BoradGameCells[i, i] && BoradGameCells[i, i].Value != i_WantedCellState) { firstDiagonalIsTrue = false; break; } } } else { firstDiagonalIsTrue = false; } if (i_GameCell.RowIndex + i_GameCell.ColumnIndex == BoradSize - 1) { for (int i = 0 ; i < BoradSize ; i++) { if (i_GameCell != BoradGameCells[i, BoradSize - 1 - i] && BoradGameCells[i, BoradSize - 1 - i].Value != i_WantedCellState) { secondDiagonal = false; break; } } } else { secondDiagonal = false; } return firstDiagonalIsTrue || secondDiagonal; }
internal void NextPlayerMove(GameCell i_GameCell, out bool o_RoundIsOver) { setCellState(i_GameCell); o_RoundIsOver = false; bool isPlayerLost = isPlayerEndedGame(i_GameCell, i_GameCell.Value); if (isPlayerLost) { currentPlayerLose(); o_RoundIsOver = true; } else { if (r_BoardGame.IsThereNoMoreFreeCells) { setTie(out o_RoundIsOver); } else { if (GameType == Enums.eGameType.PlayerVsComputer) { computerPlayingLogic(ref o_RoundIsOver); } else { togglePlayers(); } } } }