Convert periods to spaces
Nov 10, 2010 at 4:11am UTC
I'm creating a Tic-Tac-Toe game. Well, at least the beginnings of it. Currently, I'm trying to read a game board from a file, display it to the screen, and then write the contents back to a different destination file that the user selects.
My problem is this: when reading the game board from a file, it uses "." to indicate a place in which neither user has made a move. When I display it to the screen, I need those periods to be spaces. But then when time to write it back to the destination file, I need those periods to be back there.
How would one go about doing this? I'm quite lost.
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 123 124 125 126 127 128 129 130 131 132
#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] //Row 1
<< " | " << ticTacBoard[1][0]
<< " | " << ticTacBoard[2][0]
<< " " << endl
<< "---+---+---" << endl
<< " " << ticTacBoard[0][1] //Row 2
<< " | " << ticTacBoard[1][1]
<< " | " << ticTacBoard[2][1]
<< " " << endl
<< "---+---+---" << endl
<< " " << ticTacBoard[0][2] //Row 3
<< " | " << ticTacBoard[1][2]
<< " | " << 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
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[][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" ;
//Writes board to file
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
fout << ticTacBoard[j][i] << " " ;
//Makes sure it is a 3x3 grid
if (j % 3 == 0)
{
fout << endl;
}
}
}
//Close file
fout.close();
}
Any help is appreciated!
Nov 10, 2010 at 4:43am UTC
Instead of something like:
fin >> ticTacBoard[j][i];
Just put it into a temporary character and then check it.
Same for saving, just check the character before writing it.
Nov 10, 2010 at 4:47am UTC
I'm a little confused on what you mean. Could you be a little more specific?
Nov 10, 2010 at 4:51am UTC
It will be better that you just stick with one convention.
Now you need "two" functions. One for fstreams and another for the console.
1 2
if ( cell == EMPTY)
//print the empty representation
Topic archived. No new replies allowed.