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

Debug this code

/*
functions:
          main >> output instructions
                  input P1 name, p2 name
                  loop{-clearscreen-
                       output board, compute scores
                       P1 or P2 turn, output scores
                       input placement of next piece
                       check coordinates and flip away}
                  loop ends when a)P1_score+P2_score=100
                                 b)checker declares there are no moves left
                                 c)Q is entered instead of proper coordinates
                  ask if player wants another game
          
          checker >> determine whether coordinate input
                     is valid - within board, adjacent to
                     appropriate pieces
                     if input is Q: ask whether player wishes to quit
                     
          flipper >> checks horizontally, vertically and diagonally
                     for pieces that ought to be flipped and flips them
                     ->might be split in three: fliph, flipv and flipd
                     
arrays:
       board[11][11] -> play area is 10X10. 
                        row[0] and col[0]for displaying coordinates.
       Pl_name[2][8] -> luxury. Player names, 7 char max.
       coords[3] -> coordinates for next piece
       
switches:
         quit -> if yes: quit, no: continue, default: ask again;
                 best be in a loop (for default)
         play_again -> if yes: another game, no: exit program
                       default: ask again (also looped)
*/

#include<iostream>
#include<cstdio>
using namespace std;

char board[11][11];
char Pl_name[2][8];
char coords[3];
int a,b,i,j,k,x,y,z;
char row,col,pl,op;
int check,turn;

void flipH(int rw, int cl);
void flipV(int rw, int cl);
void flipD(int rw, int cl);
void coordconv (char r, char c);

int main(){
    
    //initiate board. first row values were placed manually
    //as i don't know how to loop chars
    board[0][0]='.';board[0][1]='A';board[0][2]='B';board[0][3]='C';
    board[0][4]='D';board[0][5]='E';board[0][6]='F';board[0][7]='G';
    board[0][8]='H';board[0][9]='I';board[0][10]='J';board[1][0]='1';
    board[2][0]='2';board[3][0]='3';board[4][0]='4';board[5][0]='5';
    board[6][0]='6';board[7][0]='7';board[8][0]='8';board[9][0]='9';
    board[10][0]='0';
    for(i=1;i<11;i++){
        for(j=1;j<11;j++){
            board[i][j]='_';
            }}
    board[5][6]='X';board[6][5]='X';
    board[5][5]='O';board[6][6]='O';
    
    //game loop starts here. z is max number of turns. 100 is total
    //number of pieces available for play
    for(z=0;z<100;z++){
    
    //clearscreen. duh. para malinis ang dos
    system("cls");
    
    //odd or even z determines whether it is P1 or P2's turn
    //odd:player(pl) is X, opponent(op) is O eve:vice versa.
    if(z%2==0){pl='X'; op='O'; cout<<"\nPlayer1's turn(X)\n";}
    else {pl='O';op='X'; cout<<"\nPlayer2's turn(O)\n";}
    
    //outputs number of turns so far
    cout<<"\nTurn: "<<z+1<<endl;
    
    //outputs board
    for(i=0;i<11;i++){
        for(j=0;j<11;j++){
            cout<<board[i][j]<<"   ";
            }
        cout<<endl<<endl;
        }
    
    //string coords, because I insist upon using letters for columns
    //coords input is looped, to make sure they don't mess up the game
    check=1;
    do{
    cout<<"Enter coordinates(9B,4A,etc.): ";
    gets(coords);
    row=coords[0];
    col=coords[1];
    //coordcheck function converts string coordinates to integers
    coordconv(row,col);
    }while(check==1);
    //flipV(x,y);
    
    if(board[x+1][y]==op || board[x-1][y]==op){
        flipV(x,y);}
    if(board[x][y+1]==op || board[x][y-1]==op){
        flipH(x,y);}
    if(board[x+1][y+1]==op||board[x-1][y-1]==op){
        flipD(x,y);}
    if(board[x+1][y-1]==op||board[x-1][y+1]==op){
        flipD(x,y);}
    
    
    }
    
    
    system("pause");
    return 0;
}

void coordconv (char r, char c){
     bool adj, end;
     int opcount, endcoord;
     
    switch (row) {
           case '1':
                x=1;check=0;break;
           case '2':
                x=2;check=0;break;
           case '3':
                x=3;check=0;break;
           case '4':
                x=4;check=0;break;
           case '5':
                x=5;check=0;break;
           case '6':
                x=6;check=0;break;
           case '7':
                x=7;check=0;break;
           case '8':
                x=8;check=0;break;
           case '9':
                x=9;check=0;break;
           case '0':
                x=10;check=0;break;
           default:
                   cout<<"invalid input"<<endl;
                   check=1;
                   }
    switch(col){
           case 'A':
                y=1;check=0;break;
           case 'B':
                y=2;check=0;break;
           case 'C':
                y=3;check=0;break;
           case 'D':
                y=4;check=0;break;
           case 'E':
                y=5;check=0;break;
           case 'F':
                y=6;check=0;break;
           case 'G':
                y=7;check=0;break;
           case 'H':
                y=8;check=0;break;
           case 'I':
                y=9;check=0;break;
           case 'J':
                y=10;check=0;break;
           default:
                   cout<<"invalid input"<<endl;
                   check=1;
                   }
    
    
    //following lines must determine whether entered coordinates comply
    //with othello rules. <s>will</s> MUST code these later.
    
    
    
    
}

