コード例 #1
0
ファイル: board.py プロジェクト: Aaronduino/ttrai
 def takeVisibleTrain(self, col):
     if self.auto:
         x = self.nextTrain()
     else:
         x = getColInt(raw_input("What is the card replaced by?"))
     self.visibleTrains[self.visibleTrains.index(col)] = x
     if self.visibleTrains.count(8) > 2:
         if not self.auto:
             print("Replacing all cards due to too many locomotives.")
         for i in range(0, len(self.visibleTrains)):
             if self.auto:
                 x = self.nextTrain()
             else:
                 x = getColInt(raw_input("What is the card replaced by?"))
             self.visibleTrains[i] = x
コード例 #2
0
ファイル: player.py プロジェクト: Aaronduino/ttrai
    def doNextMove(self):
        m = {}

        m['TYPE'] = int(raw_input("Move type \n(0 for buy track, 1 for draw train, 2 for draw ticket):"))

        if m['TYPE'] == 1:
            colorsLeft = 2
#             m['COLOR'] = getColInt(raw_input("What color will you draw? (letter)"))  # ? for blind draw
            da = False  # doAction?
            while colorsLeft > 0:
                m['COLOR'] = getColInt(raw_input("What color will you draw? Type ? To draw blindly. (letter)"))
                if m['COLOR'] not in self.board.visibleTrains:
                    if m['COLOR'] == 9:
                        da = True
                    else:
                        print("That is not in the visible deck (" + str(self.board.visibleTrains) + "). You have drawn " + str(2 - colorsLeft) + "cards.")
                        print("Please try again.")
#                         m['COLOR'] = getColInt(raw_input("What color will you draw, instead? (letter)"))
                elif m['COLOR'] == 8:
                    if(colorsLeft < 2):
                        print("That is not legal on this turn.")
#                         m['COLOR'] = getColInt(raw_input("What color will you draw (instead of a locomotive)? (letter)"))  # ? for blind draw
                    else:
                        self.doAction(m)
                        colorsLeft = 0
                else:
                    da = True
#                 m['COLOR'] = getColInt(m['COLOR'])
                if da:
                    colorsLeft -= 1
                    self.doAction(m)
                    da = False
        else:
            if(m['TYPE'] == 0):
                m['TO'] = int(raw_input("Enter in the id of the first city connected to the track: "))
                m['FROM'] = int(raw_input("Enter in the id of the second city connected to the track: "))

#                 m['COLOR']=raw_input("What color trains will you be using (if applicable)? ")
                if self.board.graph[int(m['FROM'])][int(m['TO'])]['cols'][0] == "*" or len(self.board.graph[int(m['FROM'])][int(m['TO'])]['cols']) > 1:  # TODO: Fix possible bug when buying grey tracks
                    m['COLOR'] = raw_input("What color trains will you be using? ")
                    m['COLOR'] = getColInt(m['COLOR'])
                m['WILDCARDS'] = raw_input("How many wildcards will you use?")
                self.doAction(m)
            elif m['TYPE'] == 2:
                self.doAction(m)
コード例 #3
0
ファイル: board.py プロジェクト: Aaronduino/ttrai
    def nextTrain(self):
        if not self.auto:
            return getColInt(raw_input("Draw a train. What is its color?"))

        if len(self.trains) < 3:
            self.tickets.append(self.usedtrains)
            shuffle(self.trains)

        return self.trains.pop(-1)
コード例 #4
0
ファイル: playerai.py プロジェクト: Aaronduino/ttrai
    def setupIn(self):
        # input setup:
        # step (starts at 0)
        # move type (-1 for step 0)
        # move par 1
        # move par 2
        # move par 3
        # edge lengths
        # edge color 1
        # edge color 2 (-1 for n/a)
        # when edges were claimed (-1 if n/a)
        # who claimed edges (-1 for n/a)
        # train car hand (how many of each))
        # ticket hand (30 cards slots, stored as the two then from for each
        # card, -1 for empty)

        #         self.nnin = [self.moveillegal, self.step, self.movetype]
        self.nnin = [self.moveillegal, self.returningtickets]
        self.nnin += [self.m0s2, self.m1s2]
        self.nnin += self.pars
        self.nnin.append(self.trainsleft)

        ticketdecklen = -1
        if len(self.board.tickets) + len(self.board.discardedtickets) < 3:
            ticketdecklen = 1
        self.nnin.append(ticketdecklen)

        traindecklen = -1
        if len(self.board.trains) < 2:
            traindecklen = 1
        self.nnin.append(traindecklen)

        othertrains = [-1] * 4
        otherpoints = [-1] * 4
        for i, j in enumerate(self.players):
            othertrains[i] = j.trainsleft
            otherpoints[i] = j.points
        self.nnin += othertrains
        self.nnin.append(self.points)
        self.nnin += otherpoints

        if self.board.auto and False:
            self.nnin.append(
                len(self.board.tickets) + len(self.board.discardedtickets))
            self.nnin.append(sum(self.board.trains))
        else:
            self.nnin.append(-1)
            self.nnin.append(-1)
        self.nnin += self.board.visibleTrains
        self.nnin += self.board.trackpts

        trainsIn = self.trains

        u = ticketdeckunshuffled()
        ticketsIn = [-1] * 30
        for i in self.tickets:
            ticketsIn[u.cards.index(i)] = 1

