Esempio n. 1
0
 def take_turn(self, game_play):
     play_number = game_play.play_number
     if play_number == 0:
         if self.player.is_first_player:
             print "This is also happening"
             gp = GamePlay(play_number=1,
                           amount=-1,
                           player_id=self.player_id)
             gp.header.stamp = rospy.Time.now()
             self.play_pub.publish(gp)
     elif play_number == 1:
         if self.player.is_first_player:
             print "This is also happening"
             gp = GamePlay(play_number=2,
                           amount=-1,
                           player_id=self.player_id)
             gp.header.stamp = rospy.Time.now()
             self.play_pub.publish(gp)
     elif play_number == 2:
         if self.player.is_first_player:
             print "This is also happening"
             gp = GamePlay(play_number=3,
                           amount=-1,
                           player_id=self.player_id)
             gp.header.stamp = rospy.Time.now()
             self.play_pub.publish(gp)
         self.unregister_game()
         print "I am at the end"
Esempio n. 2
0
    def take_turn(self, game_play):
        top_raw = ([10, 10], [12, 0])
        bottom_raw = ([0, 12], [2, 2])
        play_number = game_play.play_number
        if play_number == 0:
            if self.player.is_first_player:
                #                time.sleep(numpy.random.randint(1,4,size=1))
                row_choice = choice = numpy.random.binomial(1, .59, size=1)
                self.row_choice = row_choice
                gp = GamePlay(play_number=1,
                              amount=row_choice,
                              player_id=self.player_id)
                gp.header.stamp = rospy.Time.now()
                self.play_pub.publish(gp)
        elif play_number == 1:
            if not self.player.is_first_player:
                #                time.sleep(numpy.random.randint(1,4,size=1))
                col_choice = numpy.random.binomial(1, .6, size=1)
                self.col_choice = col_choice
                gp = GamePlay(play_number=2,
                              amount=col_choice,
                              player_id=self.player_id)
                gp.header.stamp = rospy.Time.now()
                self.play_pub.publish(gp)

        elif play_number == 2:
            if self.player.is_first_player:
                gp = GamePlay(play_number=3,
                              amount=-1,
                              player_id=self.player_id)
                gp.header.stamp = rospy.Time.now()
                self.play_pub.publish(gp)
            self.unregister_game()
Esempio n. 3
0
 def take_turn(self, game_play):
     play_number = game_play.play_number
     if play_number == 0:
         if self.player.is_first_player:
             offer = -1
             while (offer < 0 or offer > 10):
                 try:
                     offer = int(raw_input("Input the first amount, between 0 and 10: "))
                 except ValueError:
                     print "Please enter a number between 0 and 10"
                     offer = -1
                     continue
                 if (offer < 0 or offer > 10):
                     print "Please enter a valid offer amount"
             self.offer = offer
             gp = GamePlay(play_number=1,amount=offer, player_id=self.player_id)
             gp.header.stamp = rospy.Time.now()
             self.play_pub.publish(gp)
             print "Waiting for second player"
         else:
             print "Waiting for first player"
     elif play_number == 1:
         if self.player.is_first_player:
             pass    
         else:
             print "Player 1 sent "+str(game_play.amount)
             print "You can either accept or reject this offer"
             choice = "X"
             while choice != "A" and choice != "R":
                 choice = raw_input("Please, input 'A' to accept or 'R' to reject the offer: ")
                 if choice == "A":
                     self.current_amount = str(game_play.amount)
                 elif choice == "R":
                     self.current_amount = 0
                 else:
                     print "Please choose A or R"
             print "Your payoff is now: " + str(self.current_amount)
             gp = GamePlay(play_number=2,amount=ord(choice), player_id=self.player_id)
             gp.header.stamp = rospy.Time.now()
             self.play_pub.publish(gp)
     elif play_number == 2:
         if self.player.is_first_player:
             choice = chr(game_play.amount)
             if choice == 'A':
                 which_choice = "Accepted"
                 self.current_amount = self.original - self.offer
             else:
                 which_choice = "Rejected"
                 self.current_amount = 0
             print "The other player decide to" + str(which_choice) + "your offer"
             print "Your payoff for this round is:" + str(self.current_amount)
             gp = GamePlay(play_number=3,amount=-1, player_id=self.player_id)
             gp.header.stamp = rospy.Time.now(); self.play_pub.publish(gp)  
         else:
             pass
         print "Please wait for the next game to start"
         print "unregistering"
         self.unregister_game()
