-
Notifications
You must be signed in to change notification settings - Fork 0
/
T3Reader.cpp
87 lines (68 loc) · 2.78 KB
/
T3Reader.cpp
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
/*********************************************************************
** Author: Jordan Hamilton
** Date: 03/06/2018
** Description: This implements a class that replays a game of
** tic-tac-toe by reading data from a file. Data members are a
** tic-tac-toe board object from the Board class, and a character
** representing the current player. A method reads the game file
** to replay the game.
*********************************************************************/
#include <fstream>
#include "T3Reader.hpp"
/*********************************************************************
** Description: Overloaded constructor to start a game by specifying
** the first player as a char (x or o).
*********************************************************************/
T3Reader::T3Reader(char firstPlayer) {
gameBoard = Board();
currentPlayer = firstPlayer;
}
/*********************************************************************
** Description: This method takes a string represeting a file name,
** then uses integers in this file to call the makeMove method on the
** board object. The method returns true if all moves in the file were
** valid, or false if a move was invalid, if there were more moves
** in the file when the game ended, or if the file couldn't be read.
*********************************************************************/
bool T3Reader::readGameFile(std::string fileName) {
// Define an ifstream so we can read a file of game moves
std::ifstream inputFile;
// Define integers to store data from the file that we can pass to
// our board object's makeMove method.
int coordinate1, coordinate2;
inputFile.open(fileName);
// Only attempt to read the file if it was opened successfully.
if (inputFile) {
// Store coordinates for the board in our variables if the
// game hasn't ended yet.
while (inputFile >> coordinate1 >> coordinate2 ) {
if (gameBoard.gameState() == UNFINISHED) {
// Make a move, but close the file and return false
// if this wasn't a valid move.
if (!gameBoard.makeMove(coordinate1, coordinate2, currentPlayer)) {
inputFile.close();
return false;
}
// Alternate the current player after a successful move.
if (currentPlayer == 'x') {
currentPlayer = 'o';
} else {
currentPlayer = 'x';
}
} else {
// Close the file and return false if the game had ended with
// moves remaining in our file.
inputFile.close();
return false;
}
}
} else {
// Ensure the file is closed and return false if we couldn't open the file.
inputFile.close();
return false;
}
// Close the file and return true after looping through the file
// and replaying the game successfully.
inputFile.close();
return true;
}