Exemplo n.º 1
0
 public void ApplyTurnThrowsOnYellowActionWhenRedTurn()
 {
     var gameLogic = new GameLogic();
     var game = new Game()
     {
         CurrentTurnPlayer = Player.Red
     };
     gameLogic.ApplyTurn(game, Player.Yellow, 0);
 }
Exemplo n.º 2
0
 public void DetermineGameOverStateTestInProgress()
 {
     var gameLogic = new GameLogic();
     var game = new Game();
     game.Cells = new int[,]
     {
         { 2, 1, 0, 1 },
         { 1, 2, 1, 2 },
         { 1, 2, 1, 2 },
         { 1, 2, 1, 2 },
     };
     Assert.AreEqual(GameOverState.InProgress, gameLogic.DetermineGameOverState(game));
 }
Exemplo n.º 3
0
        public void ConnectPlayer(Game game, Player player)
        {
            // TODO: need this later to prevent multiple red players
            //if (game.GameOverState != GameOverState.WaitingForPlayers) throw new InvalidOperationException("cannot connect player - game already in progress");

            if (player == Player.Red) game.IsRedConnected = true;
            if (player == Player.Yellow) game.IsYellowConnected = true;

            if (game.GameOverState == GameOverState.WaitingForPlayers && game.IsRedConnected && game.IsYellowConnected)
            {
                game.CurrentTurnPlayer = Player.Red;
                game.GameOverState = GameOverState.InProgress;
            }
        }
Exemplo n.º 4
0
        public void ApplyTurn(Game game, Player player, int column)
        {
            if (game.GameOverState == GameOverState.WaitingForPlayers) throw new InvalidOperationException(string.Format("cannot apply turn - waiting for players"));
            if (game.GameOverState != GameOverState.InProgress) throw new InvalidOperationException(string.Format("cannot apply turn - game finished: {0}", game.GameOverState));
            if (game.CurrentTurnPlayer != player) throw new InvalidOperationException(string.Format("cannot apply turn - it's other player's turn: e={0} a={1}", game.CurrentTurnPlayer, player));

            var nextCellIndex = GetNextCellIndex(game, column);
            var appliedCellState  = (int)player;

            game.Cells[nextCellIndex, column] = appliedCellState;
            game.GameOverState = DetermineGameOverState(game);
            if (game.GameOverState != GameOverState.InProgress) return;

            if (player == Player.Red) game.CurrentTurnPlayer = Player.Yellow;
            else game.CurrentTurnPlayer = Player.Red;
        }
Exemplo n.º 5
0
        public void ApplyTurnTestAppliesAndTogglesPlayer()
        {
            var gameLogic = new GameLogic();
            var game = new Game() { GameOverState = GameOverState.InProgress };
            game.Cells = new int[,]
            {
                { 2, 1, 0, 0 },
                { 1, 2, 1, 2 },
                { 1, 2, 1, 2 },
                { 1, 2, 1, 2 },
            };
            var column = 3;

            gameLogic.ApplyTurn(game, Player.Red, column);

            Assert.AreEqual(GameOverState.InProgress, game.GameOverState);
            Assert.AreEqual((int)Player.Red, game.Cells[3, 0]);
            Assert.AreEqual(Player.Yellow, game.CurrentTurnPlayer);
        }
Exemplo n.º 6
0
 public void StoreGame(string id, Game game)
 {
     if (games.ContainsKey(id)) games[id] = game;
     else games.Add(id, game);
 }
Exemplo n.º 7
0
 public void DetermineGameOverStateTestRedWinColumn()
 {
     var gameLogic = new GameLogic();
     var game = new Game();
     game.Cells = new int[,]
     {
         { 0, 0, 0, 1 },
         { 0, 0, 2, 1 },
         { 0, 0, 2, 1 },
         { 0, 2, 2, 1 },
     };
     Assert.AreEqual(GameOverState.RedWin, gameLogic.DetermineGameOverState(game));
 }
Exemplo n.º 8
0
 public void GetNextCellIndexThrowsOnFullColumn()
 {
     var gameLogic = new GameLogic();
     var game = new Game();
     game.Cells = new int[,]
     {
         { 2, 0, 0, 0 },
         { 1, 0, 0, 0 },
         { 2, 0, 0, 0 },
         { 1, 0, 0, 0 },
     };
     gameLogic.GetNextCellIndex(game, 0);
 }
Exemplo n.º 9
0
        public void GetNextCellIndexReturnsExpectedValue()
        {
            var gameLogic = new GameLogic();
            var game = new Game();
            game.Cells = new int[,]
            {
                { 1, 0, 0, 0 },
                { 1, 0, 0, 0 },
                { 1, 0, 0, 0 },
                { 1, 0, 0, 0 },
            };
            var column = 0;

            // clear cells starting with top one and check that GetNextCellIndex returns just-cleared row value
            for (var row = 0; row < game.MaxRows; row += 1)
            {
                game.Cells[row, column] = 0;

                Assert.AreEqual(row, gameLogic.GetNextCellIndex(game, column));
            }
        }
Exemplo n.º 10
0
 public void DetermineGameOverStateTestRedWinDiag1()
 {
     var gameLogic = new GameLogic();
     var game = new Game();
     game.Cells = new int[,]
     {
         { 1, 0, 0, 0 },
         { 2, 1, 0, 0 },
         { 2, 2, 1, 2 },
         { 1, 2, 1, 1 },
     };
     Assert.AreEqual(GameOverState.RedWin, gameLogic.DetermineGameOverState(game));
 }
Exemplo n.º 11
0
        public void StartNewGame(string gameId)
        {
            // retrieve game
            Game game;
            try
            {
                game = GameRepository.GetGame(gameId);
            }
            catch (KeyNotFoundException ex)
            {
                Clients.Caller.message(ex.Message);
                return;
            }

            game = new Game();
            GameRepository.StoreGame("0", game);

            Clients.All.newGame();
            Clients.All.syncGame(game);
        }
Exemplo n.º 12
0
        public GameOverState DetermineGameOverState(Game game)
        {
            // check for win, if no win then change CurrentTurnPlayer

            // TODO: it maybe better to not have player-dependent logic here, as it limits us to 2 players

            // check all rows
            for (var row = 0; row < game.MaxRows; row += 1)
            {
                var isAllSame = true;
                var firstCellValue = game.Cells[row, 0];
                if (firstCellValue == (int)Player.Undefined) continue; // first cell hasn't been set yet - move along

                for (var column = 1; column < game.MaxColumns; column += 1)
                {
                    if (game.Cells[row, column] != firstCellValue)
                    {
                        isAllSame = false;
                        break;
                    }
                }

                if (isAllSame) return GameOverStateFromPlayer((Player)firstCellValue);
            }

            // check all columns
            for (var column = 0; column < game.MaxColumns; column += 1)
            {
                var isAllSame = true;
                var firstCellValue = game.Cells[0, column];
                if (firstCellValue == (int)Player.Undefined) continue; // first cell hasn't been set yet - move along

                for (var row = 1; row < game.MaxRows; row += 1)
                {
                    if (game.Cells[row, column] != firstCellValue)
                    {
                        isAllSame = false;
                        break;
                    }
                }

                if (isAllSame) return GameOverStateFromPlayer((Player)firstCellValue);
            }

            // check diagonals
            {
                var isAllSame = true;
                var firstCellValue = game.Cells[0, 0];
                if (firstCellValue != (int)Player.Undefined)
                {
                    for (var column = 0; column < game.MaxColumns; column += 1)
                    {
                        var row = column;
                        if (row >= game.MaxRows) break;
                        if (game.Cells[column, column] != firstCellValue)
                        {
                            isAllSame = false;
                            break;
                        }
                    }

                    if (isAllSame) return GameOverStateFromPlayer((Player)firstCellValue);
                }
            }
            {
                var isAllSame = true;
                var firstCellValue = game.Cells[game.MaxRows - 1, 0];
                if (firstCellValue != (int)Player.Undefined)
                {
                    for (var column = 0; column < game.MaxColumns; column += 1)
                    {
                        var row = game.MaxRows - 1 - column;
                        if (row < 0) break;

                        if (game.Cells[row, column] != firstCellValue)
                        {
                            isAllSame = false;
                            break;
                        }
                    }

                    if (isAllSame) return GameOverStateFromPlayer((Player)firstCellValue);
                }
            }

            // check for draw
            var isDraw = true;
            foreach (var cellValue in game.Cells)
            {
                if (cellValue == (int)Player.Undefined)
                {
                    isDraw = false;
                    break;
                }
            }

            if (isDraw) return GameOverState.Draw;

            return GameOverState.InProgress;
        }
Exemplo n.º 13
0
        public int GetNextCellIndex(Game game, int column)
        {
            // returns: ex 0 1 2 3
            //           r e e e e
            //           r r e e e
            //           r r r e e
            //           r r r r e
            int row;
            for (row = 0; row < game.MaxRows; row += 1)
            {
                if (game.Cells[row, column] != 0)
                    break;
            }

            if (row == 0) throw new InvalidOperationException(string.Format("cannot apply turn - column {0} full", column));
            return row - 1;
        }