예제 #1
0
        public static Nodo[] addNodosArvore(Nodo ArvoreNodo, Enums.pColor cor)
        {
            List<Nodo> result = new List<Nodo>();

            for (int i = 0; i < 137; i++){
                // Se é uma peça da mesma cor
                if (getPecaCor(ArvoreNodo.estadoTabuleiro[i]) == cor) {
                    // e ela tem pra onde ir
                    // então cria o nodo pra cada uma das possiveis jogadas
                    foreach(int casa in getCasasVisiveis(ArvoreNodo.estadoTabuleiro, i)) {
                        result.Add(new Nodo(ArvoreNodo.estadoTabuleiro,
                                            ArvoreNodo.estadoTabuleiro[i],
                                            i,
                                            casa));
                    }
                }
            }

            return result.ToArray();
        }
예제 #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            jogadores = new List<player>();
            TabuleiroVetor = new int[137]{
                0, 0, 0, 1, 0, 0, 0,
                  0, 0, 3, 3, 0, 0,
                0, 0, 2, 2, 2, 0, 0,
                  0, 0, 3, 3, 0, 0,
                0, 0, 0, 2, 0, 0, 0,
                  0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0,
                  0, 0, 0, 0, 0, 0,
                0, 0, 0, 5, 0, 0, 0,
                  0, 0, 6, 6, 0, 0,
                0, 0, 5, 5, 5, 0, 0,
                  0, 0, 6, 6, 0, 0,
                0, 0, 0, 4, 0, 0, 0
            };

            token_posicao = 0;
            JogoEmAndamento = true;

            //Draw();
            criaPecas();

            player p1 = new player();
            p1.Jogador = Enums.pType.PC;
            p1.name = "PC";

            player p2 = new player();
            p2.Jogador = Enums.pType.humano;
            p2.name = "Humano";

            jogadores.Add(p1);
            jogadores.Add(p2);

            if(radioButton1.Checked){
                p1.Cor = Enums.pColor.branca;
                p2.Cor = Enums.pColor.preta;
                token_jogador = p1;
            }
            else{
                p2.Cor = Enums.pColor.branca;
                p1.Cor = Enums.pColor.preta;
                token_jogador = p2;
            }

            lblJogada.Text = token_jogador.name + " - peças " + token_jogador.Cor.ToString() + "s";

            listBoxUltimasJogadas.Items.Clear();

            ArvoreDecisao = new Nodo(TabuleiroVetor);
            ArvoreDecisao.filhos = Core.Core.addNodosArvore(ArvoreDecisao, token_jogador.Cor);

            if (token_jogador.Jogador == Enums.pType.PC)
                JogadaPC();
        }
예제 #3
0
        public static int Negamax(Nodo node, int depth, int alfa, int beta, int saldo)
        {
            foreach (Nodo filho in node.filhos) {
                saldo = filho.CapturaBalance - saldo;
                int val = - Negamax(filho, depth - 1, -beta, -alfa, saldo) - saldo;

                // Alfa-beta corte
                if (val >= beta)
                    return val;
                if (val >= alfa)
                    return val;
            }

            if (node.filhos.Length == 0 || depth == 0)
                return node.CapturaBalance - saldo;

            return alfa;
        }
예제 #4
0
        private void JogadaPC()
        {
            if (JogoEmAndamento)
            {
                int position = 0;
                bool jogadaOk = false;
                int qtdTentativasJogadas = 0;
                int indicePositionCapturaObrigatoria = 0;

                if (CapturaObrigatoria(token_jogador.Cor))
                    indicePositionCapturaObrigatoria = RealizaCapturaObrigatoria(token_jogador.Cor);

                //Verifica se é obrigado a capturar alguma peça e tem como realizar a captura
                if (indicePositionCapturaObrigatoria != 0)
                    position = indicePositionCapturaObrigatoria;
                else
                {
                    //do {
                    //    qtdTentativasJogadas++;

                    //    // qualquer jogada...
                    //    if (!jogadaOk && qtdTentativasJogadas > 100) {
                    //        token_posicao = EscolhePecaPc(token_jogador.Cor);
                    //        foreach (int peca in Core.Core.getCasasVisiveis(TabuleiroVetor, token_posicao)) {
                    //            //SELECIONA QUALQUER CASA PARA SE MOVER E VAI CAVALO
                    //            position = peca; //no momento pega a ultima casa avaliada
                    //        }
                    //        jogadaOk = true;
                    //    }

                    //} while (!jogadaOk);

                    Enums.pColor rivalCor = (token_jogador.Cor == Enums.pColor.branca) ? Enums.pColor.preta : Enums.pColor.branca;

                    ArvoreDecisao = new Nodo(TabuleiroVetor);
                    ArvoreDecisao.filhos = Core.Core.addNodosArvore(ArvoreDecisao, token_jogador.Cor);
                    for (int i = 0; i < ArvoreDecisao.filhos.Length; i++) {
                        ArvoreDecisao.filhos[i].filhos = Core.Core.addNodosArvore(ArvoreDecisao.filhos[i], rivalCor);
                    }

                    int iBest = position;
                    int iBestFrom = token_posicao;
                    int bestCount = 0;
                    foreach (Nodo filho in ArvoreDecisao.filhos) {
                        if (filho.MovePara > 0) {
                            int minmaxNodo = Core.Core.Negamax(filho, (int)numericUpDown1.Value, 10, 15, -10);
                            if (minmaxNodo > bestCount) {
                                bestCount = minmaxNodo;
                                iBest = filho.MovePara;
                                iBestFrom = filho.MoveDe;
                            }
                        }
                    }
                    token_posicao = iBestFrom;
                    position = iBest;

                    //move a peca
                    TabuleiroVetor[position] = TabuleiroVetor[iBestFrom];
                    TabuleiroVetor[iBestFrom] = 0;
                }
                redesenhaTabuleiro();

                listBoxUltimasJogadas.Items.Add(String.Format("peca {0}: {1} -> {2}",
                                                getPecaCor(position).ToString(), token_posicao.ToString(), position.ToString()));
                ScrollHistoryToTheEnd();

                RemovePecasCapturadas(getPecaCor(position));
                CheckFinishGame();

                if (PreparaProxJogada(position) && token_jogador.Jogador == Enums.pType.PC)
                    JogadaPC();

                JogadaCompleta = true;
            }
        }