Skip to content

Commit ed65c21

Browse files
committed
Feature: Improve asteroid game scoring system with enhanced reward logic and new accuracy metrics.
1 parent a76d798 commit ed65c21

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

G33kShell.Desktop/Console/Screensavers/AI/AiGameCanvasBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private void TrainAiImpl(Action<byte[]> saveBrainBytes)
102102
var stats = $"Gen {m_generation}|Pop {m_currentPopSize}|Rating {veryBest.Rating:F1}|GOAT {m_savedRating:F1}";
103103
var extraStats = veryBest.ExtraGameStats().Select(o => $" {o.Name}: {o.Value}").ToArray().ToCsv().Trim();
104104
if (!string.IsNullOrEmpty(extraStats))
105-
stats += $" | {extraStats}";
105+
stats += $"|{extraStats}";
106106
System.Console.WriteLine(stats);
107107

108108
// Persist brain improvements.

G33kShell.Desktop/Console/Screensavers/Asteroids/Game.cs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace G33kShell.Desktop.Console.Screensavers.Asteroids;
2121
public class Game : AiGameBase
2222
{
2323
private int m_bulletsFired;
24+
private int m_asteroidsHit;
2425
private int m_perfectHits;
2526
private int m_gameTicks;
2627
private int m_leftTurns;
@@ -39,43 +40,35 @@ public override double Rating
3940
get
4041
{
4142
if (Score == 0)
42-
return -50.0; // Penalize pacifists.
43-
43+
return 0.0; // Penalize pacifists.
4444
if (m_gameTicks > 2000 && m_bulletsFired < 5)
45-
return -100.0; // Penalize idle campers.
46-
47-
var accuracyScore = HitAccuracy * 25.0; // Strongly reward aiming carefully.
48-
var perfectHitBonus = m_perfectHits * 1.0; // Bigger incentive for intentional targeting.
49-
var shotDiscipline = -(m_bulletsFired - m_perfectHits) * 0.02; // Stronger spam punishment.
50-
var rawScore = Score / 180.0; // Increase actual game score importance.
45+
return 0.0; // Penalize idle campers.
46+
if (TurnEquality < 0.3)
47+
return 0.0; // Penalize wonky-turners.
48+
if (m_thrustTicks == 0)
49+
return 0.0; // Penalize non-thrusters.
5150

52-
// Reward repositioning, but penalize constant thrust with no balance.
5351
var thrustRatio = m_thrustTicks / (double)m_gameTicks;
54-
var repositioningBonus = thrustRatio > 0.2 && thrustRatio < 0.8 ? 5.0 : -2.0;
55-
56-
// Strongly penalize sustained unidirectional spinning:
57-
var turnReward = TurnEquality > 0.5 ? TurnEquality * 5.0 : -5.0;
58-
59-
// Introduce a simple collision-avoidance reward:
60-
var survivalBonus = m_gameTicks / 4500.0; // Survive longer by avoiding collisions.
61-
62-
return rawScore + accuracyScore + perfectHitBonus +
63-
shotDiscipline + repositioningBonus +
64-
turnReward + survivalBonus;
52+
var survivalBonus = (m_gameTicks / 8000.0).Clamp(0.0, 1.0);
53+
54+
return Score / 8000.0 * 10.0 +
55+
HitRatio * 0.5 * 0.1 +
56+
PerfectHitRatio * 1.0 * 0.1 +
57+
thrustRatio * 2.0 * 0.1 +
58+
TurnEquality * 0.5 * 0.1 +
59+
survivalBonus * 1.0 * 0.1
60+
;
6561
}
6662
}
6763

6864
public override IEnumerable<(string Name, string Value)> ExtraGameStats()
6965
{
7066
yield return ("Score", Score.ToString());
71-
yield return ("HitAccuracy", HitAccuracy.ToString("P1"));
72-
yield return ("GameTicks", m_gameTicks.ToString());
67+
yield return ("Accuracy", HitRatio.ToString("P1"));
68+
yield return ("Perfect", PerfectHitRatio.ToString("P1"));
69+
yield return ("Ticks", m_gameTicks.ToString());
7370
yield return ("TurnEquality", TurnEquality.ToString("P1"));
74-
yield return ("BulletsFired", m_bulletsFired.ToString());
75-
yield return ("PerfectHits", m_perfectHits.ToString());
76-
yield return ("LeftTurns", m_leftTurns.ToString());
77-
yield return ("RightTurns", m_rightTurns.ToString());
78-
yield return ("ThrustTicks", m_thrustTicks.ToString());
71+
yield return ("Thrusts", m_thrustTicks.ToString());
7972
}
8073

8174
private double TurnEquality
@@ -94,7 +87,8 @@ private double TurnEquality
9487
public List<Asteroid> Asteroids { get; } = [];
9588
public List<Bullet> Bullets { get; } = [];
9689

97-
private double HitAccuracy => m_bulletsFired == 0 ? 0.0 : (double)m_perfectHits / m_bulletsFired;
90+
private double PerfectHitRatio => m_bulletsFired == 0 ? 0.0 : (double)m_perfectHits / m_bulletsFired;
91+
private double HitRatio => m_bulletsFired == 0 ? 0.0 : (double)m_asteroidsHit / m_bulletsFired;
9892

9993
public Game(int arenaWidth, int arenaHeight) : base(arenaWidth, arenaHeight, new Brain())
10094
{
@@ -110,6 +104,7 @@ public override Game ResetGame()
110104
Bullets.Clear();
111105
m_bulletsFired = 0;
112106
m_perfectHits = 0;
107+
m_asteroidsHit = 0;
113108
m_gameTicks = 0;
114109
m_leftTurns = 0;
115110
m_rightTurns = 0;
@@ -199,6 +194,7 @@ public override void Tick()
199194
// Bonus points if it was the asteroid we were aiming for.
200195
if (bullet.Target == hitAsteroid)
201196
m_perfectHits++;
197+
m_asteroidsHit++;
202198
}
203199

204200
for (var i = 0; i < bulletsToRemove.Count; i++)

0 commit comments

Comments
 (0)