connect four style game

Pages: 12
Alright, I have to make a code for a connect four type game. The twist is that It's connect any, meaning I need command line arguments that set the dimensions the board and set the number of pieces needed in row to win. Sadly right now I'm stuck at the very beginning of checking that the user has entered the command line arguments correctly. -r for rows, -c for columns, and -p for number of pieces. Right now I can't seem to even get my command line entries into the checking function. Any help is greatly appreciated. If you could let me know what I'm doing wrong that would be great. Here's my code:

connect_any.h (struct)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

struct game
{

        char **board;
        int r, c, p;
        char p1;
        char p2;

};

bool is_valid_arguments( char *info[] );
void set_game_info( game *, char *info[] );
char **create_table( int, int );
void play_game( game * );
bool check_winner( game );
void delete_table( game * );


connect_any.cpp (functions)
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
using namespace std;

/*
 *
 */
bool is_valid_arguments( char *info[] )
{

        cout << endl << "in arg checker" << endl;

        for ( int i = 0; i < 8; ++i )
        {

                if ( info[i] == "-r" )
                        int r = *info[i+1];
                else if ( info[i] == "-c" )
                        int c = *info[i+1];
                else if ( info[i] == "-p" )
                        int p = *info[i+1];
                else
                        cout << "Error!!!!!" << endl;

        }
        cout << endl;
}


play_ca.cpp (main)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdlib>
#include "./connect_any.h"

using namespace std;

int main( int argc, char *argv[] )
{

        int i;

        for ( i = 1; i <= argc; ++i )
                cout << argv[i] << endl;

        is_valid_arguments( &argv[i] );

        return 0;

}


The code compiles and prints the arguments but I can't get into the function for checking for correct arguments. Also that function is probably not right but I'll worry about getting into it first. Any and all help with understanding this problem (and the rest of the code) is greatly appreciated. I really have no idea how to even begin designing the actual game play. I'm getting no real help from TA's and the teacher's busy. I just want to understand what it is I want to do here. Thank again in advance. :)
Your code has multiple issues.

- Your argument checking function blindly assumes that there are at least 8 values in the arguments array. This will lead to undefined behavior in most cases.

- In main, you pass a pointer to the element past the end of the array, instead of just passing the beginning of the array. Your argument checking function never even gets to see the actual arguments because you feed it garbage.
i have the same assignment. im trying to figure out where the *info[] comes from is it created in main then passed to is_valid_argument?
It is supposed to be the value of argv. It is not created in main, it is created by the operating system and passed to main as a parameter.
How do you pass that value? I've tried so many things but nothing I do works.
is_valid_arguments(argv);

Note that after the last character pointer, there is a guaranteed null character pointer. You can take advantage of this to ensure you do not overstep the bounds of the array.
Sweet it didn't seg fault! Still can't get into the function though. So I can only have the six arguments for the parameters and of course the first one is the program which is why I did < 8 in my function. If that leads to undefined behavior, how should I go about telling it that it can't have any more or less than the 7 total arguments?
< n is for n arguments. Remember that the first index is 0 and the last is n-1. Index n is out of bounds.
Hooray! I got into the function. That makes me happy. I guess I'm not to good at counting sometimes. Ok so I apologize for how clueless I am. But now that I made it into the function, it just returns the error message 6 times. I added an exit(1); in the else statement with the error message which fixed it's printing 6 times but it still prints error no matter what is entered. Am I going about the error checking all wrong? Is there a better way that I should go about checking that -r, -c, and -p have been entered and that the argument following each one is a positive integer? Also I don't know if it matters but the -r, -c and -p are supposed to be able to be entered in any order. Then I'm supposed to recover from the case that positive integers aren't entered I thought the error check was something I could figure out on my own but I guess I was wrong. It seems to be different than if I was just prompting the user for the info, but I'm probably wrong on that to. Thanks again for all your help. :)
Last edited on
Please post your current code - I'm not psychic ;)
Oh sorry, here it is:
.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct game
{

        char **board;
        int r, c, p;
        char p1;
        char p2;

};

bool is_valid_arguments( char *info[] );
void set_game_info( game *, char *info[] );
char **create_table( int, int );
void play_game( game * );
bool check_winner( game );
void delete_table( game * );


functions
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
bool is_valid_arguments( char *info[] )
{

        cout << endl << "in arg checker" << endl;

        for ( int i = 0; i < 6; ++i )
        {

                if ( info[i] == "-r" )
                        int r = *info[i+1];
                else if ( info[i] == "-c" )
                        int c = *info[i+1];
                else if ( info[i] == "-p" )
                        int p = *info[i+1];
                else
                {

                        cout << "Error!!!!!" << endl;
                        exit(1);

                }

        }
        cout << endl;

}


main
1
2
3
4
5
6
7
8
9
10
11
12
13
int main( int argc, char *argv[] )
{

        int i;

        for ( i = 1; i <= argc-1; ++i )
                cout << argv[i] << endl;

        is_valid_arguments( argv );

        return 0;

}


output

-r
8
-c
8
-p
4

in arg checker
Error!!!!!


Yeah, my function has to be totally wrong, don't know how to make it right. :)
if ( info[i] == "-r" )

Unfortunately, you are comparing memory addresses instead of strings. (This is why we say character arrays are bad). To fix it, just convert either parameter (or both) to a std::string.

if(std::string(info[i]) == "-r")

if(info[i] == std::string("-r"))
This post was deleted after I viewed it:
JenniferPMocello wrote:
I know you are in CS 162 at Oregon State. ;)
http://www.cplusplus.com/user/JenniferPMocello/

Looks like your professor is internet savvy ;)
Hmm, oh well, TA office hours are always too full to get much help. I'm lucky to get help with more than one line of code in an hour. And I can't make it to her office hours. No one's available on weekends. Truth is I get the best help here. I would think getting help from every resource possible would be encouraged. Guess that's another thing I'm wrong about. I don't see the problem, I'd be asking her and the TA's the exact same questions if I more chances to do so. :)

Dear Jennifer,
Sorry, I'm quite dumb with CS. Despite the several office hours available (which I go to as many as possible as well as talking with my peers), I still need more help. If you look above you'll be pleased to notice I've never once asked anyone to do my code for me. I'm just trying to get extra help understanding so that future assignments won't be impossible. This 1st assignment of the term seems impossible to me. I'm afraid my options are getting more help here, or staring cluelessly at a screen and failing. I'd much rather figure this stuff out. I hope you'll allow me to continue getting extra help. I'm sure you'll let me know on monday. :)
Last edited on
Well I'm not going to worry about that ☝︎. I realized days ago that I'd never finish this code by the time it's due anyway. I haven't even made it past the first function yet. Still want to understand this stuff though...

So anyways, I converted each and both parameters to strings and added the <string> header to both function and main files. Still it just goes straight to the error no matter what. Do I need to convert anything in the body of my if statements to strings as well, but then whats in the body shouldn't matter right?
Please post your modified code so I can see how you changed it - there is always the possibility for miscommunication.
So right now its:
 
if ( string( info[i] ) == string( "-r" ) )


I have using namespace std; in my code but I tried it with and with out the std::. I tried all the combinations of converting the left or right and both parameters.
I just realized, you're not actually doing anything inside those if statements that would affect the behavior of the program. (The compiler probably removes them completely, but that's a story for another day.) Try adding print statement inside your if statements and you should see it is actually working as expected. Remember that those numbers after the arguments don't match any of your if statements.
Yeah it definitely seems like it's ignoring my if statements all together. I tried these print statements in each of my if statements:
1
2
3
4
5
6
7
8
if ( info[i] == string( "-r" ) )
{

        cout << "in if";
        int r = *info[i+1];
        cout << "r = " << r;

}

Still nothings prints. Also, what do mean by the numbers after my arguments not matching my if statements? Do I need more if statements to handle the numbers? Or some other kind of statement?
You are loping through each argument and then exiting your program if you don't find what you are looking for. That's like blowing up your house if you don't find your car keys in the first room you search.

Get rid of the call to exit() and let your program keep running.
Pages: 12