[go: nahoru, domu]

Skip to content

Commit

Permalink
Fixed a bug with vs-computer mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen-ameli committed Jul 16, 2023
1 parent 8698316 commit b155bb4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 41 deletions.
65 changes: 33 additions & 32 deletions backend/game/game_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,44 @@ def convert_to_simple_format(board: List[Dict[str, object]]) -> str:
# Convert the point counts to a string in the "simple" format
return " ".join(str(count) for count in checkers)

# Sample board data
'''
{
"user_checker": "white",
"dice1": 1,
"dice2": 1,
"board": [
{ "id": 0, "color": "white", "col": 21, "row": 0, "removed": false },
{ "id": 1, "color": "white", "col": -3, "row": 0, "removed": false },
{ "id": 2, "color": "white", "col": 20, "row": 0, "removed": false },
{ "id": 3, "color": "white", "col": 19, "row": 0, "removed": false },
{ "id": 4, "color": "white", "col": 19, "row": 1, "removed": false },
{ "id": 5, "color": "white", "col": 19, "row": 2, "removed": false },
{ "id": 6, "color": "white", "col": 20, "row": 1, "removed": false },
{ "id": 7, "color": "white", "col": 20, "row": 2, "removed": false },
{ "id": 8, "color": "white", "col": 21, "row": 1, "removed": false },
{ "id": 9, "color": "white", "col": 21, "row": 2, "removed": false },
{ "id": 10, "color": "white", "col": 22, "row": 0, "removed": false },
{ "id": 11, "color": "white", "col": 22, "row": 1, "removed": false },
{ "id": 12, "color": "white", "col": 23, "row": 0, "removed": false },
{ "id": 13, "color": "white", "col": 23, "row": 1, "removed": false },
{ "id": 14, "color": "white", "col": 23, "row": 2, "removed": false },
{ "id": 15, "color": "black", "col": -2, "row": 0, "removed": true },
{ "id": 16, "color": "black", "col": 3, "row": 0, "removed": false },
{ "id": 17, "color": "black", "col": 4, "row": 0, "removed": false },
{ "id": 18, "color": "black", "col": 5, "row": 0, "removed": false },
{ "id": 19, "color": "black", "col": 7, "row": 0, "removed": false },
{ "id": 20, "color": "black", "col": 7, "row": 1, "removed": false },
{ "id": 21, "color": "black", "col": 7, "row": 2, "removed": false },
{ "id": 22, "color": "black", "col": 7, "row": 3, "removed": false },
{ "id": 23, "color": "black", "col": 7, "row": 4, "removed": false },
{ "id": 24, "color": "black", "col": 7, "row": 5, "removed": false },
{ "id": 25, "color": "black", "col": 7, "row": 6, "removed": false },
{ "id": 26, "color": "black", "col": 7, "row": 7, "removed": false },
{ "id": 27, "color": "black", "col": 7, "row": 8, "removed": false },
{ "id": 28, "color": "black", "col": 6, "row": 0, "removed": false },
{ "id": 29, "color": "black", "col": 1, "row": 0, "removed": false }
]
{ "id": 0, "color": "white", "col": 21, "row": 0, "removed": false },
{ "id": 1, "color": "white", "col": -3, "row": 0, "removed": false },
{ "id": 2, "color": "white", "col": 20, "row": 0, "removed": false },
{ "id": 3, "color": "white", "col": 19, "row": 0, "removed": false },
{ "id": 4, "color": "white", "col": 19, "row": 1, "removed": false },
{ "id": 5, "color": "white", "col": 19, "row": 2, "removed": false },
{ "id": 6, "color": "white", "col": 20, "row": 1, "removed": false },
{ "id": 7, "color": "white", "col": 20, "row": 2, "removed": false },
{ "id": 8, "color": "white", "col": 21, "row": 1, "removed": false },
{ "id": 9, "color": "white", "col": 21, "row": 2, "removed": false },
{ "id": 10, "color": "white", "col": 22, "row": 0, "removed": false },
{ "id": 11, "color": "white", "col": 22, "row": 1, "removed": false },
{ "id": 12, "color": "white", "col": 23, "row": 0, "removed": false },
{ "id": 13, "color": "white", "col": 23, "row": 1, "removed": false },
{ "id": 14, "color": "white", "col": 23, "row": 2, "removed": false },
{ "id": 15, "color": "black", "col": -2, "row": 0, "removed": true },
{ "id": 16, "color": "black", "col": 3, "row": 0, "removed": false },
{ "id": 17, "color": "black", "col": 4, "row": 0, "removed": false },
{ "id": 18, "color": "black", "col": 5, "row": 0, "removed": false },
{ "id": 19, "color": "black", "col": 7, "row": 0, "removed": false },
{ "id": 20, "color": "black", "col": 7, "row": 1, "removed": false },
{ "id": 21, "color": "black", "col": 7, "row": 2, "removed": false },
{ "id": 22, "color": "black", "col": 7, "row": 3, "removed": false },
{ "id": 23, "color": "black", "col": 7, "row": 4, "removed": false },
{ "id": 24, "color": "black", "col": 7, "row": 5, "removed": false },
{ "id": 25, "color": "black", "col": 7, "row": 6, "removed": false },
{ "id": 26, "color": "black", "col": 7, "row": 7, "removed": false },
{ "id": 27, "color": "black", "col": 7, "row": 8, "removed": false },
{ "id": 28, "color": "black", "col": 6, "row": 0, "removed": false },
{ "id": 29, "color": "black", "col": 1, "row": 0, "removed": false }
]
}
'''
51 changes: 43 additions & 8 deletions backend/game/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def computer_prediction(request: Request):
# hints = ["6/2(2)", "4/off(2)"]
# hints = ["bar/21"]
# hints = ["3/off", "1/off"]
# hints = ["6/3", "6/off"]
# hints = ["18/-3", "5/-3"]

