Created
March 1, 2017 20:55
-
-
Save justinrixx/efac5fa179acf1660f0bc00d7af594ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*********************************************************************** | |
* Program: | |
* Lesson 08, Tic-Tac-Toe | |
* Summary: | |
* This program reads, displays, and writes a Tic-Tac-Toe board | |
************************************************************************/ | |
#include <iostream> | |
#include <fstream> | |
#include <cassert> | |
#define BOARD_SIZE 3 | |
#define P1_CHAR 'X' | |
#define P2_CHAR 'O' | |
#define EMPTY_CHAR '.' | |
using namespace std; | |
bool read( char board[][BOARD_SIZE], const char* fileName); | |
bool write( const char board[][BOARD_SIZE], const char* fileName); | |
void display(const char board[][BOARD_SIZE]); | |
bool didWin( const char board[][BOARD_SIZE], char turn); | |
/********************************************************************** | |
* Keeps the data and calles the read/display/write functions | |
***********************************************************************/ | |
int main() | |
{ | |
char board[BOARD_SIZE][BOARD_SIZE]; | |
// read the board | |
char fileName[256]; | |
cout << "Enter source filename: "; | |
cin >> fileName; | |
if (!read(board, fileName)) | |
{ | |
cout << "ERROR: Unable to open file \"" << fileName << "\"\n"; | |
return 1; | |
} | |
// display the board | |
display(board); | |
// write the board to a file | |
cout << "Enter destination filename: "; | |
cin >> fileName; | |
if (!write(board, fileName)) | |
{ | |
cout << "ERROR: Unable to open file \"" << fileName << "\"\n"; | |
return 1; | |
} | |
return 0; | |
} | |
/************************************************************** | |
* READ | |
* Read the board from the specified filename | |
*************************************************************/ | |
bool read(char board[][BOARD_SIZE], const char* fileName) | |
{ | |
assert(*fileName); | |
// open the file | |
ifstream fin(fileName); | |
if (fin.fail()) | |
return false; | |
// read 9 symbols, hopefully they are . X O | |
for (int r = 0; r < BOARD_SIZE; r++) | |
for (int c = 0; c < BOARD_SIZE; c++) | |
{ | |
fin >> board[r][c]; | |
assert(!fin.fail()); | |
assert(board[r][c] == P1_CHAR || | |
board[r][c] == P2_CHAR || | |
board[r][c] == EMPTY_CHAR); | |
} | |
// close the file | |
fin.close(); | |
return true; | |
} | |
/********************************************************** | |
* WRITE | |
* Write to fileName the board data | |
*********************************************************/ | |
bool write(const char board[][BOARD_SIZE], const char* fileName) | |
{ | |
assert(fileName[0] != '\0'); | |
// open the file | |
ofstream fout(fileName); | |
if (fout.fail()) | |
return false; | |
// write my 9 symbols | |
for (int r = 0; r < BOARD_SIZE; r++) | |
for (int c = 0; c < BOARD_SIZE; c++) | |
fout << board[r][c] << (c == 2 ? '\n' : ' '); | |
// close it! | |
fout.close(); | |
cout << "File written\n"; | |
return true; | |
} | |
/******************************************************* | |
* DISPLAY | |
* Display the contents the the screen | |
*****************************************************/ | |
void display(const char board[][BOARD_SIZE]) | |
{ | |
// loop through each row | |
for (int r = 0; r < BOARD_SIZE; r++) | |
{ | |
// only the first row is not preceeded with the --+-- magic | |
if (r != 0) | |
{ | |
for (int i = 0; i < BOARD_SIZE; i++) | |
{ | |
cout << "---"; | |
if (i != 0 && i != BOARD_SIZE - 1) | |
cout << '+'; | |
} | |
cout << endl; | |
} | |
// now, on each row, do the column stuff | |
for (int c = 0; c < BOARD_SIZE; c++) | |
{ | |
// display a space for the dot | |
if (board[r][c] == EMPTY_CHAR) | |
cout << " "; | |
else if (board[r][c] == P1_CHAR) | |
cout << " " << 'X' << " "; | |
else if (board[r][c] == P2_CHAR) | |
cout << " " << 'O' << " "; | |
else | |
// should never get here | |
assert(false); | |
// end with a | or a newline | |
cout << (c == BOARD_SIZE - 1 ? '\n' : '|'); | |
} | |
} | |
// display who won | |
if (didWin(board, P1_CHAR)) | |
cout << P1_CHAR << " won!\n"; | |
if (didWin(board, P2_CHAR)) | |
cout << P2_CHAR << " won!\n"; | |
return; | |
} | |
/******************************************** | |
* DID WIN | |
* Did a given player (determined by the "turn" | |
* variable) win the game? | |
*******************************************/ | |
bool didWin(const char board[][BOARD_SIZE], char turn) | |
{ | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment