0

How can I turn this into an array? I need a board to show blank spaces and when the user enters it gets filled with a X or an O by another function. The current board works I would like to make it into a array[3][3] and display the contents of the array.

void showboard(char &squareOne, char &squareTwo, char &squareThree, char &squareFour, char &squareFive, char &squareSix, char &squareSeven,
    char &squareEight, char &squareNine)
{


 cout << squareOne << "|" << squareTwo << "|" << squareThree << endl
  << "-+-+-"<< endl
  << squareFour << "|" << squareFive << "|" << squareSix << endl
  << "-+-+-"<< endl
  << squareSeven << "|" << squareEight << "|" << squareNine << endl;
 }
}
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Renge
  • 73
  • 1
  • 6

2 Answers2

1

You can have the showboard() function accept a reference to a 3x3 array of chars. The odd-looking parameter char (&squares)[3][3] means "reference to a 3x3 array of chars named squares".

void showboard(char (&squares)[3][3]) 
{ 
    std::cout << squares[0][0] << "|" << squares[0][1] << "|"
         << squares[0][2] << "\n" << "-+-+-"<< "\n" 
         << squares[1][0] << "|" << squares[1][1] << "|"
         << squares[1][2] << "\n" << "-+-+-"<< "\n"
         << squares[2][0] << "|" << squares[2][1] << "|"
         << squares[2][2] << std::endl; 
} 

int main()
{
    char s[3][3] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} };
    showboard(s);
}

Alternatively, here's an implementation that uses a for loop instead:

void showboard(char (&squares)[3][3]) 
{ 
    for(int i = 0; i < 3; ++i)
    {
        for(int j = 0; j < 3; ++j)
        {
            std::cout << squares[i][j];
            if(j < 2) std::cout << "|";
        }
        std::cout << "\n";
        if(i < 2) std::cout << "-+-+-" << std::endl;;
    }
} 
In silico
  • 51,091
  • 10
  • 150
  • 143
0
template <typename T, int nRows, int nCols>
class Matrix: public vector<vector<T>>
{
public:
    Matrix()
    {
        for (int r = 0; r < nRows; ++r)
        {
            vector<T> row;
            row.resize(nCols);
            fill(row.begin(), row.end(), 0);
            this->push_back(row);
        }
    }

    int rows() const { return nRows; }
    int columns() const { return nCols; }
};

typedef Matrix<int, 3, 3> Board;

void show(const Board& board)
{
    for (int i = 0; i < board.rows(); ++i)
    {
        for (int j = 0; j < board.columns(); ++j)
            cout << board[i][j] << " ";
        cout << endl;
    }
}
Grozz
  • 8,317
  • 4
  • 38
  • 53
  • +1 for a template and for clearly having too much time on your hands – Falmarri Jul 30 '10 at 06:43
  • Things that I don't like: STL containers were not meant to be derived from. You should build your interface on top of the vector functionality but not its interface: prefer composition to inheritance! In this case you are leaking your implementation details to external users and you will not be able to change your implementation at a later time, nor will you be able to control how they use your Matrix. What if they do: `Matrix<3,3> m; m.clear();`? They can check and see that `m.rows() == m.cols() == 0; ` but `m[0][0]` will fail. – David Rodríguez - dribeas Jul 30 '10 at 07:55
  • In multidimensional arrays, it is usually [recommended](http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10) to provide a function type accessor `m(1,2)` instead of array type accessors `m[1][2]`. Besides you are requiring 4 memory allocations (one for each vector), and memory can be fragmented. You can avoid 3 of the four allocations, and improve the locality of data by using a single linear vector and providing access functions that will provide the element at a particular location. Avoid premature optimizations but also early *pessimizations*. – David Rodríguez - dribeas Jul 30 '10 at 08:03
  • And I thought that my answer was an overkill for that question... Also if STL containers really weren't meant to be derived from they probably would've restricted it by one of the available language means? – Grozz Jul 30 '10 at 08:16
  • @Grozz: See http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.11 and http://stackoverflow.com/questions/922248/is-there-any-real-risk-to-deriving-from-the-c-stl-containers – In silico Aug 01 '10 at 03:26