Skip to content

Commit

Permalink
Implement push action
Browse files Browse the repository at this point in the history
  • Loading branch information
jnccd committed Feb 25, 2024
1 parent 640d409 commit 51ef83c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
9 changes: 4 additions & 5 deletions socha-backend/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ public override bool IsLegalOn(State s, Ship curShip = null, Ship otherShip = nu
pos += CubeCoords.DirToOffset(curShip.Dir);

if (s.Board.GetField(pos) == null ||
s.Board.GetField(pos).FType != FieldType.water ||
otherShip.Pos == pos)
s.Board.GetField(pos).FType != FieldType.water)
return false;
}

Expand Down Expand Up @@ -221,8 +220,8 @@ public override bool IsLegalOn(State s, Ship curShip = null, Ship otherShip = nu
curShip ??= s.CurrentPlayer.Ship;
otherShip ??= s.GetOtherPlayer(s.CurrentPlayer).Ship;

// TODO: idk
return true;
return curShip.Pos == otherShip.Pos &&
s.Board.GetField(otherShip.Pos + CubeCoords.DirToOffset(direction))?.FType == FieldType.water;
}

public override void PerformOn(State s, Ship curShip = null, Ship otherShip = null, bool modifyState = false)
Expand All @@ -231,7 +230,7 @@ public override void PerformOn(State s, Ship curShip = null, Ship otherShip = nu
otherShip ??= s.GetOtherPlayer(s.CurrentPlayer).Ship;

otherShip.Pos += CubeCoords.DirToOffset(direction);
// TODO: probably something else too
curShip.MovementPoints -= 1;
}

public override string ToXML() => $"<push direction=\"{direction}\" />\n";
Expand Down
69 changes: 49 additions & 20 deletions socha-backend/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ private List<Move> GeneratePossibleAccelerations(Ship curShip = null, Ship other
private List<Move> GeneratePossibleMoveEndings(List<Action> pastActions, State curState, Ship curShip, Ship otherShip)
{
// Check for illegal state early
if (curShip.Coal < 0 || curShip.MovementPoints < 0 || Board.GetField(curShip.Pos) == null)
if (curShip.Coal < 0 ||
curShip.MovementPoints < 0 ||
Board.GetField(curShip.Pos) == null ||
(curShip.Pos == otherShip.Pos && curShip.MovementPoints <= 0))
return new List<Move>();

// Return early if done
Expand All @@ -117,44 +120,70 @@ private List<Move> GeneratePossibleMoveEndings(List<Action> pastActions, State c

List<Move> re = new();

if (curShip.MovementPoints > 0 && (!pastActions.Any() || pastActions.Last() is not Advance))
if (otherShip.Pos == curShip.Pos)
{
// Add advance action
for (int i = 1; i < curShip.MovementPoints + 1; i++)
// Add push action
for (int i = 0; i < Enum.GetNames(typeof(Direction)).Length; i++)
{
var newAction = new Advance(i);
if ((Direction)i == curShip.Dir.Opposite())
continue;

var newAction = new Push((Direction)i);

if (newAction.IsLegalOn(this, curShip, otherShip))
{
var newCurShip = (Ship)curShip.Clone();
var newOtherShip = (Ship)otherShip.Clone();
var newActions = pastActions.Clone();
newActions.Add(newAction);
newAction.PerformOn(curState, newCurShip, newOtherShip, false);
newAction.PerformOn(this, newCurShip, newOtherShip);

// You're on a path in the woods, and at the end of that path is a cabin...
re.AddRange(GeneratePossibleMoveEndings(newActions, curState, newCurShip, newOtherShip));
}
}
}

if ((curShip.Coal > 0 || curShip.FreeTurns > 0) && (!pastActions.Any() || pastActions.Last() is not Backend.Turn))
else
{
// Add turn action
for (int i = 0; i < Enum.GetNames(typeof(Direction)).Length; i++)
// Add advance action
if (curShip.MovementPoints > 0 && (!pastActions.Any() || pastActions.Last() is not Advance))
{
var newAction = new Backend.Turn((Direction)i);

if (newAction.IsLegalOn(this, curShip, otherShip))
for (int i = 1; i < curShip.MovementPoints + 1; i++)
{
var newCurShip = (Ship)curShip.Clone();
var newOtherShip = (Ship)otherShip.Clone();
var newActions = pastActions.Clone();
newActions.Add(newAction);
newAction.PerformOn(this, newCurShip, newOtherShip);
var newAction = new Advance(i);

if (newAction.IsLegalOn(this, curShip, otherShip))
{
var newCurShip = (Ship)curShip.Clone();
var newOtherShip = (Ship)otherShip.Clone();
var newActions = pastActions.Clone();
newActions.Add(newAction);
newAction.PerformOn(curState, newCurShip, newOtherShip, false);

// You're on a path in the woods, and at the end of that path is a cabin...
re.AddRange(GeneratePossibleMoveEndings(newActions, curState, newCurShip, newOtherShip));
}
}
}

// You're on a path in the woods, and at the end of that path is a cabin...
re.AddRange(GeneratePossibleMoveEndings(newActions, curState, newCurShip, newOtherShip));
// Add turn action
if ((curShip.Coal > 0 || curShip.FreeTurns > 0) && (!pastActions.Any() || pastActions.Last() is not Backend.Turn))
{
for (int i = 0; i < Enum.GetNames(typeof(Direction)).Length; i++)
{
var newAction = new Backend.Turn((Direction)i);

if (newAction.IsLegalOn(this, curShip, otherShip))
{
var newCurShip = (Ship)curShip.Clone();
var newOtherShip = (Ship)otherShip.Clone();
var newActions = pastActions.Clone();
newActions.Add(newAction);
newAction.PerformOn(this, newCurShip, newOtherShip);

// You're on a path in the woods, and at the end of that path is a cabin...
re.AddRange(GeneratePossibleMoveEndings(newActions, curState, newCurShip, newOtherShip));
}
}
}
}
Expand Down

0 comments on commit 51ef83c

Please sign in to comment.