~eda-qa/dhlib/main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* <license>
 * This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
 * For full copyright and license information please refer to doc/license.txt.
 * </license> 
 */
package words.test;

import flash.utils.ByteArray;
import mathx.MatPoint;
import words.PermutationIterator;

class WordTest extends ui.StageLayout
{
	var tfEntry : ui.EditText;
	var dict : words.Dictionary;
	var encoder : words.Encoder;
	var boardFactory : words.WordBoardFactory;
	
	public function new()
	{
		super();
		MixinLayout();
		
		encoder = new words.Encoder_EN();
		boardFactory = words.WordBoardFactory.withSizeLang( MatPoint.at( 4, 4 ), encoder );
		
		tfEntry = ui.EditText.singleLine();
		tfEntry.text = "hello";
		layout.add( tfEntry );
		
		layout.add( 
			ui.Button.plain( ui.Action.bind( onLookup ), "Lookup Word" ) 
			);
		layout.add( 
			ui.Button.plain( ui.Action.bind( onAnagram ), "Lookup Anagrams" ) 
			);
		layout.add( 
			ui.Button.plain( ui.Action.bind( onDumpBoardDice ), "Dump Board Dice" ) 
			);
		layout.add( 
			ui.Button.plain( ui.Action.bind( onCreateBoard ), "Create Board" ) 
			);
		layout.add( 
			ui.Button.plain( ui.Action.bind( onAnagramBoard ), "Anagram Board" ) 
			);
		layout.add(
			ui.Button.plain( ui.Action.bind( onSolveBoard ), "Solve Provided Board" )
			);
		
		system.Loader.urlBinary( 
			"WordsCommonEn.raw",
			ui.Action.bind1( onBinaryLoaded, null )
			);
	}
	
	function onLookup()
	{
		if( dict == null )
		{
			trace( "No dictionary available yet." );
			return;
		}
		
		var enc = encoder.encode( tfEntry.text );
		trace( "Encoded: " + enc );
		trace( "Lookup: " + encoder.decode( enc ) );	//what actually will be looked up
		
		var value = dict.lookup( tfEntry.text );
		if( value == -1 )
			trace( "Not Found / Continues" );
		else if ( value == -2 )
			trace( "Not Found / Terminal" );
		else
			trace( "Value: " + value );
	}
	
	function onDumpBoardDice()
	{
		trace( boardFactory.getDiceStack() );
	}
	
	function onCreateBoard()
	{
		trace( boardFactory.createBoard() );
	}
	
	function onAnagramBoard()
	{
		var b = boardFactory.createBoard();
		trace( b );
		anagram( words.Anagram.withMatrix( b, dict ) );
	}
	
	/**
	 * NOTE: This test function does not respect time limits in scripts (like in Flash)
	 * and will likely timeout on long strings.  A proper function would need to reschedule
	 * itself and quasi as a thread
	 */
	function onAnagram()
	{
		anagram( words.Anagram.withString( tfEntry.text, dict ) );
	}
	
	function anagram( ana : words.Anagram )
	{
		var start = haxe.Timer.stamp();
		var step = 0;
		do
		{
			if( !ana.step( 1000 ) )
			{
				trace( "Complete." );
				break;
			}
			
			trace( "Step: " + step++ );
		} while( (haxe.Timer.stamp() - start) < 5 );	//limit time for demo purposes (Flash also has limit)
		
		var count = 0;
		var all = new Array<String>();
		for( key in ana.found.keys() )
		{
			var level = ana.found.get( key );
			trace( key + " => " + level );		
			count++;
			
			//collect all simple words
			if( level <= 2 && key.length >= 3 )
				all.push( key );
		}
		trace( "Total Count: " + count );
		
		all.sort( function( x, y ) { return x.length - y.length; } );
		var allt = "";
		for( w in all )
			allt += "\n" + w;
		trace( allt );
	}
	
	function onBinaryLoaded( binary : ByteArray )
	{
		trace( "Loaded: " + binary.bytesAvailable );
		dict = words.Dictionary.withByteArray( binary, encoder );
	}
	
	function onSolveBoard()
	{
		var letters = tfEntry.text.split( "," );
		ASSERT( letters.length == 16 );
		
		var mat = mathx.Matrix.create( 4, 4, "" );
		for( i in 0...(mat.maxLinearIndex()+1) )
		{
			var mp = mat.fromLinearIndex( i );
			mat.set( mp.x, mp.y, letters[i] );
		}
		
		var board = words.WordBoard.withMatrix( mat );
		trace( board );
		anagram( words.Anagram.withMatrix( board, dict ) );
	}
	
	static public function main()
	{
		ui.StageLayout.setup( WordTest );
	}
	
	define(`IsVerticalLayout',`')
	include(`ui/MixinLayout.ihx')
}