Skip to content

Commit

Permalink
Various, such as callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBetteridge committed Nov 19, 2016
1 parent db2649c commit ae191e1
Show file tree
Hide file tree
Showing 30 changed files with 669 additions and 444 deletions.
3 changes: 3 additions & 0 deletions Connect4.Manual/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public enum GameState
Draw = 5
}




public enum CellContent
{
Empty = 0,
Expand Down
4 changes: 2 additions & 2 deletions Connect4.Manual/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Program
private const string teamPassword = "MyPassword";

// The location of the game server
private const string serverURL = "http://yorkdojoconnect4.azurewebsites.net/";
//private const string serverURL = "http://localhost:55011/";
//private const string serverURL = "http://yorkdojoconnect4.azurewebsites.net/";
private const string serverURL = "http://localhost:55011/";

private static void MakeMove(Game game, Guid playerID, string serverURL)
{
Expand Down
165 changes: 82 additions & 83 deletions Connect4.MonteCarlo/API.cs
Original file line number Diff line number Diff line change
@@ -1,104 +1,103 @@
using System;
using System.Net.Http;

namespace Connect4.ExampleBot

internal static class API
{
internal static class API
/// <summary>
/// Get the current state of the game
/// </summary>
/// <param name="playerID"></param>
/// <param name="serverURL"></param>
/// <returns></returns>
internal static Game GetGame(Guid playerID, string serverURL)
{
/// <summary>
/// Get the current state of the game
/// </summary>
/// <param name="playerID"></param>
/// <param name="serverURL"></param>
/// <returns></returns>
internal static Game GetGame(Guid playerID, string serverURL)
{
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(serverURL);
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(serverURL);

var httpResponseMessage = httpClient.GetAsync($"api/GameState?playerID={playerID}").Result;
if (!httpResponseMessage.IsSuccessStatusCode)
{
// Something has gone wrong
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result;
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "GameState", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors));
}
else
{
// All good
var result = httpResponseMessage.Content.ReadAsAsync<Game>().Result;
return result;
}
var httpResponseMessage = httpClient.GetAsync($"api/GameState?playerID={playerID}").Result;
if (!httpResponseMessage.IsSuccessStatusCode)
{
// Something has gone wrong
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result;
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "GameState", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors));
}

/// <summary>
/// Register your team to get your unique player ID
/// </summary>
/// <param name="TeamName"></param>
/// <param name="teamPassword"></param>
/// <param name="serverURL"></param>
/// <returns></returns>
internal static Guid RegisterTeam(string TeamName, string teamPassword, string serverURL)
else
{
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(serverURL);
// All good
var result = httpResponseMessage.Content.ReadAsAsync<Game>().Result;
return result;
}
}

var httpResponseMessage = httpClient.PostAsync($"api/Register?teamName={TeamName}&password={teamPassword}", null).Result;
if (!httpResponseMessage.IsSuccessStatusCode)
{
// Something has gone wrong
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result;
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "RegisterTeam", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors));
}
else
{
// All good
var result = httpResponseMessage.Content.ReadAsAsync<string>().Result;
return new Guid(result);
}
/// <summary>
/// Register your team to get your unique player ID
/// </summary>
/// <param name="TeamName"></param>
/// <param name="teamPassword"></param>
/// <param name="serverURL"></param>
/// <returns></returns>
internal static Guid RegisterTeam(string TeamName, string teamPassword, string serverURL)
{
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(serverURL);

var httpResponseMessage = httpClient.PostAsync($"api/Register?teamName={TeamName}&password={teamPassword}", null).Result;
if (!httpResponseMessage.IsSuccessStatusCode)
{
// Something has gone wrong
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result;
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "RegisterTeam", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors));
}

/// <summary>
/// Plays a move. ColumnNumber should be between 0 and 6
/// </summary>
/// <param name="playerID"></param>
/// <param name="serverURL"></param>
/// <param name="columnNumber"></param>
internal static void MakeMove(Guid playerID, string serverURL, int columnNumber, string teamPassword)
else
{
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(serverURL);

var httpResponseMessage = httpClient.PostAsync($"api/MakeMove?playerID={playerID}&columnNumber={columnNumber}&password={teamPassword}", null).Result;
if (!httpResponseMessage.IsSuccessStatusCode)
{
// Something has gone wrong
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result;
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "MakeMove", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors));
}
// All good
var result = httpResponseMessage.Content.ReadAsAsync<string>().Result;
return new Guid(result);
}

/// <summary>
/// Starts a new game against the same player as the previous game. Your colours will
/// however be swapped (red => yellow and yellow => red)
/// </summary>
/// <param name="playerID"></param>
/// <param name="serverURL"></param>
internal static void NewGame(Guid playerID, string serverURL)
}

/// <summary>
/// Plays a move. ColumnNumber should be between 0 and 6
/// </summary>
/// <param name="playerID"></param>
/// <param name="serverURL"></param>
/// <param name="columnNumber"></param>
internal static void MakeMove(Guid playerID, string serverURL, int columnNumber, string teamPassword)
{
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(serverURL);

var httpResponseMessage = httpClient.PostAsync($"api/MakeMove?playerID={playerID}&columnNumber={columnNumber}&password={teamPassword}", null).Result;
if (!httpResponseMessage.IsSuccessStatusCode)
{
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(serverURL);
// Something has gone wrong
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result;
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "MakeMove", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors));
}
}

var httpResponseMessage = httpClient.PostAsync($"api/NewGame?playerID={playerID}", null).Result;
if (!httpResponseMessage.IsSuccessStatusCode)
{
// Something has gone wrong
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result;
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "NewGame", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors));
}
/// <summary>
/// Starts a new game against the same player as the previous game. Your colours will
/// however be swapped (red => yellow and yellow => red)
/// </summary>
/// <param name="playerID"></param>
/// <param name="serverURL"></param>
internal static void NewGame(Guid playerID, string serverURL)
{
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(serverURL);

var httpResponseMessage = httpClient.PostAsync($"api/NewGame?playerID={playerID}", null).Result;
if (!httpResponseMessage.IsSuccessStatusCode)
{
// Something has gone wrong
var errors = httpResponseMessage.Content.ReadAsStringAsync().Result;
throw new Exception(string.Format("Failed to call {0}. Status {1}. Reason {2}. {3}", "NewGame", (int)httpResponseMessage.StatusCode, httpResponseMessage.ReasonPhrase, errors));
}

}


}
5 changes: 3 additions & 2 deletions Connect4.MonteCarlo/Connect4.MonteCarlo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<ProjectGuid>{53F0D56F-E55C-4CEE-9F96-52E52F3B7386}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Connect4.ExampleBot</RootNamespace>
<AssemblyName>Connect4.ExampleBot</AssemblyName>
<RootNamespace>Connect4.MonteCarlo</RootNamespace>
<AssemblyName>Connect4.MonteCarlo</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
Expand Down Expand Up @@ -53,6 +53,7 @@
<ItemGroup>
<Compile Include="API.cs" />
<Compile Include="Game.cs" />
<Compile Include="MonteCarlo.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
Loading

0 comments on commit ae191e1

Please sign in to comment.