Could you please help me how to create a four in a row game in C?
Thanks!

Recommended Answers

All 17 Replies

What do u mean by "four in a row game"

1)Explain the game how it is.(game rules)
2)Also you can share ur coding and approach for the game.

Here is link to the game:
http://www.donutgames.com/play/four_in_a_row

Game rules: The goal is, like the name implies, to get 4 pieces of your own color in a row on the playfield. Each player takes turn to drop pieces, one at a time. The computer opponent will try to stop you, so in order to win you must trick it.

Hello Anna,
Will try my best to guide you for devloping this game.
Before that you need to have the understaing for the following things.

1)Graphics in C
2)Dealing with Files in C
3)Basics of pointers,structures and strings

now we will go by the graphics

Lets say we will develop it for windows platform in 16 bit Turbo C complier.

1st go thorugh the basics of graphic programming in C.
go to graphics mode.
draw some shapes over (x,y) coordinate.
fill some colors.

ok do one thing draw a static sketech of the game on the screen using C graphics.

then we will go the next steps and think about the logic.
Let me know in case of any concerns.
Thanks,
DP

On, no I don't want to do it with graphics, that' too much for me :)
I want to do console aplication. All 'graphics' will be done with signs like | | -> this will be one space. There will be 8 columns and 6 rows. One player will have sign X and the second one O. They will say into which column wil they put their sign.
For example: I have X sign:
I would say, that I want to put my X to third column, second player wants to put O to first column:
1 2 3 4 5 6 //number of colums
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
|O| |X| | | |

This is not the best way for graphics, but I don't know how to do it better.

Create an array which holds the board state.
(don't forget to initialize the board before using it)

1) Write code to display the board (from the array).
(Test whether everything is displayed correctly).

2) Write code to accept user input.

3) Write code which processes the user input, and places his move at the correct place on the game board (= at the correct place in the array).

4) Add the real gameplay:

  • Add rules and artificial intelligence.
  • Add scores to the game.
  • Add some other custom features.

One very important link you can't avoid to have read once is:
http://cboard.cprogramming.com/c-programming/88495-development-process.html

(The most important thing is that you develop one feature at a time, and only move on to the next thing, when everything works correctly)

1)Take a 2D array.

char storage[6][8];

At initilazation all values in the array will be NULL lets say "*".

2)Use a flag which will indicate which users turn is now.

int user_flag;
its value can be 1 or 2.

if(user_flag=1)
{
user1_turn(); //fill with X
user_flag=2; //chage flag
}
else if
if(user_flag=2)
{
user2_turn(); //fill with X
user_flag=1; //chage flag
}
else
flag error;

3)in case of user 1 : fill the array with X
and in case of user 2:fill the array with 0

4)refresh the array at each turn.
clear the screen at each turn.

and print the arry on the screen as the format specified.

5)at each turn check whether the row is filled or not.if a value is already there
user cant insert there.


6)User input will be something like:

row_no: [return]
col_no: [retrun]

i have tried to give my approach.

read the steps carefully.the game is explained here very clearly.in case of any concerns do let me know.

Hi Anna,
Now at this point forget about the Artificial Intellegence .just make it a 2 player game.

After its completion you can go for the AI.

Thanks,
DP

Hi Anna,
Now at this point forget about the Artificial Intellegence .just make it a 2 player game.

After its completion you can go for the AI.

Thanks,
DP

Yes, that would be a better idea to begin with.
BTW, Dream2Code it's highly preferable to post using code tags:
http://www.daniweb.com/forums/misc-explaincode.html

Tnx for all tips you gave me, now I'll try to write a code. But if anyone has too much time, could write it instead of me ;) :D

Tnx for all tips you gave me, now I'll try to write a code.

Agreed till here.

But if anyone has too much time, could write it instead of me ;) :D

I have too much time, but giving you free code is against the philosophy of this forum.
The problem is, when someone writes the code for you, you won't learn from it, and how can you become a (good) programmer if you don't write code yourself?
The time I would spent by writing the code for you, could easily be spent by giving help to other people on this forum, don't you think this is a better idea?

Anyway, when you come across difficulties while writing the code, and you find no way out, the best thing you can do is: post down the code you have so far, explain where you are difficulties with, and be as specific as possible while describing your difficulties.

I completely agree with you tux4life. I know that I won't learn from it, I thought it as a joke.

Yes, that would be a better idea to begin with.
BTW, Dream2Code it's highly preferable to post using code tags:
http://www.daniweb.com/forums/misc-explaincode.html

Dear Tux4life,

Actually thats a kind of pseudo code or algo you can say so i have written like that.

But its my word i will learn all rules of daniweb.As today is my 2nd day in this forum.

Thanks,
DP

I completely agree with you tux4life. I know that I won't learn from it, I thought it as a joke.

Dear Anna,
if u really concentrate the step provied by me u will surely finish it soon.

start with your coding we all are here to help you.but try it such that
in the end u can say "YES,I HAVE DONE IT".

Thanks,
DP

iamthwee, tnx for your code. That's exactly what I need, but it's very hard to read the code. Now I'm going to try write code on my own, I report when I'm finished.

commented: LOL +13

iamthwee, tnx for your code. That's exactly what I need, but it's very hard to read

really? you think so? i thought it was actually quite clever.

you should hand it in, your instructor will be impressed.

Hey Anna have you finished your 4 in a row game?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.