Esempio n. 4
0
    def take_turn(self, game_play):
        play_number = game_play.play_number
        if play_number == 0:
            if self.player.is_first_player:
                time.sleep(numpy.random.randint(1, 4, size=1))
                decision = numpy.random.multinomial(
                    1, [.1, .04, .11, .1, .08, .16, .08, .07, .05, .02, .19],
                    size=1)
                offer = numpy.nonzero(decision > 0)[1]
                self.current_amount -= offer
                gp = GamePlay(play_number=1,
                              amount=offer,
                              player_id=self.player_id)
                gp.header.stamp = rospy.Time.now()
                self.play_pub.publish(gp)
            else:
                currrent_amount = 0
        elif play_number == 1:
            if self.player.is_first_player:
                pass
            else:
                time.sleep(numpy.random.randint(1, 4, size=1))
                recv = game_play.amount * 3
                if game_play.amount == 0:
                    offer = 0
                else:
                    print game_play.amount
                    print "Probability", probability[game_play.amount - 1]
                    decision = numpy.random.multinomial(
                        1, probability[int(game_play.amount) - 1], size=1)
                    offer = numpy.nonzero(decision > 0)[1]

                #offer = numpy.random.randint(recv+1, size=1)
                gp = GamePlay(play_number=2,
                              amount=offer,
                              player_id=self.player_id)
                gp.header.stamp = rospy.Time.now()
                self.play_pub.publish(gp)
        elif play_number == 2:
            if self.player.is_first_player:
                self.current_amount += game_play.amount
                gp = GamePlay(play_number=3,
                              amount=-1,
                              player_id=self.player_id)
                gp.header.stamp = rospy.Time.now()
                self.play_pub.publish(gp)
            self.unregister_game()
Esempio n. 5
0
 def take_turn(self, game_play):
     play_number = game_play.play_number
     if play_number == 0:
         if self.player.is_first_player:
             time.sleep(numpy.random.randint(1, 4, size=1))
             decision = numpy.random.multinomial(1,
                                                 [.1, .15, .15, .1, .2, .3],
                                                 size=1)
             # .05, .05, .1, .1, .21, .49] was this but he is too nice!!
             offer = numpy.nonzero(decision > 0)[1]
             self.offer = offer
             gp = GamePlay(play_number=1,
                           amount=offer,
                           player_id=self.player_id)
             gp.header.stamp = rospy.Time.now()
             self.play_pub.publish(gp)
     elif play_number == 1:
         if not self.player.is_first_player:
             time.sleep(numpy.random.randint(1, 4, size=1))
             prob_rejection = [[.73], [.64], [.49], [.20], [.07], [0], [0],
                               [0], [0], [0], [0]]
             choice = numpy.random.binomial(
                 1, prob_rejection[game_play.amount], size=1)
             if choice == 0:
                 self.current_amount = game_play.amount
             else:
                 self.current_amount = 0
             gp = GamePlay(play_number=2,
                           amount=self.current_amount,
                           player_id=self.player_id)
             # was gam_play_amount
             gp.header.stamp = rospy.Time.now()
             self.play_pub.publish(gp)
     elif play_number == 2:
         if self.player.is_first_player:
             if game_play.amount > 0:
                 self.current_amount = self.original - self.offer
             else:
                 self.current_amount = 0
             gp = GamePlay(play_number=3,
                           amount=-1,
                           player_id=self.player_id)
             gp.header.stamp = rospy.Time.now()
             self.play_pub.publish(gp)
     else:
         self.unregister_game()
Esempio n. 6
0
    def take_first_turn(self):

        # NOTE: CAN AVOID CRASH BY SKIPPINGH THIS IF SENTENCE: THE LOGIC THOUGH MAY MAKE PROBLEM WITH MULTIPLE PLAYERS AND WE NEED TO WORK MORE ON THIS POINT

        if not self.player.is_first:
            gp = GamePlay(play_number=0,
                          amount=-1,
                          player_id=self.player.player_id)
            gp.header.stamp = rospy.Time.now()
            self.play_pub.publish(gp)

        time.sleep(
            0.1
        )  # This is necessary to make sure the video thread has had time to update the game topic by now; otherwise it will publish on old topic
        if self.video is not None:  # This will send duplicate message for Player 1 and 2, but master will keep the most recent
            self.video.send_msg(play_number=0, amount=-1, msgtime=time.time())
Esempio n. 7
0
    def ok_button_clicked(self, widget, data=None):
        timenow = time.time()

        offer = self.spin_bid.get_value_as_int()

        if self.video is not None:
            self.video.send_msg(play_number=1, amount=offer, msgtime=timenow)

        self.set_balance(10 - offer)
        gp = GamePlay(play_number=1,
                      amount=offer,
                      player_id=self.player.player_id)
        gp.header.stamp = rospy.Time.now()
        self.play_pub.publish(gp)
        self.toggle_user_interaction()
        self.shared_console.clear()
        self.shared_console.append_text(
            '\n\nYou sent %d.\nWaiting for second player...\n' % offer)
