/// <summary> /// Removes the sign we last placed /// </summary> public virtual bool RemoveLastSign() { if (numberOfSignsInGame < 1 || removeCount > 0) { return(false); // Only remove if we placed two signs already } // Remove sign CellHolder cellHolder = GetCellHolderAtGridPos(previousGridPos); if (!cellHolder.IsFull()) { return(false); // Return if we don't have a sign there } // At this point we surely are going to remove the sign if (SignWasRemovedEvent != null) { SignWasRemovedEvent(previousGridPos); } cellHolder.RemoveCurrentCellWithoutStoring(); // move marker, do this before changin turns because we want the color to be the exact opposite of the sign at secondToPrevious pos if (lastPlacedMarker != null) { lastPlacedMarker.MoveMarkerTo(new Vector2(secondToPreviousGridPos[0], secondToPreviousGridPos[1]), SignResourceStorage.Instance.GetColorRelatedTo(gameLogic.WhoseTurn)); } // Revert back to previous turn in gamelogic gameLogic.SetPreviousTurn(); // The order of these are very crucial // We just strated a game so we want to restart it if (numberOfSignsInGame == 1) { gameLogic.RestartCurrentGame(); previousGridPos = null; } else { previousGridPos[0] = secondToPreviousGridPos[0]; previousGridPos[1] = secondToPreviousGridPos[1]; } numberOfSignsInGame--; removeCount++; return(true); }
/// <summary> /// Returns whether someone has won the game based on the placement of a sign /// So it should be called after a sign has been placed /// </summary> /// <param name="gridPos">Where the sign has been placed</param> /// <returns>Returns BLOCKED when no one won yet</returns> public TTTGameLogic.GameWonData DidWinGame(int[] gridPos) { CellHolder currCellHolder = GetCellHolderAtGridPos(gridPos); Cell.CellOcc currCellType = currCellHolder.CurrentTemplate.cellOcc; // Used for return data TTTGameLogic.GameWonData gameWonData = new TTTGameLogic.GameWonData(); CellHolder[] winCells = new CellHolder[WIN_CONDITION]; winCells[0] = currCellHolder; // Go through directions for (int i = 0; i <= 1; i++) { for (int k = -1; k <= 1; k++) { if (!(k == 0 && i == 0) && !(i == 0 && k == 1)) // Dont want 0 0 direction or up dir { int count = 1; // Used to determine whether someone has won: if after the loop it is WIN_CONDITION someone has won // Go till we found end in this direction or founf out that someone has won for (int j = 1; j < WIN_CONDITION && count < WIN_CONDITION; j++) { CellHolder ch = GetCellHolderRelativeTo(gridPos, i * j, k * j); // ch is null // OR ch is not full // OR ch is disabled // OR cell type is not the one we have in this cell if (!(ch != null && ch.IsFull() && !ch.IsDisabled && ch.CurrentTemplate.cellOcc == currCellType)) { break; } winCells[count] = ch; count++; } // We need to go in the other direction as well for (int j = 1; j < WIN_CONDITION && count < WIN_CONDITION; j++) { CellHolder ch = GetCellHolderRelativeTo(gridPos, -i * j, -k * j); // ch is null // OR ch is not full // OR ch is disabled // OR cell type is not the one we have in this cell if (!(ch != null && ch.IsFull() && !ch.IsDisabled && ch.CurrentTemplate.cellOcc == currCellType)) { break; } winCells[count] = ch; count++; } if (count >= WIN_CONDITION) { gameWonData.gameWon = true; gameWonData.winType = currCellType; gameWonData.HoldersWithWon = winCells; return(gameWonData); } } } } gameWonData.gameWon = false; return(gameWonData); }