Esempio n. 1
0
        private Game.Position[] findCoins(GameMessage gameMessage)
        {
            List <Game.Position> positions = new List <Game.Position>();

            int s = gameMessage.game.getMapSize();

            for (int i = 0; i < s; i++)
            {
                for (int j = 0; j < s; j++)
                {
                    Game.Position p = new Game.Position(i, j);
                    if (game.getTileTypeAt(p) == TileType.BLITZIUM)
                    {
                        positions.Add(p);
                    }
                }
            }
            try
            {
                //Console.WriteLine(positions[0].ToString());
            }
            catch
            {
                Console.WriteLine("no more coins");
            }
            return(positions.ToArray());
        }
Esempio n. 2
0
        private void CreateBoard()
        {
            int slotCount = Width * Height;

            board = new Slot[slotCount];

            for (int i = 0; i != slotCount; ++i)
            {
                board[i] = new Slot();
            }

            GenerateSpacesIteration[] iterations = new GenerateSpacesIteration[] {
                new GenerateSpacesIteration(
                    new Game.Position(0, 1),
                    new Game.Position(0, 0),
                    new Game.Position(Height, Width - winSize + 1)
                    ),

                new GenerateSpacesIteration(
                    new Game.Position(1, 0),
                    new Game.Position(0, 0),
                    new Game.Position(Height - winSize + 1, Width)
                    ),

                new GenerateSpacesIteration(
                    new Game.Position(1, 1),
                    new Game.Position(0, 0),
                    new Game.Position(Height - winSize + 1, Width - winSize + 1)
                    ),

                new GenerateSpacesIteration(
                    new Game.Position(-1, 1),
                    new Game.Position(winSize - 1, 0),
                    new Game.Position(Height, Width - winSize + 1)
                    )
            };

            foreach (GenerateSpacesIteration iteration in iterations)
            {
                for (int i = iteration.start.Row; i != iteration.end.Row; ++i)
                {
                    for (int j = iteration.start.Col; j != iteration.end.Col; ++j)
                    {
                        Game.Position[] space = new Game.Position[winSize];

                        for (int k = 0; k != winSize; ++k)
                        {
                            var currentPosition = new Game.Position(
                                i + iteration.dir.Row * k,
                                j + iteration.dir.Col * k
                                );

                            space[k] = currentPosition;

                            SlotAt(currentPosition).spaces.Add(space);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
    public void Test(bool shouldSucceed, params int[] positions)
    {
        var state = new Game.State();
        var board = new int[Game.Position.LastIndex];

        int i           = 0;
        var pos         = new List <Game.Position>();
        var expectation = new List <Game.WinningCondition>();

        while (i < positions.Length)
        {
            var player = positions[i++];
            var p      = new Game.Position(positions[i++], positions[i++], positions[i++]);
            state.SetPlayerAtPosition(player, p);
            pos.Add(p);

            if (pos.Count == 3)
            {
                expectation.Add(new Game.WinningCondition(player, pos));
                pos = new List <Game.Position>();
            }
        }


        var reality = Game.WinningCondition.FromGameState(state.RawBoardData);

        if (shouldSucceed)
        {
            Assert.That(reality, Is.EqualTo(expectation));
        }
        else
        {
            Assert.That(reality, !Is.EqualTo(expectation));
        }
    }
Esempio n. 4
0
    public void TestConversion()
    {
        for (int size = 0; size < 3; size++)
        {
            for (int y = 0; y < 3; y++)
            {
                for (int x = 0; x < 3; x++)
                {
                    var source      = new Game.Position(size, x, y);
                    var destination = new Game.Position(source.index);

                    Assert.That(destination.size, Is.EqualTo(source.size));
                    Assert.That(destination.x, Is.EqualTo(source.x));
                    Assert.That(destination.y, Is.EqualTo(source.y));
                    Assert.That(destination.isNest, Is.EqualTo(source.isNest));
                    Assert.That(destination.player, Is.EqualTo(source.player));

                    Assert.That(destination.size, Is.EqualTo(size));
                    Assert.That(destination.x, Is.EqualTo(x));
                    Assert.That(destination.y, Is.EqualTo(y));

                    Assert.That(destination, Is.EqualTo(source));
                }
            }
        }
    }
Esempio n. 5
0
        private bool isInDanger(GameMessage gameMessage, int danger_meter)
        {
            int steps_to_safety = getStepToSafety(getClosestSafe());
            int steps_to_die    = 99999;

            foreach (Player player in gameMessage.players)
            {
                if (player.id == me.id)
                {
                    continue;
                }
                Game.Position enenemy_pos     = player.position;
                Game.Position endangered_tail = getClosestTail(enenemy_pos);
                int           current_enemy_kill_potential = pfGrid.GetPath(toAStarPos(endangered_tail), toAStarPos(enenemy_pos)).Length;
                if (steps_to_die > current_enemy_kill_potential)
                {
                    steps_to_die = current_enemy_kill_potential;
                }
            }
            if (steps_to_die + danger_meter < steps_to_safety)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 6
0
    public void MoveTo(Game.Position position)
    {
        this.gamePosition = position;
        Vector3 target = Board.GetVectorForGamePosition(position);

        targetPosition = target;
    }
Esempio n. 7
0
 public Position[] initPath(Game.Position blitzPos)
 {
     // initAStar();
     hasPath          = true;
     currentPath      = pfGrid.GetPath(toAStarPos(me.position), toAStarPos(blitzPos), MovementPatterns.LateralOnly);
     currentPathIndex = 1;
     return(currentPath);
 }
Esempio n. 8
0
 public GenerateSpacesIteration(
     Game.Position dir,
     Game.Position start,
     Game.Position end)
 {
     this.dir   = dir;
     this.start = start;
     this.end   = end;
 }
Esempio n. 9
0
    public Peg SpawnPeg(int player, Game.Position position)
    {
        var peg = CreatePeg(position.size);

        peg.transform.localPosition = GetVectorForGamePosition(position);
        peg.Player       = player;
        peg.gamePosition = position;
        return(peg);
    }
Esempio n. 10
0
 public static Vector3 GetVectorForGamePosition(Game.Position gamePosition)
 {
     if (gamePosition.isStable)
     {
         return(GetVectorForStableIndex(gamePosition.player * 3 + gamePosition.x));
     }
     else
     {
         return(GetVectorForBoardPosition(gamePosition.x, gamePosition.y));
     }
 }
Esempio n. 11
0
 public void EndTurn(Game.Position position)
 {
     if (board.HasWon(position))
     {
         Win(CurrentTurn);
     }
     else if (!board.HasAnyEmpty)
     {
         Tie();
     }
     else
     {
         NextTurn();
     }
 }
Esempio n. 12
0
    public void Mark(Slot slot)
    {
        position = new Game.Position(slot.position.y, slot.position.x);

        board.Mark(position, currentSymbol.Value.symbol);
        board.DisableSlots();

        slot.Mark(currentSymbol.Value);
        slot.GetComponentInChildren <SpriteMaskAnimation>().Completed += OnMarkDisplayCompleted;

        if (game.CurrentTurn.NotifyRemote)
        {
            networkHelper.SendPosition(position);
        }
    }
Esempio n. 13
0
        private bool checkSuicide(Player me, Game game, Game.Position position)
        {
            //Position fPos = me.getFowardPositition();
            TileType tile = game.getTileTypeAt(position);

            if (tile == TileType.ASTEROIDS || tile == TileType.BLACK_HOLE)
            {
                return(true);
            }

            if (me.isTail(position))
            {
                return(true);
            }

            return(false);
        }
Esempio n. 14
0
        private Game.Position getClosestTail(Game.Position enenemy_pos)
        {
            if (me.tail.Length == 0)
            {
                return(me.position);
            }
            Game.Position min     = me.tail[0];
            int           mindist = 999;

            foreach (Game.Position tail in me.tail)
            {
                if (tail.distance(enenemy_pos) < mindist)
                {
                    min = tail;
                }
            }
            return(min);
        }
Esempio n. 15
0
        public Player.Move playPath(GameMessage message)
        {
            if (currentPath.Length <= 1)
            {
                Random        random     = new Random();
                Player.Move[] legalMoves = getLegalMovesForCurrentTick(message);
                try {
                    return(legalMoves[random.Next(legalMoves.Length)]);
                }
                catch {
                    return(Player.Move.FORWARD);
                }
            }
            Position nextPosAS = currentPath[currentPathIndex];

            Console.WriteLine(currentPath.ToString());
            Console.WriteLine(me.direction.ToString());
            Console.WriteLine(nextPosAS.ToString());
            Game.Position nextPos = new Game.Position(nextPosAS.X, nextPosAS.Y);
            currentPathIndex++;
            if (currentPathIndex >= currentPath.Length)
            {
                hasPath = false;
            }
            if (!me.canGoTo(nextPos))
            {
                bool isSuicideLeft  = checkSuicideLeft();
                bool isSuicideRight = checkSuicideRight();
                if (isSuicideLeft && !isSuicideRight)
                {
                    return(Player.Move.TURN_RIGHT);
                }
                if (!isSuicideLeft && isSuicideRight)
                {
                    return(Player.Move.TURN_LEFT);
                }
                return(Player.Move.FORWARD);
            }
            return(positionToMove(nextPos));
        }
Esempio n. 16
0
        private Game.Position[] findSafes(GameMessage gameMessage)
        {
            List <Game.Position> positions = new List <Game.Position>();

            int s = gameMessage.game.getMapSize();

            for (int i = 0; i < s; i++)
            {
                for (int j = 0; j < s; j++)
                {
                    Game.Position p = new Game.Position(i, j);
                    if (game.getTileTypeAt(p) == TileType.CONQUERED || game.getTileTypeAt(p) == TileType.CONQUERED_PLANET)
                    {
                        if (game.getTileOwnerId(p) == me.id)
                        {
                            positions.Add(p);
                        }
                    }
                }
            }

            return(positions.ToArray());
        }
Esempio n. 17
0
        private Game.Position getClosestSafe()
        {
            if (safes.Length == 0)
            {
                return(me.spawnPosition);
            }

            Game.Position min     = safes[0];
            int           minDist = min.distance(me.position);

            for (int i = 1; i < safes.Length; i++)
            {
                Game.Position pos = safes[i];

                if (pos.distance(me.position) < minDist)
                {
                    min     = pos;
                    minDist = pos.distance(me.position);
                }
            }
            Console.WriteLine(min.ToString());
            return(min);
        }
Esempio n. 18
0
        public void initAStar()
        {
            int mapX = game.map.Length;
            int mapY = game.map[0].Length;

            pfGrid = new Grid(mapX, mapY, 1);
            for (int i = 0; i < mapX; i++)
            {
                for (int j = 0; j < mapY; j++)
                {
                    Game.Position pos  = new Game.Position(i, j);
                    TileType      tile = game.getTileTypeAt(pos);
                    if (me.isTail(pos) && !me.spawnPosition.sameAs(pos))
                    {
                        pfGrid.BlockCell(new Position(i, j));
                    }
                    if (Game.isBlock(tile))
                    {
                        pfGrid.BlockCell(new Position(i, j));
                    }
                }
            }
            currentPathIndex = 1;
        }
Esempio n. 19
0
        public Player.Move nextMove(GameMessage gameMessage)
        {
            // Here is where the magic happens, for now the moves are random. I bet you can do better ;)

            Random random = new Random();

            game = gameMessage.game;
            gameMessage.getPlayerMapById.TryGetValue(gameMessage.game.playerId, out me);

            initAStar();
            // Player.Move[] legalMoves = getLegalMovesForCurrentTick(gameMessage);
            if (me.killed)
            {
                first_move       = true;
                hasPath          = false;
                dangerMovesCount = 0;
                modeExpand       = false;
                modeReturn       = false;
            }

            Game.Position target;
            coins = findCoins(gameMessage);
            safes = findSafes(gameMessage);
            if (game.getTileTypeAt(me.position) != TileType.CONQUERED)
            {
                if (isInDanger(gameMessage, dangerMeter))
                {
                    modeReturn = true;
                    modeExpand = false;
                }


                if (modeReturn)
                {
                    target = returnToSafe();
                }
                else
                {
                    if (dangerMovesCount >= dangerCAc)
                    {
                        dangerMovesCount = 0;

                        modeExpand   = true;
                        expendTarget = getExpand();
                    }
                    if (modeExpand)
                    {
                        if (me.position.sameAs(new Game.Position(expendTarget.x, expendTarget.y)))
                        {
                            modeExpand = false;
                            modeReturn = true;
                            target     = returnToSafe();
                        }
                        else
                        {
                            target = expendTarget;
                        }
                    }
                    else
                    {
                        modeReturn = false;
                        dangerMovesCount++;
                        target = getClosestCoin();
                    }
                }
            }
            else
            {
                dangerMovesCount = 0;
                modeReturn       = false;
                dangerMovesCount++;
                target = getClosestCoin();
            }


            //if (!hasPath)
            initPath(target);
            Console.WriteLine("modeReturn:" + modeReturn.ToString() + " modeExpand:" + modeExpand.ToString());
            return(playPath(gameMessage));

            // You can print out a pretty version of the map but be aware that
            // printing out long strings can impact your bot performance (30 ms in average).
            // Console.WriteLine(gameMessage.game.prettyMap);

            /*  if (legalMoves.Length == 0)
             * {
             *    return Player.Move.FORWARD;
             * }
             * return legalMoves[random.Next(legalMoves.Length)];*/
        }
Esempio n. 20
0
 public void SendPosition(Game.Position position)
 {
     Send(client.GetStream(), new Message.Position(position.Row, position.Col));
 }
Esempio n. 21
0
 private Position toAStarPos(Game.Position pos)
 {
     return(new Position(pos.x, pos.y));
 }
Esempio n. 22
0
        private Game.Position getExpand()
        {
            switch (me.direction)
            {
            case Player.Direction.UP: {
                Game.Position rightPos     = new Game.Position(me.position.x + expendCount, me.position.y);
                Position[]    pathToExpend = pfGrid.GetPath(toAStarPos(me.position), toAStarPos(rightPos), MovementPatterns.LateralOnly);
                if (pathToExpend.Length != 0 && pathToExpend.Length - 1 <= expendCount)
                {
                    return(rightPos);
                }
                Game.Position leftPos       = new Game.Position(me.position.x + expendCount, me.position.y);
                Position[]    pathToExpend2 = pfGrid.GetPath(toAStarPos(me.position), toAStarPos(rightPos), MovementPatterns.LateralOnly);
                if (pathToExpend.Length != 0 && pathToExpend2.Length - 1 <= expendCount)
                {
                    return(leftPos);
                }
                modeExpand = false;
                modeReturn = true;
                return(getClosestSafe());
            }

            case Player.Direction.DOWN: {
                Game.Position rightPos     = new Game.Position(me.position.x + expendCount, me.position.y);
                Position[]    pathToExpend = pfGrid.GetPath(toAStarPos(me.position), toAStarPos(rightPos), MovementPatterns.LateralOnly);
                if (pathToExpend.Length != 0 && pathToExpend.Length - 1 <= expendCount)
                {
                    return(rightPos);
                }
                Game.Position leftPos       = new Game.Position(me.position.x + expendCount, me.position.y);
                Position[]    pathToExpend2 = pfGrid.GetPath(toAStarPos(me.position), toAStarPos(rightPos), MovementPatterns.LateralOnly);
                if (pathToExpend.Length != 0 && pathToExpend2.Length - 1 <= expendCount)
                {
                    return(leftPos);
                }
                modeExpand = false;
                modeReturn = true;
                return(getClosestSafe());
            }


            case Player.Direction.LEFT:
            {
                Game.Position rightPos     = new Game.Position(me.position.x, me.position.y + expendCount);
                Position[]    pathToExpend = pfGrid.GetPath(toAStarPos(me.position), toAStarPos(rightPos), MovementPatterns.LateralOnly);
                if (pathToExpend.Length != 0 && pathToExpend.Length - 1 <= expendCount)
                {
                    return(rightPos);
                }
                Game.Position leftPos       = new Game.Position(me.position.x, me.position.y - expendCount);
                Position[]    pathToExpend2 = pfGrid.GetPath(toAStarPos(me.position), toAStarPos(rightPos), MovementPatterns.LateralOnly);
                if (pathToExpend.Length != 0 && pathToExpend2.Length - 1 <= expendCount)
                {
                    return(leftPos);
                }
                modeExpand = false;
                modeReturn = true;
                return(getClosestSafe());
            }

            default:
            {
                Game.Position rightPos     = new Game.Position(me.position.x, me.position.y + expendCount);
                Position[]    pathToExpend = pfGrid.GetPath(toAStarPos(me.position), toAStarPos(rightPos), MovementPatterns.LateralOnly);
                if (pathToExpend.Length != 0 && pathToExpend.Length - 1 <= expendCount)
                {
                    return(rightPos);
                }
                Game.Position leftPos       = new Game.Position(me.position.x, me.position.y - expendCount);
                Position[]    pathToExpend2 = pfGrid.GetPath(toAStarPos(me.position), toAStarPos(rightPos), MovementPatterns.LateralOnly);
                if (pathToExpend.Length != 0 && pathToExpend2.Length - 1 <= expendCount)
                {
                    return(leftPos);
                }
                modeExpand = false;
                modeReturn = true;
                return(getClosestSafe());
            }
            }
        }
Esempio n. 23
0
 public void Mark(Game.Position position)
 {
     Mark(board.SlotAt(position));
 }
Esempio n. 24
0
        private void ExecuteProperty(KgsSgfProperty prop, KgsGame ongame)
        {
            switch (prop.Name)
            {
            case "RULES":
                RulesDescription rules = prop;
                ongame.Info.BoardSize = new GameBoardSize(rules.Size);
                foreach (var player in ongame.Controller.Players)
                {
                    player.Clock = rules.CreateTimeControl();
                }
                ongame.Info.NumberOfHandicapStones = rules.Handicap;
                ongame.Info.Komi        = rules.Komi;
                ongame.Info.RulesetType = KgsHelpers.ConvertRuleset(rules.Rules);
                // (future work) TODO (Petr) ensure that even written late, these values are respected
                break;

            case "PLAYERNAME":
            case "PLAYERRANK":
            case "DATE":
            case "PLACE":
                // We do not need this information - we already have some and don't need the trest.
                break;

            case "PHANTOMCLEAR":
                // I don't know what to do with this yet.
                break;

            case "ADDSTONE":
                ongame.Controller.AddHandicapStonePosition(new Position(prop.Loc.X,
                                                                        KgsCoordinates.TheirsToOurs(prop.Loc.Y, ongame.Info.BoardSize)));
                break;

            case "COMMENT":
                // "Putti [2k]: hi\n
                string[] splitByNewlines =
                    prop.Text.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var s in splitByNewlines)
                {
                    var tuple = KgsRegex.ParseCommentAsChat(s);
                    if (tuple != null)
                    {
                        var chatMessage = new ChatMessage(tuple.Item1, tuple.Item2,
                                                          DateTimeOffset.Now, tuple.Item1 == ongame.Controller.Server.Username ? ChatMessageKind.Outgoing : ChatMessageKind.Incoming);
                        ongame.Controller.KgsConnector.ChatMessageFromServer(chatMessage);
                    }
                }
                break;

            case "DEAD":
                if (ongame.Controller.Phase.Type != Modes.LiveGame.Phases.GamePhaseType.LifeDeathDetermination)
                {
                    ongame.Controller.SetPhase(Modes.LiveGame.Phases.GamePhaseType.LifeDeathDetermination);
                }
                ongame.Controller.BlackDoneReceived = false;
                ongame.Controller.WhiteDoneReceived = false;
                ongame.Controller.KgsConnector.ForceKillGroup(new Position(prop.Loc.X, KgsCoordinates.TheirsToOurs(prop.Loc.Y, ongame.Info.BoardSize)));
                break;

            case "TIMELEFT":
                StoneColor colorTimeLeft = (prop.Color == "black" ? StoneColor.Black : StoneColor.White);
                ongame.Controller.Players[colorTimeLeft].Clock.UpdateFromKgsFloat(prop.Float);
                break;

            case "MOVE":
                Move       move;
                string     propColor = prop.Color;
                StoneColor color     = propColor == "white" ? StoneColor.White : StoneColor.Black;
                if (!prop.Loc.IsPass)
                {
                    XY       whereTo  = prop.Loc;
                    Position position = new Game.Position(whereTo.X, KgsCoordinates.TheirsToOurs(whereTo.Y, ongame.Info.BoardSize));
                    move = Move.PlaceStone(color, position);
                }
                else
                {
                    move = Move.Pass(color);
                }
                foreach (var player in ongame.Controller.Players)
                {
                    if (player.Agent is KgsAgent)
                    {
                        ((KgsAgent)player.Agent).StoreMove(this.Layer, color, move);
                    }
                }
                break;

            default:
                break;
            }
        }
Esempio n. 25
0
 private int getStepToSafety(Game.Position safetyPos)
 {
     Position[] pathToSafety = pfGrid.GetPath(toAStarPos(me.position), toAStarPos(safetyPos), MovementPatterns.LateralOnly);
     return(pathToSafety.Length);
 }
Esempio n. 26
0
        private Player.Move positionToMove(Game.Position position)
        {
            switch (me.direction)
            {
            case Player.Direction.UP:
            {
                if (position.x > me.position.x)
                {
                    return(Player.Move.TURN_RIGHT);
                }
                else if (position.x < me.position.x)
                {
                    return(Player.Move.TURN_LEFT);
                }
                else
                {
                    return(Player.Move.FORWARD);
                }
                break;
            }

            case Player.Direction.DOWN:
            {
                if (position.x < me.position.x)
                {
                    return(Player.Move.TURN_RIGHT);
                }
                else if (position.x > me.position.x)
                {
                    return(Player.Move.TURN_LEFT);
                }
                else
                {
                    return(Player.Move.FORWARD);
                }
                break;
            }

            case Player.Direction.RIGHT:
            {
                if (position.y > me.position.y)
                {
                    return(Player.Move.TURN_RIGHT);
                }
                else if (position.y < me.position.y)
                {
                    return(Player.Move.TURN_LEFT);
                }
                else
                {
                    return(Player.Move.FORWARD);
                }
                break;
            }

            case Player.Direction.LEFT:
            {
                if (position.y < me.position.y)
                {
                    return(Player.Move.TURN_RIGHT);
                }
                else if (position.y > me.position.y)
                {
                    return(Player.Move.TURN_LEFT);
                }
                else
                {
                    return(Player.Move.FORWARD);
                }
                break;
            }

            default:
            {
                return(Player.Move.FORWARD);
            }
            }
        }