### I finally made it! What do you think?

Finally created my Connect 4 game! What do you guys think? Do I use too many if statements in my winCondition?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187`` ``````#include #include #include using namespace std; void timer(unsigned short i){ clock_t delay = i*CLOCKS_PER_SEC; clock_t start = clock(); while(clock() - start < delay); } class ConnectFour{ public: ConnectFour(){markerX = 'X', markerY = 'Y';} void assignBoard(char multiArray[][7]){ for(int r=0; r<6; r++){ for(int c=0; c<7; c++){ multiArray[r][c] = 'O'; } } } void displayBoard(char multiArray[][7]){ for(int r=0; r<6; r++){ cout << " "; for(int c=0; c<7; c++){ cout << multiArray[r][c] << "|"; } cout << endl; } cout << " " << "1 2 3 4 5 6 7" << endl; } void turn(unsigned short& loop){ cout << endl; if(loop%2 == 0) cout << " " << "Player X: "; else cout << " " << "Player Y: "; } bool errHandle(int& value){ if(!cin){ cout.put('\a'); system("cls"); system("color 1A"); cout << "ERROR: Non-integer value has been registered!\n\nTERMINATING\n"; exit(EXIT_FAILURE); } else if(value <= 0 || value > 7){ system("cls"); cout << "Please only enter values between 1 and 7\n"; timer(2); system("cls"); return true; } } void pos(char multiArray[][7], int& value, unsigned short& loop){ char currentPlayerMarker; if(loop%2 == 0) currentPlayerMarker = markerX; else currentPlayerMarker = markerY; for(int row = 5; row >=0; row--){ if(multiArray[row][value-1] == 'O'){ multiArray[row][value-1] = currentPlayerMarker; break; } } } bool boardLimit(char multiArray[][7], int& value){ short counter = 1; for(int c=0; c<6; c++){ if(markerX == multiArray[0][c] && value == counter || markerY == multiArray[0][c] && value == counter){ system("cls"); cout << "Row " << value << " is filled, pick another.\n"; timer(3); system("cls"); return true; } counter++; } } void winCondition(char multiArray[][7]){ bool checkX = false, checkY = false; /* Checking condition vertically */ for(int row=5; row>=0; row--){ for(int column=0; column<4; column++){ if(multiArray[row][column] == markerX && multiArray[row][column+1] == markerX && multiArray[row][column+2] == markerX && multiArray[row][column+3] == markerX){ checkX = true; break; } else if(multiArray[row][column] == markerY && multiArray[row][column+1] == markerY && multiArray[row][column+2] == markerY && multiArray[row][column+3] == markerY){ checkY = true; break; } } if(checkX == true || checkY == true) break; } /* Checking condition horizontally */ for(int column=0; column<6; column++){ for(int row=5; row>2; row--){ if(multiArray[row][column] == markerX && multiArray[row-1][column] == markerX && multiArray[row-2][column] == markerX && multiArray[row-3][column] == markerX){ checkX = true; break; } else if(multiArray[row][column] == markerY && multiArray[row-1][column] == markerY && multiArray[row-2][column] == markerY && multiArray[row-3][column] == markerY){ checkY = true; break; } } if(checkX == true || checkY == true) break; } /* Checking condition obliquelly */ for(int row=5; row>=0; row--){ for(int column=0; column<=3; column++){ if(multiArray[row][column] == markerX && multiArray[row-1][column+1] == markerX && multiArray[row-2][column+2] == markerX && multiArray[row-3][column+3] == markerX){ checkX = true; break; } else if(multiArray[row][column] == markerY && multiArray[row-1][column+1] == markerY && multiArray[row-2][column+2] == markerY && multiArray[row-3][column+3] == markerY){ checkY = true; break; } } if(checkX == true || checkY == true) break; } /* Checking condition reversed obliquelly */ for(int row=5; row>=0; row--){ for(int column=6; column>=3; column--){ if(multiArray[row][column] == markerX && multiArray[row-1][column-1] == markerX && multiArray[row-2][column-2] == markerX && multiArray[row-3][column-3] == markerX){ checkX = true; break; } else if(multiArray[row][column] == markerY && multiArray[row-1][column-1] == markerY && multiArray[row-2][column-2] == markerY && multiArray[row-3][column-3] == markerY){ checkY = true; break; } } if(checkX == true || checkY == true) break; } if(checkX == true){ system("cls"); system("color 2B"); displayBoard(multiArray); cout << "\n Player X wins!\n"; exit(EXIT_SUCCESS); } else if(checkY == true){ system("cls"); system("color 2B"); displayBoard(multiArray); cout << "\n Player Y wins!\n"; exit(EXIT_SUCCESS); } } private: char markerX, markerY; }; main(){ char board[6][7]; unsigned short loop = 0; int value; ConnectFour game; game.assignBoard(board); while(loop < 42){ game.displayBoard(board); game.turn(loop); (cin >> value).get(); if(game.errHandle(value) == true) continue; else if(game.boardLimit(board, value) == true) continue; game.pos(board, value, loop); game.winCondition(board); loop++; system("cls"); } system("cls"); game.displayBoard(board); cout << "\n IT'S A DRAW\n"; }``````
closed account (3qX21hU5)
Congrats!

I do have some suggestions though.

1) Move the definitions of the members functions outside of the class.

2) main() always returns int. You main function is missing a return type.

3) Take care of all the warnings from the compiler like putting parenthesis around the '&&' in the || on this
`if(markerX == multiArray[0][c] && value == counter || markerY == multiArray[0][c] && value == counter) `.

4) Some of your member functions might be able to reach the end of the function without returning anything. Like in `bool boardLimit(char multiArray[][7], int& value)`. What if it reaches the end of that function? What will it return?

Them are just some things I see from the a little search. Might take a better in depth look through it later.

Thanks for the opinion ;)

The reason why I didn't put "int" infront of main(), is because in Code::blocks it isn't neccessary to do it.

Also what do you mean with 1)? Moving the definitions of the members functions? Like prototyping them outside of the class?
Just because something isn't "necessary" doesn't mean you shouldn't do it that's just being lazy =p
Programmers ARE lazy ;)
 Also what do you mean with 1)? Moving the definitions of the members functions? Like prototyping them outside of the class?

You have:
 ``1234567`` ``````class Foo { public: void func() const { std::cout << "Hello\n" } };``````

Separate it to:
 ``1234567891011`` ``````// Definition class Foo { public: void func() const; }; // Implementation void Foo::func() const { std::cout << "Hello\n" }``````

Then you can move the implementation into a separate Foo.cpp source file and save the Definition as Foo.h header file (but add inclusion guards).

Every project that uses class Foo can then simply include the Foo.h and link in the object file created from Foo.cpp.

I agree with all the above, including the one about int main() -- even though the GCC compiler allows you to be lazy, for backward compatibility reasons, you should get into the habit of usig the ANSI compliant forms of main: either int main() or int main(int argc, char* argv[]), with a return!

1. Long lines of code should be folded (this helps make them more readable not only in your IDE but here on cplusplus.com)

 ``12345678`` `````` /* Checking condition obliquelly */ for(int row=5; row>=0; row--){ for(int column=0; column<=3; column++){ if(multiArray[row][column] == markerX && multiArray[row-1][column+1] == markerX && multiArray[row-2][column+2] == markerX && multiArray[row-3][column+3] == markerX){ checkX = true; break; }``````

or even

 ``12345678910`` `````` /* Checking condition obliquelly */ for(int row=5; row>=0; row--){ for(int column=0; column<=3; column++){ if(multiArray[row][column] == markerX && multiArray[row-1][column+1] == markerX && multiArray[row-2][column+2] == markerX && multiArray[row-3][column+3] == markerX){ checkX = true; break; }``````

rather than

 ``1234567`` `````` /* Checking condition obliquelly */ for(int row=5; row>=0; row--){ for(int column=0; column<=3; column++){ if(multiArray[row][column] == markerX && multiArray[row-1][column+1] == markerX && multiArray[row-2][column+2] == markerX && multiArray[row-3][column+3] == markerX){ checkX = true; break; }``````

(A limit of 80 characters per line was common in the past, when screens were smaller and people mostly used console-based text editors. In the days of IDEs and large screens, you can use slightly longer rows, but still want to be able to see your code, the debugger output, and you app's window all at the same time -- and be able to see the whole line of code without scrolling.)

2. C++ coders usually use

`if(checkX){`

rather than

`if(checkX == true){`

as checkX is already a boolean, you don't need to compare it against true. The result of operator== is a bool value. e.g. `value = (checkZ == true)` --> true or false

also, it save a bit of typing (you did say you're lazy!)

3. Some of the variable and function names could be improved.

All variable names should say what they are, function names what they do.

e.g. checkX -> check what?

hasLineX might be better?

And "displayBoard" is a good function name, but "turn" is not. Turn what?

4. You should use pre-increment rather than post-increment

There are cases where the post-increment for performs worse, so it's a good habit to use ++i rather than i++

Same deal for decrement.

5. You could replace literals with meaningful contants

const int numRows = 6;
const int numCols = 7;

char board[numRows][numCols];

rather than

char board[6][7];

and then use them in you loops.

6. From a object oriented point of view, rather than a C++ syntax point of view... If you plan on a version 2 of you game, you should consider factoring out the board specific code into its own (Board) class, rather than using the char array directly (ie encapsulate the board).

Andy
Last edited on
I thought you use `int main( int argc , char **argv )` instead of *argv[] if you don't need access to items in argv think I read that somewhere on the internet could be wrong though was a while back
Last edited on
Topic archived. No new replies allowed.