Need Help With the program ASAP!!!!

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)

tictactoe.cpp file
____________________________________________________________________________
#include <iostream>
#include <ctype.h>
#include <ctime>
#include <fstream>
#include <string>
#include <Windows.h>
using namespace std;
// function prototypes
void loadst(string [],string []);
void play(string [],string []);
void drawsq(void);
int pickst(void);

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);
}

infile.close();
}
/******************************** PLAY FUNCTION*/
void play(string states[],string caps[])
{
int gameover,iplayer,chosen, count,i,stpick;
int boxxy[2][9] = {41,45,49,41,45,49,41,45,49,2,2,2,5,5,5,8,8,8};
string answer,blanks=" ";
char player[2] = {'X','O'};
COORD coord;
gameover = count = 0;
while(!gameover)
{
coord.X=1; coord.Y=18;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
cout << blanks;

coord.X=1; coord.Y=14;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
iplayer=count%2;
cout<<"PLAYER = "<<player[iplayer];

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(" ");

//gotoxy(boxxy[0][chosen-1],boxxy[1][chosen-1]);
coord.X=boxxy[0][chosen-1]; coord.Y= boxxy[1][chosen-1];
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
if (answer==caps[stpick])
cout<< player[iplayer];
else
cout<< player[1-iplayer];
count++;
if (count==9)
gameover = 1;
}
}
__________________________________________________________________


usstates.dat file

__________________________________________________________________
MAINE 1124660 33215 1820 23 AUGUSTA ME 05000
NEW HAMPSHIRE 920610 9304 1788 09 CONCORD NH 03900
VERMONT 511456 9609 1791 14 MONTPELIER VT 06000
MASSACHUSETTS 5737037 8257 1788 06 BOSTON MA 02800
RHODE ISLAND 947154 1214 1790 13 PROVIDENCE RI 03000
CONNECTICUT 3107576 5009 1788 05 HARTFORD CT 07000
NEW YORK 17557288 49576 1788 11 ALBANY NY 15000
PENNSYLVANIA 11866728 45333 1787 02 HARRISBURG PA 19700
DELAWARE 595225 2057 1787 01 DOVER DE 20000
MARYLAND 4216446 10577 1788 07 ANNAPOLIS MD 22000
VIRGINIA 5346279 40815 1788 10 RICHMOND VA 24700
NORTH CAROLINA 5874429 52712 1789 12 RALEIGH NC 29000
SOUTH CAROLINA 3119208 31055 1788 08 COLUMBIA SC 30000
GEORGIA 5464265 58876 1788 04 ATLANTA GA 32000
FLORIDA 9739992 58560 1845 27 TALLAHASSEE FL 35000
MICHIGAN 9258344 58216 1837 26 LANSING MI 50000
OHIO 10797419 41222 1803 17 COLUMBUS OH 46000
INDIANA 5490179 36291 1816 19 INDIANAPOLIS IN 48000
ILLINOIS 11418461 56400 1818 21 SPRINGFIELD IL 63000
WISCONSIN 4705335 56154 1848 30 MADISON WI 55000
KENTUCKY 3661433 40395 1792 15 FRANKFORT KY 43000
TENNESSEE 4590750 42244 1796 16 NASHVILLE TN 38600
ALABAMA 3890061 51609 1819 22 MONTGOMERY AL 37000
MISSISSIPPI 2520638 47716 1817 20 JACKSON MS 40000
LOUISIANA 4203972 48523 1812 18 BATON ROUGE LA 71600
ARKANSAS 2285513 53104 1836 25 LITTLE ROCK AR 73000
MISSOURI 4917444 69686 1821 24 JEFFERSON CITY MO 66000
IOWA 2913387 56290 1846 29 DES MOINES IA 53000
MINNESOTA 4077148 84068 1858 32 SAINT PAUL MN 57000
MONTANA 786690 147138 1889 41 HELENA MT 60000
NORTH DAKOTA 652695 70665 1889 40 BISMARCK ND 59000
SOUTH DAKOTA 690178 77047 1889 39 PIERRE SD 58000
WYOMING 470816 97914 1890 44 CHEYENNE WY 83200
COLORADO 2888834 104247 1876 38 DENVER CO 82000
NEBRASKA 1570006 77227 1867 37 LINCOLN NE 70000
KANSAS 2363208 82264 1861 34 TOPEKA KS 68000
OKLAHOMA 3025266 69919 1907 46 OKLAHOMA CITY OK 75000
TEXAS 14228383 267339 1845 28 AUSTIN TX 80000
NEW MEXICO 1299968 121666 1912 47 SANTA FE NM 89000
ARIZONA 2717866 113909 1912 48 PHOENIX AZ 87000
UTAH 1461037 84916 1896 45 SALT LAKE CITY UT 85000
NEVADA 799184 110540 1864 36 CARSON CITY NV 90000
IDAHO 943935 83557 1890 43 BOISE ID 84000
WEST VIRGINIA 1949644 24181 1863 35 CHARLESTON WV 27000
WASHINGTON 4130163 68192 1889 42 OLYMPIA WA 99500
OREGON 2632663 96981 1859 33 SALEM OR 98000
CALIFORNIA 23668562 158693 1850 31 SACRAMENTO CA 96700
ALASKA 400481 586400 1959 49 JUNEAU AK 99999
NEW JERSEY 7364158 7836 1787 03 TRENTON NJ 09000
HAWAII 965000 6424 1959 50 HONOLULU HI 96900

_____________________________________________________________________

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.



How are you going to tell if the state name or the city name consists of one or two words?

A row has either 8 or 9 columns. 8 columns mean state name is 1 word, 9 columns means state name has 2 words.
A row has either 8 or 9 columns. 8 columns mean state name is 1 word, 9 columns means state name has 2 words.

Not reliable. 9 columns could also mean the capital has 2 words.

Not reliable. 9 columns could also mean the capital has 2 words.


Not in the file he showed us.
How are you going to parse this one?

UTAH 1461037 84916 1896 45 SALT LAKE CITY UT 85000
This is the file with suitable format tags applied. The spacing is exactly as supplied by the OP.

usstates.dat file

MAINE           1124660  33215 1820 23 AUGUSTA        ME 05000
NEW HAMPSHIRE    920610   9304 1788 09 CONCORD        NH 03900
VERMONT          511456   9609 1791 14 MONTPELIER     VT 06000
MASSACHUSETTS   5737037   8257 1788 06 BOSTON         MA 02800
RHODE ISLAND     947154   1214 1790 13 PROVIDENCE     RI 03000
CONNECTICUT     3107576   5009 1788 05 HARTFORD       CT 07000
NEW YORK       17557288  49576 1788 11 ALBANY         NY 15000
PENNSYLVANIA   11866728  45333 1787 02 HARRISBURG     PA 19700
DELAWARE         595225   2057 1787 01 DOVER          DE 20000
MARYLAND        4216446  10577 1788 07 ANNAPOLIS      MD 22000
VIRGINIA        5346279  40815 1788 10 RICHMOND       VA 24700
NORTH CAROLINA  5874429  52712 1789 12 RALEIGH        NC 29000
SOUTH CAROLINA  3119208  31055 1788 08 COLUMBIA       SC 30000
GEORGIA         5464265  58876 1788 04 ATLANTA        GA 32000
FLORIDA         9739992  58560 1845 27 TALLAHASSEE    FL 35000
MICHIGAN        9258344  58216 1837 26 LANSING        MI 50000
OHIO           10797419  41222 1803 17 COLUMBUS       OH 46000
INDIANA         5490179  36291 1816 19 INDIANAPOLIS   IN 48000
ILLINOIS       11418461  56400 1818 21 SPRINGFIELD    IL 63000
WISCONSIN       4705335  56154 1848 30 MADISON        WI 55000
KENTUCKY        3661433  40395 1792 15 FRANKFORT      KY 43000
TENNESSEE       4590750  42244 1796 16 NASHVILLE      TN 38600
ALABAMA         3890061  51609 1819 22 MONTGOMERY     AL 37000
MISSISSIPPI     2520638  47716 1817 20 JACKSON        MS 40000
LOUISIANA       4203972  48523 1812 18 BATON ROUGE    LA 71600
ARKANSAS        2285513  53104 1836 25 LITTLE ROCK    AR 73000
MISSOURI        4917444  69686 1821 24 JEFFERSON CITY MO 66000
IOWA            2913387  56290 1846 29 DES MOINES     IA 53000
MINNESOTA       4077148  84068 1858 32 SAINT PAUL     MN 57000
MONTANA          786690 147138 1889 41 HELENA         MT 60000
NORTH DAKOTA     652695  70665 1889 40 BISMARCK       ND 59000
SOUTH DAKOTA     690178  77047 1889 39 PIERRE         SD 58000
WYOMING          470816  97914 1890 44 CHEYENNE       WY 83200
COLORADO        2888834 104247 1876 38 DENVER         CO 82000
NEBRASKA        1570006  77227 1867 37 LINCOLN        NE 70000
KANSAS          2363208  82264 1861 34 TOPEKA         KS 68000
OKLAHOMA        3025266  69919 1907 46 OKLAHOMA CITY  OK 75000
TEXAS          14228383 267339 1845 28 AUSTIN         TX 80000
NEW MEXICO      1299968 121666 1912 47 SANTA FE       NM 89000
ARIZONA         2717866 113909 1912 48 PHOENIX        AZ 87000
UTAH            1461037  84916 1896 45 SALT LAKE CITY UT 85000
NEVADA           799184 110540 1864 36 CARSON CITY    NV 90000
IDAHO            943935  83557 1890 43 BOISE          ID 84000
WEST VIRGINIA   1949644  24181 1863 35 CHARLESTON     WV 27000
WASHINGTON      4130163  68192 1889 42 OLYMPIA        WA 99500
OREGON          2632663  96981 1859 33 SALEM          OR 98000
CALIFORNIA     23668562 158693 1850 31 SACRAMENTO     CA 96700
ALASKA           400481 586400 1959 49 JUNEAU         AK 99999
NEW JERSEY      7364158   7836 1787 03 TRENTON        NJ 09000
HAWAII           965000   6424 1959 50 HONOLULU       HI 96900
OK, I admit defeat.
@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)
            return true;
    //  Cols
    for (int c=0; c<3; c++)
        if (board[0][c] == player && board[1][c] == player && board[2][c] == player)
            return true;
    //  Diagonals
    if (board[0][0] == player && board[1][1] == player && board[2][2] == player)
            return true;
    if (board[0][2] == player && board[1][1] == player && board[2][0] == player)
            return true;
    return false;   //  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)
1
2
3
4
void upshift (string & ans)
{   for (size_t i=0; i<ans.size(); i++)
        ans[i] = toupper(ans[i]);
}



Last edited on
Topic archived. No new replies allowed.