23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

P-7.8<br />

Write a program that can play tic-tac-toe effectively. (See Section 3.1.5.) To do<br />

this, you will need to create a game tree T, which is a tree where each node<br />

corresponds to a game configuration, which, <strong>in</strong> this case, is a representation of<br />

the tic-tac-toe board. The root node corresponds to the <strong>in</strong>itial configuration. For<br />

each <strong>in</strong>ternal node v <strong>in</strong> T, the children of v correspond to the game states we can<br />

reach from v's game state <strong>in</strong> a s<strong>in</strong>gle legal move for the appropriate player, A<br />

(the first player) or B (the second player). Nodes at even depths correspond to<br />

moves for A <strong>and</strong> nodes at odd depths correspond to moves for B. External nodes<br />

are either f<strong>in</strong>al game states or are at a depth beyond which we don't want to<br />

explore. We score each external node with a value that <strong>in</strong>dicates how good this<br />

state is for player A. In large games, like chess, we have to use a heuristic<br />

scor<strong>in</strong>g function, but for small games, like tic-tac-toe, we can construct the<br />

entire game tree <strong>and</strong> score external nodes as + 1, 0, − 1, <strong>in</strong>dicat<strong>in</strong>g whether<br />

player A has a w<strong>in</strong>, draw, or lose <strong>in</strong> that configuration. A good algorithm for<br />

choos<strong>in</strong>g moves is m<strong>in</strong>imax. In this algorithm, we assign a score to each <strong>in</strong>ternal<br />

node v <strong>in</strong> T, such that if v represents A's turn, we compute v's score as the<br />

maximum of the scores of v's children (which corresponds to A's optimal play<br />

from v). If an <strong>in</strong>ternal node v represents B's turn, then we compute v's score as<br />

the m<strong>in</strong>imum of the scores of v's children (which corresponds to B's optimal<br />

play from v).<br />

P-7.9<br />

Write a program that takes as <strong>in</strong>put a fully parenthesized, arithmetic expression<br />

<strong>and</strong> converts it to a b<strong>in</strong>ary expression tree. Your program should display the tree<br />

<strong>in</strong> some way <strong>and</strong> also pr<strong>in</strong>t the value associated with the root. For an additional<br />

challenge, allow for the leaves to store variables of the form x 1 , x 2 , x 3 , <strong>and</strong> so<br />

on, which are <strong>in</strong>itially 0 <strong>and</strong> which can be updated <strong>in</strong>teractively by your<br />

program, with the correspond<strong>in</strong>g update <strong>in</strong> the pr<strong>in</strong>ted value of the root of the<br />

expression tree.<br />

445

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

Saved successfully!

Ooh no, something went wrong!