Esempio n. 1
0
    IEnumerator CompStep(int x)
    {
        yield return(new WaitForSeconds(0.1f));

        memScore2 = CompPoints;
        memScore1 = PlayerPoints;
        this._aichoice.CompPoints   = CompPoints;
        this._aichoice.PlayerPoints = PlayerPoints;


        //GameObject compChoice = cells[AiChoice(_turn, x, 0), x];
        var cellNumber  = 0;
        var heavyMethod = Observable.Start(() => _aichoice.Choice(_turn, x, 0));

        //Observable.Return(heavyMethod).Subscribe();
        Observable.WhenAll(heavyMethod).Subscribe(result => cellNumber = result[0]);
        var compChoice = cells[cellNumber, x];

        for (int i = 0; i < 10; i++)
        {
            SpriteRenderer _render = compChoice.GetComponent <SpriteRenderer>();
            Color          color   = new Color(1, 0.92f, 0.016f, 1);
            color.a      -= i / 10;
            _render.color = color;
            yield return(new WaitForSeconds(0.1f));
        }
        cell_Pl_vs_Pc stepComp = compChoice.GetComponent <cell_Pl_vs_Pc>();

        stepComp.OnMouseDown();
    }
Esempio n. 2
0
 public void getBack()
 {
     if (PlayerPrefs.GetInt("BackCount") > 0)
     {
         if (_turn == 0 && turns > 0)
         {
             PlayerPrefs.SetInt("BackCount", PlayerPrefs.GetInt("BackCount") - 1);
             backText.text = string.Format("{0}", PlayerPrefs.GetInt("BackCount"));
             turns         = 0;
             print(cells.Length);
             print(string.Format("{0} , {1}", PlayerPrefs.GetInt("PlayerStepY"), PlayerPrefs.GetInt("PlayerStepX")));
             cells[PlayerPrefs.GetInt("PlayerStepY"), PlayerPrefs.GetInt("PlayerStepX")] = (GameObject)Instantiate(cell, new Vector2(poleKoefx + PlayerPrefs.GetInt("PlayerStepX") * poleKoef, poleKoefy - PlayerPrefs.GetInt("PlayerStepY") * poleKoef), Quaternion.identity);
             cell_Pl_vs_Pc cellChosen = cells[PlayerPrefs.GetInt("PlayerStepY"), PlayerPrefs.GetInt("PlayerStepX")].GetComponent <cell_Pl_vs_Pc>();
             cellChosen.firstNumber = PlayerPrefs.GetInt("PlayerStepNumber");
             cellChosen.firstColor  = PlayerPrefs.GetInt("PlayerStepColor");
             if (PlayerPrefs.GetInt("PlayerStepColor") == 0)
             {
                 PlayerPoints -= PlayerPrefs.GetInt("PlayerStepNumber");
                 PlayerPoints -= 1;
             }
             else
             {
                 PlayerPoints += PlayerPrefs.GetInt("PlayerStepNumber");
                 PlayerPoints += 1;
             }
             cell_Pl_vs_Pc cellPozition = cells[PlayerPrefs.GetInt("PlayerStepY"), PlayerPrefs.GetInt("PlayerStepX")].GetComponent <cell_Pl_vs_Pc>();
             cellPozition.x = PlayerPrefs.GetInt("PlayerStepX");
             cellPozition.y = PlayerPrefs.GetInt("PlayerStepY");
             cellChosen.Chousen();
             cells[PlayerPrefs.GetInt("CompStepY"), PlayerPrefs.GetInt("CompStepX")] = (GameObject)Instantiate(cell, new Vector2(poleKoefx + PlayerPrefs.GetInt("CompStepX") * poleKoef, poleKoefy - PlayerPrefs.GetInt("CompStepY") * poleKoef), Quaternion.identity);
             cell_Pl_vs_Pc cellChosen1 = cells[PlayerPrefs.GetInt("CompStepY"), PlayerPrefs.GetInt("CompStepX")].GetComponent <cell_Pl_vs_Pc>();
             cellChosen1.firstNumber = PlayerPrefs.GetInt("CompStepNumber");
             cellChosen1.firstColor  = PlayerPrefs.GetInt("CompStepColor");
             if (PlayerPrefs.GetInt("CompStepColor") == 0)
             {
                 CompPoints -= PlayerPrefs.GetInt("CompStepNumber");
                 CompPoints -= 1;
             }
             else
             {
                 CompPoints += PlayerPrefs.GetInt("CompStepNumber");
                 CompPoints += 1;
             }
             pointsTextComp.text   = string.Format("{0}", CompPoints);
             pointsTextPlayer.text = string.Format("{0}", PlayerPoints);
             cell_Pl_vs_Pc cellPozition1 = cells[PlayerPrefs.GetInt("CompStepY"), PlayerPrefs.GetInt("CompStepX")].GetComponent <cell_Pl_vs_Pc>();
             cellPozition1.x = PlayerPrefs.GetInt("CompStepX");
             cellPozition1.y = PlayerPrefs.GetInt("CompStepY");
             _turn           = 1;
             ChouseLine(PlayerPrefs.GetInt("PlayerStepX"), PlayerPrefs.GetInt("PlayerStepY"));
             StartCoroutine(getBackCoro(PlayerPrefs.GetInt("PlayerStepX"), PlayerPrefs.GetInt("PlayerStepY")));
             print(cells.Length);
         }
     }
     else
     {
         buyBack.SetActive(true);
     }
 }
