private void Render(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; ScreenOptions screenOptions = CalculateScreenOptions(); int tileSize = screenOptions.TileSize; int hintSizeDifference = tileSize / 2; bool toggleColor = false; for (int row = 0; row < boardRows; row++) { for (int column = 0; column < boardColumns; column++) { // Draw the board with (dark) green tiles. Rectangle r = new Rectangle( column * tileSize + screenOptions.Offset.X, row * tileSize + screenOptions.Offset.Y, tileSize, tileSize); toggleColor = !toggleColor; g.FillRectangle(toggleColor ? Brushes.DarkGreen : Brushes.Green, r); switch (board[row, column]) { case FieldState.Hint: if (showHints) { g.FillEllipse( currentPlayer == FieldState.PlayerOne ? playerOneHintBrush : playerTwoHintBrush, r.X + hintSizeDifference / 2, r.Y + hintSizeDifference / 2, tileSize - hintSizeDifference, tileSize - hintSizeDifference); } break; case FieldState.PlayerOne: g.FillEllipse(Brushes.Black, r); break; case FieldState.PlayerTwo: g.FillEllipse(Brushes.White, r); break; } } toggleColor = !toggleColor; } }
private void OnClick(object sender, MouseEventArgs e) { ScreenOptions screenOptions = CalculateScreenOptions(); // Translate the clicked location to a location on the board. int row = (int)Math.Ceiling((e.Y - screenOptions.Offset.Y) / (double)(boardRows * screenOptions.TileSize) * boardRows) - 1; int column = (int)Math.Ceiling((e.X - screenOptions.Offset.X) / (double)(boardColumns * screenOptions.TileSize) * boardColumns) - 1; // If the clicked tile is outside board bounds or // if the field has a piece, return. if (row < 0 || row > boardRows - 1 || column < 0 || column > boardColumns - 1 || board[row, column] == FieldState.PlayerOne || board[row, column] == FieldState.PlayerTwo) { return; } // Converting to an array to prevent multiple enumeration for Any and the foreach. var validPieces = CalculateValidPieces(row, column).ToArray(); // If the move is invalid, return. if (!validPieces.Any()) { return; } // Change all valid pieces to the current player's color. foreach (var pieceToChange in validPieces) { board[pieceToChange.Y, pieceToChange.X] = currentPlayer; } // Set the clicked tile to the current player's color as well. board[row, column] = currentPlayer; SwapPlayers(); Invalidate(true); }