[go: nahoru, domu]

Skip to content

Commit

Permalink
Finished fixing some of the bugs for the "vs computer" mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen-ameli committed Jul 9, 2023
1 parent 5bf972d commit 35eb2e4
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 131 deletions.
4 changes: 0 additions & 4 deletions backend/game/consumer_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ def get_initial_game_state(game: Game) -> dict:
if "profile_pics" in str(black.image):
black_img = str(black.image.url)


print(white_img)
print(black_img)

return {
"initial": True,
"player_timer": game.player_timer,
Expand Down
114 changes: 36 additions & 78 deletions backend/game/game_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,83 +33,41 @@ def convert_to_simple_format(board: List[Dict[str, object]]) -> str:

'''
{
"user_checker": "white",
"dice1": 3,
"dice2": 6,
"board": [
{ "id": 0, "color": "white", "col": 8, "row": 0, "removed": false },
{ "id": 1, "color": "white", "col": -1, "row": 0, "removed": true },
{ "id": 2, "color": "white", "col": 11, "row": 0, "removed": false },
{ "id": 3, "color": "white", "col": 11, "row": 1, "removed": false },
{ "id": 4, "color": "white", "col": 11, "row": 2, "removed": false },
{ "id": 5, "color": "white", "col": 11, "row": 3, "removed": false },
{ "id": 6, "color": "white", "col": 11, "row": 4, "removed": false },
{ "id": 7, "color": "white", "col": 16, "row": 0, "removed": false },
{ "id": 8, "color": "white", "col": 16, "row": 1, "removed": false },
{ "id": 9, "color": "white", "col": 16, "row": 2, "removed": false },
{ "id": 10, "color": "white", "col": 18, "row": 0, "removed": false },
{ "id": 11, "color": "white", "col": 18, "row": 1, "removed": false },
{ "id": 12, "color": "white", "col": 18, "row": 2, "removed": false },
{ "id": 13, "color": "white", "col": 18, "row": 3, "removed": false },
{ "id": 14, "color": "white", "col": 18, "row": 4, "removed": false },
{ "id": 15, "color": "black", "col": 23, "row": 0, "removed": false },
{ "id": 16, "color": "black", "col": 23, "row": 1, "removed": false },
{ "id": 17, "color": "black", "col": 12, "row": 0, "removed": false },
{ "id": 18, "color": "black", "col": 9, "row": 0, "removed": false },
{ "id": 19, "color": "black", "col": 4, "row": 0, "removed": false },
{ "id": 20, "color": "black", "col": 7, "row": 0, "removed": false },
{ "id": 21, "color": "black", "col": 20, "row": 0, "removed": false },
{ "id": 22, "color": "black", "col": 7, "row": 1, "removed": false },
{ "id": 23, "color": "black", "col": 7, "row": 2, "removed": false },
{ "id": 24, "color": "black", "col": 7, "row": 3, "removed": false },
{ "id": 25, "color": "black", "col": 5, "row": 0, "removed": false },
{ "id": 26, "color": "black", "col": 5, "row": 1, "removed": false },
{ "id": 27, "color": "black", "col": 5, "row": 2, "removed": false },
{ "id": 28, "color": "black", "col": 2, "row": 0, "removed": false },
{ "id": 29, "color": "black", "col": 0, "row": 0, "removed": false }
]
}
'''


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

simple = convert_to_simple_format(board)

print(simple)
process = subprocess.Popen(["gnubg" , "-q"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, universal_newlines=True)
process.stdin.write("new game\n")
process.stdin.flush()
Expand Down Expand Up @@ -161,7 +161,11 @@ def computer_prediction(request: Request):
# hints = ["6/2(2)", "4/off(2)"]
# hints = ["bar/21"]

dice1 = int(dice1)
dice2 = int(dice2)

hints = process.communicate()[0].split("-ply")[1].split("Eq.")[0].strip().split(" ")
print(hints)

moves: list[str] = []

Expand Down Expand Up @@ -234,53 +238,64 @@ def computer_prediction(request: Request):
# dice1 = 1
# dice2 = 2
# split into -> 18/17 17/16
again = True
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:
continue

# -2/21
# dice = 3 1
for move in moves.copy():
# Skipping the process if the move contains "bar" or "off".
move_a = int(move.split("/")[0])
move_b = int(move.split("/")[1])

# REMOVE THIS??
if "-3" in move or "-4" in move:
continue
# 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:
moves.remove(move)

dice_to_compare = dice1 - 1 if user_checker == "white" else 24 - dice1

# Checking for checkers on the black "enemy" house, for columns with two or more checkers
filtered_list = list(filter(lambda c: int(c['col']) == dice_to_compare and c['color'] != user_checker, board))
problem_with_dice1 = len(filtered_list) > 1

if problem_with_dice1:
middle = 25 - dice2
else:
middle = 25 - dice1

move_a = int(move.split("/")[0])
move_b = int(move.split("/")[1])
new_move_a = f"{move_a}/{middle}"
new_move_b = f"{middle}/{move_b}"
moves.insert(0, new_move_b)
moves.insert(0, new_move_a)

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

# Logic to correctly minus the dice number from move_a.
problem_with_dice1 = False
# 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
# 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 dice1 != dice2 and move_a != -1 and move_a != -2:
if dice1 != dice2:
filtered_list = list(filter(lambda c: int(c['col']) == move_a - int(dice1) - 1 and c['color'] != user_checker, board))
problem_with_dice1 = len(filtered_list) > 1
if problem_with_dice1:
middle = move_a - dice2
again = middle - dice1 != move_b
else:
middle = move_a - dice1
again = middle - dice2 != move_b

if problem_with_dice1:
middle = move_a - dice2
new_move_a = f"{move_a}/{middle}"
new_move_b = f"{middle}/{move_b}"
moves += [new_move_a, new_move_b]

else:
# if move_a == -1 or move_a == -2:
# dice_to_compare = 24 - int(dice1) if user_checker == "black" else int(dice1) - 1
# filtered_list = list(filter(lambda c: int(c['col']) == dice_to_compare and c['color'] != user_checker, board))
# problem_with_dice1 = len(filtered_list) > 1
# print(problem_with_dice1)
# # THIS MIGHT NOT WORK v------v
# if problem_with_dice1:
# middle = dice2 if user_checker == "white" else 25 - dice2
# else:
# middle = dice1 if user_checker == "white" else 25 - dice1
# else:
middle = move_a - dice1
new_move_a = f"{move_a}/{middle}"
new_move_b = f"{middle}/{move_b}"

moves += [new_move_a, new_move_b]
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

return Response({"moves": moves})
23 changes: 13 additions & 10 deletions frontend/app/game/computer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,25 @@ export default function VsComputer() {

// Setting the from position
let fromPos = Number(move.split("/")[0])
fromPos = fromPos === -1 || fromPos === -2 ? fromPos : fromPos - 1
fromPos = fromPos === -1 || fromPos === -2 ? fromPos : fromPos - 1 // if we're bringing a checker off the bar, don't change fromPos

// Setting the to position
let toPos = Number(move.split("/")[1])
toPos = toPos === -3 || toPos === -4 ? toPos : toPos - 1

// Settings the total moved number
let moved = toPos - fromPos
if (toPos === -3) moved = fromPos + 1
else if (toPos === -4) moved = 24 - fromPos
toPos = toPos === -3 || toPos === -4 ? toPos : toPos - 1 // if we're bearing off, don't change toPos

if (userChecker === "white") {
if (fromPos !== -1) fromPos = 23 - fromPos
if (toPos !== -3) toPos = 23 - toPos
}

// Settings the total moved number
let moved: number
if (fromPos === -1) moved = toPos + 1 // white brining off the bar
else if (toPos === -3) moved = fromPos + 1 // white bearing off
else if (fromPos === -2) moved = 24 - toPos // black brining off the bar
else if (toPos === -4) moved = 24 - fromPos // black bearing off
else moved = toPos - fromPos // normal move

// If the computer is removing a checker
const removedCheckers = copyCheckers.filter(c => c.col === toPos)
if (removedCheckers.length === 1 && removedCheckers[0].color !== userChecker) {
Expand All @@ -78,7 +81,6 @@ export default function VsComputer() {

// Get's the next best moves for the computer
async function getPrediction() {
console.log("getting prediction")
type Data = { data: { moves: string[] | null } }

const url = getServerUrl() + "/api/game/get-computer-prediction/"
Expand Down Expand Up @@ -114,19 +116,20 @@ export default function VsComputer() {
const data = await getSession()

const userChecker = Math.random() - 0.5 < 0 ? "white" : "black"
const userChecker2 = Math.random() - 0.5 < 0 ? "white" : "black"

const me: PlayerType = {
id: 0,
name: data ? data.user.name : "Me",
image: data ? data.user.image : "",
color: userChecker,
color: userChecker2,
}

const enemy: PlayerType = {
id: 1,
name: "Computer",
image: "",
color: switchPlayers(userChecker),
color: switchPlayers(userChecker2),
}

// Initializing the game
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/game/data/Data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { Quaternion, Vector3 } from "three"
import { EnvMap, EnvMapType, SettingsType } from "../types/Settings.type"
import { BLACK_ALMOST_WON, DEFAULT_POS, LOTS_OF_REMOVABLES, TWO_REMOVED, WHITE_ALMOST_WON } from "./CheckerPositions"
import {
BLACK_ALMOST_WON,
DEFAULT_POS,
LOTS_OF_REMOVABLES,
ONLY_ONE_MOVE_FOR_BLACK,
ONLY_ONE_MOVE_FOR_WHITE,
TWO_REMOVED,
WHITE_ALMOST_WON,
} from "./CheckerPositions"
import { WHITE_HOUSE_FULL } from "./CheckerPositions"

export const ITEMS_TO_LOAD = 21
Expand Down

0 comments on commit 35eb2e4

Please sign in to comment.