i was bored so i tried making a tic-tac-toe game in dos using c++ and i am having trouble finding when the person wins the game.
i dont know if this makes me dumb for not knowing this but it seams that you cannot use theCode://Dan Kemper ([email protected])
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
const int MAXROW = 3;
const int MAXCOL = 3;
int main()
{
//declare variables
int k=0, j=0, z=1, row=0, col=0, win=0;
int input=0;
char p1[40],p2[40];
//initialize array
char arr[MAXROW][MAXCOL] =
{
{ '-', '-', '-', },
{ '-', '-', '-', },
{ '-', '-', '-', }
};
cout << "This is a tic-tac-toe game that I made.\n\n"; //program decription
cout << "Press a ket to continue...";
getch();
system("cls");
//user input for names
cout << "Player 1, enter your name: ";
cin.get(p1,40);
cin.ignore(80,'\n');
cout << "\n\nPlayer 2, enter your name: ";
cin.get(p2,40);
cin.ignore(80,'\n');
cout << "\n\nPress a key to continue...";
getch();
system("cls");
while(1) //main loop; looks like infinate but is later broken using break;
{
while(z>=2) //internal loop
{
//prints out array
for (k = 0; k < MAXROW; k++)
{
for (j = 0; j < MAXCOL; j++)
{
cout << arr[k][j] << " ";
}
cout << endl;
}
if(z=1) //if its player 1's turn
{
cout << p1 << ", enter your choice:" << endl << endl;
cout << "Row: ";
cin >> row; //input for row
cout << endl << "Coulumn: ";
cin >> col; //input for column
arr[row][col]='X'; //puts an X in the spot the user wants
}
else //if player 2's turn
{
cout << p1 << ", enter your choice:" << endl << endl;
cout << "Row: ";
cin >> row; //input for row
cout << endl << "Coulumn: ";
cin >> col; //input for column
arr[row][col]='O'; //puts an O in the spot the user wants
}
z++; //increments variable which controld which persons turn it is
/* decides if player 1 wins */
if(arr[1][1]='X' && arr[1][2]='X' && arr[1][3]='X')
{
cout << endl << p1 << " wins!!!";
z=-1;
break;
}
if(arr[2][1]='X' && arr[2][2]='X' && arr[2][3]='X')
{
cout << endl << p1 << " wins!!!";
z=-1;
break;
}
if(arr[3][1]='X' && arr[3][2]='X' && arr[3][3]='X')
{
cout << endl << p1 << " wins!!!";
z=-1;
break;
}
if(arr[1][1]='X' && arr[2][2]='X' && arr[3][3]='X')
{
cout << endl << p1 << " wins!!!";
z=-1;
break;
}
if(arr[1][3]='X' && arr[2][2]='X' && arr[3][1]='X')
{
cout << endl << p1 << " wins!!!";
z=-1;
break;
}
/* decides if player 2 wins */
if(arr[1][1]='O' && arr[1][2]='O' && arr[1][3]='O')
{
cout << endl << p2 << " wins!!!";
z=-1;
break;
}
if(arr[2][1]='O' && arr[2][2]='O' && arr[2][3]='O')
{
cout << endl << p2 << " wins!!!";
z=-1;
break;
}
if(arr[3][1]='O' && arr[3][2]='O' && arr[3][3]='O')
{
cout << endl << p2 << " wins!!!";
z=-1;
break;
}
if(arr[1][1]='O' && arr[2][2]='O' && arr[3][3]='O')
{
cout << endl << p2 << " wins!!!";
z=-1;
break;
}
if(arr[1][3]='O' && arr[2][2]='O' && arr[3][1]='O')
{
cout << endl << p2 << " wins!!!";
z=-1;
break;
}
}
if(z=-1)
{
break;
}
z=1;
}
cout << endl << endl << "Press ESC to exit...";
while(input!=27)
{
input=getch();
}
return 0;
}
to make the comparison and find the winner. maybe i should make that whole section a comment and debugg the rest of the program and then find an alternative way to do this afterward?Code:if(arr[1][1]='X' && arr[1][2]='X' && arr[1][3]='X')
any help is much appreciated. thanks.