示例#1
0
        public override GrammarParseResult VisitSumRoll(SumRollContext context)
        {
            if (context == null)
            {
                return(GrammarParseResult.Unsuccessful(context.GetText()));
            }

            Debug.WriteLine($"VisitSumRoll \"{context.GetText()}\"");

            GrammarParseResult childNumDice = VisitExpression(context.num);

            childNumDice.Label = "# Dice";
            GrammarParseResult childDiceSides = VisitExpression(context.sides);

            childDiceSides.Label = "Dice Sides";
            int numDice   = childNumDice.Value;
            int diceSides = childDiceSides.Value;

            GrammarParseResult result = new GrammarParseResult(context.GetText());

            result.Children.Add(childNumDice);
            result.Children.Add(childDiceSides);

            result.EvaluatedText = $"{numDice}d{diceSides}";

            DiceResult diceResult = DiceUtil.Roll(numDice, diceSides);

            result.Value  = diceResult.Total;
            result.Output = String.Join(", ", diceResult.Values);

            return(result);
        }
 public EwnController()
 {
     turn               = Turn.PLAYER2;
     moveChessNum       = DiceUtil.GetChessNum();
     player1            = new AiPlayer();
     player2            = new UserPlayer();
     chessButtonHandler = new System.EventHandler(OnButtonClick);
 }
示例#3
0
    private void rollForShipEventAction()
    {
        infoText          = "Roll for Ship Event";
        diceAnimFinished += shipEventActionFinished;
        int roll = DiceUtil.rollDie(12);

        RpcShowShipEventActionRoll(roll);
    }
示例#4
0
    private void rollForShipEventTrigger(int holds)
    {
        infoText = "Check for Ship Event";
        int[] eventRoll      = DiceUtil.rollDice(3 + holds);
        bool  shipEventFound = DiceUtil.hasPairLessThan(eventRoll, ShipEventCounter);

        diceAnimFinished += shipEventTriggerFinished;
        RpcShowShipEventTriggerRoll(eventRoll, shipEventFound);
    }
示例#5
0
        public void TestRollDisadvantage()
        {
            int diceSides = 20;

            //1000 disadvantage should ensure the min (1) is returned
            int value = DiceUtil.RollAdvantage(-1000, diceSides);

            Assert.Equal(1, value);
        }
示例#6
0
        public void TestRollAdvantage()
        {
            int diceSides = 20;

            //1000 advantage should ensure the max (20) is returned
            int value = DiceUtil.RollAdvantage(1000, diceSides);

            Assert.Equal(diceSides, value);
        }
示例#7
0
        public void TestRoll(int numDice, int diceSides)
        {
            //Repeat 100 times to make sure nothing crazy is going on
            for (int i = 0; i < 100; i++)
            {
                int value = DiceUtil.Roll(numDice, diceSides);

                Assert.InRange(value, 1 * numDice, diceSides * numDice);
            }
        }
示例#8
0
    public void rollConflict(int side)
    {
        int        index      = (side == ConflictPanel.LEFTSIDE ? 0 : 1);
        int        otherIndex = (index + 1) % 2;
        List <int> fight      = conflictManager.getCurrentFight();

        int[] dice;
        int[] droneRoll = DiceUtil.rollDice(4);// new int[] { 3, 2, 5, 5 };
        if (fight[index] < 0)
        {
            dice = new int[] { 3, 2, 5, 5 }
        }
        ;                                  //dice = DiceUtil.rollDice(4);
        else
        {
            dice = DiceUtil.rollDice(3); //new int[] { 5, 5, 6 };
        }
        if (side == ConflictPanel.LEFTSIDE)
        {
            conflictManager.fightRollLeft(new List <int>(dice));
            if (fight[otherIndex] < 0)
            {
                conflictManager.fightRollRight(new List <int>(droneRoll));//DiceUtil.rollDice(4)));
            }
        }
        else
        {
            conflictManager.fightRollRight(new List <int>(dice));
            if (fight[otherIndex] < 0)
            {
                conflictManager.fightRollLeft(new List <int>(droneRoll));// DiceUtil.rollDice(4)));
            }
        }

        if (fight[otherIndex] < 0)
        {
            RpcDisplayConflictRoll(conflictManager.getLeftRoll().ToArray(), conflictManager.getRightRoll().ToArray());
        }
        else
        {
            if (side == ConflictPanel.LEFTSIDE)
            {
                RpcDisplayConflictRoll(conflictManager.getLeftRoll().ToArray(), null);
            }
            else
            {
                RpcDisplayConflictRoll(null, conflictManager.getRightRoll().ToArray());
            }
        }
    }
示例#9
0
    private void shipEventTriggerFinished(int[] outcome)
    {
        bool shipEventFound = DiceUtil.hasPairLessThan(outcome, ShipEventCounter);

        StartCoroutine(delayedAction(2f, () =>
        {
            diceAnimFinished -= shipEventTriggerFinished;
            if (shipEventFound)
            {
                rollForShipEventAction();
            }
            else
            {
                moveDrones();
            }
        }));
    }
示例#10
0
        public override GrammarParseResult VisitModifierRoll(ModifierRollContext context)
        {
            if (context == null)
            {
                return(GrammarParseResult.Unsuccessful(context.GetText()));
            }

            Debug.WriteLine($"VisitModifierRoll \"{context.GetText()}\"");

            int numAdvantage = context.ADVANTAGE().Length - context.DISADVANTAGE().Length;
            GrammarParseResult childDiceSides = VisitExpression(context.expression());

            childDiceSides.Label = "Dice Sides";
            int diceSides = childDiceSides.Value;

            GrammarParseResult result = new GrammarParseResult(context.GetText());
            string             advStr;

            if (numAdvantage == 0)
            {
                advStr = String.Empty;
            }
            else if (numAdvantage > 0)
            {
                advStr = "Highest of ";
            }
            else
            {
                advStr = "Lowest of ";
            }

            result.EvaluatedText = $"{advStr}{Math.Abs(numAdvantage)+1}d{diceSides}";
            result.Children.Add(childDiceSides);

            DiceResult diceResult = DiceUtil.RollAdvantage(numAdvantage, diceSides);

            result.Value  = diceResult.Total;
            result.Output = String.Join(", ", diceResult.Values);

            return(result);
        }
        private void NextTurn()
        {
            if (chessBoardView.chessBoardHash[0, 0] == ChessOwner.PLAYER2)
            {
                MessageBox.Show("Player2 Win");
                return;
            }
            Thread.Sleep(100);
            AiTurn();
            if (chessBoardView.chessBoardHash[ChessBoardView.CHESS_BOARD_SIZE - 1, ChessBoardView.CHESS_BOARD_SIZE - 1] == ChessOwner.PLAYER1)
            {
                MessageBox.Show("Player1 Win");
                return;
            }
            turn         = Turn.PLAYER2;
            moveChessNum = DiceUtil.GetChessNum();
            IPlayer p = player2;

            player1Label.Visible = false;
            player2Label.Visible = true;

            diceLabel.Text = moveChessNum.ToString();

            ArrayList candidates = new ArrayList();

            if ((p.Chesses[moveChessNum] as Chess).state == ChessState.ALIVE)
            {
                candidates.Add(moveChessNum);
            }
            if (candidates.Count != 0)
            {
                return;
            }

            /*int dis = 1;
             * while (candidates.Count == 0 && dis <= 5)
             * {
             *  if (moveChessNum + dis <= 5 && (p.Chesses[moveChessNum + dis] as Chess).state == ChessState.ALIVE)
             *      candidates.Add(moveChessNum + dis);
             *  if (moveChessNum - dis >= 0 && (p.Chesses[moveChessNum - dis] as Chess).state == ChessState.ALIVE)
             *      candidates.Add(moveChessNum - dis);
             *  dis++;
             * }*/

            for (int i = moveChessNum + 1; i <= 5; i++)
            {
                if ((p.Chesses[i] as Chess).state == ChessState.ALIVE)
                {
                    candidates.Add(i);
                    break;
                }
            }

            for (int i = moveChessNum - 1; i >= 0; i--)
            {
                if ((p.Chesses[i] as Chess).state == ChessState.ALIVE)
                {
                    candidates.Add(i);
                    break;
                }
            }

            if (candidates.Count == 0)
            {
                MessageBox.Show(p == player1 ? "Player2 Win" : "Player1 Win");
            }
            else if (candidates.Count == 2)
            {
                DialogResult dialogRes = MessageBox.Show("2 candidates(" + candidates[0].ToString() + ", " + candidates[1].ToString() + "). Select " + candidates[0].ToString() + " ?", "Select", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dialogRes == DialogResult.Yes)
                {
                    moveChessNum = (int)candidates[0];
                }
                else
                {
                    moveChessNum = (int)candidates[1];
                }
            }
            else if (candidates.Count == 1)
            {
                MessageBox.Show("Can only move " + (int)candidates[0] + ".");
                moveChessNum = (int)candidates[0];
            }
        }
        private void AiTurn()
        {
            moveChessNum = DiceUtil.GetChessNum();
            turn         = Turn.PLAYER1;
            IPlayer p = player1;

            diceLabel.Text       = moveChessNum.ToString();
            player1Label.Visible = true;
            player2Label.Visible = false;



            ArrayList candidates = new ArrayList();
            Minimax   mm;
            int       pos;

            Application.DoEvents();
            //Thread.Sleep(200);
            if ((p.Chesses[moveChessNum] as Chess).state == ChessState.ALIVE)
            {
                candidates.Add(moveChessNum);
            }
            if (candidates.Count != 0)
            {
                mm = new Minimax(this);

                pos = mm.Calc((p.Chesses[moveChessNum] as Chess).posId);
                Thread.Sleep(1000);
                Console.WriteLine((pos / ChessBoardView.CHESS_BOARD_SIZE).ToString() + ", " + (pos % ChessBoardView.CHESS_BOARD_SIZE).ToString());
                MoveTo(pos);
                return;
            }

            /*int dis = 1;
             * while (candidates.Count == 0 && dis <= 5)
             * {
             *  if (moveChessNum + dis <= 5 && (p.Chesses[moveChessNum + dis] as Chess).state == ChessState.ALIVE)
             *      candidates.Add(moveChessNum + dis);
             *  if (moveChessNum - dis >= 0 && (p.Chesses[moveChessNum - dis] as Chess).state == ChessState.ALIVE)
             *      candidates.Add(moveChessNum - dis);
             *  dis++;
             * }*/

            for (int i = moveChessNum + 1; i <= 5; i++)
            {
                if ((p.Chesses[i] as Chess).state == ChessState.ALIVE)
                {
                    candidates.Add(i);
                    break;
                }
            }

            for (int i = moveChessNum - 1; i >= 0; i--)
            {
                if ((p.Chesses[i] as Chess).state == ChessState.ALIVE)
                {
                    candidates.Add(i);
                    break;
                }
            }

            if (candidates.Count == 0)
            {
                MessageBox.Show("Player2 Win");
                return;
            }

            double bestEval    = double.MinValue;
            int    bestMoveNum = (int)candidates[0];

            Console.Write("Candidates: ");
            for (int i = 0; i < candidates.Count; i++)
            {
                Console.Write((int)candidates[i]);
                Console.Write("\t");
            }
            Console.WriteLine();
            for (int i = 0; i < candidates.Count; i++)
            {
                moveChessNum = (int)candidates[i];
                ArrayList range  = GetMoveRange(player1, moveChessNum);
                int       curPos = (p.Chesses[moveChessNum] as Chess).posId;

                int cm = curPos / ChessBoardView.CHESS_BOARD_SIZE;
                int cn = curPos % ChessBoardView.CHESS_BOARD_SIZE;

                for (int j = 0; j < range.Count; j++)
                {
                    int pid = (int)range[j];

                    int pm = pid / ChessBoardView.CHESS_BOARD_SIZE;
                    int pn = pid % ChessBoardView.CHESS_BOARD_SIZE;

                    ChessOwner obk = chessBoardView.chessBoardHash[pm, pn];
                    chessBoardView.chessBoardHash[cm, cn] = ChessOwner.EMPTY;
                    chessBoardView.chessBoardHash[pm, pn] = ChessOwner.PLAYER1;
                    mm = new Minimax(this);
                    if (bestEval < mm.Eval())
                    {
                        bestEval    = mm.Eval();
                        bestMoveNum = moveChessNum;
                    }
                    chessBoardView.chessBoardHash[pm, pn] = obk;
                    chessBoardView.chessBoardHash[cm, cn] = ChessOwner.PLAYER1;
                }
            }

            moveChessNum = bestMoveNum;
            mm           = new Minimax(this);
            pos          = mm.Calc((p.Chesses[moveChessNum] as Chess).posId);
            Thread.Sleep(1000);
            Console.WriteLine((pos / ChessBoardView.CHESS_BOARD_SIZE).ToString() + ", " + (pos % ChessBoardView.CHESS_BOARD_SIZE).ToString());
            MoveTo(pos);

            return;
        }
示例#13
0
 public void TestRollFailure(int numDice, int diceSides)
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => DiceUtil.Roll(numDice, diceSides));
 }