Java Reference
In-Depth Information
1
/**
2
* Routine to read the grid.
3
* Checks to ensure that the grid is rectangular.
4
* Checks to make sure that capacity is not exceeded is omitted.
5
*/
6
private void readPuzzle( ) throws IOException
7
{
8
String oneLine;
9
List<String> puzzleLines = new ArrayList<String>( );
10
11
if( ( oneLine = puzzleStream.readLine( ) ) == null )
12
throw new IOException( "No lines in puzzle file" );
13
14
columns = oneLine.length( );
15
puzzleLines.add( oneLine );
16
17
while( ( oneLine = puzzleStream.readLine( ) ) != null )
18
{
19
if( oneLine.length( ) != columns )
20
System.err.println( "Puzzle is not rectangular; skipping row" );
21
else
22
puzzleLines.add( oneLine );
23
}
24
25
rows = puzzleLines.size( );
26
theBoard = new char[ rows ][ columns ];
27
28
int r = 0;
29
for( String theLine : puzzleLines )
30
theBoard[ r++ ] = theLine.toCharArray( );
31
}
figure 10.6
The
readPuzzle
routine for reading the grid
search at line 19. If we do not have a prefix, we can stop looking and return.
Otherwise, we know that we have to continue after checking at line 26 for a
possible exact match. Line 35 returns the number of matches found when the
call to
solveDirection
can find no more words. A simple
main
program is
shown in Figure 10.9.
the game of tic-tac-toe
10.2
The
minimax strat-
egy
examines lots
of positions. We can
get by with less
without losing any
information.
Recall from Section 7.7 a simple algorithm, known as the
minimax strategy,
allows the computer to select an optimal move in a game of Tic-Tac-Toe. This
recursive strategy involves the following decisions.
Search WWH ::
Custom Search