word game: searching board for word

hi, i am developing a word game thats played in a board similar to that of connect four. you drop a letter in, and it falls to the bottom. you gain points for every word you make by dropping that letter. the word must be 3=7 letters long to count. the words can be up, down, left right, and diagnally, also in reverse order.

i will be using a list of words in a txt document to compare to the words being claimed.


my questions is... is there an easier way to search the board for created words other than going in and manually testing every possible solution (242 of them) on the board and seeing if the letter you just dropped is included in the word?

my board currently is created as follows

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

void creategrid ()
{
for (int i=0; i<=5; i++){
a1 [i]='.';
a2 [i]='.';
a3 [i]='.';
a4 [i]='.';
a4 [i]='.';
a5 [i]='.';
a6 [i]='.';
a7 [i]='.';
}}




void printgrid (){
for (int i=0; i<=5; i++){
    cout<<a1 [i]<<a2 [i]<<a3 [i]<<a4 [i]<<a5 [i]<<a6 [i]<<a7 [i]<<endl;
}




cout<<"What column would you like to drop that in? (1-7)"<<endl;
    cin>>column;
    if (column==1){
        a1counter--;
        setcount=a1counter;
        a1[setcount]=letter;
    }
    if (column==2){
        a2counter--;
        setcount=a2counter;
        a2[setcount]=letter;
    }
    if (column==3){
        a3counter--;
        setcount=a3counter;
        a3[setcount]=letter;
    }
    if (column==4){
        a4counter--;
        setcount=a4counter;
        a4[setcount]=letter;
    }
    if (column==5){
        a5counter--;
        setcount=a5counter;
        a5[setcount]=letter;
    }
    if (column==6){
        a6counter--;
        setcount=a6counter;
        a6[setcount]=letter;
    }
    if (column==7){
        a7counter--;
        setcount=a7counter;
        a7[setcount]=letter;
    }

}






but i am thinking this may be easier to deal with if its in one dynamic array like so

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main () {
char letter;
 char board[6][7];
 for (int i=0; i<=5; i++){
     for (int j=0; j<=6; j++)
     {board [i][j]='.';
     }}
     for (int i=0; i<=5; i++){
     for (int j=0; j<=6; j++){
         cout<<board[i][j];}
         cout<<endl;}
cout<<"1234567";

cout<<endl<<end;



*i would post the entire code, compilable code up, but its way to long, so i just posted the few pieces that i thought would be needed to give advice.


thanks for any responses, any and all ideas are welcome.. doing it the only way i can think of is going to take forever
Last edited on
Sure, use regular expressions to seach the longest string in each of the 8 possible directions.
could you go a bit more in depth. I feel like im thinking about this all wrong. the letter could also be in the middle fo the word or at the end.

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . t
. . . . . .ax
. . . . .byq


so like above, lets say i just dropped the a. how would i use regular expressions to try find that i made the word bat?
In this case, you have 4 possible words. BAT, TAB, TXQ, QXT. Find the 8 longest possible matches that are at least three characters in length.

Put your dictionary words in regular expressions based on word length.

1
2
3
4
map<size_t, boost:regex> words;

words[3] = boost::regex("ART|BAT|CAT|CRY...|TAB|TOE|TON...");
words[4] = boost::regex("...");


Search through the dictionary words by length. You just have to figure out what to do when you get multiple results, such as "TAB" and "BAT".

1
2
3
4
5
6
7
8
9
for (size_t i = word.length(); i > 3; --i)
{
    boost::match_results match;
    if (boost::regex_match(word, match, words[i]))
        // We've got a match.
        ...
        break;
    }
}


Make sense?
that part does, but finding the 4 possible words is where im having some difficulty. i need it to look at the grid, see which letter i just dropped, and then come up with all the words dropping that letter made. how would i use regular expressions for this?
thanks alot for the help!

while im waiting for a response, im going to be doing some research on regular expressions and see what i can come up with.
Finding the 8 possible word strings is pretty easy:

Given:
1
2
3
4
char board[7][7];

std::string up, down diagUpLeft, diagUpRight;
std::string reverseUp, reverseDown, reverseDiagUpLeft, reverseDiagUpRight;


Here's how you would find one of the diags:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
std::string findDiagUL(char board[][7], size_t x, size_t y)
{
    size_t tx = x; // position of last char
    size_t ty = y; // position of last char

    std::string result;

    while (tx > 0 and ty > 0) {--tx; --ty} // go to start pos.

    while (tx < 7 and ty < 7) {
        char c = board[tx++][ty++];
         if (c == 0) {
            if (result.length() > 0) break
            else continue;
         }
         result += c;
    }
    return result;
}


And finding the reverse is pretty easy:

reverseUp.assign(up.rbegin(), up.rend());
thanks for the responce again=]
this seems really helpful, ill go try it out and see what i come up with.

Topic archived. No new replies allowed.