// Constructor - creates a table in the specified size and initializes it to be empty OthelloSlots. public GameBoard(int i_BoardSize) { m_BoardSize = i_BoardSize; m_BoardMatrix = new OthelloSlot[i_BoardSize, i_BoardSize]; for (int i = 0; i < i_BoardSize; i++) { for (int j = 0; j < i_BoardSize; j++) { m_BoardMatrix[i, j] = new OthelloSlot(i, j); } } }
// The 'Heart' of the game. If possible - set the current slot with the given players' type. public void DoStep(OthelloSlot i_SuspiciousSlot) { if (m_ActivePlayer.SlotType == r_Player1SlotType) { m_Player1NumOfCoins++; } else { m_Player2NumOfCoins++; } m_GameBoard[i_SuspiciousSlot.Row, i_SuspiciousSlot.Column].SlotType = m_ActivePlayer.SlotType; m_AllEmptySlots.Remove(i_SuspiciousSlot); updateBoard(i_SuspiciousSlot, true); changeActivePlayer(); checkActivePlayer(PossibleOthelloMoves()); if (!m_ActivePlayer.IsHuman && m_IsGameActive) { DoStep(getSlotFromComputer(PossibleOthelloMoves())); } }
// this private method helps us check if the current direction is making the slot a possible move. private bool movePerDirection( OthelloSlot i_Slot, eSlotType i_PlayerType, int i_RowMovement, int i_ColumnMovement, bool i_LastColorWasDifferent) { bool toReturn = true; int rowToCheck = i_Slot.Row + i_RowMovement; int columnToCheck = i_Slot.Column + i_ColumnMovement; if ((rowToCheck >= 0) && (rowToCheck < m_GameBoard.Size) && (columnToCheck >= 0) && (columnToCheck < m_GameBoard.Size)) { OthelloSlot slotToCheck = m_GameBoard[rowToCheck, columnToCheck]; if (slotToCheck.SlotType == eSlotType.Empty) { toReturn = false; } else if (slotToCheck.SlotType == i_PlayerType) { toReturn = i_LastColorWasDifferent; } else { toReturn = movePerDirection( m_GameBoard[rowToCheck, columnToCheck], i_PlayerType, i_RowMovement, i_ColumnMovement, true); } } else { toReturn = false; } return toReturn; }
// This important board updates the coins colors after putting a coin on th board. private int updateBoard(OthelloSlot i_FromThisSlot, bool i_ToFlipCoins) { List<OthelloSlot> slotsToFlip = new List<OthelloSlot>(); eSlotType originalType = i_ToFlipCoins ? i_FromThisSlot.SlotType : r_Player2SlotType; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if ((i != 0) || (j != 0)) { changeSlotsPerDirection(i_FromThisSlot, originalType, i, j, false, ref slotsToFlip); } } } if (i_ToFlipCoins) { foreach (OthelloSlot slotToFlip in slotsToFlip) { slotToFlip.SlotType = originalType; if (originalType == r_Player1SlotType) { m_Player1NumOfCoins++; m_Player2NumOfCoins--; } else { m_Player1NumOfCoins--; m_Player2NumOfCoins++; } } } return slotsToFlip.Count; }
// this method changes in times of need the color of the coins. private bool changeSlotsPerDirection( OthelloSlot i_FromThisSlot, eSlotType i_OriginalType, int i_RowMovement, int i_ColumnMovement, bool i_NeedToChangeSlotsType, ref List<OthelloSlot> i_SlotsToFlip) { bool toReturn = false; int rowToCheck = i_FromThisSlot.Row + i_RowMovement; int columnToCheck = i_FromThisSlot.Column + i_ColumnMovement; if ((rowToCheck >= 0) && (rowToCheck < m_GameBoard.Size) && (columnToCheck >= 0) && (columnToCheck < m_GameBoard.Size)) { OthelloSlot slotToCheck = m_GameBoard[rowToCheck, columnToCheck]; if (slotToCheck.SlotType == eSlotType.Empty) { toReturn = false; } else if (slotToCheck.SlotType == i_OriginalType) { if (i_NeedToChangeSlotsType) { toReturn = true; } } else { toReturn = changeSlotsPerDirection( m_GameBoard[rowToCheck, columnToCheck], i_OriginalType, i_RowMovement, i_ColumnMovement, true, ref i_SlotsToFlip); if (toReturn) { i_SlotsToFlip.Add(m_GameBoard[rowToCheck, columnToCheck]); } } } else { toReturn = false; } return toReturn; }