Python Forum
Forum-wide Competition: Rock Paper Scissors [Meta-thread]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Forum-wide Competition: Rock Paper Scissors [Meta-thread]
#8
Ok, I won't delay this any longer.  I was trying to tweak it to try to out-smart Ichabod's sirius bot, but it's taken longer than I thought, and I said I'd add a little something to help kick us off, so here's my entry.

The first thing to note, is that it follows a set strategy very stringently.  Three strategies, in fact.  So the first thing it does when it's called, is figure out if it's in the middle of a strategy, and if it is, it responds with the next step in that strategy.  If it's NOT in the middle of a strategy (ie, every 3rd round), then it calculates it's current performance, and bases it's next strategy off of that.  So if it's losing, it uses one strat, if it's winning, a different one, and if the scores are perfectly tied (ie: the very first round) a third strategy.  In general, I like this approach, so I'll probably continue to tweak it to try to figure out a way around ichabod's predictive analysis.

# nilabot - nilamo
import sys

winning_match = {'R':'S', 'P':'R', 'S':'P'}
lookup = {}
# indexes match our current record... 0=tied, 1=winning, -1=losing
strategies = ['PRP', 'RSR', 'SPR']

''' lookup will end up looking like...
        RR: 0, RS: 1, RP: -1
    for all 9 combinations.
    0 is a tie, -1 a loss, +1 a win
'''
def populate_lookup():
    for left, loser in winning_match.items():
        for right in winning_match.keys():
            result = 0
            if right == loser:
                result = 1
            elif left != right:
                result = -1
            lookup[left+right] = result
    
def current_results(rounds):
    return sum(lookup[round] for round in rounds)

def current_strategy(rounds):
    current_strategy_length = len(rounds) % 3
    if current_strategy_length:
        strat_starts_with = rounds[-current_strategy_length][0]
        for strategy in strategies:
            if strategy.startswith(strat_starts_with):
                return strategy[current_strategy_length]
    return None
    
def get_response(rounds):
    strategy = current_strategy(rounds)
    # if we're in the middle of a strategy, keep going with it
    if strategy:
        return strategy
    # find out which strategy we should start next
    populate_lookup()
    record = current_results(rounds)
    ndx = 0 if record == 0 else 1 if record > 0 else -1
    
    response = strategies[ndx][0]
    return response
    
if __name__ == '__main__':
    rounds = sys.argv[1:]
    print(get_response(rounds))
Reply


Messages In This Thread
RE: Forum-wide Competition: Rock Paper Scissors [Meta-thread] - by nilamo - Oct-12-2016, 02:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  system-wide unique incrementing string or number Skaperen 11 4,307 Jul-08-2020, 08:10 PM
Last Post: Yoriz
  re module: want a regexhttps://python-forum.io/Thread-re-module-want-a-regex Skaperen 4 2,647 Sep-19-2019, 10:54 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020