-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify AIPlayers: get rid of Intellect, AIPlayer class
- Loading branch information
Konrad Jamrozik
committed
Jul 8, 2024
1 parent
a7bcaac
commit 11c872d
Showing
16 changed files
with
135 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using UfoGameLib.Lib; | ||
using UfoGameLib.Players; | ||
using UfoGameLib.State; | ||
|
||
namespace UfoGameLib.Controller; | ||
|
||
public interface IAIPlayer | ||
{ | ||
public void PlayGameTurn(GameStatePlayerView state, GameTurnController controller); | ||
|
||
public static IAIPlayer New(ILog log, AIPlayerName name) | ||
{ | ||
var playerMap = new Dictionary<AIPlayerName, IAIPlayer> | ||
{ | ||
[AIPlayerName.Basic] = new BasicAIPlayer(log), | ||
[AIPlayerName.DoNothing] = new DoNothingAIPlayer(), | ||
}; | ||
return playerMap[name]; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Collections.Immutable; | ||
using System.Reflection; | ||
|
||
namespace UfoGameLib.Lib; | ||
|
||
public static class Reflection | ||
{ | ||
public static ImmutableList<string> GetDerivedTypeNames<T>() | ||
{ | ||
Assembly assembly = Assembly.GetAssembly(typeof(T))!; | ||
|
||
var derivedTypes = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(T))); | ||
|
||
return derivedTypes.Select(t => t.Name).ToImmutableList(); | ||
} | ||
|
||
public static ImmutableList<string> GetInterfaceImplementationNames<T>() | ||
{ | ||
Assembly assembly = Assembly.GetAssembly(typeof(T))!; | ||
|
||
var derivedTypes = assembly.GetTypes().Where(t => typeof(T).IsAssignableFrom(t)); | ||
|
||
return derivedTypes.Select(t => t.Name).ToImmutableList(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Collections.Immutable; | ||
using System.Text.Json.Serialization; | ||
using Lib.Contracts; | ||
using Lib.Json; | ||
using UfoGameLib.Controller; | ||
using UfoGameLib.Lib; | ||
|
||
namespace UfoGameLib.Players; | ||
|
||
[JsonConverter(typeof(StringJsonConverter<AIPlayerName>))] | ||
public class AIPlayerName : IEquatable<AIPlayerName> | ||
{ | ||
private static readonly ImmutableList<string> ValidNames = GetValidNames(); | ||
|
||
public static readonly AIPlayerName Basic = new AIPlayerName(nameof(BasicAIPlayer)); | ||
public static readonly AIPlayerName DoNothing = new AIPlayerName(nameof(DoNothingAIPlayer)); | ||
|
||
private static bool IsValid(string name) => ValidNames.Contains(name); | ||
|
||
private readonly string _name; | ||
|
||
private static ImmutableList<string> GetValidNames() | ||
=> Reflection.GetInterfaceImplementationNames<IAIPlayer>(); | ||
|
||
public AIPlayerName(string name) | ||
{ | ||
Contract.Assert( | ||
IsValid(name), | ||
$"The type name '{name}' is not a valid name of AIPlayer-derived class."); | ||
_name = name; | ||
} | ||
|
||
|
||
public override string ToString() | ||
{ | ||
return $"{_name}"; | ||
} | ||
|
||
public bool Equals(AIPlayerName? other) | ||
{ | ||
if (ReferenceEquals(null, other)) | ||
return false; | ||
if (ReferenceEquals(this, other)) | ||
return true; | ||
return _name == other._name; | ||
} | ||
|
||
public override bool Equals(object? obj) | ||
{ | ||
if (ReferenceEquals(null, obj)) | ||
return false; | ||
if (ReferenceEquals(this, obj)) | ||
return true; | ||
return obj.GetType() == GetType() && Equals((AIPlayerName)obj); | ||
} | ||
|
||
public override int GetHashCode() | ||
=> _name.GetHashCode(); | ||
|
||
public static bool operator ==(AIPlayerName? left, AIPlayerName? right) | ||
=> Equals(left, right); | ||
|
||
public static bool operator !=(AIPlayerName? left, AIPlayerName? right) | ||
=> !Equals(left, right); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// codesync: UfoGameLib.Players | ||
|
||
export type AIPlayerName = 'DoNothing' | 'Basic' | ||
export type AIPlayerName = 'BasicAIPlayer' | 'DoNothingAIPlayer' |