예제 #1
0
        public void spawnNewState(State state)
        {
            int          temp;
            State        s;
            List <State> sList         = new List <State>();
            List <int>   choose        = new List <int>();
            List <int>   evalue        = new List <int>();
            List <Move>  availableMove = state.board.getAvailableMove(state.player, true);

            for (int i = 0; i < availableMove.Count; i++)
            {
                s = state.Clone();
                s.board.movePiece(availableMove[i].source, availableMove[i].dest, null, false);
                temp = heuristic(s.board, s.player);
                s.updatePlayer();
                s.createMove = availableMove[i];
                sList.Add(s);
                evalue.Add(temp);
            }
            for (int i = 0; i < evalue.Count; i++)
            {
                if (choose.Count < 5)
                {
                    choose.Add(i);
                    continue;
                }
                else
                {
                    for (int j = 0; j < choose.Count; j++)
                    {
                        if (evalue[i] > evalue[choose[j]])
                        {
                            choose[j] = i;
                            break;
                        }
                    }
                }
            }
            foreach (int i in choose)
            {
                //if (sList[i].board.gameOver == false)
                //{
                state.availableChild++;
                sList[i].parent = state;
                state.child.Add(sList[i]);
                notPlayed.Add(sList[i]);
                //}
            }
        }
예제 #2
0
        public Move selectMove(State state)
        {
            Random r = new Random();
            //List<Move> availableMove = state.board.getAvailableMove(state.player.color);
            //Move choose = availableMove[r.Next(availableMove.Count)];
            //return choose;
            List <Move> availableMove = state.board.getAvailableMove(state.player, true);
            //tempListMove = availableMove;
            List <Move>   bestMove = new List <Move>();
            List <double> evalue   = new List <double>();
            State         s;
            int           max = Int32.MinValue, temp;
            Move          choose = null;

            foreach (Move move in availableMove)
            {
                s = state.Clone();
                s.board.movePiece(move.source, move.dest, null, false);
                temp = heuristic(s.board, state.player);
                //if (temp >= max)
                //{
                //    max = temp;
                //    choose = move;
                //}
                if (bestMove.Count < 5)
                {
                    bestMove.Add(move);
                    evalue.Add(temp);
                }
                else
                {
                    for (int i = 0; i < evalue.Count; i++)
                    {
                        if (temp > evalue[i])
                        {
                            evalue[i]   = temp;
                            bestMove[i] = move;
                            break;
                        }
                    }
                }
            }
            return(bestMove[r.Next(bestMove.Count)]);
            //return availableMove[r.Next(availableMove.Count)];
        }
예제 #3
0
        public void simulate()
        {
            //Random r=new Random();
            //int choose = r.Next(notPlayed.Count);

            //State state = notPlayed[choose];
            State state = chooseState(rootState);

            if (state.playAmount > 4)
            {
                spawnNewState(state);
            }
            stateList.Add(state);
            notPlayed.Remove(state);
            state.parent.availableChild--;
            //notPlayed.RemoveAt(choose);

            State rState     = state.Clone();
            int   moveAmount = 0;

            while (moveAmount < 100)
            {
                Move move = selectMove(rState);
                rState.board.movePiece(move.source, move.dest, null, false);
                rState.updatePlayer();
                //if (moveAmount == 0)
                //{
                //    State s = state.Copy();
                //    s.parent = state;
                //    notPlayed.Add(s);
                //}
                if (rState.board.gameOver)
                {
                    winner = rState.board.winner;
                    break;
                }
                moveAmount++;
            }
            if (moveAmount < 120)
            {
                backpropagate(state, winner);
            }
        }