connect 4 using dynamic memory allocation

This is a discussion on connect 4 using dynamic memory allocation within the C++ Programming forums, part of the General Programming Boards category; I am creating a connect 4 game using dynamic memory allocation the question is; Write a two-player game of Connect ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    36

    connect 4 using dynamic memory allocation

    I am creating a connect 4 game using dynamic memory allocation the question is;

    Write a two-player game of Connect Four where the user can set the width and height of the board and each player gets a turn to drop a token into the slot. Display the board using + for one side, x for the other, and _ to indicate blank spaces.

    I have created the board. However I am unsure as how to make a start on getting the players to make moves.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    char **create_table(int width, int height, char blank) {
        char **p_p_connect4 = new char*[height];
    
    
        for(int i = 0; i < height; i++) {
            p_p_connect4[i] = new char [width];
        }
    
    
        for(int i = 0; i < height; i++) {
            for(int j = 0; j < height; j++) {
                p_p_connect4[i][j] = blank;
            }
        }
    
    
        return p_p_connect4;
    }
    
    
    void printTable(char **p_p_connect4, int width, int height) {
        for(int i = 0; i < height; i++) {
            for(int j = 0; j < height; j++) {
                cout << p_p_connect4[i][j];
                if (width-1) {
                    cout << " | ";
                }
            }
                cout << endl;
                if (height-1) {
                    for (int k =0; k < (width - 1)*5;++k) {
                        cout << "-";
                    }
                    cout << endl;
                }
        }
    }
    int main()
    {
        char blank = {'_'};
        int height;
        int width;
        cout.flush() << "Please insert the sizes of table: ";
        cin >> width;
        cin >> height;
        cout << endl;
    
    
        char **p_p_connect4 = create_table(width,height,blank);
        printTable(p_p_connect4,width,height);
    
    
        return 0;
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Before you do anything else, you should get rid of your create_table function. Learn to use std::vector for arrays. It's safer, easier, faster and more convenient.
    Also, if you want to flush a stream (which typically isn't necessary unless you need to guarantee the text appears on the screen or in a file), you should flush after you've written the text. Your cout statement is backwards.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation
    By slash.hack in forum C Programming
    Replies: 9
    Last Post: 09-30-2011, 04:31 AM
  2. Help with dynamic memory allocation
    By malooch in forum C Programming
    Replies: 2
    Last Post: 12-13-2006, 12:26 PM
  3. dynamic memory allocation
    By inquisitive in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2004, 01:07 AM
  4. dynamic memory allocation - Please help
    By Space_Cowboy in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2002, 04:20 PM
  5. Dynamic Memory Allocation
    By fr0ggs in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2001, 03:34 AM

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