Esempio n. 3
0
    IEnumerator getBackCoro(int x, int y)
    {
        yield return(new WaitForEndOfFrame());

        PolygonCollider2D cellColl   = cells[y, x].AddComponent <PolygonCollider2D>();
        cell_Pl_vs_Pc     cellChosen = cells[y, x].GetComponent <cell_Pl_vs_Pc>();

        cellChosen.Chousen();
    }
Esempio n. 4
0
 private void Generate()
 {
     for (int i = 0; i < poleRazmer; i++)
     {
         for (int j = 0; j < poleRazmer; j++)
         {
             cells[i, j] = (GameObject)Instantiate(cell, new Vector2(poleKoefx + j * poleKoef, poleKoefy - i * poleKoef), Quaternion.identity);
             cell_Pl_vs_Pc cellPozition = cells[i, j].GetComponent <cell_Pl_vs_Pc>();
             cellPozition.x = j;
             cellPozition.y = i;
         }
     }
 }
Esempio n. 5
0
    public void ChouseLine(int x, int y, bool start = false)
    {
        OffAllCollaider();
        bool isEnd = true;

        if (_turn == 0)
        {
            StartCoroutine(CompStep(x));
            for (int j = 0; j < poleRazmer; j++)
            {
                if (cells[j, x] != null && j != y || cells[j, x] != null && start)
                {
                    isEnd = false;
                    //PolygonCollider2D cellColl = cells[j, x].AddComponent<PolygonCollider2D>();
                    //SpriteRenderer _render = cells[j, x].GetComponent<SpriteRenderer>();
                    //_render.color = Color.blue;
                    cell_Pl_vs_Pc cellChosen = cells[j, x].GetComponent <cell_Pl_vs_Pc>();

                    cellChosen.Chousen();
                }
            }
            _turn = 1;
            if (isEnd)
            {
                EndGame(0, x, y);
            }
            //StartCoroutine(CompStep(x));
            return;
        }
        else
        {
            for (int i = 0; i < poleRazmer; i++)
            {
                if (cells[y, i] != null && i != x || cells[y, i] != null && start)
                {
                    isEnd = false;
                    PolygonCollider2D cellColl = cells[y, i].AddComponent <PolygonCollider2D>();
                    //SpriteRenderer _render = cells[y, i].GetComponent<SpriteRenderer>();
                    //_render.color = Color.blue;
                    cell_Pl_vs_Pc cellChosen = cells[y, i].GetComponent <cell_Pl_vs_Pc>();
                    cellChosen.Chousen();
                }
            }
            _turn = 0;
            if (isEnd)
            {
                EndGame(1, x, y);
            }
            return;
        }
    }
Esempio n. 6
0
 private void OffAllCollaider()
 {
     for (int i = 0; i < poleRazmer; i++)
     {
         for (int j = 0; j < poleRazmer; j++)
         {
             if (cells[i, j] != null)
             {
                 Collider2D cellColl = cells[i, j].GetComponent <Collider2D>();
                 Destroy(cellColl);
                 cell_Pl_vs_Pc cellChosen = cells[i, j].GetComponent <cell_Pl_vs_Pc>();
                 cellChosen.UnChousen();
             }
         }
     }
 }
