[source = "cpp"]
#include "stdafx.h"
using namespace std;
class game {
public:
game();
char getcurrPlayer();
void flipPlayer();
void printBoard();
void playerMove();
char validMove(int);
void updateBoard(int);
bool checkWinner();
bool winner;
private:
char currPlayer;
char ttGrid[3][3];
int totalMoves;
};
game::game()
{
//
//Init Grid to Zeroes
//Init Vars
int k = 0;
for (int i = 0; i <= 2; i++)
{
for(int j = 0; j <= 2; j++)
{
ttGrid[j] = 'A';
}
}
currPlayer = 'O';
winner = false;
}
char game::getcurrPlayer()
{
return currPlayer;
}
void game::flipPlayer()
{
//Flips Player so that X or O gets sequential turn
if (currPlayer == 'O')
{
currPlayer = 'X';
}
else
{
currPlayer = 'O';
}
}
void game::printBoard()
{
//Prints the Board to Console
//Havent figured out how to repaint it, rather then keep printing it
char c;
int b = 0;
cout << endl;
for(int i = 0; i <= 2; i++)
{
cout << "\t";
for (int j = 0; j <= 2; j++)
{
b++;
c = (ttGrid[j] == 'A' ? ('0' + b) : ttGrid[j]);
cout << c;
if (j != 2)
{
cout << " | ";
}
}
if (i != 2)
{
cout << endl;
cout << "\t" <<"--+---+--" << endl;
}
}
}
void game::playerMove()
{
//Object call to switch player
//Have Player Enter Choice
//Object call to validate move
//Object call to update board
int move;
printBoard();
flipPlayer();
cout << endl;
cout << endl;
cout << "Current Player: " << currPlayer << " Its your turn, hurry up and make a move by " << endl;
cout << "picking a number on the board that isnt occupied." << endl;
cout << "Enter Move: ";
cin >> move;
char valid = validMove(move);
while (valid != 'Y')
{
cout << "COME ON!!, you chose an invalid move......." << endl;
cout << "Enter Another Move: ";
cin >> move;
valid = validMove(move);
}
updateBoard(move);
winner = checkWinner();
}
bool game::checkWinner()
{
int lastSq;
int lastSq1;
//Check Rows
//
for (int i = 0; i < 3; i++)
{
lastSq = 0;
for (int j = 0; j < 3; j++)
{
if(ttGrid[j] == currPlayer)
lastSq++;
}
if (lastSq == 3)
return true;
}
//Check Columns
//
for (int i = 0; i < 3; i++)
{
lastSq = 0;
for (int j = 0; j < 3; j++)
{
if(ttGrid[j] == currPlayer)
lastSq++;
}
if (lastSq == 3)
return true;
}
//Check Diagnal
//
int y = 0;
int c = 2;
lastSq = 0;
lastSq1 = 0;
for (int i = 0; i < 3; i++,y++,c--)
{
if (ttGrid[y] == currPlayer)
lastSq++;
if (ttGrid[c] == currPlayer)
lastSq1++;
if (lastSq == 3 || lastSq1 == 3)
return true;
}
return false;
}
char game::validMove(int myMove)
//Check to see if number chosen in var move is in range and the index isnt already taken
{
char gridVal = ttGrid[(myMove - 1) / 3] [(myMove - 1) % 3];
if (myMove > 0 && myMove < 10 && gridVal == 'A')
{
return 'Y';
}
else
{
return 'N';
}
}
void game::updateBoard(int myMove)
//Checking to see if the square isnt already chosen
{
ttGrid[(myMove - 1) / 3] [(myMove - 1) % 3] = currPlayer;
}
int main()
{
int newGame;
cout << "WELCOME TO TIC TAC TOE!!" << endl;
cout << "Press 1 to Start a game ";
cin >> newGame;
//Start Main Game Loop - Used to play multiple games
while(newGame == 1)
{
game myGame;
//Win Game Loop - Used to check for winner and end current game
while(!myGame.winner)
{
myGame.playerMove();
}
char player = myGame.getcurrPlayer();
myGame.printBoard();
cout << endl << endl << endl << "Player " << player << " Wins!" << endl;;
cout << "Press 1 to play again or Press 2 to quit ";
cin >> newGame;
}
return 0;
}
[/source/
my Tic Tac Toe code
anyone want to critque it?
this is really my first substantial code after beginning to learn c++....
it doesnt have a "tie" check but will be adding that soon.
anyway, let me know what you think,id love to hear how i can improve it...
It's good to see more and more people making tic-tac-toe without jumping onto something more complex. Good job, keep it up!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement