/// <summary> /// The equivalent of dropping one of the dots into the frame /// </summary> /// <param name="index">1-7, the horizontal left to right coordinate of the hole to "drop" it into.</param> public void DropDot(int index) { if (index <= Columns && index >= 1) { Connect4Colour[] column = ConnectFourGrid[index - 1]; int columnCount = column.Count(x => x != null); if (columnCount < Rows) { column[Utillity.Clamp(columnCount, 0, Rows)] = CurrPlayerColour; //check if there are matches whenever a dot drops LookForFourInARow(index - 1, columnCount); if (!GameWon) { //swap colour if (CurrPlayerColour == Connect4Colour.Red) { CurrPlayerColour = Connect4Colour.Yellow; } else { CurrPlayerColour = Connect4Colour.Red; } } } } }
private bool IsFourInARowForDir(int gridX, int gridY, int xChange, int yChange) { int matchCount = 0; int xUpperBound = Utillity.Clamp(gridX + (3 * xChange), 0, Columns - 1); int yUpperBound = Utillity.Clamp(gridY + (3 * yChange), 0, Rows - 1); //yes it's hacky //no i don't care, it works bool xRan = false; bool yRan = false; bool skipOne = false; for (int x = Utillity.Clamp(gridX - (3 * xChange), 0, Columns - 1); x <= xUpperBound && !xRan; x += xChange) { for (int y = Utillity.Clamp(gridY - (3 * yChange), 0, Rows - 1); y <= yUpperBound && !yRan; y += yChange) { if (ConnectFourGrid[x][y] == CurrPlayerColour) { matchCount++; if (matchCount >= 4) { return(true); } } else { matchCount = 0; } if (yChange == 0) { yRan = true; skipOne = true; } } if (xChange == 0) { xRan = true; } if (skipOne) { continue; } if (yChange == 0) { if (ConnectFourGrid[x][gridY] == CurrPlayerColour) { matchCount++; if (matchCount >= 4) { return(true); } } else { matchCount = 0; } } } return(false); }