protected void startButton_Click(object sender, EventArgs e)
 {
     if (startButton.Enabled == true)
     {
         this.startButton.Enabled = false;
         this.winner.Visible = true;
         this.winner.Text = "Game in progress!";
         this.pickPlayerLabel.Visible = false;
         this.playBlack.Visible = false;
         this.playRed.Visible = false;
         //X is a double[] that contains the weigths for the
         //evaluation function
         this.board = new CheckersBoard(0, x);
         double random = rand.NextDouble();
         if (random < 0.33)
         {
             this.AIID = 'H';
         }
         else if (random > 0.66)
         {
             this.AIID = 'E';
         }
         else
         {
             this.AIID = 'P';
         }
         this.gameHistory = new List<CheckersMove>();
         if (playBlack.Checked)
         {
             this.alg = new Algorithms(board.GetCurrentPlayer(),0.5);
             this.computerMove();
         }
         else
         {
             alg = new Algorithms(new CheckersGame.CheckersPlayer(1));
         }
         board.GameMaxLength = 150;
         playerMove.Text = "nothing";
         generateBoardElements();
         Session.Add("board", this.board);
         Session.Add("alg", this.alg);
         Session.Add("gameHistory", this.gameHistory);
         Session.Add("AIID", this.AIID);
     }
 }
        private void moveList_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListView list = sender as ListView;
            if (list != null)
            {
                if (list.SelectedIndex != -1)
                {
                    MessageBoxResult result = MessageBox.Show("Do you wish to restore the game from this position? ",
                        "Restore",
                        MessageBoxButton.YesNo,
                        MessageBoxImage.Exclamation,
                        MessageBoxResult.No);
                    if (result == MessageBoxResult.Yes)
                    {

                        List<CheckersMove> moves = moveList.Tag as List<CheckersMove>;
                        int selectedIndex = list.SelectedIndex;
                        for (int index = list.Items.Count - 1; index > selectedIndex; --index)
                        {

                            list.Items.RemoveAt(index);
                            moves.RemoveAt(index);
                        }
                        board = new CheckersBoard(0, x);
                        board.GameOver += new CheckersBoard.EndGame(board_GameOver);
                        board.GameMaxLength = 200;
                        foreach (var move in moves)
                        {
                            board.MakeMove(move);
                            StringBuilder builder = new StringBuilder(move.PlayerID.ToString() == "1" ? "White: " : "Black: ");
                            foreach (var item in move.GetPath())
                            {
                                builder.Append(Convert.ToChar(65 + item.Y));
                                builder.Append((item.X + 1).ToString());
                                builder.Append('-');
                            }
                            // builder.Remove(builder.Length - 1, 1);
                            builder.Append(move.Duration.TotalSeconds.ToString());

                        }
                        moveList.ScrollIntoView(moveList.Items[moveList.Items.Count - 1]);
                        this.repaint(null);
                        if (alg.ComputerPlayerID == board.GetCurrentPlayer().GetPlayerID())
                        {
                            computerThread = new Thread(this.computerMove);
                            computerThread.Start();
                        }
                    }

                }

            }
            e.Handled = true;
        }
        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);
        }
        private void load_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog fileDiag = new System.Windows.Forms.OpenFileDialog();
            System.Windows.Forms.DialogResult result = fileDiag.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                FileStream file = new FileStream(fileDiag.FileName, FileMode.Open, FileAccess.Read);
                StreamReader reader = new StreamReader(file);
                string currentString = reader.ReadLine();
                List<CheckersMove> list = new List<CheckersMove>();
                try
                {
                    while (currentString != null)
                    {
                        string[] parseData = currentString.Split(' ');
                        int pathLength = int.Parse(parseData[0]);
                        List<CheckersGame.Point> pathPoints = new List<CheckersGame.Point>();
                        for (int index = 0; index < pathLength; ++index)
                        {
                            pathPoints.Add(new CheckersGame.Point(int.Parse(parseData[index * 2 + 1]), int.Parse(parseData[(index + 1) * 2])));
                        }
                        bool isJump = false;
                        if (parseData[pathLength * 2 + 1] == "True")
                            isJump = true;
                        int playerID = int.Parse(parseData[parseData.Length - 1]);
                        list.Add(new CheckersMove(pathPoints, isJump, playerID));
                        currentString = reader.ReadLine();
                    }
                }
                catch
                {

                    MessageBox.Show("This is not a valid document");
                    return;
                }
                moveList.Tag = list;
                moveList.Items.Clear();

                board = new CheckersBoard(0, x);
                board.GameOver += new CheckersBoard.EndGame(board_GameOver);
                board.GameMaxLength = 200;
                foreach (var move in list)
                {
                    board.MakeMove(move);

                    StringBuilder builder = new StringBuilder(move.PlayerID.ToString() == "1" ? "White: " : "Black: ");
                    foreach (var item in move.GetPath())
                    {
                        builder.Append(Convert.ToChar(65 + item.Y));
                        builder.Append((item.X + 1).ToString());
                        builder.Append('-');
                    }
                    builder.Remove(builder.Length - 1, 1);
                    moveList.Items.Add(builder.ToString());

                }
                moveList.ScrollIntoView(moveList.Items[moveList.Items.Count - 1]);
                this.repaint(null);
                if (board.GetCurrentPlayer().GetPlayerID() == alg.ComputerPlayerID)
                {
                    computerThread = new Thread(computerMove);
                    computerThread.Start();

                }

                reader.Close();
                file.Close();
            }
        }