tictactoe help

problem:
I have seen this same code pasted around on here, but none of those threads specifically deal with my problem. I have been working on this for a few hours now and have run into an issue when I try to send the x and y values back to the board to print them out. I have tried multiple ways, sometimes getting weird characters and sometimes nothing. Maybe I do not fully understand the constructor stuff, I read my book and I have tried their way of doing it and resulted in program crash. If you give it a look and give me a hint on where my issue is I greatly appreciate it. Thanks.

assignment:
Write a program that allows two players to play the ticTacToe game. Your program must contain the class ticTacToe to implement a ticTacToe object. Include a 3-by-3 2d array as a private member variable, to create the board. If needed, include additional member variables. Some of the operations on a ticTacToe object are printing the current board, getting a move, checking if a move is valid, and determining the winner after each move. Add additional operations as needed.


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
#include<iostream>
#include<cstdlib>
using namespace std;
 
class TicTacToe{
      public:
             void print();
             void play();
             char determine();
             TicTacToe();
 
      private:
              int p1x,p1y;
              int p2x,p2y;
              char TicTacToeBoard[3][3];
      };
 
 
 
      TicTacToe::TicTacToe(){
               char TicTacToeBoard[3][3] = {{'!','!','!'},{'!','!','!'},{'!','!','!'} }; // fill board
               TicTacToeBoard[p1x][p1y] = 'X'; // put x where p1 has coordinates
               TicTacToeBoard[p2x][p2y] = 'O'; // put y where p1 has coordinates
                             }
 
int main(){
 
          TicTacToe playgame,winner; //create objects of type TicTacToe
          playgame.play(); // play game
          winner.determine(); // determine winner
 
          system("pause");
          return 0;
          }    
 
 
void TicTacToe::print(){ // for printing board
 
     int row;
     int col;
     cout << "----------------------" << endl;
     for(row = 0;row < 3; row++){
             for(col = 0;col < 3; col++){
                     cout << TicTacToeBoard[row][col];
                     }
                     cout << endl;
                     }
     cout << "----------------------" << endl;                
}
 
void TicTacToe::play(){ // play game
 
     TicTacToe showboard;
     int turn = 1;
     while(turn <= 9){ 
 
     // let player 1 enter coordinates
     cout << "Player 1" << endl;
     cout << "X:";
     cin >> p1x;
 
     cout << "Y:";
     cin >> p1y;
     showboard.print(); // print board
     cout << endl;
 
     turn++;
     // let player 2 enter coordinates
     cout << "Player 2" << endl;
     cout << "X:";
     cin >> p2x;
 
     cout << "Y:";
     cin >> p2y;
     showboard.print(); // print board
     cout << endl;
 
     turn++;
 
 
 
     }
 
     }
 
char TicTacToe::determine(){ // determine the winner might be incorrect
 
    if( TicTacToeBoard[0][0]==TicTacToeBoard[0][1] && TicTacToeBoard[0][1] == TicTacToeBoard[0][2] ){
        return TicTacToeBoard[0][0];
        }
	else if( TicTacToeBoard[1][0]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[0][2] ){
        return TicTacToeBoard[1][0];
        }
	else if( TicTacToeBoard[2][0]==TicTacToeBoard[2][1] && TicTacToeBoard[2][1] == TicTacToeBoard[0][2] ){
        return TicTacToeBoard[2][0];
        }
 
    else if( TicTacToeBoard[0][0]==TicTacToeBoard[1][0] && TicTacToeBoard[1][0] == TicTacToeBoard[2][0] ){
        return TicTacToeBoard[0][0];
        }
    else if( TicTacToeBoard[0][1]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[2][1] ){
        return TicTacToeBoard[0][1];
        }
    else if( TicTacToeBoard[0][2]==TicTacToeBoard[1][2] && TicTacToeBoard[1][2] == TicTacToeBoard[2][2] ){
        return TicTacToeBoard[0][2];
        } 
 
    else if( TicTacToeBoard[0][0]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[2][2] ){
        return TicTacToeBoard[0][0];
        }
    else if( TicTacToeBoard[0][2]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[2][2] ){
        return TicTacToeBoard[0][2];
        }
    else return ' ';    
 
}

I'm a newbie also, but watch for rem statements that might confuse you, for example your:

