Hey All i am making a connect four game. I have done most of the code but need help in some bits

1) Which is how to code to see if there a win from vertical, horizontal or diagonal. - The Grid is 7 by 7. - I no idea how to code this and if i need in a function on its own or in the main game loop, also do i need make one for both p1 and p2? or will one just do??

2) After someone wins I want be able to score the result of both p1 and p2 (win lost or a draw) into a notepad file. I also want make a function to get that notepad to bubble sort all the top 20 players and show them in ranking order. Also is there a way of getting the notepad file not to allow ppl to edit the scores without playing the game??


For Exampple for the ranks:-

Joe blongs Won 5 Lost 4 Draw 4
Joe1 blogs1 Won 5 Lost 4 Draw 4
Joe2 blogs Won 5 Lost 0 Draw 0
Joe3 blogs1 Won 4 Lost 0 Draw 0


Below is the code I have so far...

void gamestart()
{
  system("cls");
  
  player player1;
  player player2;
  
  player1.counter = 'X';
  player2.counter = 'O';

  cout<<"Player 1 Please Enter Your First Name\n";
  cout<<endl;
  cin >> player1.name;
  cout<<endl;
  
  cout<<"Player 2 Please Enter Your First Name\n";
  cout<<endl;
  cin>>player2.name;
  cout<<endl;

  cout<<player1.name<<" Vs "<<player2.name;
  cout<<endl;
  cout<<endl;
 
  while (bool game = true) 
  {
    int choice,move = 1;

	system("cls");

	cout<<player1.name<< " Your Counter Is A "<<player1.counter;
	cout<<endl;
	cout<<endl;
    
	cout<<player2.name<< " Your Counter Is A "<<player2.counter;
	cout<<endl;
	cout<<endl;

	drawGrid();

	if (move = 1)  { 
		do {
			do { 
				cout<<player1.name<<" Please Enter A Column 1 To 7\n";
				cout<<endl;
				cin>>choice;
				choice--;
			} while ((choice <0) ||(choice >7)); 
		} while (frame[choice].count ==7);
	}
	{ drawGrid();
      addCounter(player1.counter,choice);
      move++;
   }

    system("cls"); // 	clears p1 - SOLVES THE BOARD PRINTING TWICE!

	if (move = 2)  {  
		do {
			do { 
			    cout<<player1.name<< " Your Counter Is A "<<player1.counter;
	            cout<<endl;
	            cout<<endl;
  	            cout<<player2.name<< " Your Counter Is A "<<player2.counter;
	            cout<<endl;
                cout<<endl;
			    drawGrid();
				cout<<player2.name<<" Please Enter A Column 1 To 7\n";
				cout<<endl;
				cin>>choice;
				choice--;
			} while ((choice <0) ||(choice >7)); 
		} while (frame[choice].count ==7);
	}
	{ addCounter(player2.counter,choice);
      move++;
   }	
  } // end main game loop
} // end game start

Thanks.

Member Avatar for GreenDay2001

Aint giving you the code, but here;s how it has to ve dont. Suppose u define a 7x7 matrix in double dimention array. Initially fill them either with false meaning not filled. As the game continues fill them with apropriate values.

Now here's how it to be done in 3x3 matric. I cant type for 7x7 matrix :)
(x,y)
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,2

Now you see, along horizontal x is contant, along vertical y is constant, along diagonal either x==y or x+y==2.

that's ow it could be done


For sorting it, read the file into array then sort it as you do it int array.

Hey thanks would that be best in a function then and put in p1 and p2 or code in both if u see what I mean.

Hey All i am making a connect four game. I have done most of the code but need help in some bits

1) Which is how to code to see if there a win from vertical, horizontal or diagonal. - The Grid is 7 by 7. - I no idea how to code this and if i need in a function on its own or in the main game loop, also do i need make one for both p1 and p2? or will one just do??

As vishesh suggests, a 7x7 array would work great. You can define the board to be int board[7][7]; .
Then for every move (say board[2][3] for 3rd row, 4th col) you look at all the locations that could make up a win.

The more functions you create, the better (within reason)

2) After someone wins I want be able to score the result of both p1 and p2 (win lost or a draw) into a notepad file. I also want make a function to get that notepad to bubble sort all the top 20 players and show them in ranking order. Also is there a way of getting the notepad file not to allow ppl to edit the scores without playing the game??

Open the score file in append mode. Output the new score, close the file. This will give you a list of all the games which you can process when you output the scores.

Hey, thx. Just to 100% clear do i need to declar another var or just go back to the function i make the board and put [7], [7] otherwise i dont see how that board var can look the grid? Or I assume u code the winning function with what I allready used??

Thanks

hmm I'm prop still be silly here but i still cnt think how code what you saying.

struct column
{ 
char rows[7]; 
int count;
// board????
};
column frame[7];
void clearGrid()
{

for (int i=0;i<8;i++)
{
for (int j=0; j<7; j++)
{ 
frame[j].rows[i] = ' ';
}
}
for (int j=0; j<7; j++) frame[j].count = 0; 
}
void drawGrid()
{ 
cout<<" Column\n";
for (int col=1; col<8; col++) cout << ' ' <<col;
cout<<endl;
for (int i=6; i>= 0; i--) 
{ 
for (int j=0; j<7; j++)
{ 
cout << "|" << frame[j].rows[i];  // used for testing if the col full 
}
cout << "|";
cout << endl;
}
for (int j=0; j<7; j++) cout << ' ' << frame[j].count;
cout<<endl;
cout<<endl;
}
void addCounter(char counter, int col)
{
frame[col].rows[frame[col].count++] = counter;
}

Thats how i'm 1) showing the grid 2) how to check if the grid full or not and 3) to put a counter in if its not full. The bit i dont get is do i need to go back to the [frame]col and make that [7][7] or make a totaly diffrent function n int the board [7][7] and code it along with [frame]col? Or in my stuct for the column could i put the board [7][7] there? soz if i'm being thick here.

Thanks

What's with that column structure?

A combination of real code and psuedo code to play a location:

int board[7][7];

clear board -- set all locations to 0.  SPACE can be used.

accept location to move: row,col  // must be 0-6, check for error
if (board[row][col] != 0)
{
    display "location already played";
}
else
{
    board[row][col] = player;    // player is 1 or 2 for example.
}

hey, so dont u think i need that column structure?? its so it makes the game board.

Is there a better of swaping a player turn then what I did?? I'll have a play with my code 2night wud i need to change anything in the functions i have atm??

Thanks

Hey, I got a new questions.

The board [7] [7] how do I get the board to know its checking the grid?? And then with all those 0,1 1,1 etc to check for an X or O??.

Could someone point to an example or point me in the right lines??

I've tried searching for another connect four game or online example of location and cant find any.

I've found one example but dont know if this is what i'm trying todo : http://library.thinkquest.org/C0111571/manual.php?tid=26 - is that the write lines?

Thanks.

char board[7][7] ;

for(i = 0; i < 7; ++i)
  for(j = 0; j < 7; ++j)
      board[i][j] = '*';  //fill board with *s

turn = player1 = X;
row = 2; //using choices 0-6;
col = 5; //using choices 0-6;

board[row][col] = 'X';

//display board
for(i = 0; i < 7; ++i)
{
  for (j = 0; j < 7; ++j)
  {
     cout << board[i][j];
  }
  cout << endl;
}
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.