C++ help

fes_786

Inactive User
Joined
Nov 30, 2005
Messages
3,894
Reaction score
278
Location
uk
hi peeps

ok ive been learning c++ from 1 of those dummy books

i have been set a task.

i have to design a text based version of connect four

u know that connect four game which u have to get four in a row to win

well it needs to support 1 player against comp
and 1 player against player 2

i want to start with player 1 vs player 2

this is how im thinkin about it:


need to show a 2d grid 7 columns by 6 rows (empty at start)

then a option were the person can choose to play with comp or with friend or exit

ok now i know i need an array the same size as the grid

how do i go about making the grid
and i would like this to be in a function so i just call it.

sorry for these basic questions
and if some one can help me out over msn then feel free to add me on ur msn

cheers
 
hi peeps

ok ive been learning c++ from 1 of those dummy books

i have been set a task.

i have to design a text based version of connect four

u know that connect four game which u have to get four in a row to win

well it needs to support 1 player against comp
and 1 player against player 2

i want to start with player 1 vs player 2

this is how im thinkin about it:


need to show a 2d grid 7 columns by 6 rows (empty at start)

then a option were the person can choose to play with comp or with friend or exit

ok now i know i need an array the same size as the grid

how do i go about making the grid
and i would like this to be in a function so i just call it.

sorry for these basic questions
and if some one can help me out over msn then feel free to add me on ur msn

cheers

Which book is it?

Or is it coursework lol?
 
not yet but im reading up still

i will post later today about how far i get
 
Are you writing this in .net? Also, the grid is the easiest thing about the project, you just create a frid out of whatever you like.

Is this going to be a windows app or a console app?

One thing you may not have come across yet in your thoughts - how are you planning on detecting the presence of four same coloured markers in a row? You will need to come up with some rules first that decide a win scenario.
 
ok its going to be in a console app

not .net

so its all text based

but the grid has to be updated with each players turn

to detect a win condition i was thinking of using a loop which detects the conntents of the array

so if player 1 has counters in 2boxs but the next 1 is empty or blocked it steps out of the loop and next player takes his turn

the reason i wanted this in a seprate function is so after each turn i dont need to repeat loads of code just enter/ call function
 
Why not start a search after each marker is placed? Saves looping.

Your win conditions would need to check for the presence of markers, so if a marker has a likewise colour located to the north of its position and to the east with nothing else near it, then there is no win etc.

Also, maybe you want to get away from using an array... This will mean you can only store one peice of info at a time. You will need to look into using a multi-dimensional array or a hash. Have you thought about the data you will need to store in it?
 
ohh another thing i forgot to mention

when player places counter in a cell

it will drop to the bottom

like in the real game

and a 2d array should sort it?

and will read up on the search

cheers
 
Thats easy, for each vertical column in the grid you create you just place the marker in the first empty space.

Stay away from 2d arrays, they will not do you any good.
 
so a grid like this:

0 | 1 | 2 | 3 | 5 | 6 | 7 |
A |
B |
C |
D |
E |
F |

so lets say a player wanted to put a coin in A1

how would i tell the programme to drop the coin into F1 because its empty

.etc

and how would i go about storing the moves of each player and keeping track of each players counter
without using a 2d array
 
Well, a 2D array will only hold one thing for you, its just a collection of values. So what you will end up doing if you go through this route is simply have a whole bunch of 2D arrays you need to search through.

If you have a hashtable that represents the playing area, you would be able to store the location of the marker, who placed it and the count of markers near it that fall under your win conditions.

You wouldnt need to mess around with programtically figuring out where the marker went - you just grab that from the player. So you would put a promt up saying "P1, enter your next move", the player would then type in a value like A1 for instance. All you need to do is check this is a valid location prior to entering it into your hashtable - and in that instance you are only checking for two things; is it a valid location and is it empty.

For storing the number of moves by a player, you can have a simple 2D array - as this wont impact the rest of the game. If you needed to limit the number of moves in a match then you would just loop through this after each sucessful turn.
 
so how would i go about creating a hastable that represents my grid?

sorry first time i heard about a hash table
 
The hash would only be a logical representation of the grid.... If you think about all of the contraints that make up the game, i.e. the rules, then you will see how you want your has to look.

The actual grid itself is only a by product of the data you are manipulating and the rules of the game. It is the least of your worries.
 
jesus and i was just about to ask about doing DOOM 7 for the PS5 . think i'll stick to ds lite lol good luck Fes and fair play
 
It's a long time since i have programmed, but i would guess a 3d array would work for this just make the 3rd value 0,1 or 2 for empty,red or blue to satistfy your algorithms.
 
Last edited:
You dont need to limit yourself to three ranges. C++ will let you create a multidimensional array, which is what he needs to look into.
 
its been a while since I had to rack my brains on a programming issue as well.

Well Im thinking a 2d array could be used but you could use 0,1, and 2 or A, B and C, any 3 different values to represent an empty slot, person A filled slot or person B filled slot.

Each time a person specifies a slot, you can check to see if it is zero, only then populate it with the person player identifier.
Then after each slot is filled, do the maths, check to see if 4 in a row has been hit.

This would then simply be going thru the array (in all valid directions).

You dont have to store their last moves, but the drawback is its probably not that efficient due to the checks on the array after each slot is filled.

EDIT: similar to meangreenie I guess :)
 
Guys, seriously, he doesnt want to use a 2D array.

I dont want to do the work for him, I think the discussion on this so far has been quite good - but... With a multidimensional array he would store data like, the colour of the marker, where it is and its immediate neighbours.

You wouldnt need a bit in the array to say if a slot has already been taken or not, you would just perform a check on the array to see if that part of the array is set to null, or if it contains data relating the the logical layout of the grid.

You dont need to deal with the concept of tracking players moves either, you just need to build a function that checks to see if there are four markers in a row that match a winning constraint.
 
a number of functions, one to check 4 non zero values in a row, diagonally, vertically and horizontally

eg

int matchingValues = 0;
int lastValue = 0;
for i = 0 to MAX_ROWS {
for j = 0 to MAX_COLS {
if (myarr[j] > 0 && (myarr[j] == lastValue || j == 0)) {
matchingValues ++;
if (matchingValues == 4) {
cout << "WE HAVE A WINNER" <<;
}
}
}
}
 
Back
Top