public CheckersWindow() { this.InitializeComponent(); board = new CheckersBoard(0, x); board.GameMaxLength = 150; lastMove = new CheckersMove(null, false, 1); moveList.Tag = new List<CheckersMove>(); myRepaint = new Repaint(this.repaint); CheckersPlayer player = new CheckersPlayer(ComputerPlayer); alg = new Algorithms(player); int blackWhite = 1; for (int row = 0; row < 8; ++row) { for (int col = 0; col < 8; ++col) { if (blackWhite % 2 == 0) { BlackSquare square = new BlackSquare(); square.SetValue(Grid.ColumnProperty, col); square.SetValue(Grid.RowProperty, row); square.Tag = new System.Windows.Point(row, col); square.DragEnter += new DragEventHandler(square_DragEnter); square.Drop += new DragEventHandler(square_Drop); square.MouseLeftButtonDown += new MouseButtonEventHandler(square_MouseLeftButtonDown); UIBoard.Children.Add(square); blackSquares.Add(square); } else { WhiteSquare square = new WhiteSquare(); square.SetValue(Grid.ColumnProperty, col); square.SetValue(Grid.RowProperty, row); UIBoard.Children.Add(square); } blackWhite++; } blackWhite++; } StringBuilder stringBuilder = new StringBuilder("Current Player is:\n"); stringBuilder.Append(board.GetCurrentPlayer().GetPlayerID().ToString() == "-1" ? "Black" : "White"); currentPlayerLabel.Content = stringBuilder.ToString(); repaint(null); board.GameOver += new CheckersBoard.EndGame(board_GameOver); if (board.GetCurrentPlayer().GetPlayerID() == alg.ComputerPlayerID) { computerThread = new Thread(this.computerMove); computerThread.Start(); } this.visualGameOver = new EndGame(this.theGameIsOver); }
void square_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.parentSquare = sender as BlackSquare; }
void square_Drop(object sender, DragEventArgs e) { BlackSquare square = sender as BlackSquare; if (currentItemContainer != null) { if (square.Opacity != 1) { currentItemContainer.Children.Clear(); currentItemContainer = null; square.backGround.Children.Add((Ellipse)e.Data.GetData(typeof(Ellipse))); parentSquare.Opacity = 1; parentSquare = null; System.Windows.Point squareCoords = (System.Windows.Point)square.Tag; if (isInMove == false) { isInMove = true; } List<CheckersMove> newLegalMoves = new List<CheckersMove>(); foreach (var item in movesForCurrentPiece) { CheckersGame.Point moveCoords = item.GetPathAt(moveIndex); if (moveCoords != null && moveCoords.IsEqual((int)squareCoords.X, (int)squareCoords.Y)) { newLegalMoves.Add(item); } } movesForCurrentPiece = newLegalMoves; ++moveIndex; if (movesForCurrentPiece.Count == 1)// && moveIndex == movesForCurrentPiece[0].GetPath().Count) { if (SearchDepth == 8) { lastPlayerMove = movesForCurrentPiece[0]; evaluator = new CheckersBoard(board, false); } board.MakeMove(movesForCurrentPiece[0]); repaint(movesForCurrentPiece[0]); if (!board.GameIsOver()) { computerThread = new Thread(this.computerMove); computerThread.Start(); } isInMove = false; } } //else // MessageBox.Show("Illegal move!", "Illegal", MessageBoxButton.OK, MessageBoxImage.Exclamation); } foreach (var elem in blackSquares) { elem.Opacity = 1.0; foreach (var item in elem.backGround.Children) if (item is Ellipse) { Ellipse elli = item as Ellipse; int row = (int)elem.GetValue(Grid.RowProperty); int col = (int)elem.GetValue(Grid.ColumnProperty); elli.Tag = new System.Windows.Point(row, col); } } }