6 Replies - 1021 Views - Last Post: 10 October 2010 - 01:10 AM Rate Topic: -----

#1 light09   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 21-November 09

Update Board in vector

Posted 29 September 2010 - 01:39 AM

The following is a checkers like game implemented with 2d vector.( No compile error)
I have tried to update the board but it does not work.
I can't figure it out because i have passed the board by reference so the change should take place.
I believe the logical error lays at line 218 onwards.
I am still debugging but if could see the logical error that i created, please do let me know.
Thank you.
#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;

void DisplayBoard( vector< vector<char> > & board);
void move( vector<  vector<char> > &  board);




int main()
{
  vector<vector<char> > board ( 7, vector<char>( 7 ) );
  char p1, p3;





 DisplayBoard (board);
 move(board);
 DisplayBoard (board);
 





return 0;
}

void DisplayBoard(  vector<  vector<char> >&  board )
{
      for ( int i  = 0; i < 7; ++i)
  {

         for ( int j = 0; j < 7; ++j)
           {
                    board[i][j] = '1';

switch(i) {
  case 0:
    switch(j) {
      case 0:
      case 1:
      case 5:
      case 6:
   		board[i] [j] = ' ';
   		break;
   	}
   	break;
  case 1:
    switch(j) {
      case 0:
      case 1:
      case 5:
      case 6:
   		board[i] [j] = ' ';
   		break;
   	}
   	break;
   	case 3 :
   	if( j == 3) {
   	    board[i][j] = '0';
   	    break;
   	}
   	break;



  case 5:
    switch(j) {
      case 0:
      case 1:
      case 5:
      case 6:
   		board[i] [j] = ' ';
   		break;
   	}
   	break;
  case 6:
      switch(j) {
      case 0:
      case 1:
      case 5:
      case 6:
   		board[i] [j] = ' ';
   		break;
      }
      break;
      }
  }
  }



     for ( int i = 0; i < 7; ++i ) {
         if (i == 0)
         cout  << "A" << setw(1)<< "|" << " ";
         if ( i == 1)
         cout  << "B" << setw(1)<< "|" << " ";
         if ( i == 2)
         cout  << "C" << setw(1)<< "|" << " ";
         if ( i == 3)
         cout  << "D" << setw(1)<< "|" << " ";
         if ( i == 4)
         cout  << "E" << setw(1)<< "|" << " ";
         if ( i == 5)
         cout  << "F" << setw(1)<< "|" << " ";
         if ( i == 6)
         cout  << "G" << setw(1)<< "|" << " ";


    for ( int j = 0; j < 7; ++j )
     {
      cout << board[i][j] <<' ';

    }
    cout<<"\n";
  }
  cout << " +----------------"<<endl;
}

void move( vector<  vector<char> > &  board)
{

    char a1, a2,p1,p2,p3;
    int j1, j2;
    cout << "From : ";
    cin >> a1 >> j1;



    switch(a1) {
  case 'A' : a1 = 0;
                 break;
  case 'B' :a1 = 1;
                 break;
  case 'C' : a1 = 2;
                  break;
  case 'D' : a1 = 3;
                 break;
  case 'E' : a1 = 4;
                 break;
  case 'F' : a1 = 5;
                  break;
  case 'G' : a1 = 6;
                  break;

    }
    switch( j1)
{
    case 1 : j1 = 0;
                 break;
    case 2 : j1 = 1;
                  break;
     case 3 : j1 = 2;
                  break;
      case 4 : j1 = 3;
                    break;
       case 5 : j1 = 4;
                     break;
        case 6 : j1 = 5;
                     break;
         case 7 : j1 =  6;
                       break;
}





     cout << "To : ";
    cin >>a2 >>j2;
    switch(a2) {
         case 'A' : a2 = 0;
                 break;
  case 'B' :  a2 = 1;
                 break;
  case 'C' : a2 = 2;
                  break;
  case 'D' : a2 = 3;
                 break;
  case 'E' : a2 = 4;
                 break;
  case 'F' : a2 = 5;
                  break;
  case 'G' : a2 = 6;
                  break;
}

switch( j2)
{
    case 1 : j2 = 0;
                 break;
    case 2 : j2 = 1;
                  break;
     case 3 : j2 = 2;
                  break;
      case 4 : j2 = 3;
                    break;
       case 5 : j2 = 4;
                     break;
        case 6 : j2 = 5;
                     break;
         case 7 : j2 =  6;
                       break;
}
    p1 = board[a1][j1];
    p3 = board [a2][ j2];

  if ( a1 == a2)
  p2 = board[a1][j1 + 1];
  else
  p2 = board[ a1 + 1] [j1];

     if ( p1 == '1' && p2 == '1' && p3 == '0')
     {
        p1 = '0';
        p2 = '0';
        p3 = '1';
      
   }
     else
      cout << "Invalid move " << endl;
  }

















