Skip to content

Commit 10c4a4e

Browse files
committed
working with switch game
1 parent 6f5607f commit 10c4a4e

2 files changed

Lines changed: 64 additions & 43 deletions

File tree

src/components/SwitchGame.jsx

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ const SwitchGame = () => {
4444
// console.log(switchState);
4545
// }, [switchState]);
4646

47-
const handleToggle = (switchName, newValue) => {
48-
setSwitchState((prevState) =>
49-
prevState.map((item) =>
50-
item.switchName === switchName ? { ...item, isOn: newValue } : item
51-
)
52-
);
53-
};
47+
// const handleToggle = (switchName, newValue) => {
48+
// setSwitchState((prevState) =>
49+
// prevState.map((item) =>
50+
// item.switchName === switchName ? { ...item, isOn: newValue } : item
51+
// )
52+
// );
53+
// };
5454

5555
const handleMix = () => {
5656
setGameState("mixing");
@@ -73,85 +73,108 @@ const SwitchGame = () => {
7373
const handleSolve = () => {
7474
console.log("solve checkpoint");
7575
let sequence = solve(switchState);
76-
let delayGap = 0;
77-
sequence.forEach((switchName) => {
78-
delayGap++;
76+
// let delayGap = 0;
77+
// sequence.forEach((switchName) => {
78+
// delayGap++;
79+
// setTimeout(() => {
80+
// gameLogic(switchName);
81+
// }, delayGap * 800);
82+
// });
83+
for (let i = 0; i < sequence.length; i++) {
7984
setTimeout(() => {
80-
gameLogic(switchName);
81-
}, delayGap * 600);
82-
});
83-
console.log(sequence);
85+
const selectedSwitch = sequence[i];
86+
console.log(selectedSwitch);
87+
gameLogic(selectedSwitch);
88+
}, i * 600);
89+
}
90+
//console.log(sequence);
8491
};
8592

8693
const gameLogic = (switchName) => {
87-
const switchIndex = parseInt(switchName.replace("switch", ""));
94+
//const switchIndex = parseInt(switchName.replace("switch", ""));
8895
// console.log(`You clicked switch ${switchIndex}`);
89-
switch (switchIndex) {
90-
case 0:
96+
switch (switchName) {
97+
case "switch0":
9198
setSwitchState((prevState) =>
9299
prevState.map((item) =>
93-
item.switchName === "switch1" ? { ...item, isOn: !item.isOn } : item
100+
item.switchName === "switch1" || item.switchName === "switch0"
101+
? { ...item, isOn: !item.isOn }
102+
: item
94103
)
95104
);
96105
break;
97-
case 1:
106+
case "switch1":
98107
setSwitchState((prevState) =>
99108
prevState.map((item) =>
100-
item.switchName === "switch0" || item.switchName === "switch2"
109+
item.switchName === "switch0" ||
110+
item.switchName === "switch1" ||
111+
item.switchName === "switch2"
101112
? { ...item, isOn: !item.isOn }
102113
: item
103114
)
104115
);
105116
break;
106-
case 2:
117+
case "switch2":
107118
setSwitchState((prevState) =>
108119
prevState.map((item) =>
109-
item.switchName === "switch1" || item.switchName === "switch3"
120+
item.switchName === "switch1" ||
121+
item.switchName === "switch2" ||
122+
item.switchName === "switch3"
110123
? { ...item, isOn: !item.isOn }
111124
: item
112125
)
113126
);
114127
break;
115-
case 3:
128+
case "switch3":
116129
setSwitchState((prevState) =>
117130
prevState.map((item) =>
118-
item.switchName === "switch2" || item.switchName === "switch4"
131+
item.switchName === "switch2" ||
132+
item.switchName === "switch3" ||
133+
item.switchName === "switch4"
119134
? { ...item, isOn: !item.isOn }
120135
: item
121136
)
122137
);
123138
break;
124-
case 4:
139+
case "switch4":
125140
setSwitchState((prevState) =>
126141
prevState.map((item) =>
127-
item.switchName === "switch3" || item.switchName === "switch5"
142+
item.switchName === "switch3" ||
143+
item.switchName === "switch4" ||
144+
item.switchName === "switch5"
128145
? { ...item, isOn: !item.isOn }
129146
: item
130147
)
131148
);
132149
break;
133-
case 5:
150+
case "switch5":
134151
setSwitchState((prevState) =>
135152
prevState.map((item) =>
136-
item.switchName === "switch4" || item.switchName === "switch6"
153+
item.switchName === "switch4" ||
154+
item.switchName === "switch5" ||
155+
item.switchName === "switch6"
137156
? { ...item, isOn: !item.isOn }
138157
: item
139158
)
140159
);
141160
break;
142-
case 6:
161+
case "switch6":
143162
setSwitchState((prevState) =>
144163
prevState.map((item) =>
145-
item.switchName === "switch5" || item.switchName === "switch7"
164+
item.switchName === "switch5" ||
165+
item.switchName === "switch6" ||
166+
item.switchName === "switch7"
146167
? { ...item, isOn: !item.isOn }
147168
: item
148169
)
149170
);
150171
break;
151-
case 7:
172+
case "switch7":
152173
setSwitchState((prevState) =>
153174
prevState.map((item) =>
154-
item.switchName === "switch6" ? { ...item, isOn: !item.isOn } : item
175+
item.switchName === "switch6" || item.switchName === "switch7"
176+
? { ...item, isOn: !item.isOn }
177+
: item
155178
)
156179
);
157180
break;
@@ -193,8 +216,8 @@ const SwitchGame = () => {
193216
disabled={gameState === "mixing" || gameState === "solving"}
194217
type="checkbox"
195218
checked={switchItem.isOn}
196-
onChange={(e) => {
197-
handleToggle(switchItem.switchName, e.target.checked);
219+
onChange={() => {
220+
// handleToggle(switchItem.switchName, e.target.checked);
198221
gameLogic(switchItem.switchName);
199222
}}
200223
/>
@@ -224,7 +247,7 @@ const SwitchGame = () => {
224247
}}
225248
>
226249
{(() => {
227-
console.log(gameState);
250+
//console.log(gameState);
228251
switch (gameState) {
229252
case "solved":
230253
return "Mix it";

src/components/switch-game-mcts.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ const isGoalState = (state) => {
99

1010
const applyMove = (state, move) => {
1111
let newState = cloneState(state);
12-
1312
newState.set(move, !newState.get(move));
14-
1513
if (newState.has(move - 1)) {
1614
newState.set(move - 1, !newState.get(move - 1));
1715
}
18-
1916
if (newState.has(move + 1)) {
2017
newState.set(move + 1, !newState.get(move + 1));
2118
}
@@ -67,7 +64,7 @@ const rollout = (state, maxDepth) => {
6764
let currentState = cloneState(state);
6865
for (let d = 0; d < maxDepth; d++) {
6966
if (isGoalState(currentState)) {
70-
return 1; // win
67+
return 1;
7168
}
7269
const moves = getPossibleMoves(currentState);
7370
const move = randomElement(moves);
@@ -84,7 +81,6 @@ const mcts = (rootState, iterations = 10000, maxDepth = 10) => {
8481
let node = root;
8582
let state = cloneState(rootState);
8683

87-
// SELECTION
8884
while (node.untriedMoves.length === 0 && node.children.length > 0) {
8985
node = bestChild(node, Math.sqrt(2));
9086
state = node.state;
@@ -93,7 +89,6 @@ const mcts = (rootState, iterations = 10000, maxDepth = 10) => {
9389
// EXPANSION
9490
if (node.untriedMoves.length > 0) {
9591
const move = randomElement(node.untriedMoves);
96-
9792
node.untriedMoves.splice(node.untriedMoves.indexOf(move), 1);
9893
const newState = applyMove(state, move);
9994
const child = new Node(newState, move, node);
@@ -102,7 +97,7 @@ const mcts = (rootState, iterations = 10000, maxDepth = 10) => {
10297
state = newState;
10398
}
10499

105-
// if we found goal
100+
// WE FOUND A SOLUTION?
106101
if (isGoalState(state)) {
107102
solution = getSequence(node);
108103
break;
@@ -119,10 +114,11 @@ const mcts = (rootState, iterations = 10000, maxDepth = 10) => {
119114
}
120115
}
121116

122-
// if solution found?
117+
// NO SOLUTION...? <= this shouldn't happen
123118
if (solution) {
124119
return solution;
125120
} else {
121+
// YES SOLUTION
126122
const bestChildNode = root.children.reduce((a, b) =>
127123
a.visits > b.visits ? a : b
128124
);
@@ -141,9 +137,11 @@ const solve = (switchState) => {
141137
});
142138
console.log("Initial state:", initialState);
143139

140+
// limitations
144141
const iterations = 10000;
145142
const maxDepth = 10;
146143

144+
// TRIGGER MCTS
147145
let result = mcts(initialState, iterations, maxDepth);
148146

149147
console.log("Solution sequence:", result);

0 commit comments

Comments
 (0)