/// <summary> /// Called when mouse click is released on canvas board. Makes moves for player and IA /// </summary> /// <param name="sender">sender object</param> /// <param name="e">mouse event</param> private void CanBoard_MouseUp(object sender, MouseButtonEventArgs e) { //If game as started and it is player turn if (isPlaying && (!isIA || turnToWhite)) { //Getting canvas board dimensions double canvasHeight = canBoard.ActualHeight / 8.0; double canvasWidth = canBoard.ActualWidth / 8.0; //Getting mouse positions double eX = e.GetPosition(canBoard).X; double eY = e.GetPosition(canBoard).Y; //Setting counter positions int squareIdI = (int)(eX / canvasWidth); int squareIdJ = (int)(eY / canvasHeight); //If move can be played if (board.IsPlayable(squareIdI, squareIdJ, turnToWhite)) { //Move is played bool changePlayer = board.PlayMove(squareIdI, squareIdJ, turnToWhite); //If game is finished if (board.Ended) { //Displaying a label with the winner DisplayWinner(); } //Changing players if (changePlayer) { turnToWhite = !turnToWhite; } } } //If game as started and it is IA turn if (isPlaying && isIA && !turnToWhite) { bool changePlayer = false; while (!changePlayer && !board.Ended) { //IA finds its next move to play Tuple <int, int> IA = board.GetNextMove(board.GetBoard(), 1, turnToWhite); //Move is played changePlayer = board.PlayMove(IA.Item1, IA.Item2, turnToWhite); //If game is finished if (board.Ended) { //Displaying a label with the winner DisplayWinner(); } } //Changing players if (changePlayer) { turnToWhite = !turnToWhite; } } //Updating board PrintBoard(); //Updating scores this.WhiteScore = board.GetWhiteScore(); this.BlackScore = board.GetBlackScore(); }
/// <summary> /// Demande à l'IA de jouer un coup. /// Méthode asynchrone pour permettre l'affichage du coup du joueur avant que l'IA joue. /// </summary> private void PlayIAMove() { int difficulty = 2; // Le joueur humain est bloqué foreach (var cell in cells) { cell.IsEnabled = false; } // Copie des variables utilisées dans un BackgroundWorker pour éviter la concurrence Board copyBoard = new Board(board); bool copyWhiteTurn = whiteTurn; int copyDifficulty = difficulty; Tuple <int, int> move = null; BackgroundWorker bgWorker = new BackgroundWorker(); // On fait réfléchir l'IA dans un BackgroudWorker bgWorker.DoWork += (sender, ev) => { Thread.Sleep(1000); move = copyBoard.GetNextMove(copyBoard.GetBoard(), copyDifficulty, copyWhiteTurn); }; // Quand elle trouve un coup, on met à jour le board bgWorker.RunWorkerCompleted += (sender, ev) => { int c = move.Item1; int l = move.Item2; // Vérifie que l'IA n'a pas passé son tour if (c >= 0 && l >= 0) { if (board.PlayMove(c, l, whiteTurn)) { // Coup jouable et joué whiteTurn = !whiteTurn; } else { // Impossible de jouer ce coup } } var newWhiteTurn = whiteTurn; CheckFinished(); if (newWhiteTurn != whiteTurn) { // Le joueur a passé son tour (whiteTurn a été modifié dans CheckFinished()) PlayIAMove(); } // Débloque le joueur humain foreach (var cell in cells) { cell.IsEnabled = true; } }; // On lance le background worker bgWorker.RunWorkerAsync(); }