Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added C# #23

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 69 additions & 0 deletions starter_kits/C#/Halite3/Halite3.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F799CB2A-B5F9-43EF-990E-7C17954E190B}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Halite3</RootNamespace>
<AssemblyName>Halite3</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="hlt\Command.cs" />
<Compile Include="hlt\Constants.cs" />
<Compile Include="hlt\Direction.cs" />
<Compile Include="hlt\Dropoff.cs" />
<Compile Include="hlt\Entity.cs" />
<Compile Include="hlt\EntityId.cs" />
<Compile Include="hlt\Game.cs" />
<Compile Include="hlt\GameMap.cs" />
<Compile Include="hlt\Input.cs" />
<Compile Include="hlt\Log.cs" />
<Compile Include="hlt\MapCell.cs" />
<Compile Include="hlt\Player.cs" />
<Compile Include="hlt\PlayerId.cs" />
<Compile Include="hlt\Position.cs" />
<Compile Include="hlt\Ship.cs" />
<Compile Include="hlt\Shipyard.cs" />
<Compile Include="MyBot.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
52 changes: 52 additions & 0 deletions starter_kits/C#/Halite3/MyBot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Halite_Project.hlt;

namespace Halite_Project {
class MyBot {
public static void Main(string[] args) {
long rngSeed;
if(args.Length > 1) {
rngSeed = int.Parse(args[1]);
} else {
rngSeed = Stopwatch.GetTimestamp();
}
Random rng = new Random((int) rngSeed);

Game game = new Game();
// At this point "game" variable is populated with initial map data.
// This is a good place to do computationally expensive start-up pre-processing.
// As soon as you call "ready" function below, the 2 second per turn timer will start.
game.Ready("MyC#Bot");

Log.CreateLog("Successfully created bot! My Player ID is " + game.myId + ". Bot rng seed is " + rngSeed + ".");

while(game.turnNumber < Constants.MAX_TURNS) {
game.UpdateFrame();
Player me = game.me;
GameMap gameMap = game.gameMap;

List<Command> commandQueue = new List<Command>();

foreach(Ship ship in me.ships.Values) {
if(gameMap.At(ship).halite < Constants.MAX_HALITE / 10 || ship.IsFull()) {
EDirection randomDirection = Direction.ALL_CARDINALS[rng.Next(4)];
commandQueue.Add(ship.Move(randomDirection));
} else {
commandQueue.Add(ship.StayStill());
}
}

if (game.turnNumber <= 200 && me.halite >= Constants.SHIP_COST && !gameMap.At(me.shipyard).IsOccupied()) {
commandQueue.Add(me.shipyard.Spawn()); //spawn a new ship
}

game.EndTurn(commandQueue);
}
}
}
}
36 changes: 36 additions & 0 deletions starter_kits/C#/Halite3/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Halite3")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Halite3")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("f799cb2a-b5f9-43ef-990e-7c17954e190b")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
40 changes: 40 additions & 0 deletions starter_kits/C#/Halite3/hlt/Command.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Halite_Project.hlt {
public class Command {
public string command;

public static Command SpawnShip() {
return new Command("g");
}

public static Command TransformShipIntoDropoffSite(EntityId id) {
return new Command("c " + id);
}

public static Command Move(EntityId id, EDirection direction) {
return new Command("m " + id + " " + direction);
}

private Command(string command) {
this.command = command;
}

public override bool Equals(object obj) {
if (this == obj) return true;
if (obj == null || GetType() == obj.GetType()) return false;

Command command1 = (Command)obj;

return command.Equals(command1.command);
}

public override int GetHashCode() {
return command.GetHashCode();
}
}
}
100 changes: 100 additions & 0 deletions starter_kits/C#/Halite3/hlt/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Halite_Project.hlt {
class Constants {
/** The maximum amount of halite a ship can carry. */
public static int MAX_HALITE;
/** The cost to build a single ship. */
public static int SHIP_COST;
/** The cost to build a dropoff. */
public static int DROPOFF_COST;
/** The maximum number of turns a game can last. */
public static int MAX_TURNS;
/** 1/EXTRACT_RATIO halite (rounded) is collected from a square per turn. */
public static int EXTRACT_RATIO;
/** 1/MOVE_COST_RATIO halite (rounded) is needed to move off a cell. */
public static int MOVE_COST_RATIO;
/** Whether inspiration is enabled. */
public static bool INSPIRATION_ENABLED;
/** A ship is inspired if at least INSPIRATION_SHIP_COUNT opponent ships are within this Manhattan distance. */
public static int INSPIRATION_RADIUS;
/** A ship is inspired if at least this many opponent ships are within INSPIRATION_RADIUS distance. */
public static int INSPIRATION_SHIP_COUNT;
/** An inspired ship mines 1/X halite from a cell per turn instead. */
public static int INSPIRED_EXTRACT_RATIO;
/** An inspired ship that removes Y halite from a cell collects X*Y additional halite. */
public static double INSPIRED_BONUS_MULTIPLIER;
/** An inspired ship instead spends 1/X% halite to move. */
public static int INSPIRED_MOVE_COST_RATIO;

public static void PopulateConstants(string stringFromEngine) {
string[] rawTokens = Regex.Split(stringFromEngine, "[{}, :\"]+");// stringFromEngine.Split(new string[] { "[{}, :\"]+" }, StringSplitOptions.None);
List<string> tokens = new List<string>();

for(int i = 0; i < rawTokens.Length; i++) {
if(rawTokens[i].Length != 0) {
tokens.Add(rawTokens[i]);
}
}

if((tokens.Count % 2) != 0) {
Log.CreateLog("Error: constants: expected even total number of key and value tokens from server.");
throw new Exception();
}

Dictionary<string, string> constantsMap = new Dictionary<string, string>();

for(int i = 0; i < tokens.Count; i += 2) {
constantsMap.Add(tokens[i], tokens[i + 1]);
}

SHIP_COST = GetInt(constantsMap, "NEW_ENTITY_ENERGY_COST");
DROPOFF_COST = GetInt(constantsMap, "DROPOFF_COST");
MAX_HALITE = GetInt(constantsMap, "MAX_ENERGY");
MAX_TURNS = GetInt(constantsMap, "MAX_TURNS");
EXTRACT_RATIO = GetInt(constantsMap, "EXTRACT_RATIO");
MOVE_COST_RATIO = GetInt(constantsMap, "MOVE_COST_RATIO");
INSPIRATION_ENABLED = GetBool(constantsMap, "INSPIRATION_ENABLED");
INSPIRATION_RADIUS = GetInt(constantsMap, "INSPIRATION_RADIUS");
INSPIRATION_SHIP_COUNT = GetInt(constantsMap, "INSPIRATION_SHIP_COUNT");
INSPIRED_EXTRACT_RATIO = GetInt(constantsMap, "INSPIRED_EXTRACT_RATIO");
INSPIRED_BONUS_MULTIPLIER = GetDouble(constantsMap, "INSPIRED_BONUS_MULTIPLIER");
INSPIRED_MOVE_COST_RATIO = GetInt(constantsMap, "INSPIRED_MOVE_COST_RATIO");
}

private static int GetInt(Dictionary<string, string> map, string key) {
return int.Parse(GetString(map, key));
}

private static double GetDouble(Dictionary<string, string> map, string key) {
return double.Parse(GetString(map, key));
}

private static bool GetBool(Dictionary<string, string> map, string key) {
string stringValue = GetString(map, key);

switch (stringValue) {
case "true": return true;
case "false": return false;
default:
Log.CreateLog("Error: constants: " + key + " constant has value of '" + stringValue +
"' from server. Do not know how to parse that as boolean.");
throw new Exception();
}
}

private static string GetString(Dictionary<string, string> map, string key) {
if (!map.ContainsKey(key)) {
Log.CreateLog("Error: constants: server did not send " + key + " constant.");
throw new Exception();
}

return map[key];
}
}
}
41 changes: 41 additions & 0 deletions starter_kits/C#/Halite3/hlt/Direction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