#         index = 0
#         for i in self.tickets:
#             #             self.print_func(str(i))
#             ticketsIn[index]=i[0]
#             ticketsIn[index+1]=i[1]
#             index+=2
#             if index>58:
#                 break

        self.nnin += trainsIn
        self.nnin += ticketsIn

#         self.nnin += [i[2] for i in self.board.graph.edges_iter(data='weight', default=-1)]
        self.nnin += [i[2]
                      for i in self.board.graph.edges_iter(data='length', default=-1)]
#         print([i for i in self.board.graph.edges_iter(data='length', default=-1)])

        for i in self.board.graph.edges_iter(data='cols', default=-1):
            val = getColInt(i[2][0])
            if len(i[2]) > 1:
                val *= 10
                val += getColInt(i[2][1])
            self.nnin.append(val)

#         for i in self.board.graph.edges_iter(data='cols', default=-1):
#             self.nnin.append(getColInt(i[2][0])
#         for i in self.board.graph.edges_iter(data='cols', default=-1):
#             if(len(i[2])<2):
#                 self.nnin.append(-1)
#             else:
#                 self.nnin.append(getColInt(i[2][1]))

#         self.nnin += [i[2] for i in self.board.graph.edges_iter(data='whenclaimed', default=-1)]
        self.nnin += [i[2]
                      for i in self.board.graph.edges_iter(data='claimed', default=-1)]
コード例 #5
0
ファイル: playerai.py プロジェクト: Aaronduino/ttrai
    def doNextMoveRedo(self):
        # TODO: Rewrite returnTicket func to match new action selection
        self.m1s2 = -1
        self.m0s2 = -1
        self.pars = [-1] * 9
        self.returningtickets = -1

        self.setupIn()

        move = self.getBrainAction(self.nnin)
        # 0 = Draw Ticket
        # 1-7 = Return Ticket(s)
        # 8-16 = Draw Color Pars
        # 17-106 = Buy Track

        if move == 0:
            m = {'TYPE': 2}
            return self.doAction(m)
        elif move <= 7:
            return False
        elif move <= 16:
            #             print("HI!!!")
            m = {'TYPE': 1}
            color = move - 8
            m['COLOR'] = color
            if not self.checkLegal(m):
                return False
            self.doAction(m)
            if m['COLOR'] == 8:
                return True

            m1 = m
            self.m1s2 = 1
            self.setupIn()
            col1 = -1
            while not (8 <= col1 < 16):
                col1 = self.getBrainAction(self.nnin)
                self.brainBackwards(self.rewards['illegalmove'])
            m1['COLOR'] = col1 - 8
#             if not self.checkLegal(m1):
#                 return False

            self.doAction(m1)

            # It is ok if it can't draw the second card legally; it just won't
            # benefit!
            return True
        elif move <= 335:
            #             print(move-17)
            m = {'TYPE': 0}
            i = 0
            for t, f, d in self.board.graph.edges(data=True):
                if move - 17 < i:
                    print(i)
                    print("ERROR!!!")
                if d['cols'][0] == '*':
                    found = False
                    for j in range(0, 8):
                        if i + j == move - 17:
                            #                             print("Wild")
                            m['COLOR'] = j

                            m['TO'] = t
                            m['FROM'] = f

                            needed = d['length']
                            has = self.trains[m['COLOR']]
                            if needed > has:
                                m['WILDCARDS'] = needed - has
                            else:
                                m['WILDCARDS'] = 0
                            found = True
                    i += 8
                    if found:
                        break
                else:
                    if i == move - 17:
                        m['TO'] = t
                        m['FROM'] = f
#                         print("Single")
#                         if d['cols'][0]=='*':
#                             self.m0s2=1
#                             self.setupIn()
#                             col=-1
#                             while not (8<=col<16):
#                                 col = self.getBrainAction(self.nnin)
#                                 self.brainBackwards(self.rewards['illegalmove'])
#                             m['COLOR']=col-8
#                         else:
                        m['COLOR'] = getColInt(d['cols'][0])

                        needed = d['length']
                        has = self.trains[m['COLOR']]
                        if needed > has:
                            m['WILDCARDS'] = needed - has
                        else:
                            m['WILDCARDS'] = 0
                        break
                    elif len(d['cols']) == 2:
                        if i + 1 == move - 17:
                            m['TO'] = t
                            m['FROM'] = f
                            m['COLOR'] = getColInt(d['cols'][1])
#                             print("Double")
                            needed = d['length']
                            has = self.trains[m['COLOR']]
                            if needed > has:
                                m['WILDCARDS'] = needed - has
                            else:
                                m['WILDCARDS'] = 0
                            break
                    i += len(d['cols'])
