Hey everyone I've created this Tic Tac Toe program and it seems to work. I just wanted to know if somebody could check it out and see if there are eny errors in it or changes that i should make. The files to the program can be found below. Thanks whoever checks it out!!!
Spagett912 0 Light Poster
#include "TicTacToe.h"
bool TicTacToe::check(int board[][3])
{
end = false;
//Player 1 Wins
if (board[0][0] == 1 && board[0][1] == 1 && board[0][2] == 1)
{
p1Win++;
end = true;
return end;
}
else if (board[1][0] == 1 && board[1][1] == 1 && board[1][2] == 1)
{
p1Win++;
end = true;
return end;
}
else if (board[2][0] == 1 && board[2][1] == 1 && board[2][2] == 1)
{
p1Win++;
end = true;
return end;
}
else if (board[0][0] == 1 && board[1][0] == 1 && board[2][0] == 1)
{
p1Win++;
end = true;
return end;
}
else if (board[0][1] == 1 && board[1][1] == 1 && board[2][1] == 1)
{
p1Win++;
end = true;
return end;
}
else if (board[0][2] == 1 && board[1][2] == 1 && board[2][2] == 1)
{
p1Win++;
end = true;
return end;
}
else if (board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1)
{
p1Win++;
end = true;
return end;
}
else if (board[2][0] == 1 && board[1][1] == 1 && board[0][2] == 1)
{
p1Win++;
end = true;
return end;
}
//Player 2 wins
if (board[0][0] == 2 && board[0][1] == 2 && board[0][2] == 2)
{
p2Win++;
end = true;
return end;
}
else if (board[1][0] == 2 && board[1][1] == 2 && board[1][2] == 2)
{
p2Win++;
end = true;
return end;
}
else if (board[2][0] == 2 && board[2][1] == 2 && board[2][2] == 2)
{
p2Win++;
end = true;
return end;
}
else if (board[0][0] == 2 && board[1][0] == 2 && board[2][0] == 2)
{
p2Win++;
end = true;
return end;
}
else if (board[0][1] == 2 && board[1][1] == 2 && board[2][1] == 2)
{
p2Win++;
end = true;
return end;
}
else if (board[0][2] == 2 && board[1][2] == 2 && board[2][2] == 2)
{
p2Win++;
end = true;
return end;
}
else if (board[0][0] == 2 && board[1][1] == 2 && board[2][2] == 2)
{
p2Win++;
end = true;
return end;
}
else if (board[2][0] == 2 && board[1][1] == 2 && board[0][2] == 2)
{
p2Win++;
end = true;
return end;
}
//tie
else if (turnCount == 9)
{
tie++;
end = true;
return end;
}
}
#include "TicTacToe.h"
void TicTacToe::display(int board[][3])
{
int i, j;
int num = 1;
cout << " 1 2 3 ";
for(i=0;i<3; i++)
{
cout << endl << num << " ";
num++;
for(j=0;j <3;j++)
{
if (board[i][j] == 0)
{
cout << " ";
}
else if (board[i][j] == 1)
{
cout << " X ";
}
else
cout << " O ";
if(j <= 1)
{
cout<< " | ";
}
}
cout << endl;
if (i <= 1)
{
cout << "________________\n";
}
}
cout << endl;
}
#include "TicTacToe.h"
void TicTacToe::displayWins()
{
cout << "Player 1 has won " << p1Win << " games\n"
<< "Player 2 has won " << p2Win << "games\n"
<< tie << " games were ties." << endl;
}
#include "TicTacToe.h"
void TicTacToe::initboard()
{
int i = 0;
int j = 0;
int num = 1;
turnCount = 1;
p1Win = 0;
p2Win = 0;
tie = 0;
cout << " 1 2 3";
for (i = 0; i < 3; i++)
{
cout << endl << num << " ";
num++;
for (j = 0; j < 3; j++)
{
board[i][j] = 0;
cout << board[i][j] << " ";
}
}
cout << endl << endl;
}
#include "TicTacToe.h"
int main(int argc, char *argv[])
{
char des;
bool invalid;
TicTacToe game;
game.initboard();
do
{
invalid = false;
game.move();
game.displayWins();
do{
cout << "Would you like to play again?";
cin >> des;
des = toupper(des);
if( des != 'Y' && des != 'N')
{
cout << "Invalid Entry!\n";
invalid = true;
}
}while(invalid);
}while(des != 'N');
system("PAUSE");
return EXIT_SUCCESS;
}
#include "TicTacToe.h"
void TicTacToe::move()
{
int x;
int y;
bool error;
do
{
do
{
do
{
do
{
turn();
cout << "Player " << pTurn << ", Input where you would like to move (x y): ";
error = false;
cin >> x;
if (x < 1 || x > 3)
{
error = true;
cout << " Invalid Entry!\n";
}
}while(error);
cin >> y;
if (y < 1 || y > 3)
{
error = true;
cout << " Invalid Entry!\n";
}
}while(error);
x = x - 1;
y = y - 1;
if (board[x][y] != 0)
{
cout << "Invalid Input. Spot is occupied.\n";
error = true;
}
}
while(error);
if (pTurn == 1)
{
board[x][y] = 1;
turnCount++;
}
else if (pTurn == 2)
{
board[x][y] = 2;
turnCount++;
}
check( board);
system("cls");
display(board);
}while(!end);
}
#include "TicTacToe.h"
TicTacToe::ticTacToe()
{
setwin(0);
setlose(0);
setTie(0);
}
TicTacToe::ticTacToe(win, lose, tie)
{
setwin(win);
setlose(lose);
setTie(tie);
}
int TicTacToe::getwin() const { return win; }
int TicTacToe::getlose() const { return lose; }
int TicTacToe::getTie() const { return tie; }
int TicTacToe::getx() const { return x; }
int TicTacToe::gety() const { return y; }
void TicTacToe::setwin(win)
{
Win = win;
}
void TicTacToe::setlose(lose)
{
Lose = lose;
}
void TicTacToe::setTie(tie)
{
Tie = tie;
}
#ifndef TicTacToe_h
#define TicTacToe_h
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class TicTacToe
{
public:
void initboard();
void move();
void turn();
void displayWins();
void display(int board[][3]);
bool check(int board[][3]);
private:
int board[3][3];
int pTurn;
int turnCount;
int p1Win ,p2Win , tie;
bool end;
};
#endif
#include "TicTacToe.h"
void TicTacToe::turn()
{
if (turnCount % 2 != 0)
{
pTurn = 1;
}
else if (turnCount % 2 == 0)
{
pTurn = 2;
}
cout << "Player " << pTurn << "'s turn\n\n";
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Either you are using a crappy compiler or you did not post the current code:
ticTacToe.cpp
>>TicTacToe::ticTacToe()
Spelled wrong.
TicTackToe.h
missing the constructors referenced in the *.cpp implementation file.
I'm giving up now.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.