This is a script to generate game tree for the tic-tac-toe game. Player plays with "X", bot plays with "O". The bot moves here are rational, following a strategy to aim for victory:
def check_winner(board):
winning_positions = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], # Rows
[0, 3, 6], [1, 4, 7], [2, 5, 8], # Columns
[0, 4, 8], [2, 4, 6] # Diagonals
]
for positions in winning_positions:
if board[positions[0]] == board[positions[1]] == board[positions[2]] != '-':
return board[positions[0]]
return None
def generate_board_states(board, depth, current_player):
if depth == 0 or check_winner(board) is not None:
return f'<li>{board}</li>'
states = []
for i, cell in enumerate(board):
if cell == '-':
new_board = list(board)
new_board[i] = current_player
next_player = 'O' if current_player == 'X' else 'X'
child_states = generate_board_states(new_board, depth - 1, next_player)
states.append(f'<li>{board}<ul>{child_states}</ul></li>')
return ''.join(states)
def generate_html_game_tree():
root_board = '---------'
max_depth = 9
current_player = 'X'
html = f'<ul>{generate_board_states(root_board, max_depth, current_player)}</ul>'
return f'<html><body>{html}</body></html>'
# Generate the HTML game tree and save it to a file
html_game_tree = generate_html_game_tree()
with open('game_tree.html', 'w') as file:
file.write(html_game_tree)
Rewrite script.
Player uses "X" always, and instead of using ul and li tags for nesting there should be radio buttons with id attributes having values like "s-xo------" there "s" is a prefix to fulfil requirements on attribute naming ("s" for "state") and other 9 symbols determine the current state of the board for the player and opponent. The player by clicking the radio will choose his and opponent's move in the same time, so player not only put "X" but a combination of "X" and "O" for each turn. Instead of formal nesting each checked radio should make the corresponding div element visible like so:
<style>
#sxo-------:checked ~ .sxo-------{ display: block;}
</style>
These are the rules for the init state of the board:
<style>
div {display: none;}
.s {display: block;}
</style>
So, the key is that instead of normal nesting there should be logical nesting using class sequence: user sets the radio and all corresponding divs become visible:
<style>
div {display: none;}
#sxo-------:checked ~ .sxo-------{ display: block;}
</style>
Ensure that:
- The generated output should contain attribute values as follows:
class="sxo-------" instead of class="s['X', 'O', '-', '-', '-', '-', '-', '-', '-']" and so on for all the rest and ensure symbols are in lower case;
- The same rule for id attributes
- The generated output should contain intermediate player-opponent moves:
<div class="s---------">
<input name="state" id="sxo-------" type="radio"/>
<label for="sxo-------">xo-------</label>
<input name="state" id="sx-o------" type="radio"/>
<label for="sx-o------">x-o------</label>
<input name="state" id="sx--o-----" type="radio"/>
<label for="sx--o-----">x--o-----</label>
...
<input name="state" id="sx-------o" type="radio"/>
<label for="sx-------o">x-------o</label>
...
<input name="state" id="s-------xo" type="radio"/>
<label for="s-------xo">-------xo</label>
</div>
<div class="sxo-------">
<input name="state" id="sxo-x--o--" type="radio"/>
<label for="sxo-x--o--">sxo-x--o--</label>
...
</div>
...
<div class="sxo-x--o--">
...
<input name="state" id="sxo-xxoo--" type="radio"/>
<label for="sxo-xxoo--">sxo-xxoo--</label>
...
</div>
...
(div tags with classes from this example should exist in the generated output)
4) The initial state of the board should be like this:
<style>
.sxo-------,
.sx-o------,
.sx--o-----,
.sx---o----,
.sx----o---,
.sx-----o--,
.sx------o-,
.sx-------o,
.s-xo------,
.s-x-o-----,
.s-x--o----,
.s-x---o---,
.s-x----o--,
.s-x-----o-,
.s-x------o,
.sox-------,
.s--xo-----,
.s--x-o----,
.s--x--o---,
.s--x---o--,
.s--x----o-,
.s--x-----o,
.so-x------,
.s-ox------,
.s---xo----,
.s---x-o---,
.s---x--o--,
.s---x---o-,
.s---x----o,
.so--x-----,
.s-o-x-----,
.s--ox-----,
.s----xo---,
.s----x-o--,
.s----x--o-,
.s----x---o,
.so---x----,
.s-o--x----,
.s--o--x---,
.s---o-x---,
.s----ox---,
.s------xo-,
.s------x-o,
.so-----x--,
.s-o----x--,
.s--o---x--,
.s---o--x--,
.s----o-x--,
.s-----ox--,
.s-------xo,
.so------x-,
.s-o-----x-,
.s--o----x-,
.s---o---x-,
.s----o--x-,
.s-----o-x-,
.s------ox-,
.so-------x,
.s-o------x,
.s--o-----x,
.s---o----x,
.s----o---x,
.s-----o--x,
.s------o-x,
.s-------ox { display: block; }
</style>
(Be sure all of these div with provided classes are exist)