I am trying to display the horizontal words that's in a scrabble board but, my code is outputting everything.

ex: 1 2 3 4 5
+ - - - - - +
row1=> | G |
row2=> | O F F |
row3=> | O A |
row4=> | F A T |
row5=> | T |
+ - - - - - +
1 2 3 4 5

HORIZONTAL: OFF FAT 2 WORDS
VERTICAL: OFT GO FAT 3 WORDS

My code:
//Display Horizontal
string temp;
int numHorz;
for(int i=0; i < boardsize; ++i)
{
temp = " ";
numHorz = 0;

for(int j= 1; j< (boardsize); ++j)
{
if(Board[i][j] != ' ')
{
if(temp == " ")
{
temp = Board[i][j];
temp += Board[i][j];
}

}
else
{
if(temp.length() >= 2)
{
cout << temp;
numHorz++;
}
}

if(temp.length() >= 2)
{
cout << temp;
temp = " ";
numHorz++;
}

}
}
Last edited on
Designing output for a text game requires some thinking — specifically about spaces.
It is most helpful to get out a sheet of graph paper and draw what you want to see, then design the output to work row-by-row, inserting the proper spaces between things.
closed account (48T7M4Gy)
@OP
Having a go has paid off. Maybe this is a start.

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
#include <string>

using namespace std;

int main()
{
    // SETUP THE BOARD
    const int size = 5;
    char board[size][size];
    
    for(int r = 0; r < size; ++r)
    {
        for(int c = 0; c < size; ++c)
            board[r][c] = '-';
    }
    
    string word = "OFF";
    
    // SETUP FOR HORIZONTAL WORD
    int start_row = 2;
    int start_col = 1;
    
    // INSERT WORD INTO BOARD
    for(int i = 0; i < word.length(); ++i)
        board[start_row][start_col + i] = word[i];
    
    // DISPLAY BOARD
    for(int r = 0; r < size; ++r)
    {
        for(int c = 0; c < size; ++c)
            cout << board[r][c] << ' ';
        cout << '\n';
    }
    
    return 0;
}


Obviously r is row, and c is column. Look at this line carefully and vertical words are easy to manage with a fairly minor change board[start_row][start_col + i] = word[i];
Last edited on
Topic archived. No new replies allowed.