I
I
IvanTwice2019-12-16 15:56:32
Unity
IvanTwice, 2019-12-16 15:56:32

How to realize potential win in Unity c# Tic Tac Toe game?

Hello, I ran into a problem in the game Tic Tac Toe, when playing with a computer. Now the computer's moves are made using random, I need to make it so that it prevents the potential winning of the spawn, that is, when it stands 2 in a row, and puts its symbol in the 3rd empty slot.

private IEnumerator BotStep() {
        yield return new WaitForSeconds(0.5f);
        List<int> numbers = new List<int>();
        for (int i = 0; i < values.Length; i++) {
            if (values[i] == 0) {
                Debug.Log(i + " in list");
                numbers.Add(i);
            }
        }
        var potentialWin = CheckPreWin(2) != -1 ? CheckPreWin(2) : CheckPreWin(1) != -1 ? CheckPreWin(1) : -1;
        var maxLength = numbers.Count;
        var random = UnityEngine.Random.Range(0, maxLength);
        var index = numbers[random];
        numbers[random] = potentialWin;
        
        Debug.Log("BOT CAN CHOOSE THIS");
        buttons[index].interactable = false;
        buttons[index].GetComponent<Image>().sprite = o;
        values[index] = 2;
        Debug.Log("123");
        CheckWin(2);
        isXStep = true;

    }
    private void CheckWin(int number) {
             var compareCount = 0;
             var isDraw = 0;
             var isWin = false;
             for (int i = 0; i < 8; i++) {
                 for (int j = 0; j < 3; j++) {
                     var winNumber = WinCombinations[i, j];
                     if (values[winNumber] == number) {
                         compareCount += 1;
                     } else {
                         compareCount = 0;
                         break;
                     }
                 }
     
                 if (compareCount == 3) {
                     isWin = true;
                     Debug.Log(number + "  WIN");
                     GameOver.SetActive(true);
                     WhoWin.transform.GetComponent<Image>().sprite = number == 1 ? x :o;
                     Wintext.transform.GetComponent<Text>().text = "WIN";
                     break;   
                 }
             }
       
             foreach (var num in values) {
             if (num == 0) isDraw += 1;
                 //if (compareCount == 3) isDraw -= 1;
                 //if (compareCount == 0 ) isDraw += 1;
             }
        if (isDraw == 0 && !isWin) {    
                 Debug.Log("MATCH IS DRAW");
                 GameOver.SetActive(true);
                 WhoWin.transform.GetComponent<Image>().sprite = draw;
                 Wintext.transform.GetComponent<Text>().text = " ";
             }
         }

    
    private int CheckPreWin(int number) {
        var potentialWin = 0;    
        var compareCount = 0;
        var neededNumber = -1;
        var isDraw = 0;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 3; j++) {
                var winNumber = WinCombinations[i, j];
                if (values[winNumber] == number) {
                    compareCount += 1;
                } if (values[winNumber] != number) {
                    neededNumber = values[winNumber];
                }     
            }
                if (compareCount == 2) break;
                else if (compareCount < 2) {
                    compareCount = 0;
                    neededNumber = -1;
                }
        }

        return neededNumber;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
namee, 2019-12-17
@namee

RLY? Task for the 10th grade of high school.
Oh, I remember the times, BASIC, corvette and tic-tac-toe written in the 10th grade, which could not be won.
Crack your brains! Or drop this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question