1
by edA-qa mort-ora-y
first |
1 |
/* <license> |
2 |
* This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y |
|
3 |
* For full copyright and license information please refer to doc/license.txt. |
|
4 |
* </license> |
|
5 |
*/
|
|
6 |
package words.test; |
|
7 |
||
8 |
import flash.utils.ByteArray; |
|
9 |
import mathx.MatPoint; |
|
10 |
import words.PermutationIterator; |
|
11 |
||
12 |
class WordTest extends ui.StageLayout |
|
13 |
{
|
|
14 |
var tfEntry : ui.EditText; |
|
15 |
var dict : words.Dictionary; |
|
16 |
var encoder : words.Encoder; |
|
17 |
var boardFactory : words.WordBoardFactory; |
|
18 |
||
19 |
public function new() |
|
20 |
{
|
|
21 |
super(); |
|
22 |
MixinLayout(); |
|
23 |
||
24 |
encoder = new words.Encoder_EN(); |
|
25 |
boardFactory = words.WordBoardFactory.withSizeLang( MatPoint.at( 4, 4 ), encoder ); |
|
26 |
||
27 |
tfEntry = ui.EditText.singleLine(); |
|
28 |
tfEntry.text = "hello"; |
|
29 |
layout.add( tfEntry ); |
|
30 |
||
31 |
layout.add( |
|
32 |
ui.Button.plain( ui.Action.bind( onLookup ), "Lookup Word" ) |
|
33 |
);
|
|
34 |
layout.add( |
|
35 |
ui.Button.plain( ui.Action.bind( onAnagram ), "Lookup Anagrams" ) |
|
36 |
);
|
|
37 |
layout.add( |
|
38 |
ui.Button.plain( ui.Action.bind( onDumpBoardDice ), "Dump Board Dice" ) |
|
39 |
);
|
|
40 |
layout.add( |
|
41 |
ui.Button.plain( ui.Action.bind( onCreateBoard ), "Create Board" ) |
|
42 |
);
|
|
43 |
layout.add( |
|
44 |
ui.Button.plain( ui.Action.bind( onAnagramBoard ), "Anagram Board" ) |
|
45 |
);
|
|
46 |
layout.add( |
|
47 |
ui.Button.plain( ui.Action.bind( onSolveBoard ), "Solve Provided Board" ) |
|
48 |
);
|
|
49 |
||
50 |
system.Loader.urlBinary( |
|
51 |
"WordsCommonEn.raw", |
|
52 |
ui.Action.bind1( onBinaryLoaded, null ) |
|
53 |
);
|
|
54 |
}
|
|
55 |
||
56 |
function onLookup() |
|
57 |
{
|
|
58 |
if( dict == null ) |
|
59 |
{
|
|
60 |
trace( "No dictionary available yet." ); |
|
61 |
return; |
|
62 |
}
|
|
63 |
||
64 |
var enc = encoder.encode( tfEntry.text ); |
|
65 |
trace( "Encoded: " + enc ); |
|
66 |
trace( "Lookup: " + encoder.decode( enc ) ); //what actually will be looked up |
|
67 |
||
68 |
var value = dict.lookup( tfEntry.text ); |
|
69 |
if( value == -1 ) |
|
70 |
trace( "Not Found / Continues" ); |
|
71 |
else if ( value == -2 ) |
|
72 |
trace( "Not Found / Terminal" ); |
|
73 |
else
|
|
74 |
trace( "Value: " + value ); |
|
75 |
}
|
|
76 |
||
77 |
function onDumpBoardDice() |
|
78 |
{
|
|
79 |
trace( boardFactory.getDiceStack() ); |
|
80 |
}
|
|
81 |
||
82 |
function onCreateBoard() |
|
83 |
{
|
|
84 |
trace( boardFactory.createBoard() ); |
|
85 |
}
|
|
86 |
||
87 |
function onAnagramBoard() |
|
88 |
{
|
|
89 |
var b = boardFactory.createBoard(); |
|
90 |
trace( b ); |
|
91 |
anagram( words.Anagram.withMatrix( b, dict ) ); |
|
92 |
}
|
|
93 |
||
94 |
/**
|
|
95 |
* NOTE: This test function does not respect time limits in scripts (like in Flash) |
|
96 |
* and will likely timeout on long strings. A proper function would need to reschedule |
|
97 |
* itself and quasi as a thread |
|
98 |
*/
|
|
99 |
function onAnagram() |
|
100 |
{
|
|
101 |
anagram( words.Anagram.withString( tfEntry.text, dict ) ); |
|
102 |
}
|
|
103 |
||
104 |
function anagram( ana : words.Anagram ) |
|
105 |
{
|
|
106 |
var start = haxe.Timer.stamp(); |
|
107 |
var step = 0; |
|
108 |
do
|
|
109 |
{
|
|
110 |
if( !ana.step( 1000 ) ) |
|
111 |
{
|
|
112 |
trace( "Complete." ); |
|
113 |
break; |
|
114 |
}
|
|
115 |
||
116 |
trace( "Step: " + step++ ); |
|
117 |
} while( (haxe.Timer.stamp() - start) < 5 ); //limit time for demo purposes (Flash also has limit) |
|
118 |
||
119 |
var count = 0; |
|
120 |
var all = new Array<String>(); |
|
121 |
for( key in ana.found.keys() ) |
|
122 |
{
|
|
123 |
var level = ana.found.get( key ); |
|
124 |
trace( key + " => " + level ); |
|
125 |
count++; |
|
126 |
||
127 |
//collect all simple words |
|
128 |
if( level <= 2 && key.length >= 3 ) |
|
129 |
all.push( key ); |
|
130 |
}
|
|
131 |
trace( "Total Count: " + count ); |
|
132 |
||
133 |
all.sort( function( x, y ) { return x.length - y.length; } ); |
|
134 |
var allt = ""; |
|
135 |
for( w in all ) |
|
136 |
allt += "\n" + w; |
|
137 |
trace( allt ); |
|
138 |
}
|
|
139 |
||
140 |
function onBinaryLoaded( binary : ByteArray ) |
|
141 |
{
|
|
142 |
trace( "Loaded: " + binary.bytesAvailable ); |
|
143 |
dict = words.Dictionary.withByteArray( binary, encoder ); |
|
144 |
}
|
|
145 |
||
146 |
function onSolveBoard() |
|
147 |
{
|
|
148 |
var letters = tfEntry.text.split( "," ); |
|
149 |
ASSERT( letters.length == 16 ); |
|
150 |
||
151 |
var mat = mathx.Matrix.create( 4, 4, "" ); |
|
152 |
for( i in 0...(mat.maxLinearIndex()+1) ) |
|
153 |
{
|
|
154 |
var mp = mat.fromLinearIndex( i ); |
|
155 |
mat.set( mp.x, mp.y, letters[i] ); |
|
156 |
}
|
|
157 |
||
158 |
var board = words.WordBoard.withMatrix( mat ); |
|
159 |
trace( board ); |
|
160 |
anagram( words.Anagram.withMatrix( board, dict ) ); |
|
161 |
}
|
|
162 |
||
163 |
static public function main() |
|
164 |
{
|
|
165 |
ui.StageLayout.setup( WordTest ); |
|
166 |
}
|
|
167 |
||
168 |
define(`IsVerticalLayout',`') |
|
169 |
include(`ui/MixinLayout.ihx') |
|
170 |
}
|