4 Replies - 1567 Views - Last Post: 30 November 2012 - 02:22 AM Rate Topic: -----

#1 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Simple card-game with multiple oponents

Posted 28 November 2012 - 07:03 AM

So guys i have the code for a simple card game (human vs computer) and i want to enhance it by adding the option of more bots to play against ... i would like to do that by using a class here is the code for the 1 computer player


def computer_plays(): 
    global d, table, computer_hand 
    target_val=table[-1].value 
    target_sym=table[-1].symbol
#i use this to see what the computer plays and be able to see if his AI works well
    HHS2=[str(x) for x in computer_hand]
    print HHS2
#//////////////    
    for c in computer_hand:
           
        if c.value==target_val or c.symbol==target_sym or c.value=='A':
            
            print "the bot plays",c 
            computer_hand.remove(c) 
            table.append(c) 
            if len(computer_hand)<1: return "computer wins"
            elif c.value=='K' :
                print ' The bot played King so it plays again !'
                return computer_plays()
            elif c.value=='Q':
                for i in range(2) :
                    human_hand.append(d.draw())
                print ' the bot played Queen so you have to draw two cards'
                return "continue"
            else: return "continue"   
    new_card=d.draw() 
    if new_card=="empty": return "count" 
    else: 
        print "the bot draws a card" 
        computer_hand.append(new_card) 
        return computer_plays()



This is just the part of the code that involves how the computer thinks and plays and there are several things used here that are defined to the rest of program

Any idea or guideline of how can i make this work will be apreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Simple card-game with multiple oponents

#2 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Simple card-game with multiple oponents

Posted 28 November 2012 - 08:39 AM

Here's a rough outline of what I would do:

Make a class to represent a bot. It needs to keep track of the bots hand. Then it should have a play() method, which essentially does what your computer_plays() function does now. Then you'd just make a list of your bots, and iterate through it, calling the play() method on each bot.
Was This Post Helpful? 1
  • +
  • -

#3 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Re: Simple card-game with multiple oponents

Posted 28 November 2012 - 01:45 PM

View PostTayacan, on 28 November 2012 - 08:39 AM, said:

Here's a rough outline of what I would do:

Make a class to represent a bot. It needs to keep track of the bots hand. Then it should have a play() method, which essentially does what your computer_plays() function does now. Then you'd just make a list of your bots, and iterate through it, calling the play() method on each bot.


Instead of play method can i just copy paste the computer_plays ?
and can the list of the bots be made inside bot class or should i define in somewhere else ?
sorry if my questions are stupid but i am a beginner, thanks a lot for your help
Was This Post Helpful? 0
  • +
  • -

#4 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Simple card-game with multiple oponents

Posted 29 November 2012 - 04:29 AM

You'll probably have to modify it a bit, but no, you don't have to name it "play". You'll have to make the list of bots outside the class. Have you worked with object-oriented programming before? If not, here's a quick explanation (although I'd recommend that you read some more about it elsewhere).

A class is sort of like a blueprint - for instance, a Bot class would describe what Bots, in general, behave like. All Bots have some cards they can play. A specific bot does not have the same cards as some other specific bot, but they both do have cards. So your class needs to reflect this.
class Bot:
    def __init__(self,cards):
        self.cards = cards


Do you know what "self" means? It refers to a specific instance of your class - a specific bot. Like this:
bot1 = Bot(card_list)
bot2 = Bot(another_card_list)

print(bot1.cards)
print(bot2.cards)


What's going on here? When we write Bot(card_list), the __init__ method in the Bot class is called. But that method takes two parameters, and we only gave it one!

Actually, we're creating a new instance of the Bot class, which means that the first parameter of __init__, the one called self, is set to be the new instance that we're creating. This also means that inside the __init__ method, when we say self.cards = cards, that's actually like saying bot1.cards = card_list.

Alright, so now we know how to create new Bots and give them cards. So how can we make them play, one by one? Let's extend our class a bit!
class Bot:
    def __init__(self,cards):
        self.cards = cards

    def computer_plays(self):
        # Your code here


Now, we can call the new method like this:
bot1.computer_plays()
bot2.computer_plays()


And, to make it even smarter:
bots = [Bot(cardlist1)
       ,Bot(cardlist2)
       ,Bot(cardlist3)]

for bot in bots:
    bot.computer_plays()


Was This Post Helpful? 2
  • +
  • -

#5 fledgeling  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 10-October 12

Re: Simple card-game with multiple oponents

Posted 30 November 2012 - 02:22 AM

View PostTayacan, on 29 November 2012 - 04:29 AM, said:

You'll probably have to modify it a bit, but no, you don't have to name it "play". You'll have to make the list of bots outside the class. Have you worked with object-oriented programming before? If not, here's a quick explanation (although I'd recommend that you read some more about it elsewhere).

A class is sort of like a blueprint - for instance, a Bot class would describe what Bots, in general, behave like. All Bots have some cards they can play. A specific bot does not have the same cards as some other specific bot, but they both do have cards. So your class needs to reflect this.
class Bot:
    def __init__(self,cards):
        self.cards = cards


Do you know what "self" means? It refers to a specific instance of your class - a specific bot. Like this:
bot1 = Bot(card_list)
bot2 = Bot(another_card_list)

print(bot1.cards)
print(bot2.cards)


What's going on here? When we write Bot(card_list), the __init__ method in the Bot class is called. But that method takes two parameters, and we only gave it one!

Actually, we're creating a new instance of the Bot class, which means that the first parameter of __init__, the one called self, is set to be the new instance that we're creating. This also means that inside the __init__ method, when we say self.cards = cards, that's actually like saying bot1.cards = card_list.

Alright, so now we know how to create new Bots and give them cards. So how can we make them play, one by one? Let's extend our class a bit!
class Bot:
    def __init__(self,cards):
        self.cards = cards

    def computer_plays(self):
        # Your code here


Now, we can call the new method like this:
bot1.computer_plays()
bot2.computer_plays()


And, to make it even smarter:
bots = [Bot(cardlist1)
       ,Bot(cardlist2)
       ,Bot(cardlist3)]

for bot in bots:
    bot.computer_plays()





Thank you very much sir :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1