Is This A Good Question/Topic? 0
  • +

Replies To: Update Board in vector

#2 light09   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 21-November 09

Re: Update Board in vector

Posted 29 September 2010 - 02:03 AM

I realized some ambiguity in my programming style.
When the user is prompt with from : user should be able to elect
the position that they want.
I just updated that portion.
Attached is the updated code.
If you have any constructive feedback on the way i wrote the code,
please post them.
Thank you.
#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;

void DisplayBoard( vector< vector<char> > & board);
void move( vector<  vector<char> > &  board);




int main()
{
  vector<vector<char> > board ( 7, vector<char>( 7 ) );
  char p1, p3;
 






 DisplayBoard (board);
 move(board);







return 0;
}

void DisplayBoard(  vector<  vector<char> >&  board )
{
      for ( int i  = 0; i < 7; ++i)
  {

         for ( int j = 0; j < 7; ++j)
           {
                    board[i][j] = '1';

switch(i) {
  case 0:
    switch(j) {
      case 0:
      case 1:
      case 5:
      case 6:
   		board[i] [j] = ' ';
   		break;
   	}
   	break;
  case 1:
    switch(j) {
      case 0:
      case 1:
      case 5:
      case 6:
   		board[i] [j] = ' ';
   		break;
   	}
   	break;
   	case 3 :
   	if( j == 3) {
   	    board[i][j] = '0';
   	    break;
   	}
   	break;



  case 5:
    switch(j) {
      case 0:
      case 1:
      case 5:
      case 6:
   		board[i] [j] = ' ';
   		break;
   	}
   	break;
  case 6:
      switch(j) {
      case 0:
      case 1:
      case 5:
      case 6:
   		board[i] [j] = ' ';
   		break;
      }
      break;
      }
  }
  }



     for ( int i = 0; i < 7; ++i ) {
         if (i == 0)
         cout  << "A" << setw(1)<< "|" << " ";
         if ( i == 1)
         cout  << "B" << setw(1)<< "|" << " ";
         if ( i == 2)
         cout  << "C" << setw(1)<< "|" << " ";
         if ( i == 3)
         cout  << "D" << setw(1)<< "|" << " ";
         if ( i == 4)
         cout  << "E" << setw(1)<< "|" << " ";
         if ( i == 5)
         cout  << "F" << setw(1)<< "|" << " ";
         if ( i == 6)
         cout  << "G" << setw(1)<< "|" << " ";


    for ( int j = 0; j < 7; ++j )
     {
      cout << board[i][j] <<' ';

    }
    cout<<"\n";
  }
  cout << " +----------------"<<endl;
  cout <<setw(4) << " 1" << setw(1) << " 2" << setw(1) << " 3" << setw(1) <<  " 4" << setw(1) << " 5" << setw(1)
          <<" 6" << setw(1) <<  " 7" << setw(1) << endl;
}