using System;
using System.Collections.Generic;

namespace Halite_Project.hlt {
public enum EDirection {
NORTH = 'n',
EAST = 'e',
SOUTH = 's',
WEST = 'w',
STILL = 'o'
}

public class Direction {
public EDirection direction = EDirection.STILL;

public static List<EDirection> ALL_CARDINALS = new List<EDirection>();

static Direction() {
ALL_CARDINALS.Add(EDirection.NORTH);
ALL_CARDINALS.Add(EDirection.SOUTH);
ALL_CARDINALS.Add(EDirection.EAST);
ALL_CARDINALS.Add(EDirection.WEST);
}

public EDirection InvertDirection() {
switch (direction) {
case EDirection.NORTH: return EDirection.SOUTH;
case EDirection.EAST: return EDirection.WEST;
case EDirection.SOUTH: return EDirection.NORTH;
case EDirection.WEST: return EDirection.EAST;
case EDirection.STILL: return EDirection.STILL;
default: throw new Exception("Unknown direction " + direction);
}
}

private Direction(EDirection direction) {
this.direction = direction;
}
}
}
18 changes: 18 additions & 0 deletions starter_kits/C#/Halite3/hlt/Dropoff.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Halite_Project.hlt
{
public class Dropoff : Entity
{
public Dropoff(PlayerId owner, EntityId id, Position position) : base(owner, id, position) { }

public static Dropoff _generate(PlayerId playerId)
{
Input input = Input.ReadInput();

EntityId dropoffId = new EntityId(input.GetInt());
int x = input.GetInt();
int y = input.GetInt();

return new Dropoff(playerId, dropoffId, new Position(x, y));
}
}
}