Help in fixing my Tic-Tac-Toe
May 23, 2012 at 5:02am UTC
At the moment im a beginner at programming and i wanted C++ to be my first langauges.Tic Tac Toe program isnt functioning the way i want it to do.
key issues:
-wont allow me to stop after winning until the for loop closes
-wont allow me to only using one allocated slot for each X and O.overlaps if i use the same coordinates in the program.
-and it wont allow me to stop the game after someone wins.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
char tic [3][3];
int row,col;
char X ='x' ;
char O = 'o' ;
int count = 0;
for ( int x = 0; x < 3; x++)
{
for ( int n = 0; n < 3; n++)
{
tic[x][n] = '#' ;
}
}
cout << "Welcome to Tic-Tac-Toe" << endl;
cout << "player 1 X starts first" << endl;
for ( int i = 0; i < 3 ; i ++)
{
for ( int p = 0; p < 3 ; p++)
{
count++;
cout << "row coordinate between (0-2):" ;
cin >> row;
while ( row < 0 || row > 2)
{
cout << " try again." ;
cin >> row;
}
cout << " Column coordinate between (0-2):" ;
cin >> col;
while ( col < 0 || col > 2)
{
cout << " try again." ;
cin >> col;
}
if ((count%2) != 0)
{
tic[row][col] = X;
if ( (tic[0][0] == 'x' ) && (tic[1][1] == 'x' && tic[2][2] == 'x' ))
{
cout << "Player 1 wins!" << endl;
break ;
}
else if ( (tic[0][1] == 'x' ) && (tic[0][2] == 'x' && tic[0][0] == 'x' ))
{
cout << "Player 1 wins!" << endl;
break ;
}
else if ( (tic[1][0] == 'x' ) && (tic[1][1] == 'x' && tic[1][2] == 'x' ))
{
cout << "Player 1 wins!" << endl;
break ;
}
else if ( (tic[2][0] == 'x' ) && (tic[2][1] == 'x' && tic[2][2] == 'x' ))
{
cout << "Player 1 wins!" << endl;
break ;
}
else if ( (tic[0][0] == 'x' ) && (tic[1][0] == 'x' && tic[2][0] == 'x' ))
{
cout << "Player 1 wins!" << endl;
break ;
}
else if ( (tic[1][0] == 'x' ) && (tic[1][1] == 'x' && tic[2][1] == 'x' ))
{
cout << "Player 1 wins!" << endl;
break ;
}
else if ( (tic[2][0] == 'x' ) && (tic[2][1] == 'x' && tic[2][2] == 'x' ))
{
cout << "Player 1 wins!" << endl;
break ;
}
else if ( (tic[2][0] == 'x' ) && (tic[1][1] == 'x' && tic[0][2] == 'x' ))
{
cout << "Player 1 wins!" << endl;
break ;
}
}
else if (( count%2) == 0)
{
tic[row][col] = O;
if ( (tic[0][0] == 'o' ) && (tic[1][1] == 'o' && tic[2][2] == 'o' ))
{
cout << "Player 2 wins!" << endl;
break ;
}
else if ( (tic[0][1] == 'o' ) && (tic[0][2] == 'o' && tic[0][0] == 'o' ))
{
cout << "Player 2 wins!" << endl;
break ;
}
else if ( (tic[1][0] == 'o' ) && (tic[1][1] == 'o' && tic[1][2] == 'o' ))
{
cout << "Player 2 wins!" << endl;
break ;
}
else if ( (tic[2][0] == 'o' ) && (tic[2][1] == 'o' && tic[2][2] == 'o' ))
{
cout << "Player 2 wins!" << endl;
break ;
}
else if ( (tic[0][0] == 'o' ) && (tic[1][0] == 'o' && tic[2][0] == 'o' ))
{
cout << "Player 2 wins!" << endl;
break ;
}
else if ( (tic[1][0] == 'o' ) && (tic[1][1] == 'o' && tic[2][1] == 'o' ))
{
cout << "Player 2 wins!" << endl;
break ;
}
else if ( (tic[2][0] == 'o' ) && (tic[2][1] == 'o' && tic[2][2] == 'o' ))
{
cout << "Player 2 wins!" << endl;
break ;
}
else if ( (tic[2][0] == 'o' ) && (tic[1][1] == 'o' && tic[0][2] == 'o' ))
{
cout << "Player 2 wins!" << endl;
break ;
}
}
cout << tic[0][0] << " " << tic[0][1] << " " << tic[0][2] << endl;
cout << tic[1][0] << " " << tic[1][1] << " " << tic[1][2] << endl;
cout << tic[2][0] << " " << tic[2][1] << " " << tic[2][2] << endl;
}
}
return (0);
}
May 23, 2012 at 6:25am UTC
put all ur game code in a function like playtictactoe. call it from main function, once the player wins print win message and call return;
Topic archived. No new replies allowed.