954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Making a connect 4 gameboard with charecter arrays

so im trying to build a connect 4 gameboard using charecter arrays and it has to look like this
O | O | O | O
-------------
O | O | O | O
-------------
O | O | O | O
-------------
O | O | O | O


Column: A B C D


so far ive got everything working except for the barriers on the odd numbered lines, it just gives me gibberish for some reason

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
 
// ----------- Function definitions -------------

void setgameboard(char board[7][12]){
    int i,j,k;
     for (i=0;i<7;i++){//row loop
         if (i==0||i==2||i==4||i==6){ //if row isnt barrier
                        for (j=0;j<13;j++){//start row that isnt barrier
                              if(j==0||j==4||j==8||j==12)  {board [i][j]='O';} //is open space for token
                               if(j==1||j==3||j==5||j==7||j==9||j==11){board[i][j]=' ';}//is space
                               if(j==2||j==6||j==10){board[i][j]='|';}//is divider
                                   }  //close if row isnt barrier loop
          if (i==1||i==3|| i==5){for (k=0;k<13;k++){board[i][k]='-';}}          //if row is barrier            
                                             } //close column loop
}//close row loop

}
     
     int main()
{

       // -------- Constant declarations --------

       // -------- Variable declarations -------- 
char board[7][12];
int i,j;
        //--------Main Program Body----------
    cout << "**** ***"<<endl<<endl; //title

setgameboard(board);
for (i=0;i<7;i++){ for (j=0;j<13;j++){cout<<board[i][j];}cout<<endl;}

  cout << endl << endl;
       system("PAUSE");     // Pause the output window for reading
       return 0;
       }

is it just something with my print or am i making a mistake with the function? is there an easier way to do this? please help! thanks!

beejay321
Light Poster
25 posts since Jan 2011
Reputation Points: 7
Solved Threads: 0
 

Couple of things....

1) your arrays/indexes are not in sync. Your board is "char[7,12]", meaning the indexes go from 0..6 and 0..11, respectively. Look at some of your loops - you have them checking for "j<13", meaning "j" can go up to 12, which is outside the bounds of the array. I was getting error messages when I ran the code about the stack being corrupt.

2) Your "for" loop for drawing the dashes (on your odd lines) was inside the "if" statement checking for the even lines. Once I properly indented the code it jumped right out at me. Like this:

void setgameboard(char board[7][12])
{
    int i,j,k;
     for (i=0;i<7;i++)
     {//row loop
         if (i==0||i==2||i==4||i==6)
         { //if row isnt barrier
            for (j=0;j<13;j++)
             {//start row that isnt barrier
	         if(j==0||j==4||j==8||j==12)
	         {
		     board [i][j]='O';
	         } //is open space for token
                 if(j==1||j==3||j==5||j==7||j==9||j==11)
	         {
		     board[i][j]=' ';
	         }//is space
                 if(j==2||j==6||j==10)
	         {
		     board[i][j]='|';
	         }//is divider
             }  //close if row isnt barrier loop
             if (i==1||i==3|| i==5)
	     {
	         for (k=0;k<13;k++)
	         {
		     board[i][k]='-';
	         }
             }          //if row is barrier            
        } //close column loop
    }//close row loop
}

Lines 23-29 should be moved below line 30, to be out of the "if" statement on line 6.

mcriscolo
Posting Whiz in Training
218 posts since Apr 2008
Reputation Points: 57
Solved Threads: 64
 

You are making it hard on yourself. The board should be 4x4 because those are the playing positions. On output you should add the borders. That would alleviate all the checking to see if you are going to trash a border or not.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
You are making it hard on yourself. The board should be 4x4 because those are the playing positions. On output you should add the borders. That would alleviate all the checking to see if you are going to trash a border or not.


i was blind but now i see thanks!! so your saying something like this:

void printboard(char board[4][4]){
    int i,j,k;
    for(i=0;i<4;i++){
    cout<<board[i][0]<<" | ";
    cout<<board[i][1]<<" | ";
    cout<<board[i][2]<<" | ";
    cout<<board[i][3]<<cout<<endl;
    if(i!=3){cout<<"-------------"<<endl;}}

}    
     
     
     
     
     
     void setgameboard(char board[4][4]){
         int i,j; 
          for(i=0;i<4;i++){for(j=0;j<4;j++){board[i][j]='O';}}
        board[1][4]='\0';  }
     int main()
{

       // -------- Constant declarations --------

       // -------- Variable declarations -------- 
char board[4][4];
        //--------Main Program Body----------
    cout << "**** ***"<<endl<<endl; //title
setgameboard(board);
printboard(board);
  cout << endl << endl;
       system("PAUSE");     // Pause the output window for reading
       return 0;
       }


but i still get gibberish on the end of each line is something wrong with my null?

beejay321
Light Poster
25 posts since Jan 2011
Reputation Points: 7
Solved Threads: 0
 

Line 19: what's that for? Again, since you are using the index of "4", that's past the end of the array (an array declared "char board[4,4]" will have indexes of 0..3 and 0..3, respectively.

Also, line 7 should be:

cout<<board[i][3]<<endl;

The extra "cout" would cause problems.

mcriscolo
Posting Whiz in Training
218 posts since Apr 2008
Reputation Points: 57
Solved Threads: 64
 

Please work on your formatting. Your code is difficult to follow. This should help.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: