File I/O Tic Tac Toe Stuck

Okay I got this far in the program. I hope you guys will be able to help me, File I/O with functions is a pain IMO and I need some guidance :P. The program is a simple Tic Tac Toe with pretty much the ability of storing data into a text document (End result hopefully).

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Date:	April, 2013

#include <fstream>
#include <iostream>
#include <string>
#include "main.h"
#include <iomanip>

using namespace std;

// Const, Enums, User Defined Data Types
enum GameState{ 
				GAME_WIN,
				GAME_LOSE,
				GAME_TIE,
				GAME_MAX
			  };

//Function Prototypes

void DisplayIntro(void);
void InitializeGame(bool& gameOver, char& playerSymbol, char gameSquares[]);
void DisplayGameBoard(char gameSquares[]);
char GetValidUserInput(char playerSymbol, char gameSquares[]);
void SetGameBoardPosition(char posLocation, char playerSymbol, char gameSquares[]);
bool CheckForGameOver(char& playerSymbol, char gameSquares[], GameState& gameOver );
void UpdatePlayer(char& playerSymbol, char symbol1, char symbol2 );
char RequestAdditionalGame(void);
void DisplayCredits(void);


// Main Function

int main(void)
{
	bool gameOver = false;		    // game loop control flag
	char posLocation;				// user input variable for position choice
	char playAgain = 'A';			// program exit control flag
	char playerSymbol;				// current player symbol
	char gameSquares[9];			// board display variables
	GameState gameOverState = GAME_MAX; //game state ( win, lose or tie )
	DisplayIntro();                 // Display introduction screen
	while( playAgain == 'A' )
	{
		InitializeGame(gameOver, playerSymbol, gameSquares);			// Initializes the game board
		DisplayGameBoard(gameSquares);									// Displays the game board

		while( !gameOver )			                                    //  Loop until a winner is determined or all squars have been used
		{
			posLocation = GetValidUserInput(playerSymbol, gameSquares);    // Get vaild user inpur
			SetGameBoardPosition(posLocation, playerSymbol, gameSquares);  // Set the appropriate board position
			DisplayGameBoard(gameSquares);                                 // Display the game board
			gameOver = CheckForGameOver(playerSymbol, gameSquares, gameOverState);        // Check for end of game condition and change the player symbol
		}
		playAgain = RequestAdditionalGame();   // Determine whether to play the game again
	}
  DisplayCredits();                         // Display the credit screen
  return 0;
}

void UpdatePlayer(char& playerSymbol, char symbol1, char symbol2 )
{
  if(playerSymbol == symbol1)			// alternating between the X and O player
    playerSymbol = symbol2;
  else
    playerSymbol = symbol1;
}


bool CheckForGameOver(char& playerSymbol, char gameSquares[], GameState& gameOver )
{
    char buffer[2];
    bool freeMove = false;

    if( (gameSquares[0] == gameSquares[1] && gameSquares[1] == gameSquares[2]) || (gameSquares[3] == gameSquares[4] && gameSquares[4] == gameSquares[5]) || (gameSquares[6] == gameSquares[7] && gameSquares[7] == gameSquares[8]) ||  // checking game victory conditions
        (gameSquares[0] == gameSquares[3] && gameSquares[3] == gameSquares[6]) || (gameSquares[1] == gameSquares[4] && gameSquares[4] == gameSquares[7]) || (gameSquares[2] == gameSquares[5] && gameSquares[5] == gameSquares[8]) ||
        (gameSquares[0] == gameSquares[4] && gameSquares[4] == gameSquares[8]) || (gameSquares[2] == gameSquares[4] && gameSquares[4] == gameSquares[6]) )
    {
		gameOver = GAME_WIN;
		cout << endl << playerSymbol<< "  WINS!!" << endl << endl << endl;
  		return true;															
    }
    else 
    {
      int i = 0;
      while( i < 9 && !freeMove)
      {
        itoa(i+1,buffer,10);
        if( gameSquares[i] == buffer[0] )
          freeMove = true;

        i++;
      }

      if( freeMove )
      {
		UpdatePlayer(playerSymbol, 'O', 'X' );
        return false;
      }
      else
      {
        cout << endl << "It's a tie!!" << endl << endl << endl;
		gameOver = GAME_TIE;
        return true;
      }
    }
}


void SetGameBoardPosition(char posLocation, char playerSymbol, char gameSquares[])
{
  char buffer[2];

  buffer[0] = posLocation;
  
  int location = atoi(buffer);
  gameSquares[location-1] = playerSymbol;
}

char GetValidUserInput( char playerSymbol, char gameSquares[])
{
  bool validInput = false;
  char entry;

  while( !validInput)
  {
      cout << "Enter the location number where you want to place an \"" << playerSymbol << "\": ";  
      cin >> entry;		// getting user input for the board position
      if( entry >= '1' && entry <= '9')
      {
			if( (entry == '1' && gameSquares[0] == '1') || (entry == '2' && gameSquares[1] == '2') || (entry == '3' && gameSquares[2] == '3') ||
				(entry == '4' && gameSquares[3] == '4') || (entry == '5' && gameSquares[4] == '5') || (entry == '6' && gameSquares[5] == '6') ||
				(entry == '7' && gameSquares[6] == '7') || (entry == '8' && gameSquares[7] == '8') || (entry == '9' && gameSquares[8] == '9') )			
			{
			  validInput = true;
			}
			else 
			{
			  cout << "You have entered a position that has already been used. ";  
			  validInput = false;
			}
      }
      else
      {
			cout << "You have entered an incorrect value.  Please enter a number from 1 to 9. ";  
      }
      cout << endl << endl;
  }
  return entry;
}

