java完成單詞搜刮迷宮游戲。本站提示廣大學習愛好者:(java完成單詞搜刮迷宮游戲)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成單詞搜刮迷宮游戲正文
本文實例講述了java完成單詞搜刮迷宮游戲。分享給年夜家供年夜家參考。詳細剖析以下:
我們在雜志上,常常可以或許看到找單詞的小游戲,在一個二維表格中,存在各類字母,我們可以從八個偏向找單詞。這個用盤算機處置非常便利,然則,算法的利害很主要,由於如果用蠻力算法完成,那末消耗的時光是弗成想象的。
這是數據構造與成績求解Java說話描寫一書中給的完成思緒
完全代碼以下,正文寫的很明確了
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 單詞搜刮迷宮
*
* */
public class WordSearch
{
/**
* 在結構函數中結構兩個輸出流,單詞的輸出流,和表格的輸出流
* */
public WordSearch( ) throws IOException
{
puzzleStream = openFile( "輸出表格文件途徑:" );
wordStream = openFile( "輸出單詞文件途徑:" );
System.out.println( "文件讀取中..." );
readPuzzle( );
readWords( );
}
/**
* @return matches 共有若干個單詞婚配
* 依照每一個地位從八個偏向搜刮
* rd 表現行上得增量,eg:rd=-1,表現向上一行
* cd 表現列上得增量 eg:cd=-1。表現向左一步
* 所以rd=1,cd=0表現南
* rd=-1,cd=0表現北,
* rd=-1,cd=1,表現西南
*/
public int solvePuzzle( )
{
int matches = 0;
for( int r = 0; r < rows; r++ )
for( int c = 0; c < columns; c++ )
for( int rd = -1; rd <= 1; rd++ )
for( int cd = -1; cd <= 1; cd++ )
if( rd != 0 || cd != 0 )
matches += solveDirection( r, c, rd, cd );
return matches;
}
/**
* 在指定的坐標上,依照給定的偏向搜刮,前往婚配的單詞數目
* @return number of matches
*/
private int solveDirection( int baseRow, int baseCol, int rowDelta, int colDelta )
{
String charSequence = "";
int numMatches = 0;
int searchResult;
charSequence += theBoard[ baseRow ][ baseCol ];
for( int i = baseRow + rowDelta, j = baseCol + colDelta;
i >= 0 && j >= 0 && i < rows && j < columns;
i += rowDelta, j += colDelta )
{
charSequence += theBoard[ i ][ j ];
searchResult = prefixSearch( theWords, charSequence );
/**
* 上面的 if( searchResult == theWords.length )
* 必需要斷定,不然會湧現越界的風險,及當最初一個單詞之婚配前綴時,前往的是索引-1
* */
if( searchResult == theWords.length )
break;
/**
* 假如沒有呼應的前綴,直接跳過這個基點的搜刮,即便持續搜刮,做的也是無用功
* */
if( !theWords[ searchResult ].startsWith( charSequence ) )
break;
if( theWords[ searchResult ].equals( charSequence ) )
{
numMatches++;
System.out.println( "發明了 " + charSequence + " 在 " +
baseRow+1 + "行 " + baseCol + " 列 " +
i + " " + j );
}
}
return numMatches;
}
/**
* 先說明Arrays.binarySearch(Object[] ,Object)
* 應用二進制搜刮算法來搜刮指定命組,以取得指定對象。在停止此挪用之前,
* 必需依據數組元素的天然次序 對數組停止升序排序(經由過程下面的 Sort(Object[] 辦法)。
* 假如沒有對數組停止排序,則成果是不明白的。(假如數組包括弗成互相比擬的元素(例如,字符串和整數),
* 則沒法 依據數組元素的天然次序對數組停止排序,是以成果是不明白的。)
* 假如數組包括多個等於指定對象的元素,則沒法包管找到的是哪個。
*/
private static int prefixSearch( String [ ] a, String x )
{
int idx = Arrays.binarySearch( a, x );
if( idx < 0 )
return -idx - 1;
else
return idx;
}
/**
* 讀取文件內容,取得輸出流
*/
private BufferedReader openFile( String message )
{
String fileName = "";
FileReader theFile;
BufferedReader fileIn = null;
do
{
System.out.println( message + ": " );
try
{
fileName = in.readLine( );
if( fileName == null )
System.exit( 0 );
theFile = new FileReader( fileName );
fileIn = new BufferedReader( theFile );
}
catch( IOException e )
{ System.err.println( "Cannot open " + fileName ); }
} while( fileIn == null );
System.out.println( "Opened " + fileName );
return fileIn;
}
/**
* 讀入表格
* */
private void readPuzzle( ) throws IOException
{
String oneLine;
List<String> puzzleLines = new ArrayList<String>( );
if( ( oneLine = puzzleStream.readLine( ) ) == null )
throw new IOException( "No lines in puzzle file" );
columns = oneLine.length( );
puzzleLines.add( oneLine );
while( ( oneLine = puzzleStream.readLine( ) ) != null )
{
if( oneLine.length( ) != columns )
System.err.println( "Puzzle is not rectangular; skipping row" );
else
puzzleLines.add( oneLine );
}
rows = puzzleLines.size( );
theBoard = new char[ rows ][ columns ];
int r = 0;
for( String theLine : puzzleLines )
theBoard[ r++ ] = theLine.toCharArray( );
}
/**
* 讀取曾經依照字典排序的單詞列表
*/
private void readWords( ) throws IOException
{
List<String> words = new ArrayList<String>( );
String lastWord = null;
String thisWord;
while( ( thisWord = wordStream.readLine( ) ) != null )
{
if( lastWord != null && thisWord.compareTo( lastWord ) < 0 )
{
System.err.println( "沒有依照字典次序排序,此次跳過" );
continue;
}
words.add( thisWord.trim() );
lastWord = thisWord;
}
theWords = new String[ words.size( ) ];
theWords = words.toArray( theWords );
}
// Cheap main
public static void main( String [ ] args )
{
WordSearch p = null;
try
{
p = new WordSearch( );
}
catch( IOException e )
{
System.out.println( "IO Error: " );
e.printStackTrace( );
return;
}
System.out.println( "正在搜刮..." );
p.solvePuzzle( );
}
private int rows;
private int columns;
private char [ ][ ] theBoard;
private String [ ] theWords;
private BufferedReader puzzleStream;
private BufferedReader wordStream;
private BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
}
願望本文所述對年夜家的java法式設計有所贊助。