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!
#include<iostream>
#include "ConnectFourBoard.h"
usingnamespace 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 = newchar*[ROW_AMOUNT];
for (size_t index = 0; index < ROW_AMOUNT; ++index)
{
boardAsCharacters[index] = newchar[COLUMN_AMOUNT];
}
for (size_t index = 0; index < ROW_AMOUNT; ++index)
{
lastPositions = newint*[ROW_AMOUNT];
}
}
ConnectFourBoard::ConnectFourBoard(const ConnectFourBoard& board)
{
boardAsCharacters = newchar*[ROW_AMOUNT];
for (size_t index = 0; index < ROW_AMOUNT; ++index)
{
boardAsCharacters[index] = newchar[COLUMN_AMOUNT];
for (size_t columnIndex = 0; columnIndex < COLUMN_AMOUNT; ++columnIndex)
{
boardAsCharacters[index][columnIndex] = board.boardAsCharacters[index][columnIndex];
}
}
lastPositions = newint*[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:
staticconstunsignedint ROW_AMOUNT = 9;
staticconstunsignedint 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__
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*?
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...
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...