~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to app-words/words/WordBoardFactory.mhx

  • Committer: edA-qa mort-ora-y
  • Date: 2010-02-16 05:36:32 UTC
  • Revision ID: eda-qa@disemia.com-20100216053632-60lt7fndfi3fgblw
first

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
7
 
 
8
import mathx.MatPoint;
 
9
import mathx.Matrix;
 
10
 
 
11
/**
 
12
 *  A class for producing word boards.
 
13
 */
 
14
class WordBoardFactory
 
15
{
 
16
        var size : MatPoint;
 
17
        var encoder : Encoder;
 
18
        
 
19
        function new( size : MatPoint, encoder : Encoder )
 
20
        {
 
21
                this.size = size;
 
22
                this.encoder = encoder;
 
23
        }
 
24
 
 
25
        static public function withSizeLang( mp : MatPoint, enc : Encoder )
 
26
        {
 
27
                return new WordBoardFactory( mp, enc );
 
28
        }
 
29
        
 
30
        /**
 
31
         * Produces an array of sequences which would be all the sides
 
32
         * of the dice in a game set.  It does not indicate to which each
 
33
         * dice the value belongs, and I don't think we need this now, we
 
34
         * can live with the occassional invalid/very difficult game.
 
35
         */
 
36
        public function getDiceStack() : Array<String>
 
37
        {
 
38
                var dist = encoder.getSeqDistrib().copy();
 
39
                
 
40
                var total = 0;
 
41
                for( seq in dist )
 
42
                {
 
43
                        total += TypeConvert.asInt( seq[1] );
 
44
                        seq[2] = 1;     //guarantee one each
 
45
                }
 
46
                
 
47
                var numDice = size.x * size.y;
 
48
                var numSides = numDice * 6;
 
49
                
 
50
                //distrib quickly, each guaranteed one, and then the rest based on
 
51
                //assignment, butin order, all 2 first, then 3, etc... (if any remain)
 
52
                var sidesRemain = numSides - dist.length;       //less guaranteed amounts
 
53
                
 
54
                while( sidesRemain > 0 )
 
55
                {
 
56
                        for( seq in dist )
 
57
                        {
 
58
                                var expect = Math.round( seq[1] / total * numSides );
 
59
                                if( seq[2] < expect )
 
60
                                {
 
61
                                        seq[2]++;
 
62
                                        sidesRemain--;
 
63
                                        if( sidesRemain == 0 )
 
64
                                                break;
 
65
                                }
 
66
                        }
 
67
                }
 
68
                
 
69
                var stack = new Array<String>();
 
70
                for( seq in dist )
 
71
                        for( i in 0...seq[2] )
 
72
                                stack.push( seq[0] );
 
73
                                
 
74
                return stack;
 
75
        }
 
76
        
 
77
        public function createBoard() : WordBoard
 
78
        {
 
79
                var mat = Matrix.create( size.x, size.y, "" );
 
80
                var dice = ArrayUtil.shuffle( getDiceStack() );
 
81
                for( i in 0...(mat.maxLinearIndex()+1) )
 
82
                {
 
83
                        var mp = mat.fromLinearIndex( i );
 
84
                        mat.set( mp.x, mp.y, dice[i] );
 
85
                }
 
86
                
 
87
                return WordBoard.withMatrix( mat );
 
88
        }
 
89
        
 
90
}