void flipV(int rw, int cl){
     int l,m,n;
     bool adj=false, end=false;
     int opcount, endcoord;
     char dir;
          
     //vertcheck
     if(board[x+1][y]==op){
     adj=true;
     for(l=1;l<(11-x);l++){
         if(board[x+l][y]==pl){
             end=true; 
             dir='u';
             endcoord=(x+l);
             //cout<<"\nendcoord= "<<endcoord<<endl; 
             break;}
         }}
     if(board[x-1][y]==op){
     adj=true;
     for(l=1;l<x;l++){
         if(board[x-l][y]==pl){
             end=true;         
             dir='d';
             endcoord=(x-l);
             break;}
         }}
     
     //flip time!
     if(adj==true && end==true){
         if(dir='u'){
                    for(m=1;x+m<endcoord;m++){
                        board[x+m][y]=pl;
                        board[x][y]=pl;}}
         if(dir='d') {
              cout<<"\ncheckpt 1\n";
              for(n=1;x-n>endcoord;n++){
                  cout<<"\ncheckpt2\n";
                  board[x-n][y]=pl;
                  board[x][y]=pl;}
                  }
         }//end flipH
         //system("pause");
}

void flipH(int rw, int cl){
     int l,m,n;
     bool adj=false, end=false;
     int opcount, endcoord;
     char dir;
          
     //horzcheck
     //if((board[x][y+1]==op)||(board[x][y-1]==op))adj=true;
     if(board[x][y+1]==op){
     adj=true;
     for(l=1;l<(11-x);l++){
         if(board[x][y+l]==pl){
             end=true; 
             dir='r';
             endcoord=(y+l);
             cout<<"\nendcoord= "<<endcoord<<endl; 
             break;}
         }}
     if(board[x][y-1]==op){
     adj=true;
     for(l=1;l<y;l++){
         if(board[x][y-l]==pl){
             end=true; 
             dir='l';
             endcoord=(y-l);
             break;}
         }}
     
     //flip time!
     if(adj==true && end==true){
         if (dir=='r'){
                    for(m=1;y+m<endcoord;m++){
                        board[x][y+m]=pl;
                        board[x][y]=pl;
                        }}
         if (dir=='l'){
              cout<<"\ncheckpt1b\n";
              for(n=1;y-n>endcoord;n++){
                  board[x][y-n]=pl;
                  board[x][y]=pl;}
                  }
         }
         //system("pause");
     }

void flipD(int rw, int cl){
     int l,m,n,o;
     bool adj=false, end=false;
     int opcount,endrow,endcol;
     char dir;
          
     //diagcheck
     if(board[x+1][y+1]==op){cout<<"\ncheckpt1\n";
     adj=true;
     for(l=1,m=1;m<(11-x)&&l<(11-y);l++,m++){
         if(board[x+m][y+l]==pl){
             end=true; 
             dir='4';
             endcol=(y+l);
             endrow=(x+m);
             break;}
         }}
     if(board[x-1][y-1]==op){
     adj=true;
     for(m=1,l=1;(m<x)&&(l<y);l++,m++){
         if(board[x-m][y-l]==pl){
             end=true; 
             dir='2';
             endcol=(y-l);
             endrow=(x-m);
             break;}
         }}
     if(board[x+1][y-1]==op){cout<<"\ncheckpt2\n";
     adj=true;
     for(m=1,l=1;(m<(11-x))&&(l<y);l++,m++){
         if(board[x+m][y-l]==pl){
             end=true; 
             dir='3';
             endcol=(y-l);
             endrow=(x+m);
             break;}
         }}
     if(board[x-1][y+1]==op){
     adj=true;
     for(m=1,l=1;(m<x)&&(l<(11-y));l++,m++){
         if(board[x-m][y+l]==pl){
             end=true; 
             dir='1';
             endcol=(y+l);
             endrow=(x-m);
             break;}
         }}
     
     
     //flip time!
     if(adj==true && end==true){
         if (dir=='4'){cout<<"\ncheckpt1B\n";
                    for(l=1,m=1;(y+l<endcol)&&(x+m<endrow);l++,m++){
                        board[x+m][y+l]=pl;
                        board[x][y]=pl;
                        }}
         if (dir=='3'){cout<<"\ncheckpt2B\n";
                    for(l=1,m=1;(y-l>endcol)&&(x+m<endrow);l++,m++){
                        board[x+m][y-l]=pl;
                        board[x][y]=pl;
                        }}
         if (dir=='2'){
                    for(l=1,m=1;(y-l>endcol)&&(x-m>endrow);l++,m++){
                        board[x-m][y-l]=pl;
                        board[x][y]=pl;
                        }}
         if (dir=='1'){
                    for(l=1,m=1;(y+l<endcol)&&(x-m>endrow);l++,m++){
                        board[x-m][y+l]=pl;
                        board[x][y]=pl;
                        }}
         }
     //system("pause");
     }


there seems to be an error for my reversi code

silverpanda
Newbie Poster
4 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

What do you need help with?

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

Um... We're willing to help, but we're not stool pigeons. You actually have to do something yourself first.

Have you attempted any of your own debugging? If so what have you tried and what have you found?

Are you getting compiler errors? What are they?

etc...

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

i compiled it and it works

but sometimes when i type in the coordinates the output/piece does not show.

silverpanda
Newbie Poster
4 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Press "F5". Winrar!

pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
 

This article has been dead for over three months

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