Need help with checkers program

This is a discussion on Need help with checkers program within the C++ Programming forums, part of the General Programming Boards category; I am working on a simple checkers program.. however I ran into several errors. My move function doesn't work, and ...

  1. #1
    Kyo
    Guest

    Post Need help with checkers program

    I am working on a simple checkers program.. however I ran into several errors. My move function doesn't work, and only seems to work sometimes. Here's the code: (Please note, program isn't complete, I'm only trying to get the move function to work).

    Code:
    #include<iostream.h>
    #include<apmatrix.h>
    
       void CheckKing();//kings will be implemented later
    
               
       void Move(apmatrix<char> &Board, int row, int col, int row2, int col2, char turn, int &count1, int &count2)
               
       {
          char c=turn;
       
       if(c=='W')
          {
             if(Board[row][col]=='W')
             {
                cout<<"first stage"<<endl;
                if(Board[row-1][col-1]==Board[row2][col2])
                {
                     if((Board[row2][col2]=='R')&&(Board[row2-1][col2-1]==' '))
                   {
                      cout<<"Don't enter"<<endl;
                      Board[row2-1][col2-1]='W';
                      Board[row2][col2]=' ';
                      count1--;
                   }
                   if(Board[row2][col2]==' ')
                   {
                      Board[row2][col2]='W';
                      Board[row][col]=' ';
                   
                   }
                }
             
                if(Board[row-1][col+1]==Board[row2][col2])
                   cout<<" LWJLKSJFLKS:J"<<endl;
                {
                   if((Board[row2][col2]=='R')&&(Board[row2][col2]==' '))
                   {
                      cout<<"Don't enter"<<endl;
                      Board[row2-1][col2-1]='W';
                      Board[row2][col2]=' ';
                      count1--;
                   }
                   if(Board[row2][col2]==' ')
                   {
                      cout<<"Munching"<<endl;
                      Board[row2][col2]='W';
                      Board[row][col]=' ';
                   }
                }
             }
          }
          if(c=='R')
          {
             if(Board[row][col]=='W')
             {
                if(Board[row+1][col+1]==Board[row2][col2])
                {
                   if((Board[row2][col2]=='W')&&(Board[row2+1][col2+1]==' '))
                   {
                   
                      Board[row2-1][col2-1]='R';
                      Board[row2][col2]=' ';
                      count2--;
                   }
                   if(Board[row2][col2]==' ')
                   {
                      Board[row2][col2]='R';
                      Board[row][col]=' ';
                   
                   }
                }
                if(Board[row+1][col-1]==Board[row2][col2])
                {
                   if((Board[row2][col2]=='W')&&(Board[row2+1][col2-1]==' '))
                   {
                   
                      Board[row2-1][col2-1]='R';
                      Board[row2][col2]=' ';
                      count2--;
                   }
                   if(Board[row2][col2]==' ')
                   {
                      Board[row2][col2]='R';
                      Board[row][col]=' ';
                   
                   }
                }
             }
          }
       }
    
    
    
    
               
       apmatrix<char> Make(apmatrix<char> a)
               
       {
          cout<<"MAKING"<<endl;
          for(int k=1;k<=7;k=k+2)
             a[0][k]='R';
          for(int k=0;k<=6;k=k+2)
             a[1][k]='R';
          for(int k=1;k<=7;k=k+2)
             a[2][k]='R';
       
       
          for(int k=0;k<=6;k=k+2)
             a[5][k]='W';
          for(int k=1;k<=7;k=k+2)
             a[6][k]='W';
          for(int k=0;k<=6;k=k+2)
             a[7][k]='W';
          return(a);
       
       }
    
               
       void Display(apmatrix<char> Board)
               
       {
          int count=0;
          int count2=0;
          cout<<"DISPLAY"<<endl;
          for(int k=0; k<=7; k++)
          {
             cout<<endl;
             if(k==0)
                cout<<"  0 1 2 3 4 5 6 7"<<endl;
             cout<<count;
          
             cout<<"|";
             count++;
          
             for(int i=0; i<=7; i++)
             {
                cout<<Board[k][i];
                cout<<"|";
             
             }
          }
          cout<<endl<<endl;
       }
    
               
       int main()
               
       {
          int row;
          int col;
          int row2;
          int col2;
          int count1=12;
          int count2=12;
          char db='W';
          apmatrix<char> a(8,8,' ');
          a=Make(a);
          while(db)
          {
          
          
             Display(a);
             cout<<"Hello, and welcome!"<<endl;
             cout<<"First player, enter the first move"<<endl;
             cin>>row;
             cin>>col;
             cin>>row2;
             cin>>col2;
             Move(a,row,col,row2,col2,db,count1,count2);
             cout<<"DONE"<<endl;
             Display(a);
          }
       
       
       
       
       
       
        return(0);
       }
    And ignore the cout statements, those were for my debugging purposes.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Can't help you too much because I don't have the AP classes anymore (and to tell the truth, I couldn't stand them), but here is some potential advice about your board representation, and getting things to work.

    The fact that checkers is played on an 8x8 board is irrelevant since only a 4x4 subset is used. Just keeping the 4x4 may make things easier. Now, note if you shift the sqaures over on the 8x8 board, we see that on the bottom row, pieces can only move straight up or to the left (no left on the left boundary of course). On the next row, they can move straight up or right (except the boundary), and repeats this one more time. (Note, I am assuming playing on black squares with a white square on the lower right - if you set up the board differently, the rights and lefts may be reversed).

    Also note that jumping is a knight-like move (from chess), but the pieces must still follow the directional rules from above.

    Hopes this helps somewhat.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21