public Grid() { this.Cells = new List<Cell>(); for (int rowIndex = 0; rowIndex <= LIMIT; rowIndex++) { for (int colIndex = 0; colIndex <= LIMIT; colIndex++) { Cell cell = new Cell(string.Format("{0}{1}", rowIndex, colIndex)); cell.CellOccupied += CheckIfPlayerWon; cell.CellOccupied += CheckIfGridFilled; this.Cells.Add(cell); } } }
public void SetUp() { topLeft = new Cell(); topMiddle = new Cell(); topRight = new Cell(); middleLeft = new Cell(); middleMiddle = new Cell(); middleRight = new Cell(); bottomLeft = new Cell(); bottomMiddle = new Cell(); bottomRight = new Cell(); var cells = new List<Cell>{topLeft, topMiddle, topRight, middleLeft, middleMiddle, middleRight, bottomLeft, bottomMiddle, bottomRight}; board = new Board(cells); }
public void ResetGame(bool firstTime, bool resetScore) { playingState = PlayingState.FirstPlayerTurn; for (int x = 0; x < 7; x++) { for (int y = 0; y < 6; y++) { cells[y * 7 + x] = new Cell(x, y, Cell.Type.Empty); } } if (!firstTime) { CalculateSizes(new Vector2(0, 0), new Vector2(Game1.G.graphics.PreferredBackBufferWidth, Game1.G.graphics.PreferredBackBufferHeight)); } if (resetScore) { firstWins = 0; secondWins = 0; } }
private bool IsOnDiagnol(Cell cell) { return (cell.Position.X + cell.Position.Y == 0 || cell.Position.X + cell.Position.Y == LIMIT || cell.Position.X + cell.Position.Y == 2 * LIMIT); }
public void AddCell(Cell selectedCell) { this.SelectedCells.Add(selectedCell); }
public void Update() { backButton.Update(); againButton.Update(); if (playingState == PlayingState.FirstPlayerTurn || playingState == PlayingState.SecondPlayerTurn) { // Update falling cell Y and check for win if (fallingCell != null) { fallingCell.Y += 0.1f; if (Math.Abs(fallingCell.Y % 1f) < 0.1f) { int currentCell = (int)fallingCell.Y * 7 + (int)fallingCell.X; int bottomCell = (int)(fallingCell.Y + 1) * 7 + (int)fallingCell.X; if (bottomCell >= 42 || cells[bottomCell].type != Cell.Type.Empty) { cells[currentCell].type = fallingCell.type; cells[currentCell].CalculateSizes(backX, backY, backWidth, backHeight, cellSize); fallingCell = null; CheckForWin(cells[currentCell]); } } if (fallingCell != null) { fallingCell.CalculateSizes(backX, backY, backWidth, backHeight, cellSize); } } // Create falling cell when clicking var mState = Mouse.GetState(); if (!clicking && fallingCell == null && mState.LeftButton == ButtonState.Pressed && mState.X > backX && mState.X < backX + backWidth && mState.Y > backY && mState.Y < backY + backHeight) { int clickingColumn_ = -1; for (int i = 0; i < 7; i++) { int x = backX + cellSize * i + (int)((backWidth - 7 * cellSize) / 7 * (i + 0.25f)); if (mState.X >= x && mState.X <= x + cellSize) { clickingColumn_ = i; } } if (clickingColumn_ >= 0) { clicking = true; clickingColumn = clickingColumn_; } } else if (clicking && fallingCell == null && mState.LeftButton == ButtonState.Released && mState.X > backX && mState.X < backX + backWidth && mState.Y > backY && mState.Y < backY + backHeight) { int clickingColumn_ = -1; for (int i = 0; i < 7; i++) { int x = backX + cellSize * i + (int)((backWidth - 7 * cellSize) / 7 * (i + 0.25f)); if (mState.X >= x && mState.X <= x + cellSize) { clickingColumn_ = i; } } if (clickingColumn_ == clickingColumn) { clicking = false; fallingCell = new Cell(clickingColumn, -1, playingState == PlayingState.FirstPlayerTurn ? Cell.Type.FirstPlayer : Cell.Type.SecondPlayer); fallingCell.CalculateSizes(backX, backY, backWidth, backHeight, cellSize); clickingColumn = -1; } } } }
protected void EndCheck(Cell cell, bool win) { if (cell.type == Cell.Type.FirstPlayer) { if (win) { playingState = PlayingState.FirstPlayerWin; firstWins++; } else if (!win && cells.All(x => x.type != Cell.Type.Empty)) playingState = PlayingState.DrawGame; else playingState = PlayingState.SecondPlayerTurn; } else { if (win) { playingState = PlayingState.SecondPlayerWin; secondWins++; } else if (!win && cells.All(x => x.type != Cell.Type.Empty)) playingState = PlayingState.DrawGame; else playingState = PlayingState.FirstPlayerTurn; } }
protected void CheckForWin(Cell cell) { bool win = false; // Horizontal check for (int x = 0; x < 7; x++) { if (cells[(int)cell.Y * 7 + x].type == cell.type) { int matchingCells = 1; firstWinCell = (int)cell.Y * 7 + x; x++; while (x < 7 && cells[(int)cell.Y * 7 + x].type == cell.type) { matchingCells++; x++; } if (matchingCells >= 4) { endWinCell = (int)cell.Y * 7 + x - 1; win = true; EndCheck(cell, win); return; } } } // Vertical check for (int y = 0; y < 6; y++) { if (cells[y * 7 + (int)cell.X].type == cell.type) { int matchingCells = 1; firstWinCell = y * 7 + (int)cell.X; y++; while (y < 6 && cells[y * 7 + (int)cell.X].type == cell.type) { matchingCells++; y++; } if (matchingCells >= 4) { endWinCell = (y - 1) * 7 + (int)cell.X; win = true; EndCheck(cell, win); return; } } } // Diagonal check 1 int startX = (int)cell.X, startY = (int)cell.Y; while (startX > 0 && startY < 5) { startX--; startY++; } for (int x = startX; x <= 6; x++) { for (int y = startY; y >= 0; y--) { if (x <= 6 && y >= 0 && cells[y * 7 + x].type == cell.type) { int matchingCells = 1; firstWinCell = y * 7 + x; x++; y--; while (x <= 6 && y >= 0 && cells[y * 7 + x].type == cell.type) { matchingCells++; x++; y--; } if (matchingCells >= 4) { endWinCell = (y + 1) * 7 + x - 1; win = true; EndCheck(cell, win); return; } } } } // Diagonal check 2 startX = (int)cell.X; startY = (int)cell.Y; while (startX < 6 && startY < 5) { startX++; startY++; } for (int x = startX; x >= 0; x--) { for (int y = startY; y >= 0; y--) { if (x >= 0 && y >= 0 && cells[y * 7 + x].type == cell.type) { int matchingCells = 1; firstWinCell = y * 7 + x; x--; y--; while (x >= 0 && y >= 0 && cells[y * 7 + x].type == cell.type) { matchingCells++; x--; y--; } if (matchingCells >= 4) { endWinCell = (y + 1) * 7 + x + 1; win = true; EndCheck(cell, win); return; } } } } EndCheck(cell, win); }
bool checkStep(Cell cell, ECellType type) { if (cell.state == ECellType.Empty && type != ECellType.Empty) return true; return false; }
protected void MakeMove(Point? point = null) { var oldCurrentPoint = currentPoint; currentPoint = point.GetValueOrDefault(currentPoint); if( field[currentPoint.X, currentPoint.Y] == Cell.Empty ) { field[currentPoint.X, currentPoint.Y] = turn; if( ! CheckGameOver() ) { var oldTurn = turn; turn = (oldTurn == Cell.Cross) ? Cell.Nought : Cell.Cross; Emit(OnTurnChanged, this, turn, oldTurn); } Invalidate(); } currentPoint = oldCurrentPoint; }
public void PrepareField() { currentPoint = new Point(1,1); for(int i = 0; i < 3; ++i) for(int j = 0; j < 3; ++j) field[i,j] = Cell.Empty; turn = Cell.Cross; Emit(OnTurnChanged, this, turn, Cell.Empty); Invalidate(); }
public void Play(Cell cell) { myCells.Contains(cell); }
private void SelectCell(Point clickedPosition) { Button buttonClicked = _cellMap[clickedPosition]; AdjustDisplay(buttonClicked); _actedCell = _grid.GetCell(clickedPosition); AssignPlayerToCell(); }
public void Test_That_Does_Not_Throw_When_Valid_Coord_Is_Used(Cell cell) { Assert.DoesNotThrow(() => board.Play(cell)); }