#             print(i)
#             print("-" + str(move - 17))
            return self.doAction(m)

        print("Couldn't return from getMoveRedo()! Force returning.")
        return
コード例 #6
0
ファイル: player.py プロジェクト: Aaronduino/ttrai
    def doAction(self, pars):
        try:
            if pars['TYPE'] == 0:
                if pars['COLOR'] == None or pars['COLOR'] == -1:
                    pars['COLOR'] = getColInt(self.board.graph[pars['TO']][pars['FROM']]['cols'])
        except:
            pass
        if self.auto:
            if not self.checkLegal(pars):
    #             self.doNextMove()
    #             print("ILLEGAL... "+str(pars))
                return False
#         print("LEGAL... " + str(pars))

        self.logger.info(str(pars))

        if pars['TYPE'] == 0:
            # TODO: Check if the player has enough (plastic) trains to build a track
            # TODO: Make a clear difference between train car cards and the (plastic) trains in code

            # FIXME: The way these attributes are set may be corrupting the graph, not sure
#             self.board.graph[pars['TO']][pars['FROM']]['claimed']=self.id
#             self.board.graph[pars['TO']][pars['FROM']]['whenclaimed']=self.board.claimCount
#             nx.set_edge_attributes(self.board.graph[pars['TO']][pars['FROM']], 'claimed', self.id)
#             nx.set_edge_attributes(self.board.graph[pars['TO']][pars['FROM']], 'whenclaimed', self.board.claimCount)
            e = self.board.graph[pars['TO']][pars['FROM']]
#             data={}
            data = {'claimed':self.id, 'weight':e['weight'], 'whenclaimed':self.board.claimCount, 'length':e['length'], 'cols':e['cols']}
            self.board.graph.add_edge(pars['FROM'], pars['TO'], data)
#             self.board.graph.add_edge(pars['FROM'], pars['TO'], whenclaimed=self.board.claimCount)
#             print(self.board.graph.edges(data=True))
            self.board.claimCount += 1

            length = self.board.graph[pars['TO']][pars['FROM']]['length']
            self.trainsleft -= length

            self.getPoints(self.board.trackpts[length - 1])
            if not self.auto:
                if raw_input("Did you complete any tickets? (yes/no)").lower() == "yes":
                    self.getPoints(int(raw_input("How many total points did you get (from ticket(s))?")))
                return True

            count = length - pars['WILDCARDS']
#             for i in reversed(range(0, sum(self.trains))):
#                 if self.trains[i]==pars['COLOR']:
#                     count-=1
#                     self.board.useTrain(self.trains.pop(i))
#                     if count==0:
#                         break
            for i in range(0, count):
                self.trains[pars['COLOR']] -= 1
                self.board.useTrain(pars['COLOR'])

            count = pars['WILDCARDS']
#             for i in reversed(range(0, len(self.trains))):
#                 if self.trains[i]==8:
#                     count-=1
#                     self.board.useTrain(self.trains.pop(i))
#                     if count==0:
#                         break
            self.trains[8] -= count
            for i in range(0, count):
                self.board.useTrain(8)



            # Check if a ticket was completed
            G = self.board.graph.copy()
#             remlist=[()]
            for (u, v, d) in G.edges(data='claimed'):
                if d == -1:
                    G.remove_edge(u, v)
#             for (u,v) in remlist:
#                 G.remove_edge(u, v)
#                 if d<0.5: print('(%d, %d, %.3f)'%(n,nbr,d))
            keeptickets = []
            i = 0
#             self.print_func("T HAND: ")
#             self.print_func(self.tickets);
            for t in self.tickets:
                if nx.has_path(G, t[0], t[1]):
                    self.getPoints(t[2])
                else:
                    keeptickets.append(t)
                i += 1

#             newtickets1=[]
#             for i in keepids:
#                 newtickets1.append(self.tickets[i])
            if len(keeptickets) > 0:
                self.tickets = keeptickets
#             self.tickets=newtickets1

        elif pars['TYPE'] == 1:
            if pars['COLOR'] == 9:
                if self.auto:
                    self.trains[self.board.nextTrain()] += 1
            else:
                try:
                    self.board.takeVisibleTrain(pars['COLOR'])
                    self.trains[pars['COLOR']] += 1
                except Exception as e:
                    warnings.warn("Error while drawing visible train!")
                    warnings.warn(str(e))
                    return False
        elif pars['TYPE'] == 2:
            if not self.auto:
                return True
            self.newTickets = [];
#             self.print_func("BOARD: ")
#             self.print_func(self.board.tickets)
            for i in range(0, 3):
#                 print(len(self.board.tickets))
                n = self.board.nextTicket()
#                 print(n)
                self.newTickets.append(n)
            self.returnUnwantedTickets()

#             self.print_func("T HAND NEW: ")
#             self.print_func(self.newTickets)
            for i in self.newTickets:
                self.tickets.append(i)
#             self.tickets=sorted(self.tickets)
        return True