Thread: Help in checkers game!

  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    3

    Help in checkers game!

    Please help me i have this due soon,also here is my source code for the game, does anybody know what i should do to make the pieces move? thank you.
    Code:
    #include <stdio.h>
    #include <Windows.h>  //include the windows API to print unicode
     //allows print unicode
    
    
    //Sets-Up Checker Pieces by giving a value in a multidimensional array.
    void initializeBoard(int board[8][8])
    {
    int i, j, k;
        
        for(i = 1; i < 9; i++)
        {
            for(j = 1; j < 9; j++)
            {
                if(((i == 6)&&(j%2 == 1))||((i == 7)&&(j%2 == 0))||((i == 8)&&(j%2 == 1))) //Player 1 Pieces; valid square value is changed to 1
                {
                    board[i][j] = 1;
                }            
                else if(((i == 1)&&(j%2 == 0))||((i == 2)&&(j%2 == 1))||((i == 3)&&(j%2 == 0))) //Player 2 Pieces; valid square value is changed to 2
                {
                    board[i][j] = 2;
                }   
                else if((i + j)%2 == 1) //Checks all Valid Squares and value is set to 3
                {
                    board[i][j] = 3;
                }          
                else
                {
                    board[i][j] = 0;
                }
            }
        }
    }
    void printBoard(int board[8][8])
    {
        int i, j, k;
        int a = 0; //piece number for black
        int b = 13; //piece number for white
        //print column number
        
        for(k = 1; k != 9; k++)
        {
            printf("  %d ",k);
        }
        //print board
        printf("\n+---+---+---+---+---+---+---+---+\n"); //top row divider
        for(i = 0; i <9; i++) //prints row with i being the number of rows
        {
            for(j = 0; j <9; j++)//prints column with j being the number of rows
            {
                if(board[i][j] == 1)
                {
                    //spacing issues
                    a += 1; 
                    if(a < 10) //if a single digit print piece with space
                    {
                        printf("|D%d ", a);
                    }
                    else //if not single digit print piece without space
                    {
                        printf("|D%d", a); 
                    }
                }
                else if(board[i][j] == 2)
                {
                    //spacing issues
                    b -= 1; 
                    if(b < 10) //if a single digit print piece with space
                    {
                        printf("|L%d ", b);
                    }
                    else //if not single digit print piece without space
                    {
                        printf("|L%d", b); 
                    }
                }
                else if(board[i][j] == 3)
                {
                    printf("| â–  ");
                }
                else
                {
                    printf("|   ");//print a column
                }
            }
            printf("| %d", i); //outer right side border with row numbers
            printf("\n"); //new line to signify new row
            printf("+---+---+---+---+---+---+---+---+\n"); //row divider
        }
    }
    int main()
    {
        SetConsoleOutputCP(CP_UTF8);
        int board[8][8]; //board
        initializeBoard(board);
        printBoard(board);
        return 0;
    }
    Last edited by helpmepls123; 12-02-2022 at 04:27 AM. Reason: Different code.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for(i = 1; i <9; i++)
    Array subscripts start at 0, not 1
    So you're wandering off the ends of the board array.

    You can avoid all the if/else to determine the field width by just doing this.
    Code:
    printf("|D%2d ", a);
    Initialise the board.
    Code:
    int board[8][8] = { { 0 } }; //board
    But you actually need to initialise the top two ranks with white pieces, and the bottom two ranks with black pieces, as per the layout of a checkers game.


    > does anybody know what i should do to make the pieces move? thank you.
    Well the first thing to do would be to prompt "which piece do you want to move" followed by "where do you want to move it to".
    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
    Dec 2022
    Posts
    3
    Quote Originally Posted by Salem View Post
    > for(i = 1; i <9; i++)
    Array subscripts start at 0, not 1
    So you're wandering off the ends of the board array.

    You can avoid all the if/else to determine the field width by just doing this.
    Code:
    printf("|D%2d ", a);
    Initialise the board.
    Code:
    int board[8][8] = { { 0 } }; //board
    But you actually need to initialise the top two ranks with white pieces, and the bottom two ranks with black pieces, as per the layout of a checkers game.


    > does anybody know what i should do to make the pieces move? thank you.
    Well the first thing to do would be to prompt "which piece do you want to move" followed by "where do you want to move it to".
    Thank you for that, i have updated the code. please check for the corrections needed.
    Last edited by Salem; 12-02-2022 at 05:25 AM. Reason: Removed crayola colours

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Thank you for that, i have updated the code. please check for the corrections needed.
    Your loops are still wrong.

    All of them should be
    for ( i = 0 ; i < 8 ; i++ )
    or
    for ( j = 0 ; j < 8 ; j++ )

    Also, post the latest code.
    Don't keep editing the original post.
    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.

  5. #5
    Registered User
    Join Date
    Dec 2022
    Posts
    3
    updated code, so to validate the pieces do i have to make a function that validates the pieces?
    Code:
    #include <stdio.h>
    #include <Windows.h>  //include the windows API to print unicode
     //allows print unicode
    
    
    //Sets-Up Checker Pieces by giving a value in a multidimensional array.
    void initializeBoard(int board[8][8])
    {
    int i, j, k;
        /* If the value of board[i][j] is equal to 1, then that piece is Player 1's piece.
           If the value of board[i][j] is equal to 2, then that piece is Player 2's piece.
           board[i][j] is equal to 0 means that the location is not an outer bound area, (white squares)
           board[i][j] is equal to 3 means that the location is a possible position but is empty */
        //Player 1 Pieces
        for(i = 0; i < 8; i++)
        {
            for(j = 0; j < 8; j++)
            {
                if(((i == 6)&&(j%2 == 1))||((i == 7)&&(j%2 == 0))||((i == 8)&&(j%2 == 1))) //Player 1 Pieces; valid square value is changed to 1
                {
                    board[i][j] = 1;
                }            
                else if(((i == 1)&&(j%2 == 0))||((i == 2)&&(j%2 == 1))||((i == 3)&&(j%2 == 0))) //Player 2 Pieces; valid square value is changed to 2
                {
                    board[i][j] = 2;
                }   
                else if((i + j)%2 == 1) //Checks all Valid Squares and value is set to 3
                {
                    board[i][j] = 3;
                }          
                else
                {
                    board[i][j] = 0;
                }
            }
        }
    }
    void printBoard(int board[8][8])
    {
    	int i, j, k;
        int a = 0; //piece number for black
        int b = 13; //piece number for white
        //print column number
        
        for(k = 0; k != 9; k++)
        {
            printf("  %d ",k);
        }
        //print board
        printf("\n+---+---+---+---+---+---+---+---+\n"); //top row divider
        for(i = 0; i <8; i++) //prints row with i being the number of rows
        {
            for(j = 0; j <8; j++)//prints column with j being the number of rows
            {
                if(board[i][j] == 1)
                {
                    //spacing issues
                    a += 1; 
                    if(a < 10) //if a single digit print piece with space
                    {
                        printf("|D%d ", a);
                    }
                    else //if not single digit print piece without space
                    {
                        printf("|D%d", a); 
                    }
                }
                else if(board[i][j] == 2)
                {
                    //spacing issues
                    b -= 1; 
                    if(b < 10) //if a single digit print piece with space
                    {
                        printf("|L%d ", b);
                    }
                    else //if not single digit print piece without space
                    {
                        printf("|L%d", b); 
                    }
                }
                else if(board[i][j] == 3)
                {
                    printf("| â–  ");
                }
                else
                {
                    printf("|   ");//print a column
                }
            }
            printf("| %d", i); //outer right side border with row numbers
            printf("\n"); //new line to signify new row
            printf("+---+---+---+---+---+---+---+---+\n"); //row divider
        }
    }
    int main()
    {
        SetConsoleOutputCP(CP_UTF8);
        int board[8][8]; //board
        initializeBoard(board);
        printBoard(board);
        return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    $ ./a.out 
      0   1   2   3   4   5   6   7   8 
    +---+---+---+---+---+---+---+---+
    |   |   |   |   |   |   |   |   | 0
    +---+---+---+---+---+---+---+---+
    |L12|   |L11|   |L10|   |L9 |   | 1
    +---+---+---+---+---+---+---+---+
    |   |L8 |   |L7 |   |L6 |   |L5 | 2
    +---+---+---+---+---+---+---+---+
    |L4 |   |L3 |   |L2 |   |L1 |   | 3
    +---+---+---+---+---+---+---+---+
    |   |   |   |   |   |   |   |   | 4
    +---+---+---+---+---+---+---+---+
    |   |   |   |   |   |   |   |   | 5
    +---+---+---+---+---+---+---+---+
    |   |D1 |   |D2 |   |D3 |   |D4 | 6
    +---+---+---+---+---+---+---+---+
    |D5 |   |D6 |   |D7 |   |D8 |   | 7
    +---+---+---+---+---+---+---+---+
    Why is there an 8 at the end of the top row?
    Why are there 3 rows of L pieces?
    Why don't the L pieces start in the first row?

    /* If the value of board[i][j] is equal to 1, then that piece is Player 1's piece.
    If the value of board[i][j] is equal to 2, then that piece is Player 2's piece.
    board[i][j] is equal to 0 means that the location is not an outer bound area, (white squares)
    board[i][j] is equal to 3 means that the location is a possible position but is empty */
    Use an enum.
    Code:
    enum {
        EMPTY = 0,  // unused space
        WHITE = 1,  // white piece
        BLACK = 2,  // black piece
        FREE = 3,  // valid, but unused position
    };
    ...
                if(((i == 6)&&(j%2 == 1))||((i == 7)&&(j%2 == 0))||((i == 8)&&(j%2 == 1))) //Player 1 Pieces; valid square value is changed to 1
                {
                    board[i][j] = WHITE;
                }            
                else if(((i == 1)&&(j%2 == 0))||((i == 2)&&(j%2 == 1))||((i == 3)&&(j%2 == 0))) //Player 2 Pieces; valid square value is changed to 2
                {
                    board[i][j] = BLACK;
                }   
                else if((i + j)%2 == 1) //Checks all Valid Squares and value is set to 3
                {
                    board[i][j] = FREE;
                }          
                else
                {
                    board[i][j] = EMPTY;
                }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chekers Game GIT checkers
    By sveyda in forum C Programming
    Replies: 0
    Last Post: 04-25-2014, 03:32 AM
  2. Checkers game
    By Aeoskype in forum C++ Programming
    Replies: 11
    Last Post: 05-12-2013, 01:03 PM
  3. Trying to make a checkers game..
    By Luminous Lizard in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2011, 12:04 PM
  4. C++ Checkers Game
    By SnS CEO in forum C++ Programming
    Replies: 9
    Last Post: 09-07-2005, 01:21 AM
  5. IDEA: Checkers game
    By confuted in forum Contests Board
    Replies: 0
    Last Post: 06-28-2003, 03:25 PM

Tags for this Thread