• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Choosing randomly between AI classes

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm currently completing an assignment that requires a game to randomly select either a Smart or Dumb AI player to play a human player on startup. I have a SmartPlayer class and a DumbPlayer class, as well as an abstract ComputerPlayer class that extends to SmartPlayer and DumbPlayer.

Is there a way I can do this?
 
Rancher
Posts: 285
14
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the ComputerPlayer class should have all the common methods in it which both the dumb and smart need. When you extend the class you inherit those methods.
is there a particular part of the process you are having trouble with understanding?
 
James Kilgarriff
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I could've worded my original question better.

The ComputerPlayer class has the common methods, there's no issue there. My problem is assigning the AI player at random. At the moment, my players are assigned in my Main method, but I would like them to be randomly assigned. Would I need a separate RandomPlayer class that extends to the DumbPlayer and SmartPlayer classes, that would be called in the Main method?



I would like the secondPlayer to be and then RandomPlayer select between DumbPlayer or SmartPlayer and assign it to secondPlayer but I'm sure how to do this, or if it's even possible.
 
Marshal
Posts: 80067
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One suggestion:That will choose a dumb player 25% of the time.
 
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Kilgarriff wrote:My problem is assigning the AI player at random. At the



Generally if you need to randomly (as if by statistical chance) do something in any program the approach is to use the Random class.

It is worth reading the javadoc.

The parameter to Random.nextInt defines the EXCLUSIVE value for the upper bounds on the int which is returned. The lower bounds are always zero and INCLUSIVE.

This will generate a random value and then you will write a switch or if-then-else  that creates one or the other class, depending on the value returned.

 
Campbell Ritchie
Marshal
Posts: 80067
410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Kilgarriff wrote:. . . . . .

  • One possibility: have a smart player object and a dumb player object as fields of randomised player and choose randomly which to use. In that case, randomised player constitutes a wrapper class around another sort of player. But that is awkward code to implement. Not difficult, just awkward.
  • Maybe a better possibility. Make both subclasses of computer player private inner classes of computer player. Give them all private constructors; inner classes have access to private parts of their enclosing type and vice versa. Give the class factory methods, e.g. getInstanceAtRandom, getSmartInstance, getDumbInstance. You can even overload the random method with a probability of getting one or the other, e.g. 0.1 means you have a 10% chance of getting an XYZInstance.
  • Note the Random class and the Math#random() method don't actually produce random numbers, but you won't notice any difference until you have much more stringent requirements.
     
    Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic