C++/Programming thread

For a project, i’m creating ConnectFour (4 in a row i think its called in the states) and i’ve kind of hit a wall, i’ve got my main menu, my grid built up using arrays, but i’m not acctually sure how to acctually get the user to play the game.

For example how do i update the grid when the user takes there turn?

Also, i’d like to clear the menu away from the screen once the user has made their choice.

Code:

high score is empty because i haven’t gotten round to doing that yet, and exit is a function as far as i’m aware, but not sure if i’ve implamented it right,

If anyone has any adive or tips on how to do any of that, it’d be great.

: )

Reverse-engineer the real thing.

Haha, i know the rules to connect4, just not sure how to program it hah

I’ve actually been programming connect four too for college, in java.

You have to ask the player for an x-coordinate (the column of choice) and then insert the player’s symbol (probably X or O) into the array.
Then reprint the array.

why do you use 7 1D Arrays instead of one 7x7 Array(char grid[7][7])?

That’s what I was going to ask. x.x

Well I basically just copyed your code and added some features on it. It has everything a connect four game should have except it doesn’t detect wins and it doesn’t have computer AI. Do you want me to instruct you on how to progress with your coding or just paste on this thread what I made?

yeah that’s pretty impractical

Cheers man! Paste it in, i learn better through reading/doing things than just theory, if i don’t understand any of the changes could i ask you?

Thanks man!

Well here it is. I’m still a bit of a newbie in programming plus it was a bit rushed, so some parts of code might be crudly written. But it works!

I also added the a function that checks if someone “connected four” and won. I’m not sure I want to make an AI because I don’t really know the strategies of this game. Ask me if some portion of the code is confusing you.

Also does anyone know how can you clear the console screen in iostream?
EDIT: fixed some bugs

Cool mate, it’ll be a day or two before i can test that out, today is friday, so i’ll be hungover tomorow haha.

The high score system was poorly worded by me, in nut shell, its gonna save the wins to a txt file, eg player 1 won 3 times, player 2 won once etc, not the best i know.

I wish my programming course taught us C++ not java :<
Oh well, I’ll learn it myself, and we will do it in second year.

my course learned me 1.5 years of java and 0.5 of c and 0.5 of c++ and 1 of C# and also asembly >.<

FiesEater:

I have found it looks slightly neater to write the endl on the same line as the output text, but thats just personal preference I suppose. I just wasn’t sure if you were aware that you can do that.

ex. Instead of:
cout<<" Welcome to ConnectFour! This is the main menu! Enter the option of your choice to continue!";
cout<<endl;

Try:
cout << “Welcome to ConnectFour! This is the main menu! Enter the option of your choice to continue!” << endl;

That was actually a line of code that P-Thunder left, if you look further into the code you can see that I put multiple things on the same line of cout.

Yeah, we do assembly too, but it’s kind of annoying that while the engineering students learn to program in C++, pure CS students learn fucking java.

Array subscript out of range (well, more like access violation when it comes to c/cpp)
Also, stack overflow. Your code is bugged like hell.
Also, int main();
Also, make use of classes/structures (they’re pretty much the same thing in cpp)
Also, make use of function return values.
Also, do not write code that does nothing (exit(), highscore())

Wrong. It does more than just calculating.

Spent 30 mins on this…quite fun :smiley:

https://pastebin.com/f94J8DP0

Added basic AI which just selects a random position.

Nevermind.

Founded in 2004, Leakfree.org became one of the first online communities dedicated to Valve’s Source engine development. It is more famously known for the formation of Black Mesa: Source under the 'Leakfree Modification Team' handle in September 2004.