Ejemplo n.º 1
0
        public int NextStepAddToColumn(Board p_Board, BoardValue p_BoardValue)
        {
            int bestWinCol    = 0;
            var bestWinDepth  = int.MaxValue;
            var bestLostCol   = 0;
            var bestLostDepth = int.MinValue;

            for (int col = 1; col <= p_Board.ColumnsCount; col++)
            {
                var depth = CheckWinnerNextDepth(p_Board, p_BoardValue, p_BoardValue, col, 1);
                if (depth > 0 && depth < bestWinDepth)
                {
                    bestWinDepth = depth.Value;
                    bestWinCol   = col;
                }

                if (depth < 0 && depth > bestLostDepth)
                {
                    bestLostDepth = depth.Value;
                    bestLostCol   = col;
                }
            }

            if (bestLostDepth > -4)
            {
                return(bestLostCol);
            }

            return(bestWinCol > 0 ? bestWinCol : bestLostCol);
        }
Ejemplo n.º 2
0
 private bool Check(int firstPos, int secondPos, int choice, BoardValue symbol)
 {
     if (GameBoard.GetBoardValue(firstPos).Equals(GameBoard.GetBoardValue(secondPos)) && GameBoard.GetBoardValue(firstPos).Equals(symbol))
     {
         if (GameBoard.GetBoardValue(choice).Equals(BoardValue.Empty))
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 3
0
 private void SetValue(int p_Row, int p_Column, BoardValue p_Value)
 {
     if (p_Column < 1 || p_Column > ColumnsCount)
     {
         throw new ArgumentOutOfRangeException(nameof(p_Column));
     }
     if (p_Row < 1 || p_Row > RowsCount)
     {
         throw new ArgumentOutOfRangeException(nameof(p_Row));
     }
     _Data[p_Row - 1, p_Column - 1] = p_Value;
 }
Ejemplo n.º 4
0
 public bool Set(int input, BoardValue XO)
 {
     if (input >= boardSize || input < 0) //If input is not on the board
     {
         return(false);
     }
     if (board[input].Equals(BoardValue.Empty)) //If not already taken
     {
         board[input] = XO;
         return(true);
     }
     return(false);
 }
Ejemplo n.º 5
0
		public BoardModel(int ColumnLength, int RowLength)
		{
			BoardNumbers = new BoardValue[RowLength][];

			for (int x = 0; x < BoardNumbers.Length; x++)
				BoardNumbers[x] = new BoardValue[ColumnLength];

			// create the actual objects
			for (int x = 0; x < BoardNumbers.Length; x++)
			{
				for (int y = 0; y < BoardNumbers[x].Length; y++)
					BoardNumbers[x][y] = new BoardValue();
			}
		}
Ejemplo n.º 6
0
        public BoardModel(int ColumnLength, int RowLength)
        {
            BoardNumbers = new BoardValue[RowLength][];

            for (int x = 0; x < BoardNumbers.Length; x++)
            {
                BoardNumbers[x] = new BoardValue[ColumnLength];
            }

            // create the actual objects
            for (int x = 0; x < BoardNumbers.Length; x++)
            {
                for (int y = 0; y < BoardNumbers[x].Length; y++)
                {
                    BoardNumbers[x][y] = new BoardValue();
                }
            }
        }
Ejemplo n.º 7
0
        public async Task Should_OnlyShowedTheTopMembers()
        {
            TestUser testUser1 = await CreateTestUserWithPortfolio();

            TestUser testUser2 = await CreateTestUserWithPortfolio();

            TestUser testUser3 = await CreateTestUserWithPortfolio();

            SymbolSearchResultDTO jpmStock = await FetchSymbol("JPM", ExchangeType.NYSE);

            // Add singlequote stocks
            var jpmData = new SingleQuoteData(jpmStock.SymbolId, jpmStock.Ticker, 200, 200, 200, 200, 200, 200, 0, 0, DateTime.UtcNow, DateTime.UtcNow);

            await SetSingleQuoteData(new UpsertSingleQuoteData(jpmStock.SymbolId, jpmData));

            // Add symbols User 1
            await AddSymbolToPortfolioWithAvgPrice(testUser1, jpmStock.SymbolId, 100);

            // Add symbols User 2
            await AddSymbolToPortfolioWithAvgPrice(testUser2, jpmStock.SymbolId, 200);

            // Add symbols User 3
            await AddSymbolToPortfolioWithAvgPrice(testUser3, jpmStock.SymbolId, 300);

            var leaderBoardResponse = await _client.GetAsync(ApiPath.LeaderBoard(2));

            LeaderBoardViewModel leaderBoardViewModel = await leaderBoardResponse.Content.ReadAsAsync <LeaderBoardViewModel>();

            // Verify calculations
            BoardValue bestPerformerRank1 = leaderBoardViewModel.BestPerformers.Values.ElementAt(0);
            BoardValue bestPerformerRank2 = leaderBoardViewModel.BestPerformers.Values.ElementAt(1);

            BoardValue worstPerformerRank1 = leaderBoardViewModel.WorstPerformers.Values.ElementAt(0);
            BoardValue worstPerformerRank2 = leaderBoardViewModel.WorstPerformers.Values.ElementAt(1);

            Assert.Equal(2, leaderBoardViewModel.BestPerformers.Values.Count());
            Assert.Equal(2, leaderBoardViewModel.WorstPerformers.Values.Count());

            Assert.Equal(testUser1.UserId, Guid.Parse(bestPerformerRank1.Id));
            Assert.Equal(testUser2.UserId, Guid.Parse(bestPerformerRank2.Id));

            Assert.Equal(testUser3.UserId, Guid.Parse(worstPerformerRank1.Id));
            Assert.Equal(testUser2.UserId, Guid.Parse(worstPerformerRank2.Id));
        }
Ejemplo n.º 8
0
        public bool AddToColumn(int p_Column, BoardValue p_Value)
        {
            if (p_Column < 1 || p_Column > ColumnsCount)
            {
                throw new ArgumentOutOfRangeException(nameof(p_Column));
            }

            for (int r = RowsCount; r > 0; r--)
            {
                if (GetValue(r, p_Column) == BoardValue.Empty)
                {
                    LastAddedColumnRow = new Tuple <int, int>(p_Column, r);
                    SetValue(r, p_Column, p_Value);
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 9
0
        private static void SetPlayerColorAndDrawBoard(BoardValue p_BoardValue)
        {
            System.Console.Clear();
            System.Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("Esc to exit. '1' - '7' to add coin to selected column.");
            System.Console.WriteLine("'Y' enable/disable Yellow computer player. IsEnabled: " + _YellowPlayComputer);
            System.Console.WriteLine("'R' enable/disable Red computer player. IsEnabled: " + _RedPlayComputer);

            _PlayerRound = p_BoardValue;
            System.Console.ForegroundColor = _PlayerRound == BoardValue.Red ? ConsoleColor.Red : ConsoleColor.Yellow;
            System.Console.WriteLine("|1|2|3|4|5|6|7|");

            var lastAdded = _Board.LastAddedColumnRow ?? new Tuple <int, int>(0, 0);

            for (var col = 1; col <= _Board.ColumnsCount; col++)
            {
                for (var row = 1; row <= _Board.RowsCount; row++)
                {
                    var val = _Board.GetValue(row, col);
                    System.Console.ForegroundColor = ConsoleColor.White;
                    if (col == 1)
                    {
                        System.Console.SetCursorPosition(0, row + 3);
                        System.Console.Write("|");
                    }
                    if (val == BoardValue.Red)
                    {
                        System.Console.ForegroundColor = ConsoleColor.Red;
                    }
                    if (val == BoardValue.Yellow)
                    {
                        System.Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    System.Console.SetCursorPosition((col - 1) * 2 + 1, row + 3);
                    System.Console.BackgroundColor = lastAdded.Item1 == col && lastAdded.Item2 == row ? ConsoleColor.DarkGray : ConsoleColor.Black;
                    System.Console.Write("o");
                    System.Console.BackgroundColor = ConsoleColor.Black;
                    System.Console.ForegroundColor = ConsoleColor.White;
                    System.Console.Write("|");
                }
            }
        }
Ejemplo n.º 10
0
        public async Task Should_IgnorePortfolioSymbolsWithNoAveragePrice()
        {
            TestUser testUser1 = await CreateTestUserWithPortfolio();

            TestUser testUser2 = await CreateTestUserWithPortfolio();

            SymbolSearchResultDTO jpmStock = await FetchSymbol("JPM", ExchangeType.NYSE);

            SymbolSearchResultDTO fbStock = await FetchSymbol("FB", ExchangeType.NASDAQ);

            // Add singlequote stocks
            var jpmData = new SingleQuoteData(jpmStock.SymbolId, jpmStock.Ticker, 200, 200, 200, 200, 200, 200, 0, 0, DateTime.UtcNow, DateTime.UtcNow);
            var fbData  = new SingleQuoteData(fbStock.SymbolId, fbStock.Ticker, 333.33m, 333.33m, 333.33m, 333.33m, 333.33m, 300, 0, 0, DateTime.UtcNow, DateTime.UtcNow);

            await SetSingleQuoteData(new UpsertSingleQuoteData(jpmStock.SymbolId, jpmData));
            await SetSingleQuoteData(new UpsertSingleQuoteData(fbData.SymbolId, fbData));

            // Add symbols User 1
            await AddSymbolToPortfolioWithAvgPrice(testUser1, jpmStock.SymbolId, 100);
            await AddSymbolToPortfolio(testUser1, fbStock.SymbolId);

            // Add symbols User 2
            await AddSymbolToPortfolio(testUser2, jpmStock.SymbolId);
            await AddSymbolToPortfolio(testUser2, fbStock.SymbolId);

            var leaderBoardResponse = await _client.GetAsync(ApiPath.LeaderBoard(2));

            LeaderBoardViewModel leaderBoardViewModel = await leaderBoardResponse.Content.ReadAsAsync <LeaderBoardViewModel>();

            // Verify calculations
            BoardValue bestPerformerRank1  = leaderBoardViewModel.BestPerformers.Values.ElementAt(0);
            BoardValue worstPerformerRank1 = leaderBoardViewModel.WorstPerformers.Values.ElementAt(0);

            Assert.Single(leaderBoardViewModel.BestPerformers.Values);
            Assert.Single(leaderBoardViewModel.WorstPerformers.Values);

            Assert.Equal(testUser1.UserId, Guid.Parse(bestPerformerRank1.Id));
            Assert.Equal("100.00", bestPerformerRank1.Value);

            Assert.Equal(testUser1.UserId, Guid.Parse(worstPerformerRank1.Id));
            Assert.Equal("100.00", worstPerformerRank1.Value);
        }
Ejemplo n.º 11
0
        private int?CheckWinnerNextDepth(Board p_Board, BoardValue p_BoardValueWinner, BoardValue p_BoardValue,
                                         int p_Col, int p_Depth)
        {
            if (!p_Board.AddToColumn(p_Col, p_BoardValue))
            {
                throw new Exception("Not supported algo - first check if can add to column");
            }
            var winner = p_Board.CheckWinner();

            if (winner == p_BoardValueWinner)
            {
                p_Board.RemoveFromColumn(p_Col);
                return(p_Depth);
            }

            if (winner == (p_BoardValueWinner == BoardValue.Red ? BoardValue.Yellow : BoardValue.Red))
            {
                p_Board.RemoveFromColumn(p_Col);
                return(-p_Depth);
            }

            for (int col = 1; col <= p_Board.ColumnsCount; col++)
            {
                if (!p_Board.CanAddToColumn(col))
                {
                    continue;
                }

                var foundDepth = CheckWinnerNextDepth(p_Board, p_BoardValueWinner,
                                                      p_BoardValue == BoardValue.Red ? BoardValue.Yellow : BoardValue.Red, col, p_Depth + 1);
                //if (!foundDepth.HasValue)
                //    return null;    //end of recursion

                p_Board.RemoveFromColumn(p_Col);
                if (foundDepth.HasValue)
                {
                    return(foundDepth);
                }
            }

            return(0);
        }
Ejemplo n.º 12
0
        public void Play(Board p_Board, BoardValue p_BoardValuePlayer)
        {
            var random = new Random();

            var moves = new List <Tuple <int, int> >();

            for (var i = 1; i <= p_Board.ColumnsCount; i++)
            {
                if (!p_Board.AddToColumn(i, p_BoardValuePlayer))
                {
                    continue;
                }
                moves.Add(Tuple.Create(i, MinMax(6, p_Board, p_BoardValuePlayer, false)));
                p_Board.RemoveFromColumn(i);
            }

            var maxMoveScore = moves.Max(t => t.Item2);
            var bestMoves    = moves.Where(t => t.Item2 == maxMoveScore).ToList();

            p_Board.AddToColumn(bestMoves[random.Next(0, bestMoves.Count)].Item1, p_BoardValuePlayer);
        }
Ejemplo n.º 13
0
        private static int MinMax(int p_Depth, Board p_Board, BoardValue p_BoardValuePlayer, bool p_MaximizingPlayer)
        {
            var opponentPlayer = p_BoardValuePlayer == BoardValue.Red ? BoardValue.Yellow : BoardValue.Red;

            if (p_Depth <= 0)
            {
                return(0);
            }

            var winner = p_Board.CheckWinner();

            if (winner == p_BoardValuePlayer)
            {
                return(p_Depth);
            }
            if (winner != BoardValue.Empty)
            {
                return(-p_Depth);
            }
            if (p_Board.EndGameWithoutWinner())
            {
                return(0);
            }


            var bestValue = p_MaximizingPlayer ? -1 : 1;

            for (var i = 1; i <= p_Board.ColumnsCount; i++)
            {
                if (!p_Board.AddToColumn(i, p_MaximizingPlayer ? p_BoardValuePlayer : opponentPlayer))
                {
                    continue;
                }
                var v = MinMax(p_Depth - 1, p_Board, p_BoardValuePlayer, !p_MaximizingPlayer);
                bestValue = p_MaximizingPlayer ? Math.Max(bestValue, v) : Math.Min(bestValue, v);
                p_Board.RemoveFromColumn(i);
            }

            return(bestValue);
        }
Ejemplo n.º 14
0
        public async Task Should_ReturnMostBearishAndMostBullishStocksByVotes()
        {
            TestUser testUser1 = await CreateTestUserWithPortfolio();

            TestUser testUser2 = await CreateTestUserWithPortfolio();

            TestUser testUser3 = await CreateTestUserWithPortfolio();

            SymbolSearchResultDTO jpmStock = await FetchSymbol("JPM", ExchangeType.NYSE);

            SymbolSearchResultDTO fbStock = await FetchSymbol("FB", ExchangeType.NASDAQ);

            SymbolSearchResultDTO tslaStock = await FetchSymbol("TSLA", ExchangeType.NASDAQ);

            SymbolSearchResultDTO msftStock = await FetchSymbol("MSFT", ExchangeType.NASDAQ);

            // User 1 votes
            await SubmitVoteForUser(new VoteCommand { SymbolId = jpmStock.SymbolId, Direction = VoteDirection.UpVote }, testUser1.UserId);
            await SubmitVoteForUser(new VoteCommand { SymbolId = fbStock.SymbolId, Direction = VoteDirection.UpVote }, testUser1.UserId);
            await SubmitVoteForUser(new VoteCommand { SymbolId = tslaStock.SymbolId, Direction = VoteDirection.DownVote }, testUser1.UserId);
            await SubmitVoteForUser(new VoteCommand { SymbolId = msftStock.SymbolId, Direction = VoteDirection.DownVote }, testUser1.UserId);

            // User 2 votes
            await SubmitVoteForUser(new VoteCommand { SymbolId = jpmStock.SymbolId, Direction = VoteDirection.UpVote }, testUser2.UserId);
            await SubmitVoteForUser(new VoteCommand { SymbolId = fbStock.SymbolId, Direction = VoteDirection.DownVote }, testUser2.UserId);
            await SubmitVoteForUser(new VoteCommand { SymbolId = tslaStock.SymbolId, Direction = VoteDirection.DownVote }, testUser2.UserId);
            await SubmitVoteForUser(new VoteCommand { SymbolId = msftStock.SymbolId, Direction = VoteDirection.DownVote }, testUser2.UserId);

            // User 2 votes
            await SubmitVoteForUser(new VoteCommand { SymbolId = jpmStock.SymbolId, Direction = VoteDirection.UpVote }, testUser3.UserId);
            await SubmitVoteForUser(new VoteCommand { SymbolId = fbStock.SymbolId, Direction = VoteDirection.None }, testUser3.UserId);
            await SubmitVoteForUser(new VoteCommand { SymbolId = tslaStock.SymbolId, Direction = VoteDirection.DownVote }, testUser3.UserId);
            await SubmitVoteForUser(new VoteCommand { SymbolId = msftStock.SymbolId, Direction = VoteDirection.UpVote }, testUser3.UserId);

            var leaderBoardResponse = await _client.GetAsync(ApiPath.LeaderBoard(4));

            LeaderBoardViewModel leaderBoardViewModel = await leaderBoardResponse.Content.ReadAsAsync <LeaderBoardViewModel>();

            // Verify most bullish order
            BoardValue mostBullishRank1 = leaderBoardViewModel.MostBullish.Values.ElementAt(0);
            BoardValue mostBullishRank2 = leaderBoardViewModel.MostBullish.Values.ElementAt(1);
            BoardValue mostBullishRank3 = leaderBoardViewModel.MostBullish.Values.ElementAt(2);
            BoardValue mostBullishRank4 = leaderBoardViewModel.MostBullish.Values.ElementAt(3);

            Assert.Equal(jpmStock.SymbolId.ToString(), mostBullishRank1.Id);
            Assert.Equal("JPM", mostBullishRank1.Name);
            Assert.Equal("3", mostBullishRank1.Value);

            Assert.Equal(fbStock.SymbolId.ToString(), mostBullishRank2.Id);
            Assert.Equal("FB", mostBullishRank2.Name);
            Assert.Equal("0", mostBullishRank2.Value);

            Assert.Equal(msftStock.SymbolId.ToString(), mostBullishRank3.Id);
            Assert.Equal("MSFT", mostBullishRank3.Name);
            Assert.Equal("-1", mostBullishRank3.Value);

            Assert.Equal(tslaStock.SymbolId.ToString(), mostBullishRank4.Id);
            Assert.Equal("TSLA", mostBullishRank4.Name);
            Assert.Equal("-3", mostBullishRank4.Value);

            // Verify most bearish order
            BoardValue mostBearishRank1 = leaderBoardViewModel.MostBearish.Values.ElementAt(0);
            BoardValue mostBearishRank2 = leaderBoardViewModel.MostBearish.Values.ElementAt(1);
            BoardValue mostBearishRank3 = leaderBoardViewModel.MostBearish.Values.ElementAt(2);
            BoardValue mostBearishRank4 = leaderBoardViewModel.MostBearish.Values.ElementAt(3);

            Assert.Equal(tslaStock.SymbolId.ToString(), mostBearishRank1.Id);
            Assert.Equal("TSLA", mostBearishRank1.Name);
            Assert.Equal("-3", mostBearishRank1.Value);

            Assert.Equal(msftStock.SymbolId.ToString(), mostBearishRank2.Id);
            Assert.Equal("MSFT", mostBearishRank2.Name);
            Assert.Equal("-1", mostBearishRank2.Value);

            Assert.Equal(fbStock.SymbolId.ToString(), mostBearishRank3.Id);
            Assert.Equal("FB", mostBearishRank3.Name);
            Assert.Equal("0", mostBearishRank3.Value);

            Assert.Equal(jpmStock.SymbolId.ToString(), mostBearishRank4.Id);
            Assert.Equal("JPM", mostBearishRank4.Name);
            Assert.Equal("3", mostBearishRank4.Value);
        }
Ejemplo n.º 15
0
 private bool CheckForWinOrBlock(BoardValue symbol, out int choice)
 {
     //Horizontal
     for (int i = 0; i < 9; i += 3)
     {
         if (Check(i, i + 1, i + 2, symbol))
         {
             choice = i + 2;
             return(true);
         }
     }
     for (int i = 0; i < 9; i += 3)
     {
         if (Check(i, i + 2, i + 1, symbol))
         {
             choice = i + 1;
             return(true);
         }
     }
     for (int i = 0; i < 9; i += 3)
     {
         if (Check(i + 1, i + 2, i, symbol))
         {
             choice = i;
             return(true);
         }
     }
     //Vertiacal
     for (int i = 0; i < 3; i++)
     {
         if (Check(i, i + 3, i + 6, symbol))
         {
             choice = i + 6;
             return(true);
         }
     }
     for (int i = 0; i < 3; i++)
     {
         if (Check(i, i + 6, i + 3, symbol))
         {
             choice = i + 3;
             return(true);
         }
     }
     for (int i = 0; i < 3; i++)
     {
         if (Check(i + 3, i + 6, i, symbol))
         {
             choice = i;
             return(true);
         }
     }
     //Diagonal left to right
     if (Check(0, 4, 8, symbol))
     {
         choice = 8;
         return(true);
     }
     if (Check(0, 8, 4, symbol))
     {
         choice = 4;
         return(true);
     }
     if (Check(4, 8, 0, symbol))
     {
         choice = 0;
         return(true);
     }
     //Diagonal right to left
     if (Check(2, 4, 6, symbol))
     {
         choice = 6;
         return(true);
     }
     if (Check(4, 6, 2, symbol))
     {
         choice = 2;
         return(true);
     }
     if (Check(2, 6, 4, symbol))
     {
         choice = 2;
         return(true);
     }
     else
     {
         choice = -1;
         return(false);
     }
 }
Ejemplo n.º 16
0
        public async Task Should_ReturnBestAndWorstPerformers()
        {
            TestUser testUser1 = await CreateTestUserWithPortfolio();

            TestUser testUser2 = await CreateTestUserWithPortfolio();

            TestUser testUser3 = await CreateTestUserWithPortfolio();

            SymbolSearchResultDTO jpmStock = await FetchSymbol("JPM", ExchangeType.NYSE);

            SymbolSearchResultDTO fbStock = await FetchSymbol("FB", ExchangeType.NASDAQ);

            SymbolSearchResultDTO tslaStock = await FetchSymbol("TSLA", ExchangeType.NASDAQ);

            SymbolSearchResultDTO msftStock = await FetchSymbol("MSFT", ExchangeType.NASDAQ);

            // Add singlequote stocks
            var jpmData  = new SingleQuoteData(jpmStock.SymbolId, jpmStock.Ticker, 200, 200, 200, 200, 200, 200, 0, 0, DateTime.UtcNow, DateTime.UtcNow);
            var fbData   = new SingleQuoteData(fbStock.SymbolId, fbStock.Ticker, 333.33m, 333.33m, 333.33m, 333.33m, 333.33m, 300, 0, 0, DateTime.UtcNow, DateTime.UtcNow);
            var tslaData = new SingleQuoteData(tslaStock.SymbolId, tslaStock.Ticker, 888.88m, 888.88m, 888.88m, 888.88m, 888.88m, 800, 0, 0, DateTime.UtcNow, DateTime.UtcNow);
            var msftData = new SingleQuoteData(msftStock.SymbolId, msftStock.Ticker, 225, 225, 225, 225, 225, 200, 0, 0, DateTime.UtcNow, DateTime.UtcNow);

            await SetSingleQuoteData(new UpsertSingleQuoteData(jpmStock.SymbolId, jpmData));
            await SetSingleQuoteData(new UpsertSingleQuoteData(fbData.SymbolId, fbData));
            await SetSingleQuoteData(new UpsertSingleQuoteData(tslaData.SymbolId, tslaData));
            await SetSingleQuoteData(new UpsertSingleQuoteData(msftData.SymbolId, msftData));

            // Add symbols User 1
            await AddSymbolToPortfolioWithAvgPrice(testUser1, jpmStock.SymbolId, 100.25m);
            await AddSymbolToPortfolioWithAvgPrice(testUser1, fbStock.SymbolId, 260.35m);
            await AddSymbolToPortfolioWithAvgPrice(testUser1, msftStock.SymbolId, 175.50m);

            // Add symbols User 2
            await AddSymbolToPortfolioWithAvgPrice(testUser2, jpmStock.SymbolId, 5.00m);
            await AddSymbolToPortfolioWithAvgPrice(testUser2, fbStock.SymbolId, 10.00m);

            // Add Symbols Users 3
            await AddSymbolToPortfolioWithAvgPrice(testUser3, jpmStock.SymbolId, 180.88m);
            await AddSymbolToPortfolioWithAvgPrice(testUser3, fbStock.SymbolId, 400.00m);
            await AddSymbolToPortfolioWithAvgPrice(testUser3, tslaStock.SymbolId, 1000m);
            await AddSymbolToPortfolioWithAvgPrice(testUser3, msftStock.SymbolId, 300.90m);

            var leaderBoardResponse = await _client.GetAsync(ApiPath.LeaderBoard(3));

            LeaderBoardViewModel leaderBoardViewModel = await leaderBoardResponse.Content.ReadAsAsync <LeaderBoardViewModel>();

            // Verify calculations
            BoardValue bestPerformerRank1 = leaderBoardViewModel.BestPerformers.Values.ElementAt(0);
            BoardValue bestPerformerRank2 = leaderBoardViewModel.BestPerformers.Values.ElementAt(1);
            BoardValue bestPerformerRank3 = leaderBoardViewModel.BestPerformers.Values.ElementAt(2);

            Assert.Equal(3, leaderBoardViewModel.BestPerformers.Values.Count());

            Assert.Equal(testUser2.UserId, Guid.Parse(bestPerformerRank1.Id));
            Assert.Equal(testUser1.UserId, Guid.Parse(bestPerformerRank2.Id));
            Assert.Equal(testUser3.UserId, Guid.Parse(bestPerformerRank3.Id));

            Assert.Equal(testUser2.UserId, Guid.Parse(bestPerformerRank1.Name));
            Assert.Equal(testUser1.UserId, Guid.Parse(bestPerformerRank2.Name));
            Assert.Equal(testUser3.UserId, Guid.Parse(bestPerformerRank3.Name));

            Assert.Equal("3,566.65", bestPerformerRank1.Value);
            Assert.Equal("51.91", bestPerformerRank2.Value);
            Assert.Equal("-10.61", bestPerformerRank3.Value);

            BoardValue worstPerformerRank1 = leaderBoardViewModel.WorstPerformers.Values.ElementAt(0);
            BoardValue worstPerformerRank2 = leaderBoardViewModel.WorstPerformers.Values.ElementAt(1);
            BoardValue worstPerformerRank3 = leaderBoardViewModel.WorstPerformers.Values.ElementAt(2);

            Assert.Equal(3, leaderBoardViewModel.WorstPerformers.Values.Count());

            Assert.Equal(testUser3.UserId, Guid.Parse(worstPerformerRank1.Id));
            Assert.Equal(testUser1.UserId, Guid.Parse(worstPerformerRank2.Id));
            Assert.Equal(testUser2.UserId, Guid.Parse(worstPerformerRank3.Id));

            Assert.Equal("-10.61", worstPerformerRank1.Value);
            Assert.Equal("51.91", worstPerformerRank2.Value);
            Assert.Equal("3,566.65", worstPerformerRank3.Value);
        }
Ejemplo n.º 17
0
 public BoardItem(string position, BoardValue value)
 {
     Position = position;
     Value    = value;
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Simple keyboard event handler
        /// </summary>
        /// <param name="key">Key pressed</param>
        public void KeyPressed(Key key)
        {
            switch (key)
            {
            case Key.Left:
                PlayerCursor.X = Math.Max(PlayerCursor.X - 1, 0);
                break;

            case Key.Right:
                PlayerCursor.X = Math.Min(PlayerCursor.X + 1, (CurrentPhase == GamePhase.BoardPlanning && !currentShipOrientation) ? 10 - currentShipLength : 9);
                break;

            case Key.Up:
                PlayerCursor.Y = Math.Max(PlayerCursor.Y - 1, 0);
                break;

            case Key.Down:
                PlayerCursor.Y = Math.Min(PlayerCursor.Y + 1, (CurrentPhase == GamePhase.BoardPlanning && currentShipOrientation) ? 10 - currentShipLength : (CurrentPhase == GamePhase.MainMenu ? 2 : 9));
                break;

            case Key.E:
                if (CurrentPhase == GamePhase.Game && currentPlacementViable)
                {
                    ComputerBoard.MarkSpot(PlayerCursor.X, PlayerCursor.Y);
                    UpdateScreen();
                }
                break;

            case Key.R:
                if (CurrentPhase == GamePhase.BoardPlanning)
                {
                    if (currentShipOrientation && PlayerCursor.X > 10 - currentShipLength)
                    {
                        PlayerCursor.X = 10 - currentShipLength;
                    }
                    else if (!currentShipOrientation && PlayerCursor.Y > 10 - currentShipLength)
                    {
                        PlayerCursor.Y = 10 - currentShipLength;
                    }
                    currentShipOrientation = !currentShipOrientation;
                }
                break;

            case Key.Enter:
                if (CurrentPhase == GamePhase.End)
                {
                    Console.WriteLine("Game reset");
                    ResetGame();
                }
                break;

            case Key.Space:
                if (CurrentPhase == GamePhase.BoardPlanning && currentPlacementViable)
                {
                    PlayerBoard.PlaceShipOnBoard(PlayerCursor.X, PlayerCursor.Y, currentShipLength, currentShipOrientation);
                    UpdateScreen();
                    if (--shipsLeftToPlace == 0)
                    {
                        if (--currentShipLength <= 0)
                        {
                            PlayerCursor.X = PlayerCursor.Y = 0;
                            ChangePhase(GamePhase.Game);
                            return;
                        }
                        shipsLeftToPlace = 5 - currentShipLength;
                    }
                }
                else if (CurrentPhase == GamePhase.Game && currentPlacementViable)
                {
                    Coordinate computerMove   = ComputerBrain.GenerateNextMove(PlayerBoard.GetBoardValues());
                    BoardValue playerResult   = ComputerBoard.Shoot(PlayerCursor.X, PlayerCursor.Y);
                    BoardValue computerResult = PlayerBoard.Shoot(computerMove.x, computerMove.y);

                    if (playerResult == BoardValue.Hit || playerResult == BoardValue.Sank)
                    {
                        playerScore++;
                    }
                    if (computerResult == BoardValue.Hit || computerResult == BoardValue.Sank)
                    {
                        computerScore++;
                    }

                    if (playerScore == 20 || computerScore == 20)
                    {
                        Console.WriteLine("End of the game!!");
                        string result = computerScore == playerScore ? "Remis" : playerScore == 20 ? "Wygrałeś" : "Wygrałem";
                        PostComputerMessage($"{result}! Twoj wynik: {100 + 5 * (playerScore - computerScore)}([ENTER], aby zagrać ponownie)");
                        ChangePhase(GamePhase.End);
                    }
                    else
                    {
                        PostComputerMessage(ComputerBrain.GetComputerResponse(playerResult, computerResult));
                    }

                    UpdateScreen();
                }
                else if (CurrentPhase == GamePhase.MainMenu)
                {
                    switch (PlayerCursor.Y)
                    {
                    case 0:
                        ChangePhase(GamePhase.BoardPlanning);
                        AnimateTransition(MainMenu, false);
                        UpdateScreen();
                        break;

                    case 1:
                        ChangePhase(GamePhase.Information);
                        AnimateTransition(Information, true);
                        break;

                    case 2:
                        Application.Current.Shutdown();
                        break;
                    }
                }
                else if (CurrentPhase == GamePhase.Information)
                {
                    ChangePhase(GamePhase.MainMenu);
                    AnimateTransition(Information, false);
                }
                break;
            }

            if (CurrentPhase == GamePhase.BoardPlanning)
            {
                currentPlacementViable = PlayerBoard.IsPlaceViable(PlayerCursor.X, PlayerCursor.Y, currentShipLength, currentShipOrientation);
                PlayerCursor.UpdatePhase(GamePhase.BoardPlanning, currentShipLength, currentShipOrientation, currentPlacementViable);
            }
            else if (CurrentPhase == GamePhase.Game)
            {
                currentPlacementViable = ComputerBoard.CanShoot(PlayerCursor.X, PlayerCursor.Y);
                PlayerCursor.UpdatePhase(GamePhase.Game, 1, false, currentPlacementViable);
            }
        }
Ejemplo n.º 19
0
 private void SetPlayerColor(BoardValue p_BoardValue)
 {
     _PlayerRound          = p_BoardValue;
     button1.BackColor     = button2.BackColor = button3.BackColor = button4.BackColor = button5.BackColor =
         button6.BackColor = button7.BackColor = _PlayerRound == BoardValue.Red ? Color.Red : Color.Yellow;
 }