private void Start() { if (arbol.arbol[0] != null) { camara.transform.position = arbol.arbol[0].transform.position + Vector3.up; BusquedaEnProfundidad(); BusquedaEnAnchura(); if (arbol.arbol[0].GetComponent <TicTacToeTablero>().turno % 2 == 0) { CalculaCosto("X"); } else { CalculaCosto("O"); } for (int k = 0; k < arbol.arbol.Count; k++) { TicTacToeTablero tablero = arbol.arbol[k].GetComponent <TicTacToeTablero>(); if (tablero.costo == 0.0f) { arbol.arbol[k].transform.localScale = Vector3.zero; } else { arbol.arbol[k].transform.localScale = Vector3.one * (1 + tablero.costo / 500.0f); } } } ultima_evaluacion = Time.realtimeSinceStartup; }
void CrearArbolProfundidad(ref GameObject nodo, ref int turno, int profundidad) { TicTacToeTablero tablero = nodo.GetComponent <TicTacToeTablero>(); if (turno == profundidad) { return; } if (tablero.TerminoJuego()) { Debug.Log("No se pueden crear más nodos, el juego se termino"); return; } List <int> movs = tablero.Movimientos(); for (int k = 0; k < movs.Count; k++) { GameObject siguiente = Instantiate <GameObject>(nodo); siguiente.GetComponent <TicTacToeTablero>().hijos = new List <GameObject>(); siguiente.GetComponent <TicTacToeTablero>().Mover(movs[k]); //siguiente.SetActive(false); turno++; CrearArbolProfundidad(ref siguiente, ref turno, profundidad); turno--; siguiente.GetComponent <TicTacToeTablero>().padre = nodo; nodo.GetComponent <TicTacToeTablero>().hijos.Add(siguiente); siguiente.name = "Board" + arbol.Count; arbol.Add(siguiente); } }
void CalculaCosto(string jugador) { string contrincante = (jugador == "X")? "O":"X"; for (int k = 0; k < arbol.arbol.Count; k++) { TicTacToeTablero tablero = arbol.arbol[k].GetComponent <TicTacToeTablero>(); int profundidad = tablero.turno - arbol.arbol[0].GetComponent <TicTacToeTablero>().turno; int costo; // Gana el jugador actual if (tablero.TresEnLinea(jugador)) { GameObject go = arbol.arbol[k]; costo = 10 - profundidad; while (go != null) { if (go == go.GetComponent <TicTacToeTablero>().padre) { break; } if (go.GetComponent <TicTacToeTablero>().padre == null) { break; } //if(costo < go.GetComponent<TicTacToeTablero>().costo) { // go.GetComponent<TicTacToeTablero>().costo = costo; //} go.GetComponent <TicTacToeTablero>().costo += costo; go = go.GetComponent <TicTacToeTablero>().padre; } } // Empate if (tablero.Empate()) { GameObject go = arbol.arbol[k]; while (go != null) { if (go == go.GetComponent <TicTacToeTablero>().padre) { break; } if (go.GetComponent <TicTacToeTablero>().padre == null) { break; } go.GetComponent <TicTacToeTablero>().costo += 0; go = go.GetComponent <TicTacToeTablero>().padre; } } // Gana el contrincante if (tablero.TresEnLinea(contrincante)) { GameObject go = arbol.arbol[k]; costo = profundidad - 10; while (go != null) { if (go == go.GetComponent <TicTacToeTablero>().padre) { break; } if (go.GetComponent <TicTacToeTablero>().padre == null) { break; } //if(costo < go.GetComponent<TicTacToeTablero>().costo) { // go.GetComponent<TicTacToeTablero>().costo = costo; //} go.GetComponent <TicTacToeTablero>().costo += costo; go = go.GetComponent <TicTacToeTablero>().padre; } } } }