开发者

Randomly distributing a word string in a table

Right now, I'm working on a side project where I'm trying to randomly distribute a word string into a 16/16 table(for this example, I am using the first line of the Gettysburg Address). The idea is that it would display the words in reversed order(i.e. the words can only be displayed in four directions in reverse: diagonally up, diagonally down, down-to-up and right-to-left, so something is "FOUR" becoming "ROUF" is allowed). So far, I've created the table and have used the string.split method to make my string to an array, but I'm stuck on getting the reversed order and random distribution part. Any advice on how to achieve this is definitely appreciated.

Code so far:

wind.globals.tRows = 16;
wind.globals.tCols = 16;
wind.globals.wordTable = OS.gui.add_table(view, wind.globals.tRows, wind.globals.tCols);
wind.globals.wordTable.border = '2';
wind.globals.wordTable.frame = 'box';
wind.globals.wordTable.style.position = 'absolute';
wind.globals.wordTable.style.left = '50px';
wind.globals.wordTable.style.top = '50px';
wind.globals.wordTable.style.width = '400px';
wind.globals.wordTable.style.height = '400px';
wind.globals.wordTable.style.textAlign = 'center';

wind.globals.string = "Four Score and Seven Years Ago";
//var tempVar = wind.globals.tString.split('').reverse().join('');
var tempVar = wind.globals.string.split('');
var t = Math.floor(Math.random()*tempVar.length);


for(x = 0; x < wind.globals.tRows; x++){ 

    for(y = 0; y < wind.globals.tCols; y++){

        for(i = 0; i < tempVar.length; i++){

            wind.globals.wordTable.td[x][y].width = '20';
            wind.globals.wordTable.td[x][y].innerHTML = tempVar[y].toUpperCase();

            if(tempVar[y] == "," || tempVar[y] == "." || tempVar[y] == " "){

                wind.globals.wordTable.td[x][y].innerHTML = "";

            }

        }

    }
} 

NOTE: I also forget this part, letter can intersect and be shared(i.e. the letter s can be used to connect the words "score" and "seven"

UPDATE: So after some time of away as well looking over some notes I've made, I think I've come up with an method to achieve what I need to do. The method goes in 4 parts.

1) Get a random number from 1 to 4. That will decide whether it will be horizontal, vertical, positive slope, or negative slope. 2) Get another random number to de开发者_C百科cide if I want to try to intersect it with an existing word. 3) Get random x, y coordinate for the start of the word. Taking into account the length of the word. 4) Check array for conflicting intersections. If conflict, go to prior step and try again. Otherwise, fill in the array.

Even though I have the idea, I'm still having issues coding it up since I haven't had that much programming experience yet. If anyone could give me some examples to work on, I think it will lead me to the right direction. Thanks!


Since you're working with every character in the string, there's no need to do a string.split() to turn it into an array: you can simply use string.length() to loop through the number of characters it contains, and use string.charAt() to get the character at each position.

This also solves your reversing issue, because you can use string.charAt(string.length()-pos) to get the reverse character position starting from the end of the string.

Next, get your words going into the table in a random direction I would start with an array of direction data. It would look something like this:

var directions = [ [1,0],   //horizontal
                   [0,1],   //vertical
                   [1,-1],  //diagonal-up
                   [1,1]    //diagonal-down
                 ];

You can then pick a direction randomly from this array. Then, once you've selected your starting position, you can move around the table in the desired direction by adding the co-ordinate offset data from the directions array.

Hope that helps.

There's probably quite a bit more to it, especially with regards to the starting position I hinted at just now, but I don't think I can give much more advice, as you haven't really been clear about the objectives of your program (it sounds like it might be wordsearch game, but I'm not certain?).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