Thread: Simple Multi-Array Chess Piece Relative Value Problem (Beginner)

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    15

    Simple Multi-Array Chess Piece Relative Value Problem (Beginner)

    Hello all,

    I've been working on a chess piece relative value problem. The user inputs chess piece positions for both White and Black, and the output is the sum of the relative value of chess pieces on the board.

    I have some style issues that I'd like to resolve.

    First, I am using the despicable "goto" statement to repeat my loop. I would rather not do it, but need some help figuring that part out.

    Second, I wish I didn't have to repeat my loop for both White and Black. I'd rather have one loop, if possible. Any suggestions would be appreciated here too.

    Third, if you have any other style suggestions, I would be grateful for the advice.

    Below is the ugly code.

    Code:
    #include <stdio.h>
    
    
    int evaluate_position(char board[8][8]);
    
    
    int main(){
        
        char chess[8][8] = {0};
        int x, y, z, white_number, black_number, row = 0;
        char col = 'a', rank;
        
        printf("Please enter White's pieces on the chessboard\n\n");
        
        printf("How many pieces do you have on the board? ");
        scanf("%d", &white_number);
        
        printf("Please enter the rank and position of your pieces: ");
        
        z = 1;
        
    repeat:
        while (z <= white_number) {
            scanf(" %c%d%c", &rank, &row, &col);
            
            if (row < 9 && row > 0)
            y = row - 1;
            else{
            printf("Not a valid row!\n");
                goto repeat;
            }
            
            
            if (col < 'i' && col >= 'a')
            x = col - 'a';
            else{
            printf("Not a valid column!\n");
                goto repeat;
            }
            
            
            if (chess[y][x] == 0){
               chess[y][x] = rank;
                z++;
            }
            else
                printf("Illegal Move!\n");
        }
        
        printf("\n\nPlease enter Black's pieces on the chessboard\n\n");
        
        printf("How many pieces do you have on the board? ");
        scanf("%d", &black_number);
        
        printf("Please enter the rank and position of your pieces: ");
        
        z = 1;
        
    repeat_b:
        while (z <= white_number) {
            scanf(" %c%d%c", &rank, &row, &col);
            
            if (row < 9 && row > 0)
                y = row - 1;
            else{
                printf("Not a valid row!\n");
                goto repeat_b;
            }
            
            
            if (col < 'i' && col >= 'a')
                x = col - 'a';
            else{
                printf("Not a valid column!\n");
                goto repeat_b;
            }
            
            
            if (chess[y][x] == 0){
                chess[y][x] = rank;
                z++;
            }
            else
                printf("Illegal Move!\n");
        }
    
    
        printf("\nThe total sum of the chess pieces relative value on the board is %d\n", evaluate_position(chess));
        
        if (evaluate_position(chess) > 0)
            printf("\nAdvantage White!\n\n");
        else
            printf("\nAdvantage Black!\n\n");
        
    }
    
    
    int evaluate_position(char board[8][8]){
        
        int sum = 0, sum_b = 0, j = 0;
        
        for (int i = 0; i < 8; i++)
            for (j = 0; j < 8; j++)
                {
            switch(board[i][j]){
                case 'K': sum += 0;
                    break;
                case 'Q': sum += 9;
                    break;
                case 'R': sum += 5;
                    break;
                case 'B': sum += 3;
                    break;
                case 'N': sum += 3;
                    break;
                case 'P': sum += 1;
                    break;
                case 'k': sum_b += 0;
                    break;
                case 'q': sum_b += 9;
                    break;
                case 'r': sum_b += 5;
                    break;
                case 'b': sum_b += 3;
                    break;
                case 'n': sum_b += 3;
                    break;
                case 'p': sum_b += 1;
                    break;
    
    
    
    
            }
        }
        
        return (sum - sum_b);
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,906
    No gotos.

    And better structure.

    If you find yourself doing ctrl-c,ctrl-v on large blocks of code, you should be thinking "make this a function!".

    Code:
    void input ( int numPieces, char chess[8][8] ) {
        int z = 1;
        printf("Please enter the rank and position of your pieces: ");
        while ( z <= numPieces ) {
            scanf(" %c%d%c", &rank, &row, &col);
            if (row < 9 && row > 0)
                y = row - 1;
            else {
                printf("Not a valid row!\n");
                continue;
            }
            if (col < 'i' && col >= 'a')
                x = col - 'a';
            else {
                printf("Not a valid column!\n");
                continue;
            }
             
            if (chess[y][x] == 0){
               chess[y][x] = rank;
                z++;
            }
            else
                printf("Illegal Move!\n");
        }
    }
    
    int main ( ) {
        printf("Please enter White's pieces on the chessboard\n\n");
        printf("How many pieces do you have on the board? ");
        scanf("%d", &white_number);
        input(white_number,chess);
        printf("\n\nPlease enter Black's pieces on the chessboard\n\n");
        printf("How many pieces do you have on the board? ");
        scanf("%d", &black_number);
        input(black_number,chess);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2019
    Posts
    15
    Thank Salem! Appreciate this edit and rule-of-thumb a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 01-11-2019, 08:28 PM
  2. Replies: 3
    Last Post: 01-09-2019, 02:11 PM
  3. multi-threaded chess using winsock
    By akurdas in forum Networking/Device Communication
    Replies: 9
    Last Post: 06-12-2011, 10:38 PM
  4. Creating an array with a piece of a string problem
    By steals10304 in forum C Programming
    Replies: 11
    Last Post: 08-18-2010, 10:26 PM
  5. Problem with simple piece of code
    By Furious5k in forum C++ Programming
    Replies: 10
    Last Post: 12-12-2008, 05:25 PM

Tags for this Thread