dice1 = int(dice1)
dice2 = int(dice2)
Expand Down Expand Up @@ -236,19 +238,47 @@ def computer_prediction(request: Request):
# dice2 = 2
# split into -> 18/17 17/16

if (dice1 == dice2 and len(moves) == 4) or (dice1 != dice2 and len(moves) == 2):
return Response({"moves": moves})

again = True
i = 0
while again:
for move in moves.copy():
# Skipping the process if computer is bearing off a checker.
if "-3" in move or "-4" in move:
again = False
continue

move_a = int(move.split("/")[0])
move_b = int(move.split("/")[1])

# Computer is making two moves with a single checker, starting from the bar.
if (move_a == -1 or move_a == -2) and 25 - move_b not in [dice1, dice2] and 24 - move_b != 0:
if (move_b == -3 or move_b == -4) and ((dice1 == dice2 and len(hints) < 4) or (dice1 != dice2 and len(hints) < 2)):
moves.remove(move)

# Logic to correctly minus the dice number from move_a.
problem_with_dice1 = False

# There can be a problem when the two dice aren't the same and gnubg
# is adding the two dice numbers together for a total hints value of, ex: "14/4", if we rolled a 6 and a 4
if dice1 != dice2:
filtered_list = list(filter(lambda c: int(c['col']) == move_a - dice1 - 1 and c['color'] != user_checker, board))
problem_with_dice1 = len(filtered_list) > 1

if problem_with_dice1:
middle = move_a - dice2
if middle <= 0:
moves.append(move)
continue
again = middle - dice1 != 0
else:
middle = move_a - dice1
if middle <= 0:
moves.append(move)
continue
again = middle - dice2 != 0

new_move_a = f"{move_a}/{middle}"
new_move_b = f"{middle}/{move_b}"
moves += [new_move_a, new_move_b]

elif (move_a == -1 or move_a == -2) and 25 - move_b not in [dice1, dice2] and 24 - move_b != 0:
moves.remove(move)

dice_to_compare = dice1 - 1 if user_checker == "white" else 24 - dice1
Expand All @@ -267,7 +297,7 @@ def computer_prediction(request: Request):
moves.insert(0, new_move_b)
moves.insert(0, new_move_a)

elif move_a != -1 and move_a != -2 and move_a - move_b not in [dice1, dice2]:
elif move_a != -1 and move_a != -2 and move_b != -3 and move_b != -4 and move_a - move_b not in [dice1, dice2]:
moves.remove(move)

# Logic to correctly minus the dice number from move_a.
Expand All @@ -291,10 +321,15 @@ def computer_prediction(request: Request):
moves += [new_move_a, new_move_b]

else:
if (dice1 == dice2 and len(moves) == 4) or (dice1 != dice2 and len(moves) == 2) or (move_a == -1 or move_a == -2 and len(moves) == 1):
if (dice1 == dice2 and len(moves) == 4) or (dice1 != dice2 and len(moves) == 2) or ((move_a == -1 or move_a == -2) and len(moves) == 1):
again = False
moves.remove(move)
moves.append(move)
continue

# If there's a bug and the program gets stuck in an infinite loop, we will break it after 3 iterations
if i > 3:
break
i += 1

return Response({"moves": moves})
2 changes: 1 addition & 1 deletion frontend/src/game/data/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const DEFAULT_CAMERA_QUATERNION = {
w: 0.7071071347398497,
}

export const DEFAULT_CHECKER_POSITIONS = process.env.NODE_ENV === "development" ? WHITE_HOUSE_FULL : DEFAULT_POS
export const DEFAULT_CHECKER_POSITIONS = process.env.NODE_ENV === "development" ? DEFAULT_POS : DEFAULT_POS

/**
* If we are in training dice mode
Expand Down

1 comment on commit b155bb4

@vercel
Copy link
@vercel vercel bot commented on b155bb4 Jul 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.