Esempio n. 7
0
    IEnumerator CompStep(int x)
    {
        //_compAnim.myStep();
        yield return(new WaitForSeconds(0.5f));

        print(AIchoice(_turn, x, 1));
        //GameObject compChoice = cells[AIchoice(_turn, x, 1), x];
        memScore2 = CompPoints;
        memScore1 = PlayerPoints;
        GameObject compChoice = cells[AiChoice(_turn, x, 0), x];
        //for(int i=0;i<10;i++)
        //{
        //    SpriteRenderer _render=compChoice.GetComponent<SpriteRenderer>();
        //    Color color =new Color(1, 0.92f, 0.016f, 1);
        //    color.a -= i/10;
        //    _render.color = color;
        //    yield return new WaitForSeconds(0.1f);
        //}
        cell_Pl_vs_Pc stepComp = compChoice.GetComponent <cell_Pl_vs_Pc>();

        stepComp.OnMouseDown();
    }
Esempio n. 8
0
    public int AiChoice(int _turn, int _line, int dept)
    {
        dept++;
        int bestScore1 = -9999;
        int bestScore2 = -9999;
        int bestChoice = -1;
        int currScore1 = memScore1;
        int currScore2 = memScore2;
        int choice;
        int temp;

        for (int i = 0; i < poleRazmer; i++)
        {
            if (_turn == 0)//игрок
            {
                if (cells[_line, i] != null)
                {
                    cell_Pl_vs_Pc checkNumber = cells[_line, i].GetComponent <cell_Pl_vs_Pc>();
                    if (Mathf.Abs(checkNumber.Number) == 99)
                    {
                        continue;
                    }

                    temp = checkNumber.Number;
                    checkNumber.Number = 98;
                    memScore1          = currScore1;
                    memScore2          = currScore2 + temp;
                    if (dept < maxDepth)
                    {
                        choice = AiChoice(1, i, dept);
                        //if ((memScore1 - memScore2 < bestScore1 - bestScore2 && (choice != -1 || (choice == -1 && memScore1 + CompPoints < memScore2 + PlayerPoints)) || bestScore1 == -9999))
                        if ((memScore1 - memScore2 < bestScore1 - bestScore2 && (choice != -1 || (choice == -1 && memScore1 + PlayerPoints < memScore2 + CompPoints)) || bestScore1 == -9999))
                        {
                            bestScore1 = memScore1;
                            bestScore2 = memScore2;
                            bestChoice = i;
                            if (choice == -1)
                            {
                                bestScore2 += poleRazmer * 5;
                            }
                        }
                    }
                    else
                    {
                        if ((memScore1 - memScore2 < bestScore1 - bestScore2))
                        {
                            bestScore1 = memScore1;
                            bestScore2 = memScore2;
                            bestChoice = i;
                        }
                    }
                    checkNumber.Number = Mathf.Abs(temp) - 1;
                }
                else
                {
                    continue;
                }
            }
            else
            {
                if (cells[i, _line] != null)
                {
                    cell_Pl_vs_Pc checkNumber = cells[i, _line].GetComponent <cell_Pl_vs_Pc>();
                    if (Mathf.Abs(checkNumber.Number) == 99)
                    {
                        continue;
                    }

                    temp = checkNumber.Number;
                    checkNumber.Number = 98;
                    memScore1          = currScore1 + temp;
                    memScore2          = currScore2;
                    if (dept < maxDepth)
                    {
                        choice = AiChoice(0, i, dept);
                        //if ((memScore1 - memScore2 > bestScore1 - bestScore2 && (choice != -1 || (choice == -1 && memScore1 + CompPoints > memScore2 + PlayerPoints)) || bestScore1 == -9999))
                        if ((memScore1 - memScore2 > bestScore1 - bestScore2 && (choice != -1 || (choice == -1 && memScore1 + PlayerPoints > memScore2 + CompPoints)) || bestScore1 == -9999))
                        {
                            bestScore1 = memScore1;
                            bestScore2 = memScore2;
                            bestChoice = i;
                            if (choice == -1)
                            {
                                bestScore1 += poleRazmer * 5;
                            }
                        }
                    }
                    else
                    {
                        if ((memScore1 - memScore2 > bestScore1 - bestScore2))
                        {
                            bestScore1 = memScore1;
                            bestScore2 = memScore2;
                            bestChoice = i;
                        }
                    }
                    checkNumber.Number = Mathf.Abs(temp) - 1;
                }
            }
        }
        if (bestScore1 != -9999)
        {
            memScore1 = bestScore1;
            memScore2 = bestScore2;
        }

        return(bestChoice);
    }
