some problems about writing connect 4

This is a discussion on some problems about writing connect 4 within the C++ Programming forums, part of the General Programming Boards category; i am going to write a connect 4 game with ai. however, i have no idea how to write. would ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    24

    some problems about writing connect 4

    i am going to write a connect 4 game with ai. however, i have no idea how to write. would anyone give me some ideas????? i just know i have to use 2d array.
    thanks.

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by fail50 View Post
    i am going to write a connect 4 game with ai. however, i have no idea how to write. would anyone give me some ideas????? i just know i have to use 2d array.
    thanks.
    It's hard to help you without knowing a little about your experience with C++. Are you doing C++ at university? How long have you been studying? How much C++ do you know? Have you ever created a game in C++ before?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    24
    Now I have some mistake about printing the board.
    here is the code
    Code:
    #include <iostream>
    #include<string>
    #include<iomanip>
    using namespace std;
    const int maxrow = 6;
    const int maxcol = 7;
    void logo();
    void playername(string name);
    void rules();
    void initBoard(char board[][maxcol], int maxrow, int maxcol);
    void printBoard(char board[][maxcol], int maxrow, int maxcol);
    int main()
    {
    	char board[maxrow][maxcol];
    	string name;
    	logo();
    	playername(name);
    	rules();
    	initBoard(board, maxrow, maxcol);
    	printBoard(board, maxrow, maxcol);
    }
    void logo()
    {
    	cout<<"------------Welcome to Connect 4 ----------------"<<endl;
    	cout<<"     ************Connect*************"<<endl;
    	cout<<"    **********************************"<<endl;
    	cout<<"   *****************4******************"<<endl;
    	cout<<"  *****************4*4******************"<<endl;
    	cout<<"  ****************4**4******************"<<endl;
    	cout<<"  **************4444444***************"<<endl;
    	cout<<"   *****************4****************"<<endl;
    	cout<<"    ****************4***************"<<endl;
    	cout<<"\n\n"<<endl;
    }
    void playername(string name)
    {
    	cout<<"What is player's name : ";
    	cin>>name;
    	cout<<"Hi "<<name<<endl;
    }
    void rules()
    {
    	cout<<"The following is the rules of the Connect 4 : \n";
    	cout<<"1. Each player in his turn drops one of his checkers down any of the slots \n   in the top of the grid. \n";
    	cout<<"2. The play alternates until one of the players gets four checkers of his \n   colour in a row. The four in a row can be horizontal, vertical, or diagonal.\n";
    	cout<<"3. The first player to get four in a row wins.\n";
    	cout<<"4. If the board is filled with pieces and neither player has 4 in a row, \n   then the game is a draw.\n";
    }
    void initBoard(char board[][maxcol], int row, int col){
    	for (int i = 0; i < row; i++)
    	{
    		for (int j = 0; j < col; j++)
    		{
    			board[i][j] = ' ';
    		}
    	}
    }
    void printBoard(char board[][maxcol], int row, int col)
    {
    	for (int i = 1; i <= row; i++){
    		for (int j = 1; j <= col; j++){
    			cout << setw(3) << board[row][col]<<"| ";
    		}
    		cout << endl;
    	}
    	for (char i = 'A'; i <= 'G'; i++){
    		cout << setw(3)<<i<<"  ";
    	}
    	cout << endl;
    }
    Like this image
    http://i1128.photobucket.com/albums/..._connect_4.jpg
    why there is some 'n'???
    what's the problem??

  4. #4
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    The problem is you are going outside the bounds of your board array:

    Code:
    			cout << setw(3) << board[row][col]<<"| ";
    I think what you meant here was:

    Code:
    			cout << setw(3) << board[i][j]<<"| ";
    As it happens, there is another problem with printBoard(). Remember indexing starts at 0, not 1. Instead of

    Code:
    	for (int i = 1; i <= row; i++){
    		for (int j = 1; j <= col; j++){
    you should have

    Code:
    	for (int i = 0; i != row; ++i){
    		for (int j = 0; j != col; ++j){
    Also, I believe you meant to pass that string by reference in playername. Instead of:

    Code:
    void playername(string name)
    you should have

    Code:
    void playername(string& name)

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    24
    thx!!!
    if i have any problems, i will ask you again

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    24
    how can i ask the player to input into the board???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting IP address to connect to
    By tesla in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-11-2009, 10:56 PM
  2. Problems writing some chars to files with fprintf
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-18-2006, 06:00 PM
  3. Problems writing to a file
    By Dragoon_42 in forum C++ Programming
    Replies: 5
    Last Post: 02-09-2006, 10:44 AM
  4. output from loop and writing to file (seperate problems)
    By Confuzz in forum C++ Programming
    Replies: 6
    Last Post: 09-06-2004, 08:48 PM
  5. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21