private bool checkIfValid(OthelloImage othelloImage, bool flipDiscs, int offsetX, int offsetY) { OthelloImage nextOImg; bool result = false; int x = othelloImage.X; int y = othelloImage.Y; try { nextOImg = imageMatrix[x + offsetX, y + offsetY]; if (nextOImg.Disc != OthelloDisc.none && nextOImg.Disc != whoseTurn) { // Worth checking next disc then until IndexOutOfRangeException while (true) { x += offsetX; y += offsetY; nextOImg = imageMatrix[x + offsetX, y + offsetY]; if (nextOImg.Disc != OthelloDisc.none && nextOImg.Disc == whoseTurn) { result = true; break; } else if (nextOImg.Disc == OthelloDisc.none) { break; } } } } catch (IndexOutOfRangeException) { } if (result == true && flipDiscs == true) { // flip discs x = othelloImage.X; y = othelloImage.Y; while (true) { nextOImg = imageMatrix[x + offsetX, y + offsetY]; x += offsetX; y += offsetY; if (nextOImg.Disc == whoseTurn) { break; } if (nextOImg.Disc == OthelloDisc.black) { nextOImg.ChangeToWhite(); } else { nextOImg.ChangeToBlack(); } } } return(result); }
private void AddImages() { bool fubar; BitmapImage imageSource = null; OthelloImage othelloImage = null; Image discImage = null; for (int rowIndex = 0; rowIndex < MainBoardGrid.RowDefinitions.Count; rowIndex++) { if (rowIndex % 2 != 0) { fubar = false; } else { fubar = true; } for (int columnIndex = 0; columnIndex < MainBoardGrid.ColumnDefinitions.Count; columnIndex++) { if (fubar) { imageSource = new BitmapImage(new Uri("pack://application:,,,/Images/DarkerGreen.jpg")); fubar = false; } else { imageSource = new BitmapImage(new Uri("pack://application:,,,/Images/BrighterGreen.jpg")); fubar = true; } discImage = new Image(); othelloImage = new OthelloImage(columnIndex, rowIndex, discImage) { Source = imageSource }; othelloImage.Stretch = Stretch.Fill; Thickness margin = othelloImage.Margin; margin.Top = 1; margin.Right = 1; margin.Bottom = 1; margin.Left = 1; othelloImage.Margin = margin; othelloImage.MouseDown += Image_MouseDown; imageMatrix[columnIndex, rowIndex] = othelloImage; Grid.SetRow(othelloImage, rowIndex); Grid.SetColumn(othelloImage, columnIndex); MainBoardGrid.Children.Add(othelloImage); Grid.SetRow(discImage, rowIndex); Grid.SetColumn(discImage, columnIndex); MainBoardGrid.Children.Add(discImage); if ((rowIndex == 3 && columnIndex == 3) || (rowIndex == 4 && columnIndex == 4)) { othelloImage.ChangeToWhite(); } if ((rowIndex == 3 && columnIndex == 4) || (rowIndex == 4 && columnIndex == 3)) { othelloImage.ChangeToBlack(); } } } }
public bool allowedPlacement(OthelloImage othelloImage, bool flipDiscs) { int offsetX; int offsetY; bool result = false; // check to N offsetX = 0; offsetY = -1; if (checkIfValid(othelloImage, flipDiscs, offsetX, offsetY)) { result = true; } // check to NE offsetX = 1; offsetY = -1; if (checkIfValid(othelloImage, flipDiscs, offsetX, offsetY)) { result = true; } // check to E offsetX = 1; offsetY = 0; if (checkIfValid(othelloImage, flipDiscs, offsetX, offsetY)) { result = true; } // check to SE offsetX = 1; offsetY = 1; if (checkIfValid(othelloImage, flipDiscs, offsetX, offsetY)) { result = true; } // check to S offsetX = 0; offsetY = 1; if (checkIfValid(othelloImage, flipDiscs, offsetX, offsetY)) { result = true; } // check to SW offsetX = -1; offsetY = 1; if (checkIfValid(othelloImage, flipDiscs, offsetX, offsetY)) { result = true; } // check to W offsetX = -1; offsetY = 0; if (checkIfValid(othelloImage, flipDiscs, offsetX, offsetY)) { result = true; } // check to NW offsetX = -1; offsetY = -1; if (checkIfValid(othelloImage, flipDiscs, offsetX, offsetY)) { result = true; } return(result); }