-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
100 lines (78 loc) · 3.71 KB
/
Copy pathbot.js
File metadata and controls
100 lines (78 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const API_URL = 'http://localhost:8000/api/game';
function chooseCardToPlay(legalMoves, currentTrickCards = []) {
if (!legalMoves || legalMoves.length === 0) {
throw new Error("No legal moves provided by server!");
}
if (currentTrickCards.length === 0) {
return legalMoves.reduce((min, c) => c.rank < min.rank ? c : min);
}
// 2. We moeten volgen: zoek de hoogste kaart onder het tafel-maximum
const leadSuit = currentTrickCards[0].card.suit;
const sameSuitOnTable = currentTrickCards
.filter(t => t.card.suit === leadSuit)
.map(t => t.card.rank);
const maxOnTable = Math.max(...sameSuitOnTable);
const safeCards = legalMoves.filter(c => c.suit === leadSuit && c.rank < maxOnTable);
if (safeCards.length > 0) {
return safeCards.reduce((max, c) => c.rank > max.rank ? c : max);
}
// 3. Geen veilige kaart mogelijk: speel de allerhoogste legale kaart
return legalMoves.reduce((max, c) => c.rank > max.rank ? c : max);
}
async function playBotGame() {
console.log("🎮 Starting new bot game...");
let res = await fetch(`${API_URL}/start`, { method: 'POST' }).then(r => r.json());
if (res.error || !res.game_state) {
console.error("❌ Failed to start game:", res);
return;
}
let gameId = res.game_id;
while (res.game_state && !res.game_state.is_finished) {
const state = res.game_state;
const myHand = state.players.find(p => p.id === 'p1')?.hand || [];
// 1. PASSING_CARDS phase
if (state.state === 'PASSING_CARDS') {
console.log(res.game_state.total_scores)
console.log(`➡️ Passing 3 highest cards...`);
// Zorg ervoor dat we de rank goed uitlezen als getal (of object property)
const extractRank = (c) => typeof c.rank === 'object' ? c.rank.value : c.rank;
const extractSuit = (c) => typeof c.suit === 'object' ? c.suit.value : c.suit;
// Sorteer hand van hoog naar laag op rank value
const sortedHand = [...myHand].sort((a, b) => extractRank(b) - extractRank(a));
// Pak de 3 hoogste kaarten en formatteer ze exact voor de API
const cardsToPass = sortedHand.slice(0, 3).map(c => ({
suit: extractSuit(c),
rank: parseInt(extractRank(c), 10)
}));
console.log("Passing cards:", cardsToPass);
res = await fetch(`${API_URL}/${gameId}/pass`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ cards: cardsToPass })
}).then(r => r.json());
if (res.error || !res.game_state) {
console.error("❌ Pass cards failed:", res.error || res);
break;
}
// 2. PLAYING_TRICKS phase
} else if (state.state === 'PLAYING_TRICKS' && state.current_player === 'p1') {
const currentTrickCards = state.current_trick?.cards || [];
const legalMoves = state.legal_moves || [];
const cardToPlay = chooseCardToPlay(legalMoves, currentTrickCards);
console.log(`🃏 Playing: ${cardToPlay.rank} of ${cardToPlay.suit}`);
res = await fetch(`${API_URL}/${gameId}/play`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ suit: cardToPlay.suit, rank: cardToPlay.rank })
}).then(r => r.json());
if (res.error || !res.game_state) {
console.error("❌ Play card failed:", res.error || res);
break;
}
}
}
if (res?.game_state?.is_finished) {
console.log("🏆 Game finished! Final scores:", res.game_state.total_scores);
}
}
playBotGame();