# Thread: help needed for my connect four programme.

1. ## help needed for my connect four programme.

hi there. katy here.
I am doing a "Connect 4" program for my assignment and i've done the main part, which I should not change (my lecturer told me not to). .can anyone help me to convert this function below to suit my program??i need my program to have a verticalwin/horizontalwin and a diagonalwin function. and also when all the tiles is full,i want my program to give an output such as "the game is a tie".please help me.i'm new here.so forgive me if i'm wrong.
the function that i need modified is:

Code:
```bool check(int a, int b)
{
int vertical = 1;//(|)
int horizontal = 1;//(-)
int diagonal1 = 1;//(\)
int diagonal2 = 1;//(/)
char player = place[a][b];
int i;//vertical
int ii;//horizontal

//check for vertical(|)
for(i = a +1;place[i][b] == player && i <= 5;i++,vertical++);//Check down
for(i = a -1;place[i][b] == player && i >= 0;i--,vertical++);//Check up
if(vertical >= 4)return true;

//check for horizontal(-)
for(ii = b -1;place[a][ii] == player && ii >= 0;ii--,horizontal++);//Check left
for(ii = b +1;place[a][ii] == player && ii <= 6;ii++,horizontal++);//Check right
if(horizontal >= 4) return true;

//check for diagonal 1 (\)
for(i = a -1, ii= b -1;place[i][ii] == player && i>=0 && ii >=0; diagonal1 ++, i --, ii --);//up and left
for(i = a +1, ii = b+1;place[i][ii] == player && i<=5 && ii <=6;diagonal1 ++, i ++, ii ++);//down and right
if(diagonal1 >= 4) return true;

//check for diagonal 2(/)
for(i = a -1, ii= b +1;place[i][ii] == player && i>=0 && ii <= 6; diagonal2 ++, i --, ii ++);//up and right
for(i = a +1, ii= b -1;place[i][ii] == player && i<=5 && ii >=0; diagonal2 ++, i ++, ii --);//up and left
if(diagonal2 >= 4) return true;

return false;
}```
Code:
```#include <iostream>

#include <fstream>

using namespace std;

void drawboard (ostream &, char [6][7], int, int);

int humanmove (int player, char board [6][7], int rows, int columns);

int verticalwin (char board[6][7], int rows, int columns, int moverow, int movecolumn);

int main ()

{

cout<< "Connect Four is a two-player game in which the players first ";
cout<< " choose a letter   and then take turns dropping their letters from the top into a seven-column,";

cout<< "    six-row vertically-suspended grid.";

cout<< " The pieces fall straight down, occupying the next available space within the column. ";

cout<< " The objective of the game is to connectfour of one's own discs of the same color next to each other vertically,        horizontally, or diagonally before one's opponent can do so. ";

ofstream outfile;
outfile.open("gamelog01.txt");
char board[6][7] = {0};

int player;
int row;
int col;
int times;
bool win;
int moverow;
int movecolumn;

drawboard(cout, board,6,7);

while (player = 1)
{
humanmove(player, board, 6, 7);
verticalwin(board, 6, 7, moverow, movecolumn);

player = 2;
humanmove(player, board, 6, 7);
verticalwin(board, 6, 7, moverow, movecolumn);
}

outfile.close();
return 0;
}

//-------------------------------------------------------------

void drawboard  (ostream & out, char b[6][7], int r, int c)

{

out << endl;
for (int x = r-1; x >= 0; x--)
{
out << "  |";

for (int y = 0; y < c-1; y++)
out << "----";

out << "---|" << endl << x << " |";

for (int y = 0; y < c-1; y++)
if (b[x][y] == 0)
out << "   |";
else
out << " " << b[x][y] << " |";

if (b[x][c-1] == 0)
out << "   |" << endl;
else
out << " " << b[x][c-1] << " |" << endl;
}

out << "  |";

for (int y = 0; y < c-1; y++)
out << "----";

out << "---|" << endl;
out << " ";

for (int y = 0; y < c; y++)
out << "   " << y;

out << endl;
}

int humanmove  (int player, char board[][7], int rows, int columns)

{

char symbol;

if(player == 1)
symbol = 'X';
else
symbol = 'O';

int col;
cout << "What column would Player " << player << " like to play in?: " ;
cin >> col;

while ((col < 0) || (col >= columns))
{
cout << "Invalid selection. There aren't that many columns. Please enter a different    column number: ";
cin >> col;
}

int row;
for(row = 5; row >= 0; row--)
{
if(board[row][col] != 0)
break;
}

board[row+1][col] = symbol;

if (row == 'X' || row == 'O')
{
cout << "Column " << col << " is full. Please choose another: ";
cin >> col;
}
else
{
drawboard(cout, board, 6, 7);
}
}

int verticalwin  (char board[6][7], int rows, int columns, int moverow, int movecolumn)

{

}```

2. where did that function you showed come from? Why if you could write the rest have you not posted an attempt of your own at writing the required function? What exactly do you have trouble with, because nobody is going to just offer a complete piece of work for you to appropriate. sorry- that costs money ;->

3. my fren gav it to me.it was her previous assignment.she only gav the verticalwin/horizontalwin/diagonalwin function.thing is,i'm nt sure how to make do with it n modify..any tips how do i modify it? or is there any other way to create my verticalwin function?please help.

4. Originally Posted by KatherineNguyen
or is there any other way to create my verticalwin function?
You could think about it and do it yourself, and learn something.

5. i've been trying since friday.if i could do it by then,i wouldnt hav to ask around for help.i've done 80% of my program.now the only this dats buigging me is the verticalwin/horizontalwin/diagonalwin function.i would realy appreciate if someone can at least giv hints on how to modify.

6. You should post what you have attempted as far as refactoring this function goes, but really i would abandon and code something that is more in line with your own work, it may be you are trying to fit a square peg into a round hole (sure thats a pun here...) and actually making more work for yourself trying to crowbar in the code your friend gave you. especially if you are having difficulty understanding what she has given you anyhow.

Also consider if its the best way to do it that she has shown you? Just because she managed to get it working in her project does not mean it will translate neatly to your version.

7. my fren gav it to me.it was her previous assignment.she only gav the verticalwin/horizontalwin/diagonalwin function.
Your "fren" at StackOverflow? Which may have originated here?

Lies, lies, lies. Stop trying to get everyone to do your homework for you. If you don't want to program, then drop out of your class and go study something else.

5 1/2 steps to get others to doing your homework

8. Sorry you best ask that buddy at StackOverflow!

9. I like the technique of using a female name and a random human female's picture as an avatar. Nice touch.