void move( vector<  vector<char> > &  board)
{

    char a1, a2,p1,p2,p3;
    int j1, j2;
    
    cout << "From : ";
    cin >> a1 >> j1;



    switch(a1) {
  case 'A' : a1 = 0;
                 break;
  case 'B' :a1 = 1;
                 break;
  case 'C' : a1 = 2;
                  break;
  case 'D' : a1 = 3;
                 break;
  case 'E' : a1 = 4;
                 break;
  case 'F' : a1 = 5;
                  break;
  case 'G' : a1 = 6;
                  break;

    }
    switch( j1)
{
    case 1 : j1 = 0;
                 break;
    case 2 : j1 = 1;
                  break;
     case 3 : j1 = 2;
                  break;
      case 4 : j1 = 3;
                    break;
       case 5 : j1 = 4;
                     break;
        case 6 : j1 = 5;
                     break;
         case 7 : j1 =  6;
                       break;
}





     cout << "To : ";
    cin >>a2 >>j2;
    switch(a2) {
         case 'A' : a2 = 0;
                 break;
  case 'B' :  a2 = 1;
                 break;
  case 'C' : a2 = 2;
                  break;
  case 'D' : a2 = 3;
                 break;
  case 'E' : a2 = 4;
                 break;
  case 'F' : a2 = 5;
                  break;
  case 'G' : a2 = 6;
                  break;
}

switch( j2)
{
    case 1 : j2 = 0;
                 break;
    case 2 : j2 = 1;
                  break;
     case 3 : j2 = 2;
                  break;
      case 4 : j2 = 3;
                    break;
       case 5 : j2 = 4;
                     break;
        case 6 : j2 = 5;
                     break;
         case 7 : j2 =  6;
                       break;
}
    p1 = board[a1][j1];
    p3 = board [a2][ j2];

  if ( a1 == a2)
  p2 = board[a1][j1 + 1];
  else
  p2 = board[ a1 + 1] [j1];

     if ( p1 == '1' && p2 == '1' && p3 == '0')
     {
        p1 = '0';
        p2 = '0';
        p3 = '1';
     

   }
     else
      cout << "Invalid move " << endl;

  }

















Was This Post Helpful? 0
  • +
  • -

#3 light09   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 21-November 09

Re: Update Board in vector

Posted 29 September 2010 - 02:22 AM

Another really important key point that i missed out is how to play the game.
The 1's and 0's represent tokens.Only Horizontal and Vertical moves are allowed.
Absolutely no diagonal moves.
The player is asked to enter the From(original position) and To( end position).
And the move is only valid if the new position( the To position that user keyed in)
is a token 0 and the original position( the From position that user keyed in is ) is 1.
And in between the From and To function must be a token 1.
You could take a look at .exe program for a clearer look.
Say the user keys in the following:
From : B4
To: D4
after this the board should update B4 and C4 to have token 0 but D4 will now be
token 1. I hope i had been clear.
Was This Post Helpful? 0
  • +
  • -

#4 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Update Board in vector

Posted 29 September 2010 - 03:03 AM

Sorry if this is a stupid question but do you still have a question here?
Probably worth restating it so we are clear on what help you are seeking.
Was This Post Helpful? 0
  • +
  • -

#5 light09   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 21-November 09

Re: Update Board in vector

Posted 29 September 2010 - 09:31 PM

hi janotte,
Thank you for your good intention.
Actually i still do. I'm curious to know why the tokens on the board are not updated
after the check has been validated.
When i was debugging i could cout the validated value(which are p1,p2,p3) so nothing wrong with the function(its working as it ought to)
just something with the update board..
Still am trying to put the puzzles together.
Any constructive feedback is highly regarded. Thank you.
Was This Post Helpful? 0
  • +
  • -

#6 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Update Board in vector

Posted 30 September 2010 - 04:55 AM

Okay.
I am quite bored so I went through your code and aligned everything using a single indentation and brace placement style.
This may seem silly but if you do this to code you are having trouble with you might be surprised at how many code problems leap out at you while you are doing housekeeping. It is a good way to let your left brain / right brain cross over stuff see patterns and the like.

So here is your code laid out in a pretty fashion.
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;

void DisplayBoard(vector< vector<char> > & board);
void move(vector< vector<char> > &  board);

int main(){
	vector<vector<char> > board(7, vector<char>(7));
	char p1, p3;

	DisplayBoard (board);
	move(board);
	
	return 0;
}

