-
-
Save codecademydev/fdb9fb5dab4b4d201d999dc9013a87a6 to your computer and use it in GitHub Desktop.
Codecademy export
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
#include <iostream> | |
/* | |
#include "Grid.h" | |
*/ | |
//#include "Grid.h" | |
void viewGrid( char cGrid[10] ){ | |
system ("clear"); | |
std::cout | |
<< "Tic-Tac-Toe is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3x3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical or diagonal row wins the game.\n" | |
<< " " << cGrid[7] << " | " << cGrid[8] << " | " << cGrid[9] << " \n" | |
<< "---" << "+" << "---" << "+" << "---" << "\n" | |
<< " " << cGrid[4] << " | " << cGrid[5] << " | " << cGrid[6] << " \n" | |
<< "---" << "+" << "---" << "+" << "---" << "\n" | |
<< " " << cGrid[1] << " | " << cGrid[2] << " | " << cGrid[3] << " \n"; | |
} | |
int PlayGrid( char cPlayer, char cGrid[10] ){ | |
int iPosition = 0; | |
while( iPosition < 1 || iPosition > 9 ){ | |
std::cout << "Input the number where you wish to play " << cPlayer << " : "; std::cin >> iPosition; | |
if( iPosition >= 1 && iPosition <= 9 ){ | |
if( cGrid[ iPosition ] == 'X' || cGrid[ iPosition ] == 'O' ){ | |
std::cout << "It's already taked.\n"; | |
iPosition = 0; | |
} | |
} else { | |
std::cout << "Invalide entry.\n"; | |
iPosition = 0; | |
} | |
} | |
return iPosition; | |
} | |
int checkGrid( char cPlayer, char cGrid[10] ){ | |
if( | |
( cGrid[1] == cPlayer && cGrid[2] == cPlayer && cGrid[3] == cPlayer ) || | |
( cGrid[1] == cPlayer && cGrid[5] == cPlayer && cGrid[9] == cPlayer ) || | |
( cGrid[1] == cPlayer && cGrid[4] == cPlayer && cGrid[7] == cPlayer ) || | |
( cGrid[2] == cPlayer && cGrid[5] == cPlayer && cGrid[8] == cPlayer ) || | |
( cGrid[3] == cPlayer && cGrid[6] == cPlayer && cGrid[9] == cPlayer ) || | |
( cGrid[3] == cPlayer && cGrid[5] == cPlayer && cGrid[7] == cPlayer ) || | |
( cGrid[4] == cPlayer && cGrid[5] == cPlayer && cGrid[6] == cPlayer ) || | |
( cGrid[7] == cPlayer && cGrid[8] == cPlayer && cGrid[9] == cPlayer ) | |
){ | |
viewGrid( cGrid ); | |
std::cout << "Player " << cPlayer << " Win!\n"; | |
return 10; | |
} | |
return 0; | |
} | |
// ttt.cpp | |
int main(){ | |
char cGrid[10] = { '0' | |
, '1', '2', '3' | |
, '4', '5', '6' | |
, '7', '8', '9' | |
}; | |
int i = 0; | |
char cPlayer; | |
while( i < 9 ){ | |
viewGrid( cGrid ); | |
cPlayer = ( i % 2 ) ? 'O' : 'X'; | |
cGrid[ PlayGrid( cPlayer, cGrid ) ] = cPlayer; | |
i += checkGrid( cPlayer, cGrid ); | |
i++; | |
} | |
if( i == 9 ){ | |
viewGrid( cGrid ); | |
std::cout << "Draw.\n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment