okay now it ran, but for some reason, the entire loop was completely skipped over. that cannot be since while(1) and the only way to get out of the whole thing is if the if statements inside are true which have break; in them to get out of the loop. i never get asked for the row or column at all. it asks for names and goes to press ESC to exit...
Code:
//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 key 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+1][col+1]='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+1][col+1]='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[0][0]=='X' && arr[0][1]=='X' && arr[0][2]=='X')
{
cout << endl << p1 << " wins!!!";
z=-1;
break;
}
if(arr[1][0]=='X' && arr[1][1]=='X' && arr[1][2]=='X')
{
cout << endl << p1 << " wins!!!";
z=-1;
break;
}
if(arr[2][0]=='X' && arr[2][1]=='X' && arr[2][2]=='X')
{
cout << endl << p1 << " wins!!!";
z=-1;
break;
}
if(arr[0][0]=='X' && arr[1][1]=='X' && arr[2][2]=='X')
{
cout << endl << p1 << " wins!!!";
z=-1;
break;
}
if(arr[0][2]=='X' && arr[1][1]=='X' && arr[2][0]=='X')
{
cout << endl << p1 << " wins!!!";
z=-1;
break;
}
/* decides if player 2 wins */
if(arr[0][0]=='O' && arr[0][1]=='O' && arr[0][2]=='O')
{
cout << endl << p2 << " wins!!!";
z=-1;
break;
}
if(arr[1][0]=='O' && arr[1][1]=='O' && arr[1][2]=='O')
{
cout << endl << p2 << " wins!!!";
z=-1;
break;
}
if(arr[2][0]=='O' && arr[2][1]=='O' && arr[2][2]=='O')
{
cout << endl << p2 << " wins!!!";
z=-1;
break;
}
if(arr[0][0]=='O' && arr[1][1]=='O' && arr[2][2]=='O')
{
cout << endl << p2 << " wins!!!";
z=-1;
break;
}
if(arr[0][2]=='O' && arr[1][1]=='O' && arr[2][0]=='O')
{
cout << endl << p2 << " wins!!!";
z=-1;
break;
}
}
if(z=-1)
{
break;
}
z=1;
}
system("cls");
cout << "Press ESC to exit...";
while(input!=27)
{
input=getch();
}
return 0;
}
thanks for your help.