Jump to content
  • Advertisement
Sign in to follow this  
KingRage

My first playable game- TIC TAC TOE

This topic is 5068 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

Here is my first game made in C++, it may be long, sloppy and retarted script, but it works :) Please give me any suggestions, thanks.
#include <stdlib.h>
#include <iostream>

using namespace std;

int main()
{

char spot[9] = {'1','2','3','4','5','6','7','8','9'};
int choice = 12;
int choice2 = 12;
int play = 1;

cout << "Please take a look at the numbers in the grid. Enter that number to choose that square. Player 1 is 'X' and player 2 is 'O'." << endl;

while(play == 1)
{

cout << "      |      |      " << endl;
cout << "   " << spot[0] <<"  |   " << spot[1] <<"  |   " << spot[2] <<"  " << endl;
cout << "      |      |      " << endl;
cout << "--------------------" << endl;
cout << "      |      |      " << endl;
cout << "   " << spot[3] <<"  |   " << spot[4] <<"  |   " << spot[5] <<"  " << endl;
cout << "      |      |      " << endl;
cout << "--------------------" << endl;
cout << "      |      |      " << endl;
cout << "   " << spot[6] <<"  |   " << spot[7] <<"  |   " << spot[8] <<"  " << endl;
cout << "      |      |      " << endl;

if(spot[0] != '1' && spot[1] != '2' && spot[2] != '3' && spot[3] != '4' && spot[4] != '5' && spot[5] != '6' && spot[6] != '7' && spot[7] != '8' && spot[8] != '9')
{
cout << "Tie!" << endl;
system("pause");
return 0;
}

if((spot[0] == 'O' && spot[1] == 'O' && spot[2] == 'O') || (spot[3] == 'O' &&  spot[4] == 'O' && spot[5] == 'O') || (spot[6] == 'O' && spot[7] == 'O' && spot[8] == 'O') || (spot[0] == 'O' && spot[3] == 'O' && spot[6] == 'O') || (spot[1] == 'O' && spot[4] == 'O' && spot[7] == 'O') || (spot[2] == 'O' && spot[5] == 'O' && spot[8] == 'O') || (spot[0] == 'O' && spot[4] == 'O' && spot[8] == 'O') || (spot[2] == 'O' && spot[4] == 'O' && spot[6] == 'O'))
{
cout << "Player 2 wins!" << endl;
play = 0;
system("pause");
return 0;
}

cout << "You are X's!" << endl;

cout << "Input choice: ";
cin >> choice;

if((choice == 1 && spot[0] == 'O') || (choice == 2 && spot[1] == 'O') ||(choice == 3 && spot[2] == 'O') || (choice == 4 && spot[3] == 'O') ||(choice == 5 && spot[4] == 'O') ||(choice == 6 && spot[5] == 'O') ||(choice == 7 && spot[6] == 'O') ||(choice == 8 && spot[7] == 'O') ||(choice == 9 && spot[8] == 'O'))      
{
choice = 12;
}

switch(choice)
{
case 1:
spot[0] = 'X';
break;
case 2:
spot[1] = 'X';
break;
case 3:
spot[2] = 'X';
break;
case 4:
spot[3] = 'X';
break;
case 5:
spot[4] = 'X';
break;
case 6:
spot[5] = 'X';
break;
case 7:
spot[6] = 'X';
break;
case 8:
spot[7] = 'X';
break;
case 9:
spot[8] = 'X';
case 12:
cout << "Spot already occupied by opponent" << endl;
break;
default:
cout << "Invalid choice, you lose your turn!" << endl;
}
cout << "      |      |      " << endl;
cout << "   " << spot[0] <<"  |   " << spot[1] <<"  |   " << spot[2] <<"  " << endl;
cout << "      |      |      " << endl;
cout << "--------------------" << endl;
cout << "      |      |      " << endl;
cout << "   " << spot[3] <<"  |   " << spot[4] <<"  |   " << spot[5] <<"  " << endl;
cout << "      |      |      " << endl;
cout << "--------------------" << endl;
cout << "      |      |      " << endl;
cout << "   " << spot[6] <<"  |   " << spot[7] <<"  |   " << spot[8] <<"  " << endl;
cout << "      |      |      " << endl;

if(spot[0] != '1' && spot[1] != '2' && spot[2] != '3' && spot[3] != '4' && spot[4] != '5' && spot[5] != '6' && spot[6] != '7' && spot[7] != '8' && spot[8] != '9')
{
cout << "Tie!" << endl;
system("pause");
return 0;
}

if((spot[0] == 'X' && spot[1] == 'X' && spot[2] == 'X') || (spot[3] == 'X' && spot[4] == 'X' && spot[5] == 'X') || (spot[6] == 'X' && spot[7] == 'X' && spot[8] == 'X') || (spot[0] == 'X' && spot[3] == 'X' && spot[6] == 'X') || (spot[1] == 'X'  && spot[4] == 'X' && spot[7] == 'X') || (spot[2] == 'X' && spot[5] == 'X' && spot[8] == 'X') || (spot[0] == 'X' && spot[4] == 'X' && spot[8] == 'X') || (spot[2] == 'X' && spot[4] == 'X' && spot[6] == 'X'))
{
cout << "Player 1 wins!" << endl;
play = 0;
system("pause");
return 0;
}

cout << "You are O's!" << endl;

cout << "Input choice: ";
cin >> choice2;

if((choice2 == 1 && spot[0] == 'X') || (choice2 == 2 && spot[1] == 'X') ||(choice2 == 3 && spot[2] == 'X') || (choice2 == 4 && spot[3] == 'X') ||(choice2 == 5 && spot[4] == 'X') ||(choice2 == 6 && spot[5] == 'X') ||(choice2 == 7 && spot[6] == 'X') ||(choice2 == 8 && spot[7] == 'X') ||(choice2 == 9 && spot[8] == 'X'))
{
choice2 = 12;
}
switch(choice2)
{
case 1:
spot[0] = 'O';
break;
case 2:
spot[1] = 'O';
break;
case 3:
spot[2] = 'O';
break;
case 4:
spot[3] = 'O';
break;
case 5:
spot[4] = 'O';
break;
case 6:
spot[5] = 'O';
break;
case 7:
spot[6] = 'O';
break;
case 8:
spot[7] = 'O';
break;
case 9:
spot[8] = 'O';
break;
default:
cout << "Invalid choice, you lose your turn!" << endl;
}
}

system("pause");
return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
1. You need AI
2. You need functions, not just main()
3. You shouldn't lose your turn when you choose a wrong place

But other than that I liked it.

Share this post


Link to post
Share on other sites
well done :) I would recremend error checking aka cin.good() for a certain data type. tht way someone doesn't type in something like a char into an int, causing an infinite loop going on.