TicTacToe::TicTacToe(){
char TicTacToeBoard[3][3] = {{'!','!','!'},{'!','!','!'},{'!','!','!'} }; // fill board
TicTacToeBoard[p1x][p1y] = 'X'; // put x where p1 has coordinates
TicTacToeBoard[p2x][p2y] = 'O'; // put y where p1 has coordinates

Who is who here? It suggests you define that X and O are defined by player and not position on the board. =+S
This is my new code but still isn't working perfectly, but I am getting close. The problem now, is trying to determine the winner of the game. I thought I was doing it correctly, but it is not giving me the correct winner.

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
#include<iostream>
#include<cstdlib>
using namespace std;

class TicTacToe{
      
      private:
              
             char TicTacToeBoard[3][3];
      
      public:
             
             TicTacToe();
             int p1x;
             int p1y;
             int p2x;
             int p2y;
             int row;
             int col;
             TicTacToe(int getp1x, int getp1y, int getp2x, int getp2y){
                           
                          
                           p1x = getp1x;
                           p1y = getp1y;
                           p2x = getp2x;
                           p2y = getp2y;
                           TicTacToeBoard[p1x][p1y] = 'X';
                           TicTacToeBoard[p2x][p2y] = 'O';
                           
                           };
     
     
     void Print(){
     
     cout << "----------------------" << endl;
    
     for(row = 0;row < 3; row++){
             for(col = 0;col < 3; col++){
                      if(TicTacToeBoard[row][col] != 'X' && TicTacToeBoard[row][col] != 'O'){
                      TicTacToeBoard[row][col] = '!';                      
                                 }
                     cout << TicTacToeBoard[row][col];
                     }
                     cout << endl;
                     
                     }
                     
     cout << "----------------------" << endl;                
};
     char determine(){
 
    if( TicTacToeBoard[0][0]==TicTacToeBoard[0][1] && TicTacToeBoard[0][1] == TicTacToeBoard[0][2] ){
        cout << "Winner is: " << TicTacToeBoard[0][0];
        }
	else if( TicTacToeBoard[1][0]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[0][2] ){
        cout << "Winner is: " << TicTacToeBoard[1][0];
        }
	else if( TicTacToeBoard[2][0]==TicTacToeBoard[2][1] && TicTacToeBoard[2][1] == TicTacToeBoard[0][2] ){
        cout << "Winner is: " << TicTacToeBoard[2][0];
        }
 
    else if( TicTacToeBoard[0][0]==TicTacToeBoard[1][0] && TicTacToeBoard[1][0] == TicTacToeBoard[2][0] ){
        cout << "Winner is: " << TicTacToeBoard[0][0];
        }
    else if( TicTacToeBoard[0][1]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[2][1] ){
        cout << "Winner is: " << TicTacToeBoard[0][1];
        }
    else if( TicTacToeBoard[0][2]==TicTacToeBoard[1][2] && TicTacToeBoard[1][2] == TicTacToeBoard[2][2] ){
        cout << "Winner is: " << TicTacToeBoard[0][2];
        } 
 
    else if( TicTacToeBoard[0][0]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[2][2] ){
        cout << "Winner is: " << TicTacToeBoard[0][0];
        }
    else if( TicTacToeBoard[0][2]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[2][2] ){
        cout << "Winner is: " << TicTacToeBoard[0][2];
        }
    else cout << "You both lost" << endl;    
 
};              
};

int main(){
     int count = 0;
     int p1x,p1y,p2x,p2y;     // while count < 9  
     
     while(count < 8){
                 
                 // Player 1 is X
                 // Player 2 is O
                 
                 cout << "Player 1" << endl;
                 cout << "X:";
                 cin >> p1x;
                 cout << "Y:";
                 cin >> p1y;
                 count++;
                 cout << endl;
                 cout << "Player 2" << endl;
                 cout << "X:";
                 cin >> p2x;
                 cout << "Y:";
                 cin >> p2y;
                 count++;
                 cout << endl;
                 
                 TicTacToe Test(p1x,p1y,p2x,p2y);
                 Test.Print();
                 
                 Test.determine();
                          
                 
                 }
                 
                 
    
    system("pause");
    return 0;
}
I don't know if you figured this program out yet, but I ran across an example in a book I was reading. The link I'm posting is a companion reference to the book that will (or should) bring you to a download link for the sourcecode in the book. The Tic-Tac-Toe example is in the Chapter 6 folder.

EDIT: DUH... It might help if I included the link.

http://www.delmarlearning.com/companions/content/1435457420/sourcecode/index.asp?isbn=1435457420

Just remember to understand what it is you are looking at, otherwise you won't learn from this and be able to tackle more complex programs.

Hope that helps.
Last edited on
Topic archived. No new replies allowed.