I would like to begin this section with some wise words from a great and wise man.
I didn’t want to be just a snake building champion; I wanted to be the best snake builder of all times.
Arnold Schwarzenegger
Within this repository you can find the beginning of the same journey that Mr. Schwarzenegger walked so many years ago. A journey that took him, an insignificant young boy from Steiermark in Austria, all the way to the podium at not one but seven Mr. Snake Olympia events.
Here you can find a Snake Client written in the C# language for the .Net Core platform.
I would also like to leave you with a qoute from the aforementioned great man
Anything I’ve ever attempted, I was always willing to fail. So you can’t always win, but don’t be afraid of making decisions. You can’t be paralyzed by the fear of failure or you will never push yourself. You keep pushing because you believe in yourself and in your vision and you know that it is the right thing to do, and success will come. So don’t be afraid to fail.
Arnold Schwarzenegger
- Operating system supported by .NET Core 1.0.1 (https://www.microsoft.com/net/core), or docker.
- Your favourite text-editor. Although we would recommend using either 'Visual Studio Code' or 'Visual Studio 2017'.
The solution contains three projects
- Cygni.Snake.Client
- Cygni.Snake.Client.Tests
- Cygni.Snake.SampleBot
This project contains among other things, the SnakeClient, SnakeBot and Map classes.
- SnakeClient: Provides the communication with the Cygni Snake server.
- SnakeBot: Provides an abstract base class for snake bots.
- Map: Provides an way to examine the state of the snake world.
- IGameObserver: Interface for types that can observe games.
Contains unit tests for the Cygni.Snake.Client library.
This project illustrates how to connect to the Cygni Snake server using a SnakeBot implementation and the SnakeClient class.
- Program: The main entry point. Connects to the server and requests a new game.
- MySnakeBot: The sample SnakeBot implementation.
- GamePrinter: An implementation of IGameObserver for printing snake updates to console.
Get source latest source from http://github.com/cygni/snakebot-client-dotnet.
Prerequisites:
- Visual Studio 2017 with .NET Core tooling
Make sure your current directory is the repository root, then build a new image from the Dockerfile in the root directory. This will compile and run the unit tests.
docker build -t snake .
Start container:
docker run -i --rm snake
Make sure your current directory is the repository root, then restore all dependencies:
dotnet restore
Run unit tests (optional):
dotnet test test/Cygni.Snake.Client.Tests/
Run the sample bot client:
dotnet run -p src/Cygni.Snake.SampleBot/
The file src/Cygni.Snake.SampleBot/MySnakeBot.cs contains a skeleton SnakeBot implementation. All you need to do is to implement the GetNextMove()-method to return the direction of choice for your next move. The parameter map represents the current state of the world. It exposes a property called MySnake which represents your snake. Other than that, use the intellisense to examine its API.
public class MySnakeBot : SnakeBot
{
public MySnakeBot(string name) : base(name)
{
}
public override Direction GetNextMove(Map map)
{
// figure out a good move
// do calculated move
return Direction.Down;
}
}
The Main()-method in Program.cs wires up the WebSocket connection with the SnakeClient and the SnakeBot of your choice. You can choose to omit the GamePrinter parameter in SnakeClient. Or, if you prefer, you can provide another implementation to log or do whatever cool stuff you like.
public class Program
{
public static void Main(string[] args)
{
var client = SnakeClient.CreateSnakeClient(new Uri("ws://snake.cygni.se:80/training"), new GamePrinter());
client.Start(new MySnakeBot("dotnetSnake"), true);
Console.ReadLine();
}
}