Ejemplo n.º 1
0
        GameEndCondition _innerCheck()
        {
            var inner = ChessService.CurrentGame.InnerGame;

            if (inner.half_moves >= 100)
            { // counts number of moves made without a pawn moving or capture
                return(GameEndCondition.Draw($"No captures or pawn movements made in 50 moves"));
            }
            if (inner.in_checkmate())
            {
                var opposite = inner.swap_color(inner.turn);
                var player   = opposite == "w" ? CurrentGame.White : CurrentGame.Black;
                return(GameEndCondition.Win(player, "In check with no legal moves"));
            }
            if (inner.in_stalemate())
            {
                return(GameEndCondition.Draw("Not in check but no legal moves"));
            }
            if (inner.insufficient_material())
            {
                return(GameEndCondition.Draw("Insufficient material to cause checkmate"));
            }
            if (inner.in_threefold_repetition())
            {
                return(GameEndCondition.Draw("Board repeated thrice"));
            }
            return(GameEndCondition.NotEnded());
        }
Ejemplo n.º 2
0
    public void CheckGameEndedCondition(GameEndCondition gameEndCondition)
    {
        Debug.Log("Проверяю игру на условие выйгрыша");

        if (gameEndCondition == GameEndCondition.ReceivedRightEnergyAmount)
        {
            if (_allSoulsOnGameField.Count <= 0)
            {
                if (_receivedEnergy < _needEnergyToCompleteLevel)
                {
                    GameEnd(false);
                }
                else
                {
                    GameEnd(true);
                }
            }
        }

        if (gameEndCondition == GameEndCondition.CatchedAllSouls)
        {
            if (_cathedSouls == _initialSoulsOnField)
            {
                GameEnd(true);
                return;
            }
            else if ((_cathedSouls < _initialSoulsOnField) && _gameEnded)
            {
                GameEnd(false);
                return;
            }
        }

        if (gameEndCondition == GameEndCondition.ReceivedRightEnergyAmount && _gameEnded)
        {
            if (_receivedEnergy >= _needEnergyToCompleteLevel)
            {
                GameEnd(true);
                return;
            }
            else
            {
                GameEnd(false);
                return;
            }
        }

        if (_playerKilled)
        {
            GameEnd(false);
            return;
        }
    }
Ejemplo n.º 3
0
    private IEnumerator MatchTimer()
    {
        GameEndCondition endCondition = GameDataManager.Data.EndCondition;

        Debug.Log("Match Start");
        MatchDuration = 0;

        Func <bool> TestForDuration = () => MatchDuration > endCondition.MatchDuration;
        Func <bool> TestForScore    = () => players.Aggregate(false, (test, p) => test || p.GetComponent <PlayerBehaviour>().Points >= endCondition.ScoreToWin);
        Func <bool> GameOver        = () => true;

        switch (endCondition.Mode)
        {
        case GameEndConditionMode.score:
            GameOver = TestForScore;
            break;

        case GameEndConditionMode.time:
            GameOver = TestForDuration;
            break;

        case GameEndConditionMode.both:
            GameOver = () => TestForDuration() || TestForScore();
            break;

        default:
            print("WEIRD CASE!!!");
            break;
        }

        while (!GameOver())
        {
            yield return(null);

            MatchDuration += Time.deltaTime;
            GameUIManager.Instance.UpdateTimer((int)MatchDuration);
        }

        GameFinished = true;
        string txt = "";

        foreach (var player in players)
        {
            txt += player.name + " -> " + player.GameUiPosition + " -> " + player.Points;
            Debug.Log(txt);
        }
        GameOverPlayerDataContainer.SetData(players);
        UnityEngine.SceneManagement.SceneManager.LoadScene(GameOverSceneName);
        //endGameCanvas.GetComponentInChildren<TextMeshProUGUI>().text = txt;
    }
Ejemplo n.º 4
0
        public void StartGame(GameMode mode)
        {
            levelManager.PreloadNextLevel();
            doActionOnLevelManagerReady = OnGameReadyOnStart;

            switch (mode)
            {
            case GameMode.limitedTime:
                doGameMode  = GameModeLimitedTime;
                doSaveScore = SaveLimitedTime;
                break;

            case GameMode.memoryLack:
                score       = 100;
                doGameMode  = GameModeMemoryLack;
                doSaveScore = SaveMemoryLack;
                break;

            case GameMode.scoreTarget:
                doGameMode  = GameModeScoreTarget;
                doSaveScore = SaveScoreTarget;
                break;
            }
        }
Ejemplo n.º 5
0
        void handleInGame(ChessPacket ping)
        {
            if (ping.Id == PacketId.MoveRequest)
            {
                var    r     = new OtherGame.Move();
                var    g     = CurrentGame;
                string sFrom = ping.Content["from"].ToObject <string>();
                string sTo   = ping.Content["to"].ToObject <string>();
                if (!(Side == PlayerSide.White && g.InnerGame.turn == "w" || Side == PlayerSide.Black && g.InnerGame.turn == "b"))
                {
                    Send(new ChessPacket(PacketId.MoveRequestRefuse, new MoveRefuse($"{sFrom} -> {sTo}", $"It is not your turn").ToJson()));
                    return;
                }
                r.from = g.InnerGame.parseAlgebraic(sFrom);
                r.to   = g.InnerGame.parseAlgebraic(sTo);
                var promVal = ping.Content["promote"];
                if (promVal != null)
                {
                    r.promotion = getPieceSimple(promVal.ToObject <int>());
                }

                var legalMoves = g.InnerGame.generate_moves(new Dictionary <string, string>()
                {
                    { "legal", "true" }, { "square", r.from.ToString() }
                });
                OtherGame.Move?legalMove = null;
                foreach (var mv in legalMoves)
                {
                    if (mv.from == r.from && mv.to == r.to)
                    {
                        if (!string.IsNullOrWhiteSpace(mv.promotion) && !string.IsNullOrWhiteSpace(r.promotion))
                        {
                            if (mv.promotion != r.promotion)
                            {
                                continue;
                            }
                        }
                        if (!string.IsNullOrWhiteSpace(mv.promotion) && string.IsNullOrWhiteSpace(r.promotion))
                        {
                            if (mv.promotion != "q")
                            {
                                continue;
                            }
                        }
                        if (string.IsNullOrWhiteSpace(mv.promotion) && !string.IsNullOrWhiteSpace(r.promotion))
                        {
                            continue;
                        }
                        legalMove = mv;
                        break;
                    }
                }
                if (legalMove.HasValue == false)
                {
                    Send(new ChessPacket(PacketId.MoveRequestRefuse, new MoveRefuse($"{sFrom} -> {sTo}", $"No legal move to that location").ToJson()));
                    return;
                }
                g.InnerGame.make_move(legalMove.Value);
                g.InnerGame.registerMoveMade();
                Program.LogMsg("Player made move", Discord.LogSeverity.Error, "Player");
                ChessService.CurrentGame.updateBoard();
                Broadcast(new ChessPacket(PacketId.MoveMade, ping.Content));
                CheckGameEnd();
            }
            else if (ping.Id == PacketId.IdentRequest)
            {
                var id     = ping.Content["id"].ToObject <int>();
                var player = CurrentGame.GetPlayer(id);
                var jobj   = ping.Content;
                jobj["player"] = player?.ToJson() ?? JObject.FromObject(null);
                Send(new ChessPacket(PacketId.PlayerIdent, jobj));
            }
            else if (ping.Id == PacketId.RequestScreen)
            {
                if (Player.Permission.HasFlag(ChessPerm.Moderator))
                {
                    var player = CurrentGame.GetPlayer(ping.Content["id"].ToObject <int>());
                    if (player != null)
                    {
                        player.ExpectDemand = true;
                        player.Send(new ChessPacket(PacketId.DemandScreen, new JObject()));
                    }
                }
            }
            else if (ping.Id == PacketId.RequestGameEnd)
            {
                var id     = ping.Content["id"].ToObject <int>();
                var player = id == 0 ? null : ChessService.CurrentGame.GetPlayer(id);
                var end    = id == 0 ? GameEndCondition.Draw($"A-Drawn by {Player.Name}") : GameEndCondition.Win(player, $"A-Won by {Player.Name}");
                ChessService.CurrentGame.DeclareWinner(end);
            }
            else if (ping.Id == PacketId.RequestProcesses)
            {
                if (Player.Permission.HasFlag(ChessPerm.Moderator))
                {
                    var player = CurrentGame.GetPlayer(ping.Content["id"].ToObject <int>());
                    if (player != null)
                    {
                        player.ExpectDemand = true;
                        player.Send(new ChessPacket(PacketId.DemandProcesses, new JObject()));
                    }
                }
            }
            else if (ping.Id == PacketId.ResignRequest)
            {
                ChessConnection opponent;
                if (Side == PlayerSide.White)
                {
                    opponent = ChessService.CurrentGame.Black;
                }
                else if (Side == PlayerSide.Black)
                {
                    opponent = ChessService.CurrentGame.White;
                }
                else
                { // spectators can't resign.
                    return;
                }
                ChessService.CurrentGame.DeclareWinner(GameEndCondition.Win(opponent, "Resigned"));
            }
            else if (ping.Id == PacketId.RequestRevertMove)
            {
                if (Player.Permission.HasFlag(ChessPerm.Moderator))
                {
                    CurrentGame.InnerGame.undo_move();
                    Broadcast(new ChessPacket(PacketId.MoveReverted, ping.Content));
                }
            }
        }