Backgammon

This is a discussion on Backgammon within the Game Programming forums, part of the General Programming Boards category; Hi there, How would you go about doing it? I want to be able to judge on input, whether the ...

  1. #1
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169

    Backgammon

    Hi there,

    How would you go about doing it?
    I want to be able to judge on input, whether the move typed in by one player is legal or not.

    The rules say:
    A player must use both numbers of a roll if this is legally possible (or all four numbers of a double). When only one number can be played, the player must play that number. Or if either number can be played but not both, the player must play the larger one. When neither number can be used, the player loses his turn. In the case of doubles, when all four numbers cannot be played, the player must play as many numbers as he can.
    Was thinking of permuting through all possible moves (every roll) and see how many moves can be played. Then on player's input only make sure he moves accordingly.
    Is this a good path to go?

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Well, start with making a player class, then work from there.
    example:

    Code:
    class Player
    {
    public:
       Player();
       ~Player();
    
       string setName ( string name )
       {
          cout << "Enter your player name: ";
          getline(cin, name );
          
          if ( name == "" )
          {
             setName("");
          }
       
          *m_name = name;
          return name;
       }
    
       string getName() const { return *m_name; }
    
    private:
       string *m_name;
       int move;
    };
    
    Player::Player()
    {
       m_name = new string("");
    }
    
    Player::~Player()
    {
       delete m_name;
       m_name = NULL;
    }
    Last edited by Salem; 10-01-2006 at 03:50 AM. Reason: adjusted tag position

  3. #3
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169
    Thanks, its not what Im looking for though.
    I already have a board set up, and printing nicely with pdcurses.
    Anyway, I am not looking for code examples, but rather ideas to point me in the right direction.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    It's a little early in the morning and I can't think straight yet. We are out of coffee and I'm waiting for my wife to return home from her shoping. So instead of helping you directly, I'll give you a few tips on how to solve this and other similar problems in the future.

    How to deal with stuff - Crash Course

    First you are going to grab that text and make certain keywords strike out.

    A player must use both numbers of a roll if this is legally possible (or all four numbers of a double). When only one number can be played, the player must play that number. Or if either number can be played but not both, the player must play the larger one. When neither number can be used, the player loses his turn. In the case of doubles, when all four numbers cannot be played, the player must play as many numbers as he can.
    Look at how all the when's, if's, or's, and's, but's have been emphasized. These are words that generally orient your code. They have direct relation to constructs that exist in the programming language. If Statements, AND, OR, etc...

    What other keywords should I emphasize, you may ask. Too many, I say. But here's a few more other than those above: "until", "while", "for every", etc...

    Next we grab that text and separate it into it's smaller parts (aka phrases).

    Code:
    A player must use both numbers of a roll if this is legally possible (or all four numbers of a double).
    When only one number can be played, the player must play that number.
        Or if either number can be played but not both, the player must play the larger one.
    When neither number can be used, the player loses his turn.
        In the case of doubles, when all four numbers cannot be played, the player must play as many numbers as he can.
    You are getting there. Suddenly all starts to become easier to understand, doesn't it? Notice how I formated the text so that those lines that are dependent of the previous line (i.e. lines that exist in the context of the previous line) are indented to the right. This is another visual clue to help you better understand the text.

    On many situations we stop here. You can already construct your code from here. But for the fun of it lets dig deeper imagining this was a more complex problem.

    Next we enter to the realm of text analysis. We are going to spot one or more trends and insert them in the text as visual clues. So... first lets get something straight. What is the text referring to? It is talking about movement rules. What is that the text assumes you know already? That you throw two dice and if it's doubles it is as if you had thrown 4 dice.

    So... With that in our baggage let's analyse the text and simply indicate shortly what each line is talking about.

    Code:
    
    [When to use rolls]
    A player must use both numbers of a roll if this is legally possible (or all four numbers of a double).
    [If can't play all rolls]
    When only one number can be played, the player must play that number.
        Or if either number can be played but not both, the player must play the larger one.
    In the case of doubles, when all four numbers cannot be played, the player must play as many numbers as he can.
    [If can't play any rolls]
    When neither number can be used, the player loses his turn.
    Wow! Many changes. First... I simply added small text to the top of each line that describes what the line is talking about. If more than one line talks about the same thing they all are left bellow this indicator text.

    But what is important here is that I moved the last line further up. When doing this I suddenly realize that the last line has nothing to do after all with not being able to play any rolls. If you read it carefully it is obviously describing a situation where you rolled a double, you can can use some of the movements allowed, but not all.

    This is the importance of this step. It helps you restructure the code so that you spot possible errors or imperfections in the language used by the writer. And in doing so, it helps you better structure your thoughts and... you got it right, eventually the code.

    Next... we are going to change the actual text. It is about time we get into pseudo code. We are going to reformat the whole text so that it resembles (but is not!) C++.

    Code:
    
    [When to use rolls]
    if (all_rolls_possible)
       do them;
    [If can't play all rolls]
    if (only_one_move_possible)
       do it;
    if(either_roll_can_be_played_but_not_all)
       if(it_was_a_double_roll)
          do play_as_many_as_you_can;
       else
          do play_the_larger_one;
    [If can't play any rolls]
    if(no_roll_can_be_played)
       do lose_turn;
    I can already smell C++ code. Can you?

    From here on it's programming already. The next step is to refine the pseudo code. You should create some variables in pseudo code and use them instead of the descriptive names. http://en.wikipedia.org/wiki/Pseudocode

    There's no fixed rules for Pseudo code. It's up to you to devise your own if you intend to use it only for yourself. If you intend to share it then just read the above link for a few guidelines. But remember there's is no set syntax for pseudo code as there is for C++. It's purpose is to describe the programming logic in order to help coding.

    And that's it. And my coffee hasn't come home yet
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    How do you take your coffee mario, black, white? How many sugars? Lol!

    EDIT I applogise for my reply to the OP's message, I don't think I fully understood the problem, I thought he/she was making the code from scratch but good answer mario

  6. #6
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169
    Thanks, this is more than I asked for. Im sure this will come in handy.
    I am going to attempt and make the function that should decide how many moves are possible for any given board-n-roll set up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Backgammon AI ?
    By glo in forum General AI Programming
    Replies: 7
    Last Post: 06-18-2010, 03:11 AM
  2. backgammon
    By ammonation42 in forum C Programming
    Replies: 0
    Last Post: 04-06-2009, 08:05 AM

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