Beispiel #1
0
 private GameState.FIELD_STATE[,] GetField(GameState game)
 {
     try
     {
         GameState.FIELD_STATE[,] field = new GameState.FIELD_STATE[3, 3];
         for (int i = 0; i < 3; i++)
         {
             for (int j = 0; j < 3; j++)
             {
                 field[i, j] = game.field[i * 3 + j];
             }
         }
         return field;
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         return null;
     }
 }
Beispiel #2
0
        /*
         * 0 - игра продолжается
         * 1 - есть победитель
         * 2 - ничья
        */
        private int IsEndGame(GameState game)
        {
            GameState.FIELD_STATE[,] field = this.GetField(game);

            if ((field[0, 0] == field[0, 1] && field[0, 0] == field[0, 2] && field[0, 0] != GameState.FIELD_STATE.FREE) ||
                (field[1, 0] == field[1, 1] && field[1, 0] == field[1, 2] && field[1, 0] != GameState.FIELD_STATE.FREE) ||
                (field[2, 0] == field[2, 1] && field[2, 0] == field[2, 2] && field[2, 0] != GameState.FIELD_STATE.FREE) ||
                (field[0, 0] == field[1, 0] && field[0, 0] == field[2, 0] && field[0, 0] != GameState.FIELD_STATE.FREE) ||
                (field[0, 1] == field[1, 1] && field[1, 0] == field[2, 1] && field[0, 1] != GameState.FIELD_STATE.FREE) ||
                (field[0, 2] == field[1, 2] && field[0, 2] == field[2, 2] && field[0, 2] != GameState.FIELD_STATE.FREE) ||
                (field[0, 2] == field[1, 2] && field[0, 2] == field[2, 2] && field[0, 2] != GameState.FIELD_STATE.FREE) ||
                (field[0, 0] == field[1, 1] && field[0, 0] == field[2, 2] && field[0, 0] != GameState.FIELD_STATE.FREE) ||
                (field[0, 2] == field[1, 1] && field[0, 2] == field[2, 0] && field[0, 2] != GameState.FIELD_STATE.FREE))
            {
                return 1;
            }
            else if (game.field[0] != GameState.FIELD_STATE.FREE &&
                    game.field[1] != GameState.FIELD_STATE.FREE &&
                    game.field[2] != GameState.FIELD_STATE.FREE &&
                    game.field[3] != GameState.FIELD_STATE.FREE &&
                    game.field[4] != GameState.FIELD_STATE.FREE &&
                    game.field[5] != GameState.FIELD_STATE.FREE &&
                    game.field[6] != GameState.FIELD_STATE.FREE &&
                    game.field[7] != GameState.FIELD_STATE.FREE &&
                    game.field[8] != GameState.FIELD_STATE.FREE)
            {
                return 2;
            }
            else
            {
                return 0;
            }
        }
Beispiel #3
0
        public bool Login(string login)
        {
            try
            {
                //Провека существования логина на сервере

                gameCollectionLocker.EnterReadLock();
                if (games.ContainsKey(login) == true)
                {
                    Console.WriteLine("Отказано во входе: {0}. Причина: Логин уже существует", login);
                    return false;
                }
                gameCollectionLocker.ExitReadLock();

                //Поиск свободной игры

                GameState game = GetWaitingGame();

                if (game == null)   //Свободной игры нет
                {
                    Console.WriteLine("Свободной игры нет. Создаём");

                    //Создание игры

                    game = new GameState();
                    game.firstPlayer = login;
                    game.state = GameState.GAME_STATE.WAIT_SECOND_PLAYER;
                }
                else                //Есть ожидающая игрока игра
                {
                    Console.WriteLine("Есть ожидающая игра. Присоединяемся");

                    game.secondPlayer = login;
                    game.state = GameState.GAME_STATE.FIRST_PLAYER;
                    game.currentPlayer = game.firstPlayer;
                }

                //Добавление игры в коллекцию

                gameCollectionLocker.EnterWriteLock();
                games[login] = game;
                gameCollectionLocker.ExitWriteLock();

                //Время последнего доступа клиента

                usersLastTime[login] = DateTime.Now;

                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
            finally
            {
                if (gameCollectionLocker.IsReadLockHeld == true) { gameCollectionLocker.ExitReadLock(); }
                if (gameCollectionLocker.IsWriteLockHeld == true) { gameCollectionLocker.ExitWriteLock(); }
            }
        }