private void movementCalculate(Vector3 pos) { movements = MovementCalculate.calculate(GameController.board.clone(), new Vector2(pos.x, pos.y), this.tag); GameObject aux; foreach (Movement movement in movements) { if (movement.MoveType.Equals(Movement.Move.Move)) { aux = Instantiate(move, this.transform); Vector3 to = new Vector3(movement.To.x, movement.To.y, -aux.transform.position.y); aux.transform.localPosition = to; moveObjects.Add(aux); } else if (movement.MoveType.Equals(Movement.Move.Attack)) { aux = Instantiate(attack, this.transform); Vector3 to = new Vector3(movement.To.x, movement.To.y, -aux.transform.position.y); aux.transform.localPosition = to; moveObjects.Add(aux); } else { aux = Instantiate(win, this.transform); Vector3 to = new Vector3(movement.To.x, movement.To.y, -aux.transform.position.y); aux.transform.localPosition = to; moveObjects.Add(aux); } } }
private void generate() { List <Nodo> childrens = new List <Nodo> (); List <Nodo> fathers = new List <Nodo> (); List <Movement> movements = new List <Movement> (); fathers.Add(raiz); int j = 1; for (int i = 0; i < maxDepth; i++) { childrens.Clear(); foreach (Nodo father in fathers) { //Debug.Log (father.Name + " " + father.Board.ToString()); movements.Clear(); if (tag.Equals("RedTeam")) { if (father.MaxMin.Equals(Nodo.MaxOrMin.Max)) { if (father.Board.RedTeam [0].x != -10f) { movements.AddRange(MovementCalculate.calculate(father.Board.clone(), father.Board.RedTeam [0], "RedTeam")); foreach (Movement movement in movements) { Nodo aux = new Nodo(); aux.Name = "" + j; j++; aux.Board = father.Board.clone(); aux.Board.RedTeam [0] = movement.To; if (movement.To.Equals(father.Board.BlueTeam [0])) { aux.Board.BlueTeam [0].x = -10f; aux.Board.BlueTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.BlueTeam [1])) { aux.Board.BlueTeam [1].x = -10f; aux.Board.BlueTeam [1].y = -10f; } aux.Father = father; aux.MaxMin = father.MaxMin.Equals(Nodo.MaxOrMin.Max) ? Nodo.MaxOrMin.Min : Nodo.MaxOrMin.Max; aux.Movement = movement; if (movement.To.Equals(aux.Board.BlueTeam [2])) { aux.FUtility = 1000f; Debug.Log(aux.FUtility); } else { father.Children.Add(aux); } childrens.Add(aux); } movements.Clear(); } if (father.Board.RedTeam [1].x != -10f) { movements.AddRange(MovementCalculate.calculate(father.Board.clone(), father.Board.RedTeam [1], "RedTeam")); foreach (Movement movement in movements) { Nodo aux = new Nodo(); aux.Name = "" + j; j++; aux.Board = father.Board.clone(); aux.Board.RedTeam [1] = movement.To; if (movement.To.Equals(father.Board.BlueTeam [0])) { aux.Board.BlueTeam [0].x = -10f; aux.Board.BlueTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.BlueTeam [1])) { aux.Board.BlueTeam [1].x = -10f; aux.Board.BlueTeam [1].y = -10f; } aux.Father = father; aux.MaxMin = father.MaxMin.Equals(Nodo.MaxOrMin.Max) ? Nodo.MaxOrMin.Min : Nodo.MaxOrMin.Max; aux.Movement = movement; if (movement.To.Equals(aux.Board.BlueTeam [2])) { aux.FUtility = 1000f; Debug.Log(aux.FUtility); } father.Children.Add(aux); childrens.Add(aux); } movements.Clear(); } } else { if (father.Board.BlueTeam [0].x != -10f) { movements.AddRange(MovementCalculate.calculate(father.Board.clone(), father.Board.BlueTeam [0], "BlueTeam")); foreach (Movement movement in movements) { Nodo aux = new Nodo(); aux.Name = "" + j; j++; aux.Board = father.Board.clone(); aux.Board.BlueTeam [0] = movement.To; if (movement.To.Equals(father.Board.RedTeam [0])) { aux.Board.RedTeam [0].x = -10f; aux.Board.RedTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.RedTeam [1])) { aux.Board.RedTeam [1].x = -10f; aux.Board.RedTeam [1].y = -10f; } aux.Father = father; aux.MaxMin = father.MaxMin.Equals(Nodo.MaxOrMin.Max) ? Nodo.MaxOrMin.Min : Nodo.MaxOrMin.Max; aux.Movement = movement; if (movement.To.Equals(aux.Board.RedTeam [2])) { aux.FUtility = -1000f; Debug.Log(aux.FUtility); } father.Children.Add(aux); childrens.Add(aux); } movements.Clear(); } if (father.Board.BlueTeam [1].x != -10f) { movements.AddRange(MovementCalculate.calculate(father.Board.clone(), father.Board.BlueTeam [1], "BlueTeam")); foreach (Movement movement in movements) { Nodo aux = new Nodo(); aux.Name = "" + j; j++; aux.Board = father.Board.clone(); aux.Board.BlueTeam [1] = movement.To; if (movement.To.Equals(father.Board.RedTeam [0])) { aux.Board.RedTeam [0].x = -10f; aux.Board.RedTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.RedTeam [1])) { aux.Board.RedTeam [1].x = -10f; aux.Board.RedTeam [1].y = -10f; } aux.Father = father; aux.MaxMin = father.MaxMin.Equals(Nodo.MaxOrMin.Max) ? Nodo.MaxOrMin.Min : Nodo.MaxOrMin.Max; aux.Movement = movement; if (movement.To.Equals(aux.Board.BlueTeam [2])) { aux.FUtility = -1000f; Debug.Log(aux.FUtility); } father.Children.Add(aux); childrens.Add(aux); } movements.Clear(); } } } else { if (father.MaxMin.Equals(Nodo.MaxOrMin.Max)) { if (father.Board.BlueTeam [0].x != -10f) { movements.AddRange(MovementCalculate.calculate(father.Board.clone(), father.Board.BlueTeam [0], "BlueTeam")); foreach (Movement movement in movements) { Nodo aux = new Nodo(); aux.Name = "" + j; j++; aux.Board = father.Board.clone(); aux.Board.BlueTeam [0] = movement.To; if (movement.To.Equals(father.Board.RedTeam [0])) { aux.Board.RedTeam [0].x = -10f; aux.Board.RedTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.RedTeam [1])) { aux.Board.RedTeam [1].x = -10f; aux.Board.RedTeam [1].y = -10f; } aux.Father = father; aux.MaxMin = father.MaxMin.Equals(Nodo.MaxOrMin.Max) ? Nodo.MaxOrMin.Min : Nodo.MaxOrMin.Max; aux.Movement = movement; if (movement.To.Equals(aux.Board.RedTeam [2])) { aux.FUtility = 1000f; Debug.Log(aux.FUtility); } father.Children.Add(aux); childrens.Add(aux); } movements.Clear(); } if (father.Board.BlueTeam [1].x != -10f) { movements.AddRange(MovementCalculate.calculate(father.Board.clone(), father.Board.BlueTeam [1], "BlueTeam")); foreach (Movement movement in movements) { Nodo aux = new Nodo(); aux.Name = "" + j; j++; aux.Board = father.Board.clone(); aux.Board.BlueTeam [1] = movement.To; if (movement.To.Equals(father.Board.RedTeam [0])) { aux.Board.RedTeam [0].x = -10f; aux.Board.RedTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.RedTeam [1])) { aux.Board.RedTeam [1].x = -10f; aux.Board.RedTeam [1].y = -10f; } aux.Father = father; aux.MaxMin = father.MaxMin.Equals(Nodo.MaxOrMin.Max) ? Nodo.MaxOrMin.Min : Nodo.MaxOrMin.Max; aux.Movement = movement; if (movement.To.Equals(aux.Board.RedTeam [2])) { aux.FUtility = 1000f; Debug.Log(aux.FUtility); } father.Children.Add(aux); childrens.Add(aux); } movements.Clear(); } } else { if (father.Board.RedTeam [0].x != -10f) { movements.AddRange(MovementCalculate.calculate(father.Board.clone(), father.Board.RedTeam [0], "RedTeam")); foreach (Movement movement in movements) { Nodo aux = new Nodo(); aux.Name = "" + j; j++; aux.Board = father.Board.clone(); aux.Board.RedTeam [0] = movement.To; if (movement.To.Equals(father.Board.BlueTeam [0])) { aux.Board.BlueTeam [0].x = -10f; aux.Board.BlueTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.BlueTeam [1])) { aux.Board.BlueTeam [1].x = -10f; aux.Board.BlueTeam [1].y = -10f; } aux.Father = father; aux.MaxMin = father.MaxMin.Equals(Nodo.MaxOrMin.Max) ? Nodo.MaxOrMin.Min : Nodo.MaxOrMin.Max; aux.Movement = movement; if (movement.To.Equals(aux.Board.BlueTeam [2])) { aux.FUtility = -1000f; Debug.Log(aux.FUtility); } father.Children.Add(aux); childrens.Add(aux); } movements.Clear(); } if (father.Board.RedTeam [1].x != -10f) { movements.AddRange(MovementCalculate.calculate(father.Board.clone(), father.Board.RedTeam [1], "RedTeam")); foreach (Movement movement in movements) { Nodo aux = new Nodo(); aux.Name = "" + j; j++; aux.Board = father.Board.clone(); aux.Board.RedTeam [1] = movement.To; if (movement.To.Equals(father.Board.BlueTeam [0])) { aux.Board.BlueTeam [0].x = -10f; aux.Board.BlueTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.BlueTeam [1])) { aux.Board.BlueTeam [1].x = -10f; aux.Board.BlueTeam [1].y = -10f; } aux.Father = father; aux.MaxMin = father.MaxMin.Equals(Nodo.MaxOrMin.Max) ? Nodo.MaxOrMin.Min : Nodo.MaxOrMin.Max; aux.Movement = movement; if (movement.To.Equals(aux.Board.BlueTeam [2])) { aux.FUtility = -1000f; Debug.Log(aux.FUtility); } father.Children.Add(aux); childrens.Add(aux); } movements.Clear(); } } } } fathers.Clear(); foreach (Nodo children in childrens) { if (float.IsNaN(children.FUtility)) { fathers.Add(children); } } } fathers.Clear(); /*foreach (Nodo children in childrens) { * if (float.IsNaN (children.FUtility)) { * children.FUtility = children.Board.fUtility (this.tag); * //Debug.Log (children.FUtility); * } * }*/ fathers.Add(raiz); while (fathers.Count > 0) { foreach (Nodo father in fathers) { if (father.Children.Count == 0 && float.IsNaN(father.FUtility)) { /* * if (father.Father.MaxMin.Equals (Nodo.MaxOrMin.Max)) { * father.FUtility = father.Board.fUtility (this.tag); * } else { * if (this.tag.Equals("RedTeam")){ * father.FUtility = -father.Board.fUtility ("BlueTeam"); * }else { * father.FUtility = -father.Board.fUtility ("RedTeam"); * } * } */ father.FUtility = father.Board.fUtility(this.tag); //Debug.Log (father.FUtility); } childrens.AddRange(father.Children); } fathers.Clear(); fathers.AddRange(childrens); childrens.Clear(); } childrens.Clear(); }
private void generate() { List <Nodo> childrens = new List <Nodo> (); List <Nodo> fathers = new List <Nodo> (); List <Movement> movements = new List <Movement> (); childrens.Clear(); fathers.Clear(); movements.Clear(); fathers.Add(raiz); int j = 1; for (int i = 0; i < this.maxDepth; i++) { foreach (Nodo father in fathers) { if (this.tag.Equals("BlueTeam")) //A IA esta no time azul { if (father.MaxMin.Equals(Nodo.MaxOrMin.Max)) //Verifica se o pai é max, para fazer o movimento da IA { if (father.Board.BlueTeam [0].x != -10.0f) //Verifica se o primeiro personagem está em jogo { movements.AddRange(MovementCalculate.calculate(father.Board, father.Board.BlueTeam [0], "BlueTeam")); foreach (Movement movement in movements) // Para cada movemento será criado um novo nodo filho { Nodo nodo = new Nodo(); nodo.Board = father.Board.clone(); nodo.Board.BlueTeam [0] = movement.To; if (movement.To.Equals(father.Board.RedTeam [0])) { nodo.Board.RedTeam [0].x = -10f; nodo.Board.RedTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.RedTeam [1])) { nodo.Board.RedTeam [1].x = -10f; nodo.Board.RedTeam [1].y = -10f; } nodo.Father = father; nodo.MaxMin = Nodo.MaxOrMin.Min; nodo.Movement = movement; nodo.Name = "" + j; j++; if (movement.To.Equals(GameController.board.RedTeam [2])) { nodo.FUtility = 1000; Debug.Log(nodo.FUtility); } father.addChildren(nodo); } movements.Clear(); } if (father.Board.BlueTeam [1].x != -10.0f) //Verifica se o primeiro personagem está em jogo { movements.AddRange(MovementCalculate.calculate(father.Board, father.Board.BlueTeam [1], "BlueTeam")); foreach (Movement movement in movements) // Para cada movemento será criado um novo nodo filho { Nodo nodo = new Nodo(); nodo.Board = father.Board.clone(); nodo.Board.BlueTeam [1] = movement.To; if (movement.To.Equals(father.Board.RedTeam [0])) { nodo.Board.RedTeam [0].x = -10f; nodo.Board.RedTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.RedTeam [1])) { nodo.Board.RedTeam [1].x = -10f; nodo.Board.RedTeam [1].y = -10f; } nodo.Father = father; nodo.MaxMin = Nodo.MaxOrMin.Min; nodo.Movement = movement; nodo.Name = "" + j; j++; if (movement.To.Equals(GameController.board.RedTeam [2])) { nodo.FUtility = 1000; Debug.Log(nodo.FUtility); } father.addChildren(nodo); } movements.Clear(); } } else //Verifica se o pai é min, para fazer o movimento do Player { if (father.Board.RedTeam [0].x != -10.0f) //Verifica se o primeiro personagem está em jogo { movements.AddRange(MovementCalculate.calculate(father.Board, father.Board.RedTeam [0], "RedTeam")); foreach (Movement movement in movements) // Para cada movemento será criado um novo nodo filho { Nodo nodo = new Nodo(); nodo.Board = father.Board.clone(); nodo.Board.RedTeam [0] = movement.To; if (movement.To.Equals(father.Board.BlueTeam [0])) { nodo.Board.BlueTeam [0].x = -10f; nodo.Board.BlueTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.BlueTeam [1])) { nodo.Board.BlueTeam [1].x = -10f; nodo.Board.BlueTeam [1].y = -10f; } nodo.Father = father; nodo.MaxMin = Nodo.MaxOrMin.Max; nodo.Movement = movement; nodo.Name = "" + j; j++; if (movement.To.Equals(GameController.board.BlueTeam [2])) { nodo.FUtility = -1000; Debug.Log(nodo.FUtility); } father.addChildren(nodo); } movements.Clear(); } if (father.Board.RedTeam [1].x != -10.0f) //Verifica se o primeiro personagem está em jogo { movements.AddRange(MovementCalculate.calculate(father.Board, father.Board.RedTeam [1], "RedTeam")); foreach (Movement movement in movements) // Para cada movemento será criado um novo nodo filho { Nodo nodo = new Nodo(); nodo.Board = father.Board.clone(); nodo.Board.RedTeam [1] = movement.To; if (movement.To.Equals(father.Board.BlueTeam [0])) { nodo.Board.BlueTeam [0].x = -10f; nodo.Board.BlueTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.BlueTeam [1])) { nodo.Board.BlueTeam [1].x = -10f; nodo.Board.BlueTeam [1].y = -10f; } nodo.Father = father; nodo.MaxMin = Nodo.MaxOrMin.Max; nodo.Movement = movement; nodo.Name = "" + j; j++; if (movement.To.Equals(GameController.board.BlueTeam [2])) { nodo.FUtility = -1000; Debug.Log(nodo.FUtility); } father.addChildren(nodo); } movements.Clear(); } } } else //A IA esta no time vermelho { if (father.MaxMin.Equals(Nodo.MaxOrMin.Max)) //Verifica se o pai é max, para fazer o movimento da IA { if (father.Board.RedTeam [0].x != -10.0f) //Verifica se o primeiro personagem está em jogo { movements.AddRange(MovementCalculate.calculate(father.Board, father.Board.RedTeam [0], "RedTeam")); foreach (Movement movement in movements) // Para cada movemento será criado um novo nodo filho { Nodo nodo = new Nodo(); nodo.Board = father.Board.clone(); nodo.Board.RedTeam [0] = movement.To; if (movement.To.Equals(father.Board.BlueTeam [0])) { nodo.Board.BlueTeam [0].x = -10f; nodo.Board.BlueTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.BlueTeam [1])) { nodo.Board.BlueTeam [1].x = -10f; nodo.Board.BlueTeam [1].y = -10f; } nodo.Father = father; nodo.MaxMin = Nodo.MaxOrMin.Min; nodo.Movement = movement; nodo.Name = "" + j; j++; if (movement.To.Equals(GameController.board.BlueTeam [2])) { nodo.FUtility = 1000; Debug.Log(nodo.FUtility); } father.addChildren(nodo); } movements.Clear(); } if (father.Board.RedTeam [1].x != -10.0f) //Verifica se o primeiro personagem está em jogo { movements.AddRange(MovementCalculate.calculate(father.Board, father.Board.RedTeam [1], "RedTeam")); foreach (Movement movement in movements) // Para cada movemento será criado um novo nodo filho { Nodo nodo = new Nodo(); nodo.Board = father.Board.clone(); nodo.Board.RedTeam [1] = movement.To; if (movement.To.Equals(father.Board.BlueTeam [0])) { nodo.Board.BlueTeam [0].x = -10f; nodo.Board.BlueTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.BlueTeam [1])) { nodo.Board.BlueTeam [1].x = -10f; nodo.Board.BlueTeam [1].y = -10f; } nodo.Father = father; nodo.MaxMin = Nodo.MaxOrMin.Min; nodo.Movement = movement; nodo.Name = "" + j; j++; if (movement.To.Equals(GameController.board.BlueTeam [2])) { nodo.FUtility = 1000; Debug.Log(nodo.FUtility); } father.addChildren(nodo); } movements.Clear(); } } else //Verifica se o pai é min, para fazer o movimento do Player { if (father.Board.BlueTeam [0].x != -10.0f) //Verifica se o primeiro personagem está em jogo { movements.AddRange(MovementCalculate.calculate(father.Board, father.Board.BlueTeam [0], "BlueTeam")); foreach (Movement movement in movements) // Para cada movemento será criado um novo nodo filho { Nodo nodo = new Nodo(); nodo.Board = father.Board.clone(); nodo.Board.BlueTeam [0] = movement.To; if (movement.To.Equals(father.Board.RedTeam [0])) { nodo.Board.RedTeam [0].x = -10f; nodo.Board.RedTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.RedTeam [1])) { nodo.Board.RedTeam [1].x = -10f; nodo.Board.RedTeam [1].y = -10f; } nodo.Father = father; nodo.MaxMin = Nodo.MaxOrMin.Max; nodo.Movement = movement; nodo.Name = "" + j; j++; if (movement.To.Equals(GameController.board.RedTeam [2])) { nodo.FUtility = -1000; Debug.Log(nodo.FUtility); } father.addChildren(nodo); } movements.Clear(); } if (father.Board.BlueTeam [1].x != -10.0f) //Verifica se o primeiro personagem está em jogo { movements.AddRange(MovementCalculate.calculate(father.Board, father.Board.BlueTeam [1], "BlueTeam")); foreach (Movement movement in movements) // Para cada movemento será criado um novo nodo filho { Nodo nodo = new Nodo(); nodo.Board = father.Board.clone(); nodo.Board.BlueTeam [1] = movement.To; if (movement.To.Equals(father.Board.RedTeam [0])) { nodo.Board.RedTeam [0].x = -10f; nodo.Board.RedTeam [0].y = -10f; } else if (movement.To.Equals(father.Board.RedTeam [1])) { nodo.Board.RedTeam [1].x = -10f; nodo.Board.RedTeam [1].y = -10f; } nodo.Father = father; nodo.MaxMin = Nodo.MaxOrMin.Max; nodo.Movement = movement; nodo.Name = "" + j; j++; if (movement.To.Equals(GameController.board.RedTeam [2])) { nodo.FUtility = -1000; Debug.Log(nodo.FUtility); } father.addChildren(nodo); } movements.Clear(); } } } childrens.AddRange(father.Children); } fathers.Clear(); foreach (Nodo nodo in childrens) { if (float.IsNaN(nodo.FUtility)) { fathers.Add(nodo); } } childrens.Clear(); } fathers.Clear(); fathers.Add(raiz); while (fathers.Count > 0) { foreach (Nodo father in fathers) { if (father.Children.Count == 0 && float.IsNaN(father.FUtility)) { father.FUtility = father.Board.fUtility(this.tag); } childrens.AddRange(father.Children); } fathers.Clear(); fathers.AddRange(childrens); childrens.Clear(); } childrens.Clear(); }