Tic Tac Toe converting '.' to " " in readfile

Ok, I am trying to solve this super small error and I cannot figure it out. In this program you would read a board into a 2D array (this is the practice). The file contains "X" , "O" , and "." The program would then read through it and display it in the correct board format along with the X's and O's.

The main issue that I have is I can't figure out how to convert those "." (periods) from the file and turn them into " " (spaces), on the actual display. Is this going to be an IF statement? The only thing I can think of is to write:
1
2
3
4
if (ticTacBoard[j][i] == '.')
{
    cout << " ";
}

IF this is the correct code, where do I put it?

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
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;
 
void readFile(char ticTacBoard[][9]);
void writeFile(char ticTacBoard[][9]);
void display(char ticTacBoard[][9]);
 
/**********************************************************************
* Main: Basically a delegator. Gets others to do its' dirty work.
***********************************************************************/
int main()
{
   //Declare array
   char ticTacBoard[9][9];
 
   //Calling functions/pass array
   readFile(ticTacBoard);
   display(ticTacBoard);
   writeFile(ticTacBoard);
 
   return 0;
}
 
 
/**********************************************************************
* Displays the results to the screen.
***********************************************************************/
void display(char ticTacBoard[][9])
{
   cout << " " << ticTacBoard[0][0] << " | " << ticTacBoard[1][0] << " | " << ticTacBoard[2][0] << " " << endl
        << "---+---+---" << endl
        << " " << ticTacBoard[0][1] << " | " << ticTacBoard[1][1] << " | " << ticTacBoard[2][1] << " " << endl
        << "---+---+---" << endl
        << " " << ticTacBoard[0][2] << " | " << ticTacBoard[1][2] << " | " << ticTacBoard[2][2] << " " << endl;
}
 
/**********************************************************************
* Read a file into memory.
***********************************************************************/
void readFile(char ticTacBoard[][9])
{
   //Declare variable/array
   char sourceFile[256];
 
   //Declare file-input.
   ifstream fin;
 
   //Get filename from user
   cout << "Enter source filename: ";
   cin >> sourceFile;
 
   //Open file with error checking
   fin.open(sourceFile);

   if (fin.fail())
   {
      cout << "Input file opening failed.\n";
      exit(1);
   }
 
   //Read from file into array     
    for (int i = 0; i < 3; i++)
{

    for (int j = 0; j < 3; j++) 
    {
         fin >> ticTacBoard[j][i];
         
    }
    
}

   //Close the file
   fin.close();
}
 
 
/**********************************************************************
* Write a file into memory.
***********************************************************************/
void writeFile(char ticTacBoard[][9])
{
   //Delcare file-output
   ofstream fout;
   char destinationFile[256];
 
   //Asking for user input
   cout << "Enter destination filename: ";
   cin >> destinationFile;
 
   //Open destination file & error checking
   fout.open(destinationFile);
   if (fout.fail())
   {
      cout << "Output file opening failed.\n";
      exit(1);
   }
   else
      cout << "File written" << endl;
 
   //Writes board to file
for (int i = 0; i < 3; i++)
{
     for (int j = 0; j < 3; j++) 
     {
          fout << ticTacBoard[j][i] << " ";
     }
     fout << endl;
 
}

   //Close file
   fout.close();
}  
Last edited on
closed account (48T7M4Gy)
Why not convert from '.' to ' ' at the input stage where you read the char data into the 2d array. Still use an if statement.

so are you saying to put that If statement on line 67 within that for loop scope? How exactly would you convert?
closed account (48T7M4Gy)
Sort of. You could have something like

1
2
3
4
5
6
char cell_char;
fin>>cell_char;

if(cell_char == '.')
    ticTacBoard[i][j] = ' ';
etc etc



And you'd do the corresponding thing for saving the board to file.
Hello joemf88,

I have two questions not so much for me, but for yourself.

1. Why is "ticTacBoard" defined as 9 x 9 when you only use 3 x 3?

2. Why are you accessing the array backwards, e.g., "ticTacBoard[j][i]".

Something to think about.

Hope that helps,

Andy
so should it look like something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Read from file into array     
    for (int i = 0; i < 3; i++)
{

    for (int j = 0; j < 3; j++) 
    {
         fin >> ticTacBoard[j][i];
         
        char cell_char;
        fin>>cell_char;

        if(cell_char == '.')
        ticTacBoard[i][j] = ' ';

    }
    
}


also what is char cell_char? is cell something from the iostream library?
closed account (48T7M4Gy)
The short answer is it looks OK, without testing it. There's no harm taking into account Handy's comments too.