void DisplayBoard(vector< vector<char> > & board){
	for(int i = 0; i < 7; ++i){
		for(int j = 0; j < 7; ++j){
			board[i][j] = '1';

			switch(i){
  				case 0:
    				switch(j){
      					case 0:
      					case 1:
      					case 5:
      					case 6:
   							board[i][j] = ' ';
   							break;
   					}
   					break;
  				case 1:
    				switch(j){
      					case 0:
					    case 1:
					    case 5:
					    case 6:
					   		board[i] [j] = ' ';
					   		break;
					}
   					break;
   				case 3:
   					if( j == 3){
   	    				board[i][j] = '0';
   	    				break;
   					}
   					break;
				case 5:
    				switch(j){
      					case 0:
      					case 1:
      					case 5:
      					case 6:
   							board[i][j] = ' ';
   							break;
   					}
   					break;
  				case 6:
      				switch(j){
      					case 0:
      					case 1:
      					case 5:
      					case 6:
   							board[i][j] = ' ';
   							break;
      				}
      				break;
      		} //end switch
  		} //end for
  	} //end for
	
	for(int i = 0; i < 7; ++i){
    	if(i == 0){
        	cout  << "A" << setw(1)<< "|" << " ";
		}
        if(i == 1){
         	cout  << "B" << setw(1)<< "|" << " ";
		}
        if(i == 2){
         	cout  << "C" << setw(1)<< "|" << " ";
		}
        if(i == 3){
        	cout  << "D" << setw(1)<< "|" << " ";
		}
        if(i == 4){
        	cout  << "E" << setw(1)<< "|" << " ";
		}
        if(i == 5){
        	cout  << "F" << setw(1)<< "|" << " ";
		}
        if(i == 6){
        	cout  << "G" << setw(1)<< "|" << " ";
		}
	
		for(int j = 0; j < 7; ++j ){
			cout << board[i][j] <<' ';
		}
    	cout<<"\n";
	} //end for
	
	cout << " +----------------"<<endl;
	cout << setw(4) << " 1" 
		 << setw(1) << " 2" 
		 << setw(1) << " 3" 
		 << setw(1) << " 4" 
		 << setw(1) << " 5" 
		 << setw(1) << " 6" 
		 << setw(1) << " 7" 
		 << setw(1) << endl;
} // end DisplayBoard()


void move(vector< vector<char> > & board){
	char a1, a2, p1, p2, p3;
    int  j1, j2;

	cout << "From : ";
	cin >> a1 >> j1;
	
	switch(a1){
		case 'A':
			a1 = 0;
            break;
  		case 'B':
			a1 = 1;
            break;
  		case 'C':
			a1 = 2;
            break;
  		case 'D':
			a1 = 3;
            break;
  		case 'E':
			a1 = 4;
            break;
  		case 'F':
			a1 = 5;
            break;
  		case 'G':
			a1 = 6;
            break;
    } //end switch

	switch(j1){
    	case 1:
			j1 = 0;
            break;
    	case 2:
			j1 = 1;
            break;
     	case 3:
			j1 = 2;
            break;
      	case 4:
			j1 = 3;
            break;
       	case 5:
			j1 = 4;
            break;
        case 6:
			j1 = 5;
            break;
        case 7:
			j1 = 6;
            break;
	} //end switch

	cout << "To : ";
    cin >>a2 >>j2;
    
	switch(a2){
		case 'A': 
			a2 = 0;
            break;
  		case 'B':
			a2 = 1;
            break;
  		case 'C':
			a2 = 2;
            break;
  		case 'D':
			a2 = 3;
            break;
  		case 'E':
			a2 = 4;
            break;
  		case 'F':
			a2 = 5;
            break;
  		case 'G':
			a2 = 6;
            break;
	} //end switch

	switch(j2){
    	case 1:
			j2 = 0;
            break;
    	case 2:
			j2 = 1;
            break;
     	case 3:
			j2 = 2;
            break;
      	case 4:
			j2 = 3;
            break;
       	case 5:
			j2 = 4;
            break;
        case 6:
			j2 = 5;
            break;
        case 7:
			j2 = 6;
            break;
	} //end switch
	
    p1 = board[a1][j1];
    p3 = board[a2][j2];

	if(a1 == a2){
		p2 = board[a1][j1 + 1];
	}else{
  		p2 = board[ a1 + 1] [j1];
	}

    if(p1 == '1' && p2 == '1' && p3 == '0'){
		p1 = '0';
        p2 = '0';
        p3 = '1';
    }else{
      cout << "Invalid move " << endl;
    }
} //end move()




