/* **************** End helper functions*************************** * * *************** event handler for button press******************** * contains all the game logic for checkers */ private void Button_Click(object sender, RoutedEventArgs e) { // if the game has ended, a pop text box will appear to inform the winner // afterwards the game window is closed and the title window is intialized and displayed if (game_over()) { if (p1_check_count > 0) // player 1 won { MessageBoxResult result = MessageBox.Show("PLAYER ONE WINS!", "GAME OVER"); } else // player 2 won { MessageBoxResult result = MessageBox.Show("PLAYER TWO WINS!", "GAME OVER"); } Window1 window = new Window1(); this.Visibility = Visibility.Collapsed; // hide current window window.Show(); // show the main window obj this.Close(); } var button = (Button)sender; // get row and column of the button pressed so that it can be found within the Board_array and logic can be applied column = Grid.GetColumn(button); row = Grid.GetRow(button); // currently player ones turn if (player_one_turn) { // player has clicked on piece belonging to them and wants to move it if (players_second_click) { prevRow = Grid.GetRow(prevButton); prevCol = Grid.GetColumn(prevButton); if (Board_array[prevRow, prevCol] == CheckerType.P1_check) { // the piece is a normal check // check if player made a normal move if (Board_array[row, column] == CheckerType.Free && (row - prevRow == -1) && (column - prevCol == -1 || column - prevCol == 1)) { if (!Is_kinged()) { // the space is free and is a valid movement and not a kinged move Board_array[row, column] = CheckerType.P1_check; Board_array[prevRow, prevCol] = CheckerType.Free; button.Content = "•"; button.Foreground = p1_color; borderChangeBack(prevButton); prevButton.Content = ""; } // end the turn for player one after valid move End_turn(); borderChangeBack(prevButton); } else if (Board_array[row, column] == CheckerType.Free && (row - prevRow == -2) && (column - prevCol == -2)) { // if an enemy check is open to be jumped if (Board_array[row + 1, column + 1] == CheckerType.P2_check || Board_array[row + 1, column + 1] == CheckerType.P2_king) { p2_check_count--; // decrement the amount of player 2 checks // jump enemy checker Board_array[row + 1, column + 1] = CheckerType.Free; if (Is_kinged()) { End_turn(); borderChangeBack(prevButton); } else { Board_array[row, column] = CheckerType.P1_check; Board_array[prevRow, prevCol] = CheckerType.Free; // reset the button border and update the game board borderChangeBack(prevButton); updateBoardGui(); if (p1_jump_available()) { // check if another jump can be made and if so then make current button the previous button and let player go again prevButton = button; borderChangeOnCLick(button); } else { // no more valid jumps could be made so players turn is over End_turn(); borderChangeBack(prevButton); } } } } else if (Board_array[row, column] == CheckerType.Free && (row - prevRow == -2) && (column - prevCol == 2)) { if (Board_array[row + 1, column - 1] == CheckerType.P2_check || Board_array[row + 1, column - 1] == CheckerType.P2_king) { p2_check_count--; // decrement the amount of player 2 checks // jump enemy checker from the board Board_array[row + 1, column - 1] = CheckerType.Free; if (Is_kinged()) { // check was kinged so it is end of players turn End_turn(); borderChangeBack(prevButton); } else { // checker was not kinged after jumping check Board_array[row, column] = CheckerType.P1_check; Board_array[prevRow, prevCol] = CheckerType.Free; // reset the button border and update the game board borderChangeBack(prevButton); updateBoardGui(); if (p1_jump_available()) { // check if another jump can be made and if so then make current button the previous button and let player go again prevButton = button; borderChangeOnCLick(button); } else { // no more valid jumps could be made so players turn is over End_turn(); borderChangeBack(prevButton); } } } } else { // no valid button was chosen so reset turn invalid_choice(); } } else { // the piece is a king check if (is_normal_king_move()) { button.Content = "♛"; button.Foreground = p1_color; prevButton.Content = ""; borderChangeBack(prevButton); End_turn(); } else if (is_valid_king_jump()) { p2_check_count--; // decrement the amount of player 2 checks // calculate the row and column of the jumped piece from any direction // this is because the king can move from any direction // example: row = 5 prevRow = 7: then 5 + ((5 - 7) * -.5) = 6, which is the row of the jumped check int jumped_row = (int)(row + ((row - prevRow) * -.5)); int jumped_col = (int)(column + ((column - prevCol) * -.5)); Board_array[row, column] = CheckerType.P1_king; System.Console.WriteLine("value of jumped piece " + (jumped_row) + " " + (jumped_col)); Board_array[jumped_row, jumped_col] = CheckerType.Free; Board_array[prevRow, prevCol] = CheckerType.Free; borderChangeBack(prevButton); updateBoardGui(); if (more_king_jump_available()) { // check if another jump can be made and if so then make current button the previous button and let player go again prevButton = button; borderChangeOnCLick(button); } else { // no more valid jumps could be made so players turn is over End_turn(); borderChangeBack(prevButton); } } else { invalid_choice(); } } } // this is the players first click else { // if the button clicked is owned by player 1 check or king then allow for movement if (Board_array[row, column] == CheckerType.P1_check || Board_array[row, column] == CheckerType.P1_king) { prevButton = button; // save the current button so it can be accessed later borderChangeOnCLick(button); players_second_click = true; } } } // player twos turn else { // player has clicked on piece belonging to them and wants to move it if (players_second_click) { prevRow = Grid.GetRow(prevButton); prevCol = Grid.GetColumn(prevButton); if (Board_array[prevRow, prevCol] == CheckerType.P2_check) { if (Board_array[row, column] == CheckerType.Free && (row - prevRow == 1) && (column - prevCol == -1 || column - prevCol == 1)) { if (!Is_kinged()) { Board_array[row, column] = CheckerType.P2_check; Board_array[prevRow, prevCol] = CheckerType.Free; button.Content = "•"; button.Foreground = p2_color; borderChangeBack(prevButton); prevButton.Content = ""; } // end player twos turn after valid move End_turn(); borderChangeBack(prevButton); } else if (Board_array[row, column] == CheckerType.Free && (row - prevRow == 2) && column - prevCol == -2) { // the check in between previous button and current button is a player 1 check or king if (Board_array[row - 1, column + 1] == CheckerType.P1_check || Board_array[row - 1, column + 1] == CheckerType.P1_king) { p1_check_count--; // decrement the amount of player 1 checks Board_array[row - 1, column + 1] = CheckerType.Free; if (Is_kinged()) { End_turn(); borderChangeBack(prevButton); } else { Board_array[row, column] = CheckerType.P2_check; Board_array[prevRow, prevCol] = CheckerType.Free; borderChangeBack(prevButton); updateBoardGui(); if (p2_jump_available()) { borderChangeOnCLick(button); prevButton = button; } else { End_turn(); borderChangeBack(prevButton); } } } } else if (Board_array[row, column] == CheckerType.Free && (row - prevRow == 2) && column - prevCol == 2) { p1_check_count--; // decrement the amount of player 1 checks Board_array[row - 1, column - 1] = CheckerType.Free; if (Is_kinged()) { End_turn(); borderChangeBack(prevButton); } else { Board_array[row, column] = CheckerType.P2_check; Board_array[prevRow, prevCol] = CheckerType.Free; borderChangeBack(prevButton); updateBoardGui(); if (p2_jump_available()) { borderChangeOnCLick(button); prevButton = button; } else { End_turn(); borderChangeBack(prevButton); } } } else { // no valid button was chosen so reset turn invalid_choice(); } } else { // piece chosen was a P2 king if (is_normal_king_move()) { button.Content = "♚"; button.Foreground = p2_color; prevButton.Content = ""; borderChangeBack(prevButton); End_turn(); } else if (is_valid_king_jump()) { p1_check_count--; // decrement the amount of player 1 checks // calculate the row and column of the jumped piece from any direction // this is because the king can move from any direction // example: row = 5 prevRow = 7: then 5 + ((5 - 7) * -.5) = 6, which is the row of the jumped check int jumped_row = (int)(row + ((row - prevRow) * -.5)); int jumped_col = (int)(column + ((column - prevCol) * -.5)); Board_array[row, column] = CheckerType.P2_king; System.Console.WriteLine("value of jumped piece " + (row + jumped_row) + " " + (column + jumped_col)); Board_array[jumped_row, jumped_col] = CheckerType.Free; Board_array[prevRow, prevCol] = CheckerType.Free; borderChangeBack(prevButton); updateBoardGui(); if (more_king_jump_available()) { // check if another jump can be made and if so then make current button the previous button and let player go again prevButton = button; borderChangeOnCLick(button); } else { // no more valid jumps could be made so players turn is over End_turn(); borderChangeBack(prevButton); } } else { invalid_choice(); } } } // this is the players first click else { current_player.Text = "Player 2 Turn"; // if the button clicked is owned by player 2 check or king then allow for movement if (Board_array[row, column] == CheckerType.P2_check || Board_array[row, column] == CheckerType.P2_king) { prevButton = button; // save the current button so it can be accessed in the second click players_second_click = true; borderChangeOnCLick(button); } } } } // end button_clicked method