Esempio n. 8
0
    def take_turn(self, game_play):
        gts = GtkThreadSafe()
        play_number = game_play.play_number
        if play_number == 0:

            if self.player.is_first:
                with gts:
                    self.set_balance(10)
                    self.toggle_user_interaction()
                    self.shared_console.append_text(
                        "Choose an amount, between 0 and 10 and press OK\n")
            else:
                with gts:
                    self.shared_console.append_text(
                        "Waiting for first player's offer...\n")
        elif play_number == 1:
            if self.player.is_first:
                self.shared_console.append_text(
                    "Waiting for second player's rensponse...\n")
            else:
                with gts:

                    label_prompt = gtk.Label(
                        'Player 1 has sent %d.\nYou can either accept or reject this offer.'
                        % game_play.amount)
                    dialog = gtk.Dialog('Choose One', self.parent,
                                        gtk.DIALOG_MODAL,
                                        ('Reject', gtk.RESPONSE_REJECT,
                                         'Accept', gtk.RESPONSE_ACCEPT))
                    dialog.vbox.pack_start(label_prompt, True, True, 0)
                    label_prompt.show()

                    if self.video is not None:
                        self.video.send_msg(play_number=2,
                                            amount=game_play.amount,
                                            msgtime=time.time())

                    dialog.connect("delete_event", lambda w, e: None)
                    response = gtk.RESPONSE_DELETE_EVENT
                    while response == gtk.RESPONSE_DELETE_EVENT:
                        response = dialog.run()
                    dialog.destroy()
                    if response == gtk.RESPONSE_REJECT:
                        new_balance = 0
                        send_info = -1 * game_play.amount
                        reaction = 0
                    elif response == gtk.RESPONSE_ACCEPT:
                        new_balance = game_play.amount
                        send_info = game_play.amount
                        reaction = 1

                    if self.video is not None:
                        self.video.send_msg(play_number=3,
                                            amount=send_info,
                                            msgtime=time.time())

                    self.set_balance(new_balance)
                    self.shared_console.append_text(
                        '\n\nYour payoff is now: %d' % new_balance)
                    self.log.data_writer(self.player.player_id,
                                         self.player.is_first,
                                         game_play.amount, reaction,
                                         new_balance, 0, 1)
                    gp = GamePlay(play_number=2,
                                  amount=send_info,
                                  player_id=self.player.player_id)
                    gp.header.stamp = rospy.Time.now()
                    self.play_pub.publish(gp)

        elif play_number == 2:
            if self.player.is_first:
                with gts:
                    self.shared_console.append_text('\n\nYour offer was ')
                    if game_play.amount < 0:
                        reaction = 0
                        self.shared_console.append_text(
                            'rejected. Your payoff is now 0.\n')
                        self.set_balance(0)
                        new_balance = 0
                    else:
                        reaction = 1
                        new_balance = 10 - game_play.amount
                        self.shared_console.append_text(
                            'accepted. Your payoff is now %d.\n' % new_balance)
                        self.set_balance(new_balance)
                    if self.video is not None:
                        self.video.send_msg(play_number=4,
                                            amount=-2,
                                            msgtime=time.time())
                self.log.data_writer(self.player.player_id,
                                     self.player.is_first,
                                     abs(game_play.amount), reaction,
                                     new_balance, 0, 1)
                gp = GamePlay(play_number=3,
                              amount=-2,
                              player_id=self.player.player_id)
                gp.header.stamp = rospy.Time.now()
                time.sleep(TIME_BETWEEN_GAMES)
                self.play_pub.publish(gp)

            else:
                pass
            with gts:
                self.shared_console.append_text(
                    "\n\nPlease wait for the next game to start.\n")
                self.unregister_game()
Esempio n. 9
0
 def take_first_turn(self):
     if not self.player.is_first_player:
         gp = GamePlay(play_number=0, amount=0, player_id=self.player_id)
         gp.header.stamp = rospy.Time.now()
         self.play_pub.publish(gp)
Esempio n. 10
0
    def take_first_turn(self):
# This may create other problems, but the not is to have this playing with the gui....it was that simple!
        if not self.player.is_first_player:
            gp = GamePlay(play_number=0,amount=0, player_id=self.player_id)
            gp.header.stamp = rospy.Time.now()
            self.play_pub.publish(gp)