/// <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); }
/// <summary> /// It disables all of the sign which belong to this game /// </summary> /// <param name="wonData">More data like cellgrid of current game pass in a wonData</param> public TTTGameLogic.GameWonData StopCurrentGame(TTTGameLogic.GameWonData wonData) { // We need to start a bejaras from the last placed sign's pos, which we happen to store in previousGridPos // Then set all of the found sign's cellholder to disabled // Disable marker if (lastPlacedMarker != null) { lastPlacedMarker.Disable(); } // Ended game so reset the amount of cells in game numberOfSignsInGame = 0; // Store all of the cellholders which have signs in game List <CellHolder> listOfCells = new List <CellHolder>(); listOfCells.Add(GetCellHolderAtGridPos(previousGridPos)); Queue <CellHolder> queue = new Queue <CellHolder>(); queue.Enqueue(listOfCells[0]); // Stores the min and max values of x and y (essentially bottomleft and topright corner points) int[] min = new int[] { int.MaxValue, int.MaxValue }; int[] max = new int[] { int.MinValue, int.MinValue }; // Go through cells in this game while (queue.Count > 0) { CellHolder currCH = queue.Dequeue(); if (currCH.IsDisabled) { break; } // Store the min and max of x and y we examine if (currCH.WorldPos[0] < min[0]) { min[0] = currCH.WorldPos[0]; } if (currCH.WorldPos[0] > max[0]) { max[0] = currCH.WorldPos[0]; } if (currCH.WorldPos[1] < min[1]) { min[1] = currCH.WorldPos[1]; } if (currCH.WorldPos[1] > max[1]) { max[1] = currCH.WorldPos[1]; } // Disable cell currCH.Disable(); // Get all af nthe neighbours af current cell CellHolder[] neighbours = GetAllNeighbourCellHolders(currCH.WorldPos); foreach (CellHolder ch in neighbours) { if (ch != null && !ch.IsDisabled && ch.IsFull()) // Found a cell which belongs to this game { if (!queue.Contains(ch) && !listOfCells.Contains(ch)) { queue.Enqueue(ch); listOfCells.Add(ch); } } } } wonData.table = new bool[Mathf.Abs(max[0] - min[0] + 1), Mathf.Abs(max[1] - min[1] + 1)]; // Fill the thingy with true where there is a cell. Thingy defined in WonData class foreach (CellHolder ch in listOfCells) { wonData.table[ch.WorldPos[0] - min[0], ch.WorldPos[1] - min[1]] = true; } wonData.startPos = min; // Now we can fill the holes List <int[]> holes = wonData.GetFillableHoles(); foreach (int[] pos in holes) { // Get the partion the cell is in (we surely have this one, because holes are between signs int[] partionPos = Partion.GetPartionPosOfCell(pos); Partion p; // We don't have a partion yet for this pos if (!partions.ContainsKey(partionPos)) { p = new Partion(partionPos); partions.Add(partionPos, p); } else // we already store the partion in the dictionary { partions.TryGetValue(partionPos, out p); } // Place new BLOCKED cell in partion at pos p.PlaceSign(Partion.GetLocalPosOfCell(pos), Cell.CellOcc.BLOCKED, true); } return(wonData); }