I would declare cell_char outside the double loop. cell_char is just a name for a char variable. You can choose whatever you like for a name (within reason and C++ rules)

So you end up with something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Read from file into array     
char joemf88;
for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++) 
    {
        fin >> joemf88;

        if(joemf88 == '.')
            ticTacBoard[i][j] = ' ';
        else
            tictTacBoard[i][j] = joemf88;
    }
}
Ok I think that I have fixed that part except now that I have the error that if I am saving the file to a temporary location and then reading it again, the written file is suppose to look the same as the written file.

Am I suppose to do something in the "write board to file" code to make sure that its the same?
How in the world do I write this?

Also when I switch the arrays [j][i] to the other way, all get a lot more errors.
I think that it is a 9x9 because of the board because I tried to make it a 3x3 by making sure that the array syntax shows a 3 instead of a 9. I just got more errors.

Last edited on
closed account (48T7M4Gy)
Maybe this is what you mean.

1
2
3
4
5
6
7
8
9
10
11
//Write array to file     
for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++) 
    {
        if(ticTacBoard[i][j] == ' ')
            fout << '.';
        else
            fout << tictTacBoard[i][j];
    }
}
closed account (48T7M4Gy)
This works OK. Look carefully at the changes :)

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
121
122
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

void readFile(char ticTacBoard[][3]);
void writeFile(char ticTacBoard[][3]);
void display(char ticTacBoard[][3]);

/**********************************************************************
 * Main: Basically a delegator. Gets others to do its' dirty work.
 ***********************************************************************/
int main()
{
    //Declare array
    char ticTacBoard[3][3];
    
    //Calling functions/pass array
    readFile(ticTacBoard);
    display(ticTacBoard);
    writeFile(ticTacBoard);
    
    return 0;
}


/**********************************************************************
 * Displays the results to the screen.
 ***********************************************************************/
void display(char ticTacBoard[][3])
{
    cout << " " << ticTacBoard[0][0] << " | " << ticTacBoard[0][1] << " | " << ticTacBoard[0][2] << " " << endl
    << "---+---+---" << endl
    << " " << ticTacBoard[1][0] << " | " << ticTacBoard[1][1] << " | " << ticTacBoard[1][2] << " " << endl
    << "---+---+---" << endl
    << " " << ticTacBoard[2][0] << " | " << ticTacBoard[2][1] << " | " << ticTacBoard[2][2] << " " << endl;
}

/**********************************************************************
 * Read a file into memory.
 ***********************************************************************/
void readFile(char ticTacBoard[][3])
{
    //Declare variable/array
    char sourceFile[256];
    
    //Declare file-input.
    ifstream fin;
    
    //Get filename from user
    cout << "Enter source filename: ";
    cin >> sourceFile;
    
    //Open file with error checking
    fin.open(sourceFile);
    
    if (fin.fail())
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }
    
    //Read from file into array
    char cell;
    
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            fin >> cell;
            if(cell == '.')
                ticTacBoard[i][j] = ' ';
            else
                ticTacBoard[i][j] = cell;
        }
    }
    
    //Close the file
    fin.close();
}


/**********************************************************************
 * Write a file into memory.
 ***********************************************************************/
void writeFile(char ticTacBoard[][3])
{
    //Delcare file-output
    ofstream fout;
    char destinationFile[256];
    
    //Asking for user input
    cout << "Enter destination filename: ";
    cin >> destinationFile;
    
    //Open destination file & error checking
    fout.open(destinationFile);
    if (fout.fail())
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }
    else
        cout << "File written" << endl;
    
    //Writes board to file
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            if(ticTacBoard[i][j] == ' ')
                fout << '.';
            else
                fout << ticTacBoard[i][j];
        }
        fout << '\n';
    }
    
    //Close file
    fout.close();
}



An input file looks like this, you can have it with or without spaces.
XO.
..X
O..
Last edited on
HAHAHA this is hilarious. I made the changes that I saw in that code. I kept getting errors until I just decided to make it [j][i] instead of [i][j]. I don't know why it works that way. The nested for loop runs [i] as the rows and then [j] as the columns in the multi dimensional arrays, so you would think [i][j] would be the right code.

This program runs different txt files that have different tic tac toe boards. there is 3 and finally all 3 of those boards displayed correctly. thanks a ton for your help. this was driving me crazy!
closed account (48T7M4Gy)
I just had another look at your original code which I didn't look at all that carefully beyond the immediate problem. I think the reason is your display function. You have transposed rows and columns. That's why [j][i] worked and not [i][j].

You'll need to go through and stick to one single convention in all the functions.

Topic archived. No new replies allowed.