Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3027

Having trouble initializing array in constructor?

$
0
0
I am making a tic tac toe program in which they are asking me to have a 3x3 2 dimensional array of integers and have the constructor initialize the empty board to all zeros. They also want me to place a 1 or 2 in each empty board space denoting the place where player 1 or player 2 would move The problem I'm having is, my code initializes the board to all zeros for each element of the array, and prints it out just fine, but I can't figure out how to re-initialize the values in the array to show where each player moves on the board... I was thinking about having a default constructor that has each value set to zero, and then use a setGame function that can change the values on the board to one or two depending on where the player moves....but I don't know if that's possible....and i can't picture how i'd do it..

here is my code
Code:



// header file
#include <iostream>
#include <array>


class tictac
{
public:

        tictac(int);
        void print();
        void setGame(int);


private:
 int arrayPlay[3][3];
 int move1;


};

Code:


//MAIN FUNCTION

#include <iostream>
#include <array>
#include "tictac.h"
#include <stdexcept>


using namespace std;

int main()
{
       
       
        int move;
       
       
        cout << "Welcome to tic tac toe! " << "\n\n";
        cout << "Player 1 start. Please select where to move." "\n\n";
        cout << "Top-left: enter 0" "\n" << "Top-middle: enter 1" "\n" << "Top-right: enter 2" "\n" <<
                "Middle-left: enter 3" "\n" << "Middle-middle: enter 4" "\n" << "Middle-right: enter 5" "\n" <<
                "Bottom-left: enter 6" "\n" << "Bottom-middle: enter 7" "\n" << "Bottom-right: enter 8" "\n\n" <<
                "Please enter a value player 1" "\n";
        cin >> move;
       

 


        tictac t(move);
        t.setGame(move);

        t.print();



       

       
       
       

}

Code:


// member function

#include <iostream>
#include <array>
#include <stdexcept>
#include "tictac.h"


using namespace std;

tictac::tictac(int play)
       
{       

        move1 = play;

        arrayPlay[0][0] = 0;
        arrayPlay[0][1] = 0;
        arrayPlay[0][2] = 0;
        arrayPlay[1][0] = 0;
        arrayPlay[1][1] = 0;
        arrayPlay[1][2] = 0;
        arrayPlay[2][0] = 0;
        arrayPlay[2][1] = 0;
        arrayPlay[2][2] = 0;

       
}

void tictac::setGame(int play)
{

       
        // player 1 move
                switch (play)
                {
                case 0:
                        arrayPlay[0][0] == 1;
                        break;
                case 1:
                        arrayPlay[0][1] == 1;
                        break;
                case 2:
                        arrayPlay[0][2] == 1;
                        break;
                case 3:
                        arrayPlay[1][0] == 1;
                        break;
                case 4:
                        arrayPlay[1][1] == 1;
                        break;
                case 5:
                        arrayPlay[1][2] == 1;
                        break;
                case 6:
                        arrayPlay[2][0] == 1;
                        break;
                case 7:
                        arrayPlay[2][1] == 1;
                        break;
                case 8:
                        arrayPlay[2][2] == 1;
                        break;

              }

       
}

void tictac::print()
{
        for (int i = 0; i < 3; i++)
        {
               
                cout << " " << arrayPlay[i][0] << " | " << arrayPlay[i][1]
                        << " | " << arrayPlay[i][2] << "\n" "----------" "\n";               

        }
}


Viewing all articles
Browse latest Browse all 3027

Latest Images

Trending Articles



Latest Images