Skip to content
This repository has been archived by the owner on Jul 27, 2018. It is now read-only.

Commit

Permalink
update fakeplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyl18 committed Feb 17, 2018
1 parent 0194d88 commit 8c4c138
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
34 changes: 15 additions & 19 deletions CardSharp.GUI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,26 @@ class Program
{
static void Main(string[] args)
{
var count = 0;
//Parallel.For(0, 2000, (i) => { RunTest(ref count); });
for (int i = 0; i < 25000; i++)
{
RunTest(ref count);
}

RunTest();


}
private static readonly Random Rng = new Random("fork you kamijoutoma".GetHashCode());
private static void RunTest(ref int count)
private static void RunTest()
{
var sw = Stopwatch.StartNew();
var desk = Desk.GetOrCreateDesk(Rng.NextDouble().ToString(CultureInfo.InvariantCulture));
desk.AddPlayer(new FakePlayer());
desk.AddPlayer(new FakePlayer());
desk.AddPlayer(new FakePlayer());
desk.AddPlayer(new Player("1"));
desk.AddPlayer(new Player("2"));
desk.AddPlayer(new Player("3"));

//Task.Run(() => { ShowMessage(desk); });

//ParseMessage(desk);
desk.Start();

Console.WriteLine($"Test successful: {count} / 20000, used {sw.ElapsedMilliseconds}ms");
//Console.WriteLine(desk.Message);

Interlocked.Increment(ref count);
Task.Run(() => { ShowMessage(desk); });

ParseMessage(desk);


}

private static void ParseMessage(Desk desk)
Expand All @@ -47,6 +42,7 @@ private static void ParseMessage(Desk desk)
{
var line = Console.ReadLine();
desk.ParseCommand(desk.CurrentPlayer.PlayerId, line);
Thread.Sleep(10);
}
}

Expand All @@ -60,7 +56,7 @@ private static void ShowMessage(Desk desk)
foreach (var player in desk.Players)
ShowMessage(player, $"[{player.PlayerId}]: ");

//Thread.Sleep(10);
Thread.Sleep(10);
}
}

Expand Down
12 changes: 9 additions & 3 deletions CardSharp/GameComponents/FakePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ namespace CardSharp.GameComponents
{
public class FakePlayer : Player
{
public FakePlayer() : base(GenerateName())
public FakePlayer(Desk desk) : base(GenerateName(desk))
{
HostedEnabled = true;
}

private static readonly Random Rng = new Random("fork you kamijoutoma".GetHashCode());
private static string GenerateName()

private static string GenerateName(Desk desk)
{
return $"机器人{Rng.Next(100)}";
while (true)
{
var name = $"机器人{Rng.Next(100)}";
if (desk.Players.Any(p => p.PlayerId == name)) continue;
return name;
}
}

public override string ToAtCode()
Expand Down
19 changes: 10 additions & 9 deletions CardSharp/GameSteps/CommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public void Parse(Desk desk, Player player, string command)
else
{
AnalyzeGiveUpAndMoveNext(desk);
player = desk.CurrentPlayer;
desk.BoardcastCards();
}

Expand All @@ -119,12 +120,7 @@ public void Parse(Desk desk, Player player, string command)
if (player.PublicCards) desk.AddMessageLine($"明牌:{player.Cards.ToFormatString()}");

AnalyzeGiveUpAndMoveNext(desk);

if (desk.LastSuccessfulSender == desk.CurrentPlayer)
{
desk.CurrentRule = null;
desk.LastCards = null;
}
player = desk.CurrentPlayer;

desk.BoardcastCards();
}
Expand All @@ -139,10 +135,15 @@ public void Parse(Desk desk, Player player, string command)
return;
}

if (desk.LastSuccessfulSender == desk.CurrentPlayer) {
desk.CurrentRule = null;
desk.LastCards = null;
}

if (RunHostedCheck(desk))
return;

RunAutoPassCheck(desk);
if (desk.CurrentRule != null)
RunAutoPassCheck(desk);
}

private void RunAutoPassCheck(Desk desk)
Expand All @@ -152,7 +153,7 @@ private void RunAutoPassCheck(Desk desk)

if (!exists)
{
desk.AddMessageLine($"{cp.ToAtCode()} 没有检测到你想要出的牌, 已为你自动pass.");
desk.AddMessageLine($"{cp.ToAtCode()} 没有检测到你能出的牌, 已为你自动pass.");
Parse(desk, cp, "pass");
}
}
Expand Down
4 changes: 2 additions & 2 deletions CardSharp/GameSteps/LandlordDiscuss.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ public void Parse(Desk desk, Player player, string command)
case "抢这个鸡毛掸子": // 应irol的要求. 开心就好啦.
player.Cards.AddRange(_landlordCards);
player.Cards.Sort();
desk.SetLandlord(player);
desk.SendCardsMessage();
desk.AddMessage(
$"{player.ToAtCode()}抢地主成功. 为{string.Join("", _landlordCards.Select(card => $"[{card}]"))}");
desk.SetLandlord(player);
desk.SendCardsMessage();
break;
case "不抢":
case "抢你妈":
Expand Down
2 changes: 1 addition & 1 deletion CardSharp/GameSteps/WaitingParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Parse(Desk desk, Player player, string command)
desk.AddMessage("人数不够.");
break;
case "添加机器人":
desk.AddPlayer(new FakePlayer());
desk.AddPlayer(new FakePlayer(desk));
break;
case "移除机器人":
if (desk.Players.Any(p => p is FakePlayer))
Expand Down

0 comments on commit 8c4c138

Please sign in to comment.