char RequestAdditionalGame(void)
{
  char userEntry;

    cout << "Enter \"A\" to play again, any other key to go back to Main Menu>>: " << endl << endl;		// getting user input for whether the player wants to play again
    cin >> userEntry;
    userEntry = toupper(userEntry);
    cout << endl << endl;

    return(userEntry);
}


void InitializeGame(bool& gameOver, char& playerSymbol, char gameSquares[])
{
    gameOver = false;	// initializing game variables
    playerSymbol = 'O';

    char buffer[2];		// this is where the integer is converted to
     
    for(int i = 0; i < 9; i++)
    {
      itoa(i+1,buffer,10);
      gameSquares[i] = buffer[0];
    }
}


void DisplayIntro(void)
{
  system("cls");
  cout << " ---------------------------------------------------------------------------- \n";
  cout << "                            WELCOME TO TIC TAC TOE			                      " << endl;
  cout << " ---------------------------------------------------------------------------- \n";
  cout << endl << endl;
  system("pause");
  system("cls");
  cout << endl << endl;
}


void DisplayGameBoard(char gameSquares[])
{    
  cout << " " << gameSquares[0] << " | " << gameSquares[1] << " | " << gameSquares[2] << endl;		// displaying the initial game board
  cout << "----------" << endl;
  cout << " " << gameSquares[3] << " | " << gameSquares[4] << " | " << gameSquares[5] << endl;
  cout << "----------" << endl;
  cout << " " << gameSquares[6] << " | " << gameSquares[7] << " | " << gameSquares[8] << endl;
  cout << endl;
}

void DisplayCredits(void)
{
  cout << endl << endl;
  cout << " ------------------------------- GAME CREDITS ------------------------------- \n";
  cout << "                                 Designer: Me"<< endl;
  cout << "                                Programmer: Me" << endl;
  cout << " ---------------------------------------------------------------------------- \n";
  cout << endl << endl;
  system("pause");
}


Thank you for looking at my thread and I hope to learn some things from you guys.
Here is what is left I need to do:

1. DisplayMainMenu: Displays the Main Menu as described above. Performs error checking for possible choices. Returns menu choice as an enumerated type.

2. LoadGameSymbols: Loads a list of symbols to use in game from GameSymbols.txt. Input parameters: array of game symbols to hold load symbols, count of # of symbols loaded into array.

3. WritePlayerData: Writes out the player statistics to the PlayerStats.txt file.Input parameters: array of player records and the # of records to write.

4. DisplayPlayerData: Displays the player statistics to the screen in a table format. Input parameters: array of player records and the # of records to display.

5. EnterPlayerData: Requests the user to enter a call sign for each player.Checks to ensure that both players use different call signs.Input parameters: array of player records, # of records in array.

6. ChooseSymbolsForPlayers: Displays the list of possible symbols to choose from and prompts for input for both players to choose a symbol. When Player 1 chooses a symbol then the list is updated so this symbol is not displayed for Player 2. If any player chooses an invalid symbol then an error message is displayed and the player is prompted to choose again.Input parameters: array of player records, # of records in the player array, array of game symbols and # of symbols in the symbol array.

7. PlayTicTacToe: Plays the TicTacToe game.Input parameters: array of player records, and # of records in the player record list, array of possible character symbols, # of character symbols.

8. InitPlayerInfo: Initializes the player data to default values. Input parameters: Player Data for one player.

9. IncPlayerWins: Updates # of wins for player. Input parameters: Player Data for one player.

10. IncGamesPlayed: Updates # of games played for player. Input parameters: Player Data for one player.

11. CalcPlayerWinPercent: Calculates the % for the num of wins for the player. Input parameters: Player Data for one player.

12. CalcPlayerLevel: Updates the player’s level based on the player’s % of wins.Input parameters: Player Data for one player.
Any Help guys.
Last edited on
bump
enum GameState { GAME_WIN,
GAME_LOSE,
GAME_TIE,
GAME_MAX
};
enum MenuSelect { MENU_PLAY,
MENU_DISPLAY,
MENU_WRITE,
MENU_EXIT
};
enum PlayerLevelType{ NOVICE,
INTERMEDIATE,
EXPERIENCED,
EXPERT
};

//struct
struct PlayerData {
string firstName;
char playerSymbol;
PlayerLevelType level;
int wins;
int gamesPlayed;
float winPercentage;
};
void WritePlayerData (PlayerData PlayerRecords [], int playerCount)
{
ofstream outFile;

int index = 0;

outFile.open("Output.txt");
if( outFile.is_open())
{
for (int i = 0; 1 < playerCount; i++)
{
outFile << PlayerRecords[index].firstName
<< PlayerRecords[index].playerSymbol
//<< PlayerRecords[index].PlayerLevelType[level]
<< PlayerRecords[index].wins
<< PlayerRecords[index].gamesPlayed
<< PlayerRecords[index].winPercentage;

}
}
}
hope that helps :D
Topic archived. No new replies allowed.