Did I find the right examples for you? yes no      Crawl my project      Python Jobs

# AI.df_solve

All Samples(2)  |  Call(0)  |  Derive(0)  |  Import(2)
```Solves a game using a depth-first search: the game is explored until
endgames are reached.

The endgames are evaluated to see if their are victories or defeats.
Then, a situation in which every move leads to a defeat is labelled
as a (certain) defeat, and a situation in which one move leads to a
(certain) defeat of the opponent is labelled as a (certain) victory.
Situations are evaluated until the initial condition receives a label
(victory or defeat). Draws are also possible.
(more...)
```

```        def df_solve(game, win_score, maxdepth=50, tt=None, depth=0):
"""
Solves a game using a depth-first search: the game is explored until
endgames are reached.

The endgames are evaluated to see if their are victories or defeats.
Then, a situation in which every move leads to a defeat is labelled
as a (certain) defeat, and a situation in which one move leads to a
(certain) defeat of the opponent is labelled as a (certain) victory.
Situations are evaluated until the initial condition receives a label
(victory or defeat). Draws are also possible.

This algorithm can be faster but less informative than ``id_solve``,
as it does not provide 'optimal' strategies (like shortest path to
the victory). It returns simply 1, 0, or -1 to indicate certain
victory, draw, or defeat of the first player.

Arguments:

:param game: an initialized game.
:param win_score: Score above which a score means a win.
:param maxdepth: Maximal recursion depth allowed
:param tt: (optional) a transpo. table can speed up computations.
:param depth: index of the current deapth (don't touch that :) )

"""

# Is there a transposition table and is this game in it ?
lookup = None if (tt is None) else tt.lookup(game)
if lookup != None:
return lookup['value']

if (depth == maxdepth):
raise "Max recursion depth reached :("

if game.is_over():
score = game.scoring()
value = 1 if (score>=win_score) else (-1 if -score>=win_score else 0)
tt.store(game=game, value=value, move=None)
return  value

possible_moves = game.possible_moves()

state = game
unmake_move = hasattr(state, 'unmake_move')

best_value, best_move = -1, None

for move in possible_moves:

if not unmake_move:
game = state.copy() # re-initialize move

game.make_move(move)
game.switch_player()

move_value = - df_solve(game,  win_score,  maxdepth, tt, depth+1)

if unmake_move:
game.switch_player()
game.unmake_move(move)

if move_value == 1:
tt.store(game=state, value=1, move=move)
return move_value

if move_value == 0 and best_value==-1:
# Is forcing a draw possible ?
best_value = 0
best_move = move

tt.store(game=state, value=best_value, move=best_move)

return best_value
```

```from .TwoPlayersGame import TwoPlayersGame
from .Player import Human_Player, AI_Player
from .AI import Negamax, DictTT, id_solve, df_solve

```

```from .TwoPlayersGame import TwoPlayersGame