The following features need to be added. to program.
1. The if statements to decide when the game is won by a player having 3 squares in a row.(2 pts)
2. Do not allow a state to be chosen more than once during a game.(1 pt)
3. Do not allow a square to be chosen more than once during a game.(1 pt)
4. Make the matching of the answer to the question case insensitive. .(1 pt)
int main()
{
string states[50],caps[50];
char choice='y';
srand(time(NULL)); /* seed the random number sequence */
loadst(states,caps); /* get input data*/
system("cls");
while(tolower(choice)=='y')
{
drawsq();
play(states,caps);
COORD coord = {1, 20};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
cout<< "DO YOU WANT TO PLAY AGAIN? (Y/N) ";
cin >> choice;
}
}
/**************************** DRAW SQUARE FUNCTION */
void drawsq(void)
{
string board[9] = {"1 |2 |3 ",
" | | ",
"---+---+---",
"4 |5 |6 ",
" | | ",
"---+---+---",
"7 |8 |9 ",
" | | " };
int i,x,y;
system("cls");
y=0;
x=40;
for (i=0;i<9;i++)
{
y++;
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
cout<<board[i];
}
}
/***************************** PICK RANDOM STATE */
int pickst()
{
return(rand()%50);
}
/**************************** LOAD STATES FUNCTION */
void loadst(string states[],string caps[] )
{
ifstream infile;
string temp;
infile.open("G:\\CS1K\\usstates.dat");
for (int i=0;i<50;i++)
{
getline(infile,temp);
states[i]=temp.substr(0,14);
caps[i]=temp.substr(39,14);
}
coord.X=1; coord.Y=16;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
cout << blanks;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
cout<<"WHICH BOX DO YOU CHOOSE? ";
cin >> chosen;
cin.ignore();
stpick=pickst();
coord.X=1; coord.Y=18;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
cout << "THE CAPITAL OF " << states[stpick] << " IS ? ";
getline(cin,answer);
for (i=answer.length(); i<14; i++)
answer.append(" ");
With no delimiters between fields, you're going to have a hard time parsing that file unless the data is in fixed columns, which is not apparent from what you posted.
How are you going to tell if the state name or the city name consists of one or two words?
Line 69-70: You're assuming the state name and capital are exactly 14 characters at that the capital always starts in column 39.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
@OP - I strongly recommend isolating your operating system specific calls to a single function. This will make it easier if you ever port your program to another OS or graphics environment.
1 2 3 4 5
// Put all OS specific calls in one place
void gotoxy (int x, int y)
{ COORD coord = {x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
1. The if statements to decide when the game is won by a player having 3 squares in a row.(2 pts)
You don't have a representation in your program of which cells have been played by each player. You're going to need that to determine who the winner is. Typically in a tic-tac-toe program, the board is represented as:
char board[3][3];
This allows you to check the winner as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
bool check_winner (char board[3][3], char player)
{ // Rows
for (int r=0; r<3; r++)
if (board[r][0] == player && board[r][1] == player && board[r][2] == player)
returntrue;
// Cols
for (int c=0; c<3; c++)
if (board[0][c] == player && board[1][c] == player && board[2][c] == player)
returntrue;
// Diagonals
if (board[0][0] == player && board[1][1] == player && board[2][2] == player)
returntrue;
if (board[0][2] == player && board[1][1] == player && board[2][0] == player)
returntrue;
returnfalse; // Player is not a winner
}
2. Do not allow a state to be chosen more than once during a game.(1 pt)
The easiest way to do this is to keep a parallel array of bools to indicate which states have been chosen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
bool chosen[50]; // Initialize each entry to false
..
// pickst becomes:
int pickst (bool chosen[])
{ int r;
while (true)
{ r = rand()%50;
if (chosen[r])
continue;
chosen[r] = true;
return r;
}
}
3. Do not allow a square to be chosen more than once during a game.(1 pt)
Again, this is a problem because you don;t have an internal representation of the board. Assuming board[3][3], checking for already chosen square becomes easy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int choose_square (char board[3][3])
{ int r, c, chosen;
while (true)
{ cout<<"WHICH BOX DO YOU CHOOSE? ";
cin >> chosen;
if (chosen < 1 || chosen > 9)
{ cout << "Box is out of range" << endl;
continue;
}
r = (chosen-1)/3;
c = (chosen-1)%3;
if (board[r][c] != ' ')
{ cout << "That box is occupied" << endl;
continue;
}
return chosen;
}
4. Make the matching of the answer to the question case insensitive. .(1 pt)