#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Help with writing C code for checkers


    Well the game is actually peg solitaire but it's very similar to checkers. I'm new to C so my thoughts are all over the place and could use some advice. This is what I have so far

    Well the game is actually peg solitaire but it's quite similar to checkers. This is what I have so far and could really use a lot more help.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define ROWS 5
    #define COLUMNS 5

    void board()
    {

    printf("PEG SOLITAIRE\n");

    char board [][] = { {1, 2, 3, 4, 5 },
    {6, 7, 8, 9, 10},
    {11,12,13,14,15},
    {16,17,18,19,20},
    {21,22,23,24,25} };

    int h, w;

    for (h = 0; h < COLUMNS; h++) {
    for (w = 0; h < COLUMNS; h++)
    board1[h][w] = board2[h][w];
    }

    for (h = 0; h < COLUMNS; h++) {
    printf("\n");
    for (w = 0; w < ROWS; w++)
    printf("-");
    }
    }


    void Pmove()
    {
    int peg;
    int no_peg;
    int up;
    int down;
    int right;
    int left

    //I want to add if statements here but not sure as to how I would link it to the first functions

    }



    As you can see i'm all over the place so any advice would really help
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,935
    Rep Power
    481

    Difficult


    Is the program supposed to play this peg solitaire or adjudicate moves fed by something else?

    As you have it the board array is completely local to the board function. Hence, useless.

    Perhaps you intended to use numbers to represent a notation of moves? You'll also need to represent which cells have pegs. That could be a Boolean array with the capacity to differentiate between zeros and ones in each position.

    Less than "all over the place" you're closer to "nowhere". Sorry.

    For notation I recommend using alphabet letters. You've only got 25 cells. Assuming your program is a referee the user could make a jump simply by typing 3 characters
    wy<enter>
    instead of the ridiculous hard-to-type twice-as-long 6 characters
    23 25<enter>
    The more readable alphabet also simplifies input verification. Furthermore, all alphabet characters occupy exactly one column in the board representation. Easier, easier, easier.

    How to write code: do not use cut and paste as it produces this common error when you forget to change names:
    Code:
    for (h = 0; h < COLUMNS; h++) {
      for (w = 0; h < COLUMNS; h++)
        board1[h][w] = board2[h][w];
    Last edited by b49P23TIvg; December 3rd, 2013 at 09:07 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Is the program supposed to play this peg solitaire or adjudicate moves fed by something else?

    As you have it the board array is completely local to the board function. Hence, useless.

    Perhaps you intended to use numbers to represent a notation of moves? You'll also need to represent which cells have pegs. That could be a Boolean array with the capacity to differentiate between zeros and ones in each position.

    Less than "all over the place" you're closer to "nowhere". Sorry.

    For notation I recommend using alphabet letters. You've only got 25 cells. Assuming your program is a referee the user could make a jump simply by typing 3 characters
    wy<enter>
    instead of the ridiculous hard-to-type twice-as-long 6 characters
    23 25<enter>
    The more readable alphabet also simplifies input verification. Furthermore, all alphabet characters occupy exactly one column in the board representation. Easier, easier, easier.

    How to write code: do not use cut and paste as it produces this common error when you forget to change names:
    Code:
    for (h = 0; h < COLUMNS; h++) {
      for (w = 0; h < COLUMNS; h++)
        board1[h][w] = board2[h][w];
    The code is supposed to show all the possible moves on a 5 by 5 board which initially look like the following. I think one with user input is acceptable as long as it allows all possible moves

    - - o - -
    - - o - -
    0 0 0 0 0
    - - 0 - -
    - - 0 - -

    I'm unsure as to which functions I would need to have. I would think board structure, initial board, new board, possible moves, user input, print table.

    Any further advice would be much much appreciated
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,935
    Rep Power
    481
    With the big "Post Reply"button you won't have to quote me.
    The dashes represent unreachable sites, the zeros represent pegs, and the lower case ohs represent open sites? If so, maybe you could try to make the interaction work as shown. The program displays everything up to the next question mark space prompt then waits for the player to enter a move. The character and new line following are typed by the user.
    Code:
    Board         possible
    state         moves
    
    - - o - -     - - o - -     
    - - o - -     - - o - -
    0 0 0 0 0     0 0 0 0 0
    - - 0 - -     - - A - -
    - - 0 - -     - - 0 - -
    
    ? 8
    A are the valid moves.  Try again.
    ? A
    
    - - o - -     - - o - -
    - - 0 - -     - - 0 - -
    0 0 o 0 0     A 0 o 0 B
    - - o - -     - - o - -
    - - 0 - -     - - 0 - -
    
    ? b
    
    - - o - -     - - o - -
    - - 0 - -     - - A - -
    0 0 0 o o     0 B 0 o o
    - - o - -     - - o - -
    - - 0 - -     - - 0 - -
    
    ? B
    
    - - o - -
    - - 0 - -
    0 o o O o
    - - o - -
    - - 0 - -
    
    No moves remain.  You left 4 pegs.
    Last edited by b49P23TIvg; December 4th, 2013 at 09:18 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Further


    Would it be possible to contact you via email if I need further help as it would be quicker? I've only got another 8 hours to do this project and started late due to being in the hospital for the last 4 weeks
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,935
    Rep Power
    481
    No I'm at work, followed by a wedding attendance, then choir rehearsal. I'm booked for the next 9 hours. good luck.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo