public override PlayerAction Play(Tank tank) { if (TankHelper.EnemyInFront(tank, Opponent.Tank1, Opponent.Tank2, map)) { return(PlayerActionHelper.GetAttackAction(tank)); } MyWanderingTank myTank = MyWanderingTank.GetMyTank(tank); PlayerAction pAction = new PlayerAction(); Block currentBlock = map.Blocks[tank.X, tank.Y]; BlockNeighbours neighbours = map.GetNeighbours(currentBlock); Block frontBlock = neighbours.GetNeighbour(tank.Direction); if (myTank.Turned) { pAction.PlayerActionType = PlayerActionType.Forward; pAction.Direction = tank.Direction; myTank.Turned = false; return(pAction); } if (MathHelper.GetRandomNumber(0, 3) != 0 && frontBlock != null && frontBlock.Pattern == Pattern.Clear) { pAction.PlayerActionType = PlayerActionType.Forward; pAction.Direction = tank.Direction; myTank.Turned = false; return(pAction); } //Turn int randomNumber = MathHelper.GetRandomNumber(0, 100) + myTank.Number;// % 4; var myNeighbours = map.GetNeighbours(currentBlock); var passableBlocks = myNeighbours.Neighbours.Where(c => c.Passable && c != frontBlock).ToList(); myTank.Turned = true; var passableBlocksCount = passableBlocks.Count(); if (passableBlocksCount == 0) { pAction.PlayerActionType = PlayerActionType.Forward; pAction.Direction = tank.Direction; myTank.Turned = false; return(pAction); } var index = randomNumber % passableBlocksCount; var nextBlock = passableBlocks[index]; pAction.Direction = DirectionHelper.GetDirection(currentBlock.X, currentBlock.Y, nextBlock.X, nextBlock.Y); pAction.PlayerActionType = PlayerActionType.Turn; return(pAction); }
public override PlayerAction Play(Tank tank) { if (TankHelper.EnemyInFront(tank, Opponent.Tank1, Opponent.Tank2, map)) { return(PlayerActionHelper.GetAttackAction(tank)); } PlayerAction pAction = new PlayerAction(); Block currentBlock = map.Blocks[tank.X, tank.Y]; BlockNeighbours neighbours = map.GetNeighbours(currentBlock); Block frontBlock = neighbours.GetNeighbour(tank.Direction); Block[] nextDistinations = Opponent.Flag.GetCloser(tank.X, tank.Y); //If moving forward makes the tank closer to enemy flag, move forward. if (frontBlock != null && frontBlock.Pattern == Pattern.Clear) { foreach (Block b in nextDistinations) { if (b == frontBlock) { pAction.PlayerActionType = PlayerActionType.Forward; pAction.Direction = tank.Direction; return(pAction); } } } if (Opponent.Flag.Captured) { pAction.PlayerActionType = PlayerActionType.Turn; return(pAction); } //Turn toward one of the next distinations. pAction.Direction = currentBlock.GetDirection(nextDistinations[0]); pAction.PlayerActionType = PlayerActionType.Turn; return(pAction); }
public BlockNeighbours GetNeighbours(GameObject go) { string[] stringValues = go.name.Split(','); int x = int.Parse(stringValues[0]); int y = int.Parse(stringValues[1]); BlockNeighbours bn = new BlockNeighbours(); if (x > 0) bn.left = generatedBlocks2D[x - 1][y]; if (x < gridWidth - 1) bn.right = generatedBlocks2D[x + 1][y]; if (y > 0) bn.bottom = generatedBlocks2D[x][y - 1]; if (y < gridHeight -1) bn.top = generatedBlocks2D[x][y + 1]; return bn; }