15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

420<br />

Adversarial Search<br />

This is how many games offer beginner, intermediate, and expert modes. The<br />

expert mode may search 24 levels in each tree, the intermediate mode may<br />

only search 12 levels, and the beginner mode may only search 4 levels. Because<br />

the beginner mode searches far fewer levels than the expert mode, it runs<br />

faster and doesn’t appear as smart as the intermediate or expert modes.<br />

When a computer doesn’t search beyond a fixed number of levels, it can<br />

miss potential problems that it might have discovered if it had just searched<br />

a little bit deeper. This event is dubbed the horizon effect because the computer<br />

doesn’t see the consequences beyond a certain move, so the problem<br />

appears to lie outside the computer’s sight or beyond the horizon.<br />

Alpha-beta pruning<br />

Another way to speed up searching is to use alpha-beta pruning. The idea<br />

behind this tactic is that it’s relatively pointless to keep searching a tree if a<br />

potential move would represent a horrible choice. For example, in a chess<br />

game, two possible moves might be moving the king into a position where<br />

it’ll get checkmated in two moves or moving a pawn to protect the king.<br />

If a computer always searches every possible move down to 12 levels, it<br />

wastes time evaluating the bad move that results in the king getting checkmated<br />

in two moves. To save time, alpha-beta pruning immediately stops<br />

searching a tree the moment it detects a losing move and makes the computer<br />

focus on studying good moves instead. As a result, the computer’s<br />

time can be spent more profitably examining good moves.<br />

For example, consider the tree in Figure 2-10; the boxes represent possible<br />

moves <strong>for</strong> the computer, and the circles represent possible counter-moves<br />

by a human opponent. The higher the value in each box or circle, the better<br />

the move. So the human opponent will most likely choose moves with high<br />

values. In response, the computer must look at the best possible countermoves<br />

based on what the human opponent is likely to choose.<br />

Figure 2-10:<br />

Assigning<br />

values to<br />

possible<br />

moves helps<br />

the<br />

computer<br />

evaluate the<br />

best<br />

possible<br />

move.<br />

–45 7<br />

102 87 52<br />

62<br />

6<br />

–6<br />

68 12<br />

8<br />

41<br />

39

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!