You'll notice I have introduced an innovation to your code.
Comments.
You could do a lot worse than adding some of them to your code so others (and yourself in a few months time) have some idea what is supposed to be happening where.

Now to suggest some refactoring.
You aren't taking enough advantage of the fact that characters in your code are represented by an integer value in the ASCII table. Take advantage of this to massively reduce some of the ugly clumps of code doing nothing other than converting characters to numbers and visa versa.
For example.
Look at this for() loop:
     for(int i = 0; i < 7; ++i){
         if(i == 0){
             cout  << "A" << setw(1)<< "|" << " ";
         }
         if(i == 1){
             cout  << "B" << setw(1)<< "|" << " ";
         }
         if(i == 2){
             cout  << "C" << setw(1)<< "|" << " ";
         }
         if(i == 3){
             cout  << "D" << setw(1)<< "|" << " ";
         }
         if(i == 4){
             cout  << "E" << setw(1)<< "|" << " ";
         }
         if(i == 5){
             cout  << "F" << setw(1)<< "|" << " ";
         }
         if(i == 6){
             cout  << "G" << setw(1)<< "|" << " ";
         }
    
        for(int j = 0; j < 7; ++j ){
            cout << board[i][j] <<' ';
        }
        cout<<"\n";
    } //end for


All that is doing is performing a manual conversion from an integer value to a character.
No need for that. Let the machine do the converting.
    for(int i = 0; i < 7; ++i){
		char rowLtr = 'A' + i;
        cout  << rowLtr << setw(1)<< "|" << " ";    
        for(int j = 0; j < 7; ++j ){
            cout << board[i][j] <<' ';
        }
        cout<<"\n";
    } //end for


Now that's a moderately obscure little bit of code there.
It would be a really good idea to put a comment on it that will explain what it does (not how it does it - the code is how - your comments should not also talk about how) and if it is a very obscure idea then why you choose that approach. This bit of code does not need a 'why' comment. Just a 'what' comment.

Do you think that is neater than your answer?
You could do something similar to print the column numbers in the immediately following bit of code too.
However the win there is much less so not as critical.

Let's look at a fairly grievous bit of unnecessary code. This is not good:
    switch(j1){
        case 1:
            j1 = 0;
            break;
        case 2:
            j1 = 1;
            break;
        case 3:
            j1 = 2;
            break;
        case 4:
            j1 = 3;
            break;
        case 5:
            j1 = 4;
            break;
        case 6:
            j1 = 5;
            break;
        case 7:
            j1 = 6;
            break;
    } //end switch


What you are doing there is using a switch to say if the number input to 'j1' = 'n' then set 'j1' to 'n-1'.
That is no task for human to be typing out. It's a single statement. Let the machine do it. Like this.
j1 = j1-1


That is the total effect of that whole switch.
The same applies for quite a lot of other nastiness in your code including almost identical switch statements.

Now you may say "hey but I was using that ugly switch to ensure the input was between 1 and 7. How will I enforce that now."
Well how about like this:
if(j1 >= 1 && j1 <=7){
    j1--;  // do you see why this is the same as j1 = j1-1?
}else{
    cout << "Invalid Input" << endl;
}


Of course you want to be a lot cleverer than that and not just throw a message but that's all you are doing now so that's all I did.

Do these give you a few examples of ways you might improve your code?
How about you try out these and anything else that occurs to you as a result of these ideas.
Once you have your code trimmed down to size you may well find fixing your bugs is a lot easier.

This post has been edited by janotte: 30 September 2010 - 05:16 AM

Was This Post Helpful? 2
  • +
  • -

#7 light09   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 141
  • Joined: 21-November 09

Re: Update Board in vector

Posted 10 October 2010 - 01:10 AM

Janotte the new look that you gave to the code i wrote is just beyond beautiful. I thank you
for taking the time to reply to my post and for educating me on ways to write code efficiently.
Debugging is indeed simplified. I really do appreciate the feedback. Genuinely thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1