Skip to content

Commit

Permalink
fix first move attack bug still in last release; shown opp bomb struc…
Browse files Browse the repository at this point in the history
…ture bug; always show tray
  • Loading branch information
Braath Waate committed Mar 5, 2020
1 parent 64499ab commit fd95e0c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 78 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ and mainly offers a much improved AI and some other features.

# Download

[Stratego player v0.13.3][dl]
[dl]: https://github.com/braathwaate/stratego/releases/download/v0.13.3/stratego_v0.13.3.jar
[Stratego player v0.13.4][dl]
[dl]: https://github.com/braathwaate/stratego/releases/download/v0.13.4/stratego_v0.13.4.jar

For two person play over TCP/IP, you need the Stratego server,
which you need to make from source.
Expand All @@ -26,7 +26,7 @@ or set up your computer to select which program opens the jar file.

Finally, you can try to run it from the command line:

java -jar stratego_v0.13.3.jar
java -jar stratego_v0.13.4.jar

For a more challenging game, click the Settings menu and set the difficulty level to the middle (about 1 second).
For an easier game, keep the difficulty all the way to the left
Expand Down
54 changes: 21 additions & 33 deletions src/com/cjmalloy/stratego/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class Board
protected int[] piecesInTray = new int[2];
protected int[] remainingUnmovedUnknownPieces = new int[2];
protected int[] nUnknownWeakRankAtLarge = new int[2];
protected Piece[] flag = new Piece[2]; // flags
protected int[] flag = new int[2]; // flags
protected static final int expendableRank[] = { 6, 7, 9 };
protected static final int BLUFFER_RANK_MIN = 2;
protected static final int BLUFFER_RANK_MAX = 5;
Expand Down Expand Up @@ -222,7 +222,7 @@ public boolean add(Piece p, Spot s)
if (p.getColor() == Settings.bottomColor)
p.setRank(Rank.UNKNOWN);
else if (p.getRank() == Rank.FLAG)
flag[Settings.topColor] = p;
flag[Settings.topColor] = Grid.getIndex(s.getX(), s.getY());
setPiece(p, s);
tray.remove(p);
setup[Grid.getIndex(s.getX(), s.getY())] = p;
Expand Down Expand Up @@ -283,10 +283,8 @@ else if (result == -1) {
else
setPiece(tp, m.getFrom());
}
if (tp.getRank() != Rank.FLAG) {
genChaseRank(fp.getColor());
genFleeRank(fp, tp); // depends on suspected rank
}
genChaseRank(fp.getColor());
genFleeRank(fp, tp); // depends on suspected rank
lock.unlock();
return true;
}
Expand Down Expand Up @@ -409,16 +407,7 @@ public void hideAll()
for (int i=0;i<10;i++)
for (int j=0;j<10;j++)
if (grid.getPiece(i, j) != null)

grid.getPiece(i,j).setShown(false);

hideTray();
}

public void hideTray()
{
for (Piece p: tray)
p.setShown(false);
}

public void clearPiece(int i)
Expand Down Expand Up @@ -2426,7 +2415,7 @@ protected void genSuspectedRank()
}

} // c
flag[Settings.bottomColor] = null;
flag[Settings.bottomColor] = 0;

// subtract the tray pieces from allRank[]
for (int i=0;i<getTraySize();i++) {
Expand Down Expand Up @@ -2566,8 +2555,8 @@ else if (p.hasMoved() || p.isKnown())
continue;

// The remaining unmoved pieces must be bombs (or the flag)
// Make the piece a bomb for now and immobile.
// possibleFlag() will morph one of them into a flag.
// Make the piece a bomb for now and immobile.
// possibleFlag() will morph one of them into a flag.

Rank rank = p.getRank();

Expand All @@ -2579,7 +2568,7 @@ else if (unknownBombs[c] != 0) {
} else {
p.setRank(Rank.FLAG);
p.makeKnown();
flag[c] = p;
flag[c] = p.getIndex();
grid.clearMovable(p);
}

Expand Down Expand Up @@ -2959,7 +2948,7 @@ protected void possibleFlag()
// If the flag is already known (perhaps it is the last piece
// on the board), skip this code.

Piece flagp = flag[c];
Piece flagp = getPiece(flag[c]);
if (flagp != null
&& flagp.isKnown()
&& !flagp.isSuspectedRank())
Expand Down Expand Up @@ -3011,9 +3000,9 @@ protected void possibleFlag()

int bestGuess = getBestGuess(c, maybe, maybe_count[c]);
if (c == Settings.bottomColor) {
flag[c] = getPiece(maybe[bestGuess][0]);
flag[c].setSuspectedRank(Rank.FLAG);
grid.clearMovable(flag[c]);
flag[c] = maybe[bestGuess][0];
getPiece(flag[c]).setSuspectedRank(Rank.FLAG);
grid.clearMovable(getPiece(flag[c]));
}

// Mark surrounding pieces in possible flag
Expand Down Expand Up @@ -3217,7 +3206,7 @@ else if (bp.hasMoved())
// assert flagp != null : "Well, where IS the flag?";

if (flagp != null) {
flag[c] = flagp;
flag[c] = flagp.getIndex();
flagp.setSuspectedRank(Rank.FLAG);
grid.clearMovable(flagp);
for (int d : dir) {
Expand Down Expand Up @@ -3247,7 +3236,7 @@ int getBestGuess(int color, int[][] maybe, int maybe_count)
// ensure isBombedFlag is set correctly for AI

if (color == Settings.topColor
&& maybe[i][0] == flag[Settings.topColor].getIndex())
&& maybe[i][0] == flag[Settings.topColor])
return i;

int prob = 1;
Expand Down Expand Up @@ -3414,15 +3403,15 @@ private void markBombedFlag(int[][] maybe, int intactStructures, int maybe_flag)

// If there is only 1 pattern left, the AI goes out
// on a limb and decides that the pieces in the
// bomb pattern are known bombs. This means
// bomb pattern are flag bombs. This means
// they offer no protective value to pieces that
// bluff against lower ranked pieces.

Piece b = getFlagBomb(p);

if (intactStructures <= 1) {
if (suspectedBomb(b))
b.setKnown(true);
b.set(Piece.FLAG_BOMB);

// If the piece already has a suspected rank, keep it,
// otherwise make it a suspected bomb
Expand Down Expand Up @@ -3456,7 +3445,7 @@ private void markBombedFlag(int[][] maybe, int intactStructures, int maybe_flag)
// of the last potential bomb structure, clear isBombedFlag.

if (color == Settings.topColor
&& flagi != flag[color].getIndex())
&& flagi != flag[color])
isBombedFlag[color] = false;
}
// ********* end of suspected ranks
Expand Down Expand Up @@ -3513,6 +3502,7 @@ public boolean remove(int i)

public void remove(Piece p)
{
p.setShown(true);
tray.add(p);
Collections.sort(tray,new Comparator<Piece>(){
public int compare(Piece p1,Piece p2){
Expand Down Expand Up @@ -4342,8 +4332,8 @@ public boolean isInvincibleDefender(Piece p)

public boolean isNearOpponentFlag(int to)
{
return flag[Settings.bottomColor] != null &&
Grid.steps(to, flag[Settings.bottomColor].getIndex()) <= 3;
return flag[Settings.bottomColor] != 0 &&
Grid.steps(to, flag[Settings.bottomColor]) <= 3;
}

public boolean isNearOpponentFlag(Piece p)
Expand Down Expand Up @@ -4397,8 +4387,6 @@ protected void revealRank(Piece p)
// Regenerate suspected rank because if blufferRisk = 5
// no suspected suspected rank was assigned to piece
Rank suspRank = getSuspectedRank(p);
if (suspRank == null)
return;

if (suspRank == Rank.SPY)
guess(p.getActualRank() == Rank.SPY);
Expand All @@ -4416,7 +4404,7 @@ protected void revealRank(Piece p)

// On the other hand, a Three is bluffing as a One by chasing a Two.

else if (suspRank.ordinal() <= 4)
else if (suspRank != null && suspRank.ordinal() <= 4)
guess(p.getActualRank().ordinal() <= suspRank.ordinal() + 1
|| p.getActualRank() == Rank.SPY
&& p.getActingRankChaseLow() == Rank.NIL);
Expand Down
44 changes: 22 additions & 22 deletions src/com/cjmalloy/stratego/player/AI.java
Original file line number Diff line number Diff line change
Expand Up @@ -715,10 +715,10 @@ public void getBombFlagMoves(ArrayList<Integer> moveList, int i)
Rank fprank = fp.getRank();

assert (fprank == Rank.BOMB || fprank == Rank.FLAG);
assert (b.grid.hasAttack(b.bturn, i));
assert (b.grid.hasAttack(b.bturn, i));

if (fp.isKnown())
return;
if (fp.isKnown() || fp.is(Piece.FLAG_BOMB))
return;

// We need to generate moves for suspected
// bombs because the bomb might actually be
Expand All @@ -736,31 +736,31 @@ public void getBombFlagMoves(ArrayList<Integer> moveList, int i)
// In addition, return false, to prevent the option
// of a null move.
//
// Note that AI bombs are treated identically,
// thus limiting their scope of travel to immediate
// attack.

for (int d : dir ) {
int t = i + d ;
Piece tp = b.getPiece(t);

if (tp != null
&& tp.getColor() == 1 - b.bturn
&& b.isEffectiveBombBluff(fp, tp)) {
addMove(moveList, i, t);
}
} // d
}
// Note that AI bombs are treated identically,
// thus limiting their scope of travel to immediate
// attack.

for (int d : dir ) {
int t = i + d ;
Piece tp = b.getPiece(t);

if (tp != null
&& tp.getColor() == 1 - b.bturn
&& b.isEffectiveBombBluff(fp, tp)) {
addMove(moveList, i, t);
}
} // d
}

private void getBombFlagMoves(ArrayList<Integer> moveList)
{
if (b.grid.pieceCount(b.bturn) - (b.rankAtLarge(b.bturn, Rank.BOMB) + 1) == b.grid.movablePieceCount(b.bturn))
return;
{
if (b.grid.pieceCount(b.bturn) - (b.rankAtLarge(b.bturn, Rank.BOMB) + 1) == b.grid.movablePieceCount(b.bturn))
return;

BitGrid bg = new BitGrid();

// Find the neighboring bombs and flags
// of opponent pieces (1 - b.turn)
// of opponent pieces (1 - b.turn)
// (In other words, find the player's bombs and flags
// that are under direct attack).

Expand Down
36 changes: 16 additions & 20 deletions src/com/cjmalloy/stratego/player/TestingBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ else if (rank == Rank.NINE

setUnmovedValues();

assert flag[Settings.topColor] != null : "AI flag unknown";
assert flag[Settings.topColor] != 0 : "AI flag unknown";
}

// These high priorities cause piece loss but no more than the lowest piece value (NINE).
Expand Down Expand Up @@ -1918,7 +1918,7 @@ protected void attackBomb(Piece bomb, boolean sendMiner)
// TBD: Need to do the same analysis for an attack on an opponent flag
// as is done for defending the flag to determine the priority.

int priority = (flag[bombColor].isKnown() ? DEST_PRIORITY_CHASE_ATTACK : DEST_PRIORITY_ATTACK_FLAG);
int priority = (getPiece(flag[bombColor]).isKnown() ? DEST_PRIORITY_CHASE_ATTACK : DEST_PRIORITY_ATTACK_FLAG);
genNeeded(destTmp2, 1-bombColor, 8, priority);
genPlan(PLANB, destTmp2, 1-bombColor, 8, priority, false);
}
Expand Down Expand Up @@ -2535,7 +2535,7 @@ private void defendFlag(Piece flagp, boolean sendMiner)
int color = flagp.getColor();

if (color == Settings.topColor
&& flagi != flag[color].getIndex())
&& flagi != flag[color])
return;

// Even when the flag is not completely bombed,
Expand Down Expand Up @@ -2985,8 +2985,8 @@ void protectFlagBomb(TestPiece flagBomb)

private void aiFlagSafety()
{
Piece pflag = flag[Settings.topColor];
int flagi = pflag.getIndex();
int flagi = flag[Settings.topColor];
Piece pflag = getPiece(flagi);
int color = pflag.getColor();

assert pflag.getRank() == Rank.FLAG : "aiFlag is " + pflag.getRank() + " at " + flagi +"?";
Expand Down Expand Up @@ -3045,7 +3045,7 @@ private void aiFlagSafety()
// flag is indeed known but if the distance is less than
// the maximum search ply, it prevents the horizon effect.

defendFlag(flag[Settings.topColor], true);
defendFlag(pflag, true);
}

// Always protect any remaining bombs in the
Expand All @@ -3064,10 +3064,10 @@ void genDestFlag()
{
for (int c = RED; c <= BLUE; c++) {

Piece pflag = flag[c];
if (pflag == null)
int flagi = flag[c];
if (flagi == 0)
continue;
int flagi = pflag.getIndex();
Piece pflag = getPiece(flagi);

int opponentEightsAtLarge = rankAtLarge(1-c, Rank.EIGHT);
int intactStructures = maybe_count[c] - open_count[c];
Expand Down Expand Up @@ -3156,7 +3156,7 @@ void genDestFlag()
if (c == Settings.bottomColor) {
if (sendMiner && isBombedFlag[c])
for (int d : dir) {
int j = flag[c].getIndex() + d;
int j = flag[c] + d;
if (!Grid.isValid(j))
continue;
TestPiece p = (TestPiece)getFlagBomb(getPiece(j));
Expand Down Expand Up @@ -4326,7 +4326,7 @@ && isNeededPiece(fp)
// direction helps prevent pieces from wandering
// aimlessly around the board.

assert flag[1-fpcolor] != null;
assert flag[1-fpcolor] != 0;
if (fp.getTestMoves() == 0) {
int fy = Grid.yside(fpcolor, Grid.getY(from));
int ty = Grid.yside(fpcolor, Grid.getY(to));
Expand Down Expand Up @@ -7359,13 +7359,7 @@ int apparentRisk(Piece fp, Rank fprank, boolean unknownScoutFarMove, Piece tp)
// pieces from attacking an unknown unmoved piece.

// This risk often increases with rank (i.e. a Five is more
// likely than a One to lotto), but fleeing based on rank has its own
// dangers. So if an unmoved One is approached by a Five
// and flees, the opponent now knows that the piece is
// worth attacking to reveal its rank.
// However, if the approaching piece is expendable,
// the AI assumes that the piece will attack and fleeing is
// the better of two evils.
// likely than a One to lotto).

if (isPossibleBomb(tp)
&& fprank != Rank.EIGHT
Expand All @@ -7385,7 +7379,7 @@ int apparentRisk(Piece fp, Rank fprank, boolean unknownScoutFarMove, Piece tp)
if (!fp.isKnown())
risk += blufferRisk * 2;
if (fprank == Rank.FIVE)
risk += 15;
risk += 25;
return risk; // 10% chance of attack
}

Expand Down Expand Up @@ -8950,7 +8944,9 @@ public boolean makeAggressive(Piece fp, Piece tp)
public boolean isFlagBombAtRiskFromAttacker(Piece p)
{
return p.is(Piece.MAYBE_EIGHT)
&& flag[1-p.getColor()].isKnown();
&& flag[1-p.getColor()] != 0
&& getPiece(flag[1-p.getColor()]) != null
&& getPiece(flag[1-p.getColor()]).isKnown();
}

// If the opponent has an unsuspected Spy, it is dangerous for
Expand Down

0 comments on commit fd95e0c

Please sign in to comment.