Esempio n. 9
0
    public int AIchoice(int _turn, int _line, int depth, int bestScoreComp = 0, int bestScorePl = 0) //получаем ход(0-игрок,1-компьютер),вложенность, очки компьютера, очки игрока c предыдушей вложенности
    {
        int ScorePl;                                                                                 //очки игрока
        int ScoreComp;                                                                               //очки компьютера
        int bestChoice = 0;                                                                          //лучший выбор очки
        int temp       = 0;
        int bestStep   = -1;                                                                         //лучший выбор ячейка

        if (bestScorePl == 0 && bestScoreComp == 0)                                                  //если первый уровень вложенности, то взять текущщие очки
        {
            ScorePl   = PlayerPoints;
            ScoreComp = CompPoints;
        }
        else
        {
            ScorePl   = bestScorePl;
            ScoreComp = bestScoreComp;
        }
        for (int i = 0; i < poleRazmer; i++)
        {
            if (_turn == 1)//компьютер
            {
                if (cells[i, _line] != null)
                {
                    cell_Pl_vs_Pc checkNumber = cells[i, _line].GetComponent <cell_Pl_vs_Pc>();
                    int           ai          = -1;
                    if (Mathf.Abs(checkNumber.Number) != 99)
                    {
                        temp = checkNumber.Number;
                        checkNumber.Number = 98;
                        int next = 0;

                        if (depth < maxDepth)
                        {
                            ai = AIchoice(0, i, depth + 1, ScoreComp + temp, ScorePl);
                            if (ai == -1)
                            {
                                if (ScoreComp > ScorePl)
                                {
                                    //bestStep = i;
                                    //checkNumber.Number = Mathf.Abs(temp) - 1;
                                    //return bestStep;
                                    next = -9999;
                                }
                                else
                                {
                                    next = 9999;
                                    //continue;
                                }
                            }
                            else
                            {
                                cell_Pl_vs_Pc checkNumber2 = cells[i, ai].GetComponent <cell_Pl_vs_Pc>();
                                next = checkNumber2.Number;
                            }
                        }
                        if (bestChoice == 0 || bestChoice < ScoreComp - ScorePl + temp - next || bestChoice == ScoreComp - ScorePl + temp - next && Random.Range(0, 2) == 1)
                        //if(bestScoreComp-bestScorePl<)
                        {
                            bestChoice = ScoreComp - ScorePl + temp - next;
                            bestStep   = i;
                        }
                        checkNumber.Number = Mathf.Abs(temp) - 1;
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    continue;
                }
            }
            else
            {
                if (cells[_line, i] != null)
                {
                    cell_Pl_vs_Pc checkNumber = cells[_line, i].GetComponent <cell_Pl_vs_Pc>();
                    int           ai          = -1;
                    if (Mathf.Abs(checkNumber.Number) != 99)
                    {
                        temp = checkNumber.Number;
                        checkNumber.Number = 98;
                        int next = 0;

                        if (depth < maxDepth)
                        {
                            ai = AIchoice(1, i, depth + 1, ScoreComp, ScorePl + temp);
                            if (ai == -1)
                            {
                                if (ScoreComp > ScorePl)
                                {
                                    //bestStep = i;
                                    //checkNumber.Number = Mathf.Abs(temp) - 1;
                                    //return bestStep;
                                    next = 9999;
                                }
                                else
                                {
                                    //continue;
                                    next = -9999;
                                }
                            }
                            else
                            {
                                cell_Pl_vs_Pc checkNumber2 = cells[ai, i].GetComponent <cell_Pl_vs_Pc>();
                                next = checkNumber2.Number;
                            }
                        }
                        if (bestChoice == 0 || bestChoice < ScorePl - ScoreComp + temp + next || bestChoice == ScoreComp - ScorePl + temp - next && Random.Range(0, 2) == 1)
                        {
                            bestChoice = ScorePl - ScoreComp + temp - next;
                            bestStep   = i;
                        }
                        checkNumber.Number = Mathf.Abs(temp) - 1;
                    }

                    else
                    {
                        continue;
                    }
                }
                else
                {
                    continue;
                }
            }
        }

        return(bestStep);
    }