Array [Help]


I am having trouble with storing numbers into my array board[]. It only prints spaces and | lines instead of the numbers i stored in them. And is there anyway to prevent this from being called multiple time?



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
#include<iostream>
#include<limits>

using namespace std;


class TicTacToe {
public:
	void displayBoard();
	void getMove();
	void playGame();
private:
	char board[9];
	char player; // Switch after each move.
};

int main ()
{
	TicTacToe ttt;
    
    for (int i = 0; i < 9; i++) {
        ttt.playGame();

    }
    
    
    
}

void TicTacToe::playGame()
{

    displayBoard();

    
    getMove();    

	// Your implementation here...
    
    
           
    
}

void TicTacToe::displayBoard()
{
	// Your implementation here...
    


    
  
    bool firstMove = true;
    if (firstMove  == true)
    {
        
        for (int i = 0; i < 9; i++) {
            board[i] = i + 1;
        }
        
    }
        
        

    
    firstMove == false;
    for (int i = 0; i < 9; i++) 
    {
        

        if ( (i+1) % 3 == 0 )
        {
            cout << board[i] << endl;
        }
        else 
        {
            
            cout << board[i] <<	" | ";
        }
        
        
    }

}
I would simplify DisplayBoard to just display the board when ever called, it shouldn't care what state the game is in. It is just to display the board. The first move flag for the Display board shouldn't be needed.

I would set up a GameInit function so I can set up the initial state of the board.

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

class TicTacToe {
public:
	void displayBoard();
	int getMove();
	void playGame();
        void newGameInit();
private:
	char board[9];
	char player; // Switch after each move.
};

int main ()
{
	TicTacToe ttt;
    
    for (int i = 0; i < 9; i++) {
        ttt.playGame();

    }
    
    
    
}

void TicTacToe::playGame()
{
     int GameQuit = 0;  // this is set up for two players... 
                                           // if this AI I would set something different like the Result of the game. 1 = draw; 2 = x-wins; 3 o-wins
     GameInit(); // set up the first board before changes.

    while(!GameQuit)
    {
         displayBoard();  // display board for each move.
         GameQuit = getMove(/* I would add player here so I would know which player is making move*/);     

    } 
    // put display board here to display the result of the game.       
    
}

void TicTacToe::newGameInit()
{
// your code ??!?!        
//        for (int i = 0; i < 9; i++) 
//        {
//            board[i] = i + 1;
//        }
       for(int i = 0; i < 9; i++)
//             board[i] = 0;  // make sure they don't have anything.
               board[i] = (char)32; // fill it with spaces so I get a clean print....
}


void TicTacToe::displayBoard()
{
   
    for (int i = 0; i < 9; i++) 
    {
        

        if ( (i+1) % 3 == 0 )
        {
            cout << board[i] << endl;
        }
        else 
        {
            
            cout << board[i] <<	" | ";
        }
        
        
    }

}

Last edited on
Topic archived. No new replies allowed.