private void AddPiece(object sender, MouseButtonEventArgs e) { //On click, replace the fill with the right color Rectangle rect = (Rectangle)sender; if (DataContext is MainPageData data) { bool validMoveMade; Point positionOnScreen = GetPosition(rect); var currentColor = data.CurrentTurn; if (data.Game.CurrentMode == GameMode.SinglePlayer) { validMoveMade = data.Game.TakeTurn(positionOnScreen, data.PlayerOne.Color); } else { validMoveMade = data.Game.TakeTurn(positionOnScreen, data.CurrentTurn); } if (validMoveMade) { DrawPiece(rect, currentColor); timer.Start(); UpdateHighlight(); data.Game.RunComputerTurn(); } } }
private Point GetPosition(Rectangle rect) { int rows = OptionsPage.GridSizeNum; int columns = OptionsPage.GridSizeNum; int index = GameGrid.Children.IndexOf(rect); int row = index / columns; // divide int column = index % columns; // modulus Point coord = new Point(column, row); return(coord); }
private Rectangle FindRectangle(Point point) { int index = point.y * (DataContext as MainPageData).GridSize + point.x; return(GameGrid.Children[index] as Rectangle); }