Archived

This topic is now archived and is closed to further replies.

Tic Tac toe anyone?

This topic is 5306 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hehee...i have a cute and simple* console TTT. (LOL out loud) Wanna see it?

*) VERY VERY VERY!!!

And here it is...

ttt.h

#include <windows.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>

int GameOver = 0;
int PlayerLoop = 1;
int Player = 1;

char brade[3][3]={''-'',''-'',''-'',''-'',''-'',''-'',''-'',''-'',''-''};

void DispBoard(char brade[][3]);
void Input(char brade[][3], int &Player);
void CheckWinConditions(char brade[][3]);
bool CollisionDetection(char brade[][3], int x, int y);


and the .cpp

#include "ttt.h"


void main()
{
while(!GameOver)
{
Input(brade, Player);
}
system("pause");
}


void DispBoard(char brade[][3])
{
int a, b;

for (a=0; a<=2; a++)
{
for (b=0; b<=2; b++) // Simple for loop wich goes through every place in the board

cout << brade[a][b]; // and prints it to the screen

cout << endl; // Put a new line after each row

}
}


void Input(char brade[][3], int &Player)
{
if(GameOver)
return;

int x, y;

while(PlayerLoop)
{
DispBoard(brade);

if(Player==1)
cout << "X - y, x: ";
else if(Player==2)
cout << "O - y, x: ";
cin >> x >> y;

if(CollisionDetection(brade, x, y))
{
if(Player==1)
brade[x][y]=''X'';
else if(Player==2)
brade[x][y]=''O'';
CheckWinConditions(brade);
cout << endl << endl;
break;
}
else
{
cout << "Sorry" << endl << endl;
}
}
if(Player==1)
Player = 2;
else if(Player==2)
Player = 1;
}


bool CollisionDetection(char brade[][3], int x, int y)
{
if (brade[x][y]!=''-'') // If the specified position dosn''t contain a "-", i.e. already occupied

return false; // we print out a "Sorry" because we don''t want to overwrite that cross

else if(x>2 || x<0 || y>2 || y<0) // If the player enters a value outside the range (0..2) print out a "Sorry"

return false;
else
return true;
}


void CheckWinConditions(char brade[][3])
{
if( //Rows

brade[0][0]==''X'' && brade[0][1]==''X'' && brade[0][2]==''X'' ||
brade[1][0]==''X'' && brade[1][1]==''X'' && brade[1][2]==''X'' ||
brade[2][0]==''X'' && brade[2][1]==''X'' && brade[2][2]==''X'' ||
//Columns

brade[0][0]==''X'' && brade[1][0]==''X'' && brade[2][0]==''X'' ||
brade[0][1]==''X'' && brade[1][1]==''X'' && brade[2][1]==''X'' ||
brade[0][2]==''X'' && brade[1][2]==''X'' && brade[2][2]==''X'' ||
//Diagonals

brade[0][0]==''X'' && brade[1][1]==''X'' && brade [2][2]==''X'' ||
brade[0][2]==''X'' && brade[1][1]==''X'' && brade [2][0]==''X'')
{
DispBoard(brade);
cout << "Player 1 WIN!" << endl;
GameOver = TRUE;
}
else if(//Rows

brade[0][0]==''O'' && brade[0][1]==''O'' && brade[0][2]==''O'' ||
brade[1][0]==''O'' && brade[1][1]==''O'' && brade[0][2]==''O'' ||
brade[2][0]==''O'' && brade[2][1]==''O'' && brade[2][2]==''O'' ||
//Columns

brade[0][0]==''O'' && brade[1][0]==''O'' && brade[2][0]==''O'' ||
brade[0][1]==''O'' && brade[1][1]==''O'' && brade[2][1]==''O'' ||
brade[0][2]==''O'' && brade[1][2]==''O'' && brade[2][2]==''O'' ||
//Diagonals

brade[0][0]==''O'' && brade[1][1]==''O'' && brade [2][2]==''O'' ||
brade[0][2]==''O'' && brade[1][1]==''O'' && brade [2][0]==''O'')
{
DispBoard(brade);
cout << "Player 2 WIN!" << endl;
GameOver = TRUE;
}
}

Share this post


Link to post
Share on other sites