IEnumerator JogadaIA() { isCoroutineStarted = true; yield return(new WaitForSeconds(3)); TOEstado novoEstado = null; Random rnd = new Random(); int rndI = 0; List <TOEstado> lista; switch (config.NivelDificuldade) { case EnumDificuldade.FACIL: lista = algo.GeraEstados(this.tabuleiroAtual, jogadorIa); rndI = rnd.Next(0, lista.Count); novoEstado = lista[rndI]; break; case EnumDificuldade.MEDIO: rndI = rnd.Next(0, 100); if (rndI > 60) { lista = algo.GeraEstados(this.tabuleiroAtual, jogadorIa); rndI = rnd.Next(0, lista.Count); novoEstado = lista[rndI]; } else { novoEstado = algo.MinMaxV2(this.tabuleiroAtual, jogadorIa, 0); } break; case EnumDificuldade.DIFICIL: novoEstado = algo.MinMaxV2(this.tabuleiroAtual, jogadorIa, 0); break; } this.tabuleiroAtual = novoEstado; MatrixParaTabuleiro(novoEstado.Tabuleiro); if (this.tabuleiroAtual.EhEstadoFinal()) { estadoPartida = EnumEstadoPartida.FINALIZANDOJOGO; } else { estadoPartida = EnumEstadoPartida.JOGADOR01; } isCoroutineStarted = false; }
public void TabuleiroInicial() { tabuleiroAtual = new TOEstado(); //Proximo a jogar eh o Min tabuleiroAtual.Tabuleiro = new Int32[, ] { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }; MatrixParaTabuleiro(tabuleiroAtual.Tabuleiro); if (config.IaInicia) { estadoPartida = EnumEstadoPartida.JOGADOR02; } else { estadoPartida = EnumEstadoPartida.JOGADOR01; } }
public TOEstado MinMaxV2(TOEstado estadoAtual, EnumEstado jogadorAtual, int i) { if (estadoAtual.EhEstadoFinal()) { return(estadoAtual); } List <TOEstado> estados = null; TOEstado melhorEstado = null;; Int16 aval = 0; if (jogadorAtual == EnumEstado.MIN) { estados = this.GeraEstados(estadoAtual, EnumEstado.MIN); foreach (TOEstado filho in estados) { TOEstado avaliacao = MinMaxV2(filho, EnumEstado.MAX, i + 1); filho.Ganhador = avaliacao.Ganhador; if (((Int16)avaliacao.Ganhador) <= aval) { melhorEstado = filho; aval = (Int16)avaliacao.Ganhador; } } } else if (jogadorAtual == EnumEstado.MAX) { estados = this.GeraEstados(estadoAtual, EnumEstado.MAX); foreach (TOEstado filho in estados) { TOEstado avaliacao = MinMaxV2(filho, EnumEstado.MIN, i + 1); filho.Ganhador = avaliacao.Ganhador; if (((Int16)avaliacao.Ganhador) >= aval) { melhorEstado = filho; aval = (Int16)avaliacao.Ganhador; } } } return(melhorEstado == null ? estados[0]: melhorEstado); }
public List <TOEstado> GeraEstados(TOEstado estadoAtual, EnumEstado jogador) { List <TOEstado> filhos = new List <TOEstado>(); Int32[,] matrizEstado = estadoAtual.Tabuleiro; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (matrizEstado[i, j] == 0) { Int32[,] novo = copiaMatriz(matrizEstado); TOEstado novoEstado = new TOEstado(); novo[i, j] = (Int32)jogador; novoEstado.Tabuleiro = novo; filhos.Add(novoEstado); } } } return(filhos); }