You can solve the problem with loops and arrays.
- Create an array of 9 ints for each individual piece that needs to be checed. That is 9 arrays for each row, 9 arrays for each column and 9 arrays for each "small board" (3x3 Section).
- now we have 27 arrays of 9 ints. Let's just make it an array of 27 arrays of 9 ints or
int values[27][9]
.
- Let's fill the array with values from your main 2-d board. That will require 3 loops:
- One loop to fill arrays 0-8 with values from each row
- One loop to fill arrays 9-17 with values from each column
- One loop to fill arrays 18-26 with values from each small board. This part is tricky, well get there.
- Now let's add each of 9 ints from each array and see if it equals 45 (we do it 27 times).
That's it.
See the code - it is compilable:
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
|
#include <iostream>
using namespace std;
const int ROWS = 9;
const int COLS = 9;
const int SMALLS = 9;
int main()
{
int x = 0;
int mainBoard[9][9];
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
mainBoard[i][j] = x++;
}
}
cout << "The entire board looks like this:\n";
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
cout << mainBoard[i][j] << "\t";
}
cout << endl;
}
cout << endl;
// Our helper array of arrays:
int boardPieces [ROWS + COLS + SMALLS][9];
// now fill each of these arrays with values from main board:
// first lets do ROWS:
for(int i = 0; i < ROWS; i++) // Will be done 9 times, for each row
{
for(int j = 0; j < 9; j++) // Each number from that row
{
boardPieces[i][j] = mainBoard[i][j]; // Thats easy
}
}
// now lets do all COLS
for(int i = 0; i < COLS; i++) //
{
for(int j = 0; j < 9; j++)
{
boardPieces[i+9][j] = mainBoard[j][i]; // i and j are swapped here,
//i+9 is because we want to fill 9th through 18th arrays of our data structure
}
}
// Now for some loop frenzy :) We want to get values from the small boards.
// It's three rows of three columns of three rows of three columns
for(int I = 0; I < 3; I++) // For each row of miniboards
{
for(int J = 0; J < 3; J++) // For each column in that row of miniboards
{
for(int i = 0; i < 3; i++) // for each row in that miniboard
{
for(int j = 0; j < 3; j++) // for each column in that row of miniboard
{
// Thats the fun part. How to count index in one-dimensional array
// if we fill it with values from 2-D array? The formula is
// index n = (Row * Max_Col) + Col :)
boardPieces[(I*3)+J+18][(i*3) +j] = mainBoard[(3*I)+i][(3*J)+j];
// +18 beacause ROWS and COLS are 0-17;
}
}
}
}
// Check the results
cout <<" Now, let's see what are values in each section:\n";
for(int i = 0; i < 27; i++)
{
if(i == 0)
cout << " First rows: \n";
if(i == 9)
cout << " Now cols: \n";
if(i == 18)
cout << " Now small boards: \n";
cout << "Array #" << i <<": \t";
for(int j = 0; j < 9; j++)
{
cout << boardPieces[i][j] << " ";
}
cout << endl;
}
// Now let's see how much each section is worth. It should answer your first question:
for(int i = 0; i < 27; i++)
{
int result = 0;
for(int j = 0; j < 9; j++)
{
result += boardPieces[i][j];
}
cout << "Total in array #" << i << ": " << result << endl;
}
while(1);
}
|