Share this post


Link to post
Share on other sites
Quote:
Original post by Boder
1. You need AI
2. You need functions, not just main()
3. You shouldn't lose your turn when you choose a wrong place

But other than that I liked it.

1. I'm working on that, I'm just not to that level yet
2. Yes I know that's why I'm a beginner :P
3. Yah, that's my next step, make it so that dumb people get a second chance hahahahhaha :D

Thanks tho :)

Share this post


Link to post
Share on other sites
Here I wrote a version that fixes two and three. I haven't put AI in yet.


#include <iostream>
#include <cstdlib>

using namespace std;

// DEFINES
#define VERSION 0.1

// ENUMS STRUCTS
enum SQUARE { SQ_NONE, SQ_X, SQ_O };

// FUNCTIONS

// GLOBALS
SQUARE board[3][3];

void Init_Board()
{
for (int i = 0; i<3; ++i)
for (int j=0; j<3; ++j)
board[j] = SQ_NONE;
}

void Draw_Board()
{
for (int i = 0; i<3; ++i)
{
for (int j=0; j<3; ++j)
{
switch (board[j])
{
case SQ_NONE:
cout << 3*i+j+1;
break;
case SQ_X:
cout << 'X';
break;
case SQ_O:
cout << 'O';
break;
default:
cout << '!'; // shouldn't happen
break;
}
if ( j < 2 )
cout << '|';
}
if (i < 2 )
cout << endl << "-----" << endl;
else
cout << "\n\n";
}
}

int Get_Input( SQUARE playerType )
{
int selection = -1;
int playerNumber = 0;

if (playerType == SQ_X)
playerNumber = 1;
else if (playerType == SQ_O)
playerNumber = 2;
else
{
cout << "Fatal Program Error... exiting";
exit(1);
}

while ( (selection<0) || (selection>9) )
{
cout << "Player " << playerNumber << " Enter Your Move: ";
cin >> selection;

if ( cin.good() == false )
{
cout << "Please enter a valid number." << "\n\n";
cin.clear();
cin.ignore( 80, '\n');
continue;
}

if ( selection == 0 )
{
cout << "Thank you for playing Tic Tac Toe!";
exit(0);
}
cout << endl;
}

return selection;
}

void Victory( SQUARE type )
{
int playerNumber = 0;

if ( type == SQ_X )
playerNumber = 1;
else if (type == SQ_O)
playerNumber = 2;
else
return;

cout << "Player " << playerNumber << " Wins!!!" << endl;
cout << "Congratulations!" << endl;

system("pause");
exit(0);

}

void Check_For_Win( int x, int y )
{
// diagonals
if ( board[1][1] != SQ_NONE )
{
if ((board[0][0]==board[1][1] && board[1][1]==board[2][2])||
(board[1][1]==board[0][2] && board[1][1]==board[2][0]))
Victory(board[1][1]);
}

//up and down
if ( board[y][0]==board[y][1] && board[y][1]==board[y][2] )
Victory(board[y][0]);

// left right
if ( board[0][x]==board[1][x] && board[1][x]==board[2][x] )
Victory(board[0][x]);
}
// returns 1 if error occurred, 0 if successful
int Play_Choice( int place, SQUARE type )
{
--place;
int y = place/3;
int x = place%3;

if ( board[y][x] == SQ_NONE ) // is something in the spot?
board[y][x] = type;
else
return true;

Check_For_Win( x, y);

return false;
}





int main( int argc, char * argv[] )
{
// init
Init_Board();

// variables
SQUARE currentTurn = SQ_X;
int currentChoice = 1;
int numberPlays = 0;

// header
cout << "Welcome to Tic Tac Toe v." << VERSION << endl;
cout << "Enter 0 (zero) at any time to quit." << "\n\n";

// main loop
while (true)
{
Draw_Board();

currentChoice = Get_Input(currentTurn);

if ( !Play_Choice( currentChoice, currentTurn ) ) // if no error occurred
{
if ( currentTurn == SQ_X ) // switch turns
currentTurn = SQ_O;
else
currentTurn = SQ_X;

++numberPlays; // increment number of plays
if ( numberPlays > 8) // check for cat's game
break;
}
}

cout << "CAT'S GAME! HOHOHO!" << endl;
system("pause");
return 0;
}




edit: added error checking for input

Share this post


Link to post
Share on other sites
Quote:
Original post by Rydis
also for another sidenote on bugs, it requiresme to press enter twice to quick not just once.

Okay thanks :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!