Example #1
0
 private bool CheckIfColumnNotFull(int x, DTO.GameState gameState)
 {
     if (gameState.Width - 1 < x || x < 0)
     {
         return(false);
     }
     return(gameState.Board[0, x] == 0);
 }
Example #2
0
        public Task <int> SaveGameStateWithName(DTO.GameState gameStateOuter)
        {
            var gameStateInner = _stateRepository.GetGameState(gameStateOuter.StateId);
            var namedSaveState = gameStateInner;

            namedSaveState.GameStateName = gameStateOuter.GameName;
            namedSaveState.GameStateId   = 0;
            return(_stateRepository.SaveGameState(namedSaveState));
        }
Example #3
0
 private bool CheckIfBoardIsFull(DTO.GameState gameState)
 {
     for (var x = 0; x < gameState.Width; x++)
     {
         if (gameState.Board[0, x] == 0)
         {
             return(false);
         }
     }
     return(true);
 }
Example #4
0
        private GameState MapFromPage(DTO.GameState outerGameState)
        {
            var innerGameState = new GameState()
            {
                GameStateId = outerGameState.StateId,
                BoardHeight = outerGameState.Height,
                BoardWidth  = outerGameState.Width,
                BoardJson   = SerializeBoard(outerGameState.Board),
                GameMode    = outerGameState.GameMode,
                Turn        = outerGameState.PlayerOneTurn
            };

            return(innerGameState);
        }
Example #5
0
 //https://stackoverflow.com/questions/39062111/java-how-to-check-diagonal-connect-four-win-in-2d-array
 private void CheckForWin(DTO.GameState gameState)
 {
     int[,] directions = new int[, ] {
         { 1, 0 }, { 1, -1 }, { 1, 1 }, { 0, 1 }
     };
     for (var d = 0; d < 4; d++)
     {
         int dx = directions[d, 0];
         int dy = directions[d, 1];
         for (int x = 0; x < gameState.Width; x++)
         {
             for (int y = 0; y < gameState.Height; y++)
             {
                 int lastx = x + 3 * dx;
                 int lasty = y + 3 * dy;
                 if (0 <= lastx && lastx < gameState.Width && 0 <= lasty && lasty < gameState.Height)
                 {
                     int w = gameState.Board[x, y];
                     if (w != 0 && w == gameState.Board[x + dx, y + dy] &&
                         w == gameState.Board[x + 2 * dx, y + 2 * dy] &&
                         w == gameState.Board[lastx, lasty])
                     {
                         if (w == 1)
                         {
                             gameState.Winner = DTO.GameState.Win.PLAYER_ONE;
                         }
                         else
                         {
                             gameState.Winner = DTO.GameState.Win.PLAYER_TWO;
                         }
                     }
                 }
             }
         }
     }
 }