Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 4 of 5 FirstFirst ... 2345 LastLast
Results 46 to 60 of 62
  1. #46
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    s@#$....I am finishing up this Java and you throw that...I think I will wait till later tonight when i am drinking this all off to guess that

    Everything..with the exception of two player is working...........drumroll


    GREAT!!!

    after i finish i will look at better moves for the computer..right now i am playing around with the two player..again thanks to your previous posts..will post more soon

  2. #47
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    thanks blew off the project..okay well you do have me beat...not by much..
    Last edited by mdnealy; 12-16-2009 at 07:57 PM.

  3. #48
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Heh! The middle clue is the important one. It severely limits your choices.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  4. #49
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Once again, the right answer for two player is the same as the right answer for two computer players is the right answer for allowing players to choose X and O or allowing either X or O to go first: Polymorphism.

    Code:
    protected class Player
    {
        protected String playerName;
        protected TicTacToeEnum x_or_o;
    
        public String getName( ) { return playerName; }
        public TicTacToeEnum getPlayType( ) { return x_or_o; }
    
        public void makeMove( TicTacToeBoard theBoard ) { /* do nothing */ }
    
    }
    public class ComputerPlayer extends Player
    {
        // constructor:
        public ComputerPlayer( TicTacToeEnum playType )
        {
              x_or_o = playType;
              playerName = "Computer";
        }
        public void makeMove( TicTacToeBoard theBoard ) 
        {
              ... code to make a move ... 
        }
    }
    public class HumanPlayer extends Player
    {
        private Scanner input = new Scanner(System.in);
    
        // constructor:
        public HumanPlayer( String name, TicTacToeEnum playType )
        {
              playerName = name;
              x_or_o = playType;
        }
        public void makeMove( TicTacToeBoard theBoard ) 
        {
              ... code to make a move ... 
        }
    }
    Now your game play looks like this:
    Code:
         Player p1, p2;
    
         ? 1 or 2 player ?
         if ( 1 player )
         {
              ? get name of human player ?
              ? get choice of X or O
              p1 = new HumanPlayer( name, x_or_o_choice );
              p2 = new ComputerPlayer( opposite of human's x_or_o_choice );
         } else {
              ? get name of human player ?
              ? get choice of X or O
              p1 = new HumanPlayer( name, x_or_o_choice );
              ? get name of other human player ?
              p2 = new HumanPlayer( name2, opposite of first player's x_or_o_choice );
         }
         while ( true )
         {
                show board
                p1.getMove()
                if ( gameover ) break;
                show board
                p2.getMove()
                if ( gameover ) break;
         }
         ...
    Do you see it? Now you can call p1.getMove() or p2.getMove() without knowing which kind of player is making the move! And you can ask p1 or p2 for their name or their x_or_o value again without knowing which kind of player they are.

    Not shown there is how you can allow computer player to go first, but that's easy:
    Code:
         ... after getting human player into p1 and computer into p2 ...
         if ( computer should go first )
         {
             Player swap = p1; 
             p1 = p2;
             p2 = swap;
         }
    See? It's ALL so simple once you have polymorphism.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  5. #50
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    so, would "Player" be a whole other class or would that be considered my test class?


    duh looked at it...hmmmmm

  6. #51
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    even if i wanted to eclipes won't let me make a protect class...hmm I have to go in at 530 tonight...i will ask him what he wants for the second player..

  7. #52
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Again, been writing too much C++ lately. A Java class can't be declared as protected. But its members can. So zap the protected in front of class name but leave it on the members.

    Protected means "visible in this class and in subclasses of this class and in other classses in this package, but not visible to outsiders."

    A class not declared public is "package private", meaning that all classes in the same package can access it. Since you aren't using packages yet, it realistically means all your classes can access it.

    Same with the protected members: since all your classes are in the same (default, unnamed) package, they are really available in all classes, so you can omit protected completely for now.

    C++ is a bit more sophisiticated here, in my opinion. But once you start using packages you'll find the Java scheme is adequate for most things.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  8. #53
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    okay, why can't i just make another class identical to the humanplayer and call it something different and assign it the enum. X that the computer has.

    I am trying this and it just ends up in a tie...is it because the enum can only be used in the two...i would have to add another enum like T in the additional human player class...

    am I asking this right?

    also, i need to put in to play again...do i do a boolean in test?
    Last edited by mdnealy; 12-16-2009 at 10:02 PM.

  9. #54
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    If the two classes do not both *derive* from the same Player class (that is "extends Player") then you can't refer to them via the same class name.

    Notice that in my code I said:
    Code:
        Player p1;
        Player p2;
    But then I *assigned* them as
    Code:
        p1 = new HumanPlayer( ... );
    or
        p1 = new ComputerPlayer( ... );
    You could even do:
    Code:
        ? is player 1 human?
        if ( yes )
        {
              ? get name
              ? get X or O
              p1 = new HumanPlayer( name, xo );
        } else {
              ? get X or O
              p1 = new ComputerPlayer( xo )
        }
        ? is player 2 human?
        if ( yes )
        {
              ? get name
              p2 = new HumanPlayer( name, opposite of xo );
        } else {
              p2 = new ComputerPlayer( opposite of xo )
        }
    And so have the computer play itself.

    Go read up on polymorphism. Without polymorphism, you don't really have true OO capablities.

    Re play again: Why not just ask a question "Play again? 1=yes" and then get the integer answer and, if it is anything other than 1, break out of an infinite loop.
    Code:
    while ( true )
    {
        ... play a game ...
        ? Play again? 1=yes
        if ( answer != 1 ) break; // out of the loop
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  10. #55
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    got it.....i think i am going to stick with the duplicate class of human player....This is alot of coding for a class where the feedback was minimal. He is a good guy but 3/4 programmer 1/4 teacher...it just takes time to learn what works for students...I learned more in the last week on this forum than i have 12-16 weeks in the class.

    i think i have the same problem with OO that I did in calc...

    you can have a problem and when you get a solution, it doesn't always feel like it is finished so my brain is looking for the additional work to finish it. I want to read into it too much instead of looking at it for what it is...so much for my logical mind....and here at 44 I thought that I was getting smarter finishing these classes....

    Thanks again for all your help...still going to work on making the computer smarter...(in my game)

  11. #56
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You do *NOT* need a duplicate class!!!

    There is NO REASON you can't do
    Code:
        HumanPlayer hp1 = new HumanPlayer( "Joe", TicTacToeEnum.X );
        HumanPlayer hp2 = new HumanPlayer( "Bob", TicTacToeEnum.O );
    The problem comes in the game play: You'll have to be testing *IN THE LOOP* for whether you have one or two humans and ask for moves accordingly.

    Probably something like this:
    Code:
        while ( true )
        {
             ...show board...
             hp1.getMove();
             ... if game over break ...
             ... show board ...
             if ( one player game )
             {
                 cp.getMove()
             } else {
                 hp2.getMove()
             }
             ... if game over break ...
        }
    Oh, okay. Now that I write it out like that I guess it's not such a big deal. It's just crappy coding style, all because you supposedly haven't learned about superclasses/subclasses.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  12. #57
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Actually, I guess you could even allow the user to choose 1 or 2 players and, if one player, *WHICH* player he will be. Then you can do:
    Code:
        while ( true )
        {
             ...show board...
             if ( hp1 == null )
             {
                 cp.getMove()
             } else {
                 hp1.getMove()
             }
             ... if game over break ...
             ... show board ...
             if ( hp2 == null )
             {
                 cp.getMove()
             } else {
                 hp2.getMove()
             }
             ... if game over break ...
        }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  13. #58
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    ha ha....it worked for the two human...i just changed the test for the hp1 and hp 2...works great!!! thanks!!!!

    I didnt even have to change any code except on the test page..


    HumanPlayer hp2= new HumanPlayer("bob", TicTacToeEnum.X);
    HumanPlayer hp1 = new HumanPlayer("human",TicTacToeEnum.O);


    the input for the names overrode the "bob" and "human" lol how simple was that..duh..

  14. #59
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    That's what I've been trying to tell you. And *IF* the two classes--HumanPlayer and ComputerPlayer--were subclasses of Player, then it would be even simpler, yet again.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  15. #60
    New Coder
    Join Date
    Dec 2009
    Posts
    43
    Thanks
    8
    Thanked 0 Times in 0 Posts
    i added that one line to test and just rearranged some stuff...i could have hit myself in the head...well, now i could and it is getting to the point that I wouldn't feel it...not because of the tequila but because i have been racking my brain for over a week... your coding is what put my program together but while I was waiting I really was sitting at this desk moving things around and trying to get it to work...i truly believe my contacts are now embedded in my eyeballs...


 
Page 4 of 5 FirstFirst ... 2345 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •