Example #1
0
        public void Start()
        {
            userHandler.RequestDepositAmount();
            while (!isFinished)
            {
                decimal stakeAmount = userHandler.RequestStakeAmount();
                currentPlayer.Balance -= stakeAmount;

                IEnumerable <IEnumerable <Symbol> > generatedSymbols = slotMachine.Spin(stakeAmount);
                userHandler.ShowSymbols(generatedSymbols);
                decimal spinProfit = CalculateProfit(generatedSymbols, stakeAmount);

                if (spinProfit > 0)
                {
                    currentPlayer.Balance += spinProfit;
                    userHandler.ShowWinning(spinProfit);
                }
                else
                {
                    userHandler.ShowLoss();
                }

                if (currentPlayer.Balance == 0)
                {
                    isFinished = true;
                }
            }
        }
Example #2
0
        public SpinResult Spin(decimal amount)
        {
            if (m_Player != null && m_Player.Balance > 0 && m_Player.Balance >= amount)
            {
                m_Player.Balance -= amount;
                var spinResult = m_SlotMachine.Spin(amount);

                return(spinResult);
            }

            return(new SpinResult(null, 0, m_Player.Balance, false));
        }
        public void WhenNonWinningCombinationIsGeneratedAndSteakIsEqualToBalanceThenBalanceShouldBe0()
        {
            const decimal depositAmount = 100;

            var nonWiningCombination = new Queue <Symbol>
                                       (
                new Symbol[]
            {
                _apple, _banana, _pineapple,
                _apple, _banana, _pineapple,
                _apple, _banana, _pineapple,
                _apple, _banana, _pineapple
            }
                                       );

            _randomSymbolGenerator.Generate(Arg.Any <IReadOnlyCollection <Symbol> >())
            .Returns(_ => nonWiningCombination.Dequeue());
            _slotMachine.Deposit(depositAmount);
            var spinResult = _slotMachine.Spin(depositAmount);

            Assert.That(_slotMachine.Balance, Is.EqualTo(0));
            Assert.That(spinResult.AmountWon, Is.Zero);
        }
Example #4
0
        public async Task Run(string[] args)
        {
            var deposit = ReadDeposit();

            _slotMachine.Deposit(deposit);

            while (_slotMachine.Balance > 0)
            {
                var steak = ReadSteak();

                var spinResult = _slotMachine.Spin(steak);
                PrintSpinResult(spinResult);
            }

            await Task.CompletedTask;
        }
Example #5
0
        public async Task <IActionResult> Spin(GameStakeViewModel stakeModel)
        {
            if (!ModelState.IsValid)
            {
                Response.StatusCode = 299;
                this.StatusMessage  = "Error! The bet is not completed.";
                return(PartialView("_StatusMessage", this.StatusMessage));
            }

            var user = await userManager.GetUserAsync(HttpContext.User);

            var convertedUserBalance = await this.userBalanceService.GetUserBalanceByIdAsync(user.Id);

            decimal stake = stakeModel.Amount;

            if (stake > convertedUserBalance)
            {
                Response.StatusCode = 299;
                ModelState.AddModelError("Stake", "Error! You don't have enough money to make this bet!");
                this.StatusMessage = "Error! You don't have enough money to make this bet!";
                return(PartialView("_StatusMessage", this.StatusMessage));
            }

            int rows = stakeModel.Rows;
            int cols = stakeModel.Cols;

            GameType gameType;

            if (rows == 4 && cols == 3)
            {
                gameType = GameType._4x3;
            }
            else if (rows == 5 && cols == 5)
            {
                gameType = GameType._5x5;
            }
            else if (rows == 8 && cols == 5)
            {
                gameType = GameType._8x5;
            }
            else
            {
                return(this.RedirectToAction("Index"));
            }

            await this.userBalanceService.ReduceMoneyAsync(stake, user.Id);

            string gameTypeString = gameType.ToString().Substring(1);

            var stakeTransaction = await this.transactionService.AddTransactionAsync(TransactionType.Stake, user.Id, gameTypeString, stake, user.Currency);

            var result = game.Spin(rows, cols, stake);

            var model = new GameSlotViewModel()
            {
                Rows        = rows,
                Cols        = cols,
                Matrix      = result.Matrix,
                Amount      = result.Amount,
                Balance     = Math.Round((convertedUserBalance - stake), 2),
                Currency    = user.Currency,
                WinningRows = String.Join("", result.WinningRows),
                Coefficient = result.Coefficient
            };

            if (result.Amount > 0)
            {
                var winTransaction = await this.transactionService.AddTransactionAsync(TransactionType.Win, user.Id, gameTypeString, result.Amount, user.Currency);

                await userBalanceService.DepositMoneyAsync(result.Amount, user.Id);

                model.Balance += result.Amount;
                model.Message  = $"You won {Math.Round(result.Amount, 2)} {WebConstants.CurrencySymbols[user.Currency]}.";
            }
            else
            {
                model.Message = $"Bad luck. Try again!";
            }

            return(this.PartialView("_GameSlotPartial", model));
        }