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.
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 ...
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.
Now I have some mistake about printing the board.
here is the code
Like this imageCode:#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; }
http://i1128.photobucket.com/albums/..._connect_4.jpg
why there is some 'n'???
what's the problem??
The problem is you are going outside the bounds of your board array:
I think what you meant here was:Code:cout << setw(3) << board[row][col]<<"| ";
As it happens, there is another problem with printBoard(). Remember indexing starts at 0, not 1. Instead ofCode:cout << setw(3) << board[i][j]<<"| ";
you should haveCode:for (int i = 1; i <= row; i++){ for (int j = 1; j <= col; j++){
Also, I believe you meant to pass that string by reference in playername. Instead of:Code:for (int i = 0; i != row; ++i){ for (int j = 0; j != col; ++j){
you should haveCode:void playername(string name)
Code:void playername(string& name)
thx!!!
if i have any problems, i will ask you again![]()
how can i ask the player to input into the board???