trying to simplify

this is what it displays after reading my .txt file.
Just trying to lessen the amount of code it takes.


   A B C D E F G H I
1  7 2 3|     |1 5 9
2  6    |3   2|    8
3  8    |  1  |    2
   -----+-----+-----
4    7  |6 5 4|  2
5      4|2   7|3
6    5  |9 3 1|  4
   -----+-----+-----
7  5    |  7  |    3
8  4    |1   3|    6
9  9 3 2|     |7 1 4


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
void display(char sudokuBoard[][9])
{
   //Declare variables
   char option;

   //Display Column Header
   cout << "   A B C D E F G H I" << endl

   //Row 1
        << "1  "
        << sudokuBoard[0][0]
        << " " << sudokuBoard[1][0]
        << " " << sudokuBoard[2][0]
        << "|"
        << sudokuBoard[3][0]
        << " " << sudokuBoard[4][0]
        << " " << sudokuBoard[5][0]
        << "|"
        << sudokuBoard[6][0]
        << " " << sudokuBoard[7][0]
        << " " << sudokuBoard[8][0]
        << endl

   //Row 2
        << "2  "
        << sudokuBoard[0][1]
        << " " << sudokuBoard[1][1]
        << " " << sudokuBoard[2][1]
        << "|"
        << sudokuBoard[3][1]
        << " " << sudokuBoard[4][1]
        << " " << sudokuBoard[5][1]
        << "|"
        << sudokuBoard[6][1]
        << " " << sudokuBoard[7][1]
        << " " << sudokuBoard[8][1]
        << endl

   //Row 3
        << "3  "
        << sudokuBoard[0][2]
        << " " << sudokuBoard[1][2]
        << " " << sudokuBoard[2][2]
        << "|"
        << sudokuBoard[3][2]
        << " " << sudokuBoard[4][2]
        << " " << sudokuBoard[5][2]
        << "|"
        << sudokuBoard[6][2]
        << " " << sudokuBoard[7][2]
        << " " << sudokuBoard[8][2]
        << endl

   //Visual Separator
        << "   -----+-----+-----" << endl

   //Row 4
        << "4  "
        << sudokuBoard[0][3]
        << " " << sudokuBoard[1][3]
        << " " << sudokuBoard[2][3]
        << "|"
        << sudokuBoard[3][3]
        << " " << sudokuBoard[4][3]
        << " " << sudokuBoard[5][3]
        << "|"
        << sudokuBoard[6][3]
        << " " << sudokuBoard[7][3]
        << " " << sudokuBoard[8][3]
        << endl

   //Row 5
        << "5  "
        << sudokuBoard[0][4]
        << " " << sudokuBoard[1][4]
        << " " << sudokuBoard[2][4]
        << "|"
        << sudokuBoard[3][4]
        << " " << sudokuBoard[4][4]
        << " " << sudokuBoard[5][4]
        << "|"
        << sudokuBoard[6][4]
        << " " << sudokuBoard[7][4]
        << " " << sudokuBoard[8][4]
        << endl

   //Row 6
        << "6  "
        << sudokuBoard[0][5]
        << " " << sudokuBoard[1][5]
        << " " << sudokuBoard[2][5]
        << "|"
        << sudokuBoard[3][5]
        << " " << sudokuBoard[4][5]
        << " " << sudokuBoard[5][5]
        << "|"
        << sudokuBoard[6][5]
        << " " << sudokuBoard[7][5]
        << " " << sudokuBoard[8][5]
        << endl

   //Visual Separator
        << "   -----+-----+-----" << endl

   //Row 7
        << "7  "
        << sudokuBoard[0][6]
        << " " << sudokuBoard[1][6]
        << " " << sudokuBoard[2][6]
        << "|"
        << sudokuBoard[3][6]
        << " " << sudokuBoard[4][6]
        << " " << sudokuBoard[5][6]
        << "|"
        << sudokuBoard[6][6]
        << " " << sudokuBoard[7][6]
        << " " << sudokuBoard[8][6]
        << endl

   //Row 8
        << "8  "
        << sudokuBoard[0][7]
        << " " << sudokuBoard[1][7]
        << " " << sudokuBoard[2][7]
        << "|"
        << sudokuBoard[3][7]
        << " " << sudokuBoard[4][7]
        << " " << sudokuBoard[5][7]
        << "|"
        << sudokuBoard[6][7]
        << " " << sudokuBoard[7][7]
        << " " << sudokuBoard[8][7]
        << endl

   //Row 9
        << "9  "
        << sudokuBoard[0][8]
        << " " << sudokuBoard[1][8]
        << " " << sudokuBoard[2][8]
        << "|"
        << sudokuBoard[3][8]
        << " " << sudokuBoard[4][8]
        << " " << sudokuBoard[5][8]
        << "|"
        << sudokuBoard[6][8]
        << " " << sudokuBoard[7][8]
        << " " << sudokuBoard[8][8]
        << "\n"
        << "\n";

   getOption(sudokuBoard);
}
Use an embedded for loop.
1
2
3
4
5
6
7
8
9
10
11
12
for (y=0; y<9; y++){
  if (y%3==0)
    cout<<"------------------...";
  for (x=0; x<9; x++){
    if (x%3==0)
      cout<<"|";
    if (x%3!=0)
      cout<<" ";
    cout<<sudokuBoard[x][y];
  }
  cout<<endl;
}
Last edited on
Thank you very much. With some tweeking, I got it to look a lot like what I need.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void display(char sudokuBoard[][9])
{
   //Declare variables
   char option;

   //Display Column Header
   cout << "   A B C D E F G H I" << endl;
   
   for (int y = 0; y<9; y++)
   {
   if (y==3 || y==6)
       cout<<"-----+-----+-----\n";
       for (int x = 0; x<9; x++)
       {   
       if (x==3 || x==6)
        cout<<"|";
       if (x!=3 || x!=6)
       cout<<" ";
       cout<<sudokuBoard[x][y];
  }
  cout << endl;
}



it looks like this right now.


Where is your board located? mygame.txt
Options:
   ?  Show these instructions
   D  Display the board
   E  Edit one square
   S  Show the possible values for a square
   Q  Save and Quit

   A B C D E F G H I
 7 2 3|      | 1 5 9
 6    | 3   2|     8
 8    |   1  |     2
-----+-----+-----
   7  | 6 5 4|   2
     4| 2   7| 3
   5  | 9 3 1|   4
-----+-----+-----
 5    |   7  |     3
 4    | 1   3|     6
 9 3 2|      | 7 1 4


im trying to get it to look like this

   A B C D E F G H I
1  7 2 3|     |1 5 9
2  6    |3   2|    8
3  8    |  1  |    2
   -----+-----+-----
4    7  |6 5 4|  2
5      4|2   7|3
6    5  |9 3 1|  4
   -----+-----+-----
7  5    |  7  |    3
8  4    |1   3|    6
9  9 3 2|     |7 1 4


hmmmm..and after posting in here I noticed some of the spacing is off.
Last edited on
here is where Im at..kinda stuck. A little pointer plz.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void display(char sudokuBoard[][9])
{
   //Declare variables
   char option;

   //Display Column Header
   cout << "   A B C D E F G H I" << endl;
   
   for (int y = 0; y<9; y++)
   {
       cout << y << "  ";
   if (y==3 || y==6)
       cout<<"-----+-----+-----\n";
       for (int x = 0; x<9; x++)
       {   
       if (x==3 || x==6)
        cout<<"|";
       if (x%3)
       cout<<" ";
       cout<<sudokuBoard[x][y];
  }
  cout << endl;
}




   A B C D E F G H I
0  7 2 3|     |1 5 9
1  6    |3   2|    8
2  8    |  1  |    2
3  -----+-----+-----
  7  |6 5 4|  2
4      4|2   7|3
5    5  |9 3 1|  4
6  -----+-----+-----
5    |  7  |    3
7  4    |1   3|    6
8  9 3 2|     |7 1 4
>
See what this does...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void display(char sudokuBoard[][9])
{
   //Declare variables
   char option;

   //Display Column Header
   cout << "   A B C D E F G H I" << endl;
   
   for (int y = 0; y<9; y++)
   {
      if (y==3 || y==6)
         cout<<"  -----+-----+-----\n";
      cout << y+1 << "  ";
      for (int x = 0; x<9; x++)
      {
         if (x==3 || x==6)
            cout<<"|";
         cout<<sudokuBoard[x][y];
   }
   cout << endl;
}
Topic archived. No new replies allowed.