Questions about connect 4 board

Hi,
So I am working on this connect four board stuff to test myself of some basic programming...
And I got stuck on the idea of how to display the board(Note that my board is 9*7)
Also I asked some other guys about it, but it seems I got completely stuck and dont know what to do next.(Like how to determine the winner and how to list the X and O into the board...) Can someone give me some help...


I will post my header and cpp together so you can see if I got any fatal errors somewhere among them... Thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<iostream>
#include "ConnectFourBoard.h"
using namespace std;

//never include a .cpp file.
int main()
{
	cout << "Welcome to Connect 4 Board!" << endl;
	cout << "1  2  3  4  5  6  7" << endl;
	for (int x = 0; x <= 6; x++)
	{
		for (int a = 0; a <= 8; a++)
		{
			cout<<
		}
	}
	cin.get();
	cin.get();
}
ConnectFourBoard::ConnectFourBoard()
{
	boardAsCharacters = new char*[ROW_AMOUNT];

	for (size_t index = 0; index < ROW_AMOUNT; ++index)
	{
		boardAsCharacters[index] = new char[COLUMN_AMOUNT];
	}
	for (size_t index = 0; index < ROW_AMOUNT; ++index)
	{
		lastPositions = new int*[ROW_AMOUNT];
	}
	
}
ConnectFourBoard::ConnectFourBoard(const ConnectFourBoard& board)
{
	boardAsCharacters = new char*[ROW_AMOUNT];

	for (size_t index = 0; index < ROW_AMOUNT; ++index)
	{
		boardAsCharacters[index] = new char[COLUMN_AMOUNT];

		for (size_t columnIndex = 0; columnIndex < COLUMN_AMOUNT; ++columnIndex)
		{
			boardAsCharacters[index][columnIndex] = board.boardAsCharacters[index][columnIndex];
		}
	}
	lastPositions = new int*[ROW_AMOUNT];

	for (size_t index = 0; index < ROW_AMOUNT; ++index)
	{
		lastPositions[index] = board.lastPositions[index];
	}
}

/*ConnectFourBoard::~ConnectFourBoard()
{
	delete[] boardAsCharacters;

	for (size_t index = 0; index < ROW_AMOUNT; ++index)
	{
		delete[] boardAsCharacters[index];
	}
	delete[] lastPositions;
}
*/

void displayBoard()
{
	cout << "1  2  3  4  5  6  7" << endl;
	for (int x = 0; x <= 6; x++)
	{

	}
}


//header file is definition of class and functions.

#ifndef  __CONNECT_FOUR_BOARD_HPP__
//ifndef(if not defined) create a definition for the string. means this file associated with the sequence of characters. (Check the definition.)
//go through each file. Include different file.
#define  __CONNECT_FOUR_BOARD_HPP__

#include <iostream>


class ConnectFourBoard
{
private:
	static const unsigned int ROW_AMOUNT = 9;
	static const unsigned int COLUMN_AMOUNT = 7;

public: // Constructor | Desturctor

	ConnectFourBoard();
	~ConnectFourBoard();
	ConnectFourBoard::ConnectFourBoard(const ConnectFourBoard& board);

public: // Public Member Functions. 
	//inline(hidden key word)
	void displayBoard();
	//void takeTurn(Player& player);

public:// Member Variables never public member variables.
	//camel case.(every new word is upper case letter.)
	// do not use variableName;
	char** boardAsCharacters;
	int** lastPositions;
};
//end of class connect four board
#endif//__CONNECT_FOUR_BOARD_HPP__ 
Line 14 appears to be incomplete.

In your default constructor function, you set up boardAsCharacters properly. Do you see what you're doing to lastPositions?

Again, in your copy constructor function, you set up boardAsCharacters properly but not lastPositions.

EDIT: After thinking about how an actual "Connect 4" game works, maybe those constructors are right and the mistake is in the header file. Should lastPositions be int** or int*?
Last edited on
Yah cuz I dont know how to format and printout the board like
1 2 3 4 5 6 7
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
============= this.(similarly)
and I also dont know what to do next about determine the winner and put the sign in the board etc...
Can anybody help... I'm confused and just getting started on c++...plz some one give me a hand :(
This is an example for outputting your board, but you still need to fix your ConnectFourBoard class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

int main()
{
    const int ROWS = 6;
    const int COLS = 7;
    unsigned int board[ROWS][COLS];

    srand(time(0));

    for (int i = 0; i < ROWS; ++i)
        for (int j = 0; j < COLS; ++j)
            board[i][j] = 0;

    bool player1 = true;
    for (int moves = 0; moves < 28; ++moves, player1 = !player1)
    {
        unsigned int col;
        do
        {
            col = rand() % COLS;
        } while (board[ROWS - 1][col] != 0);
        unsigned int row = 0;
        while (board[row][col] != 0)
            ++row;
        if (player1)
            board[row][col] = 1;
        else
            board[row][col] = 2;
    }

    //OUTPUT CODE BEGIN
    std::cout << "\n\t" << std::string(COLS * 4 + 1, '-') << std::endl;
    for (int row = ROWS - 1; row >= 0; --row)
    {
        std::cout << "\t| ";
        for (int col = 0; col < COLS; ++col)
        {
            if (board[row][col] == 0)
                std::cout << "  | ";
            else
                std::cout << board[row][col] << " | ";
        }
        std::cout << "\n\t" << std::string(COLS * 4 + 1, '-') << std::endl;
    }
    //OUTPUT CODE END

    return 0;
}


	-----------------------------
	|   |   |   | 2 |   | 2 |   | 
	-----------------------------
	|   |   |   | 1 |   | 1 |   | 
	-----------------------------
	| 2 | 1 | 1 | 1 |   | 1 |   | 
	-----------------------------
	| 2 | 2 | 2 | 1 |   | 1 |   | 
	-----------------------------
	| 2 | 2 | 2 | 2 | 2 | 1 | 2 | 
	-----------------------------
	| 1 | 2 | 2 | 1 | 1 | 1 | 1 | 
	-----------------------------
 
should I do the same for my lastposition pointer just as the boardAsCharacters?
I do not know how you intend to use lastposition so I cannot say. As iy is now, you are creating a memory leak in your default constructor. You also never allocate space for the "columns" of your array.

In the copy constructor, you are just copying over the addresses of the array and not the values in the array.
I intend to use lastposition to record the users' X and O as their personal sign in the board. But I got confused of that part since somebody else told me about that and I never get it clear...
If lastposition is supposed to record the users' X and O on the board, then what does boardAsCharacters do?

I suggest you ask the person who gave you the code for lastposition to clarify how it's supposed to work.
Topic archived. No new replies allowed.