~yacinechaouche/fortune/products

« back to all changes in this revision

Viewing changes to Autopilot/strategy2.py

  • Committer: yacinechaouche at yahoo
  • Date: 2014-10-12 00:26:01 UTC
  • Revision ID: yacinechaouche@yahoo.com-20141012002601-dv4syqtl2113doao
Added two strategy players in new Autopilot directory.
So far, strategy1 player is doing better that strategy2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import pexpect
 
2
import sys
 
3
 
 
4
fortune              = pexpect.spawn("python fortune.py")
 
5
fortune.logfile_read = sys.stdout
 
6
fortune.dont_wait    = False
 
7
def buy_land():
 
8
    fortune.expect(" >")
 
9
    fortune.sendline("buy max land")
 
10
    response = fortune.expect([" >","max = 0.00", "max = -0.00"])
 
11
    if response in [1,2]:
 
12
        fortune.sendline("")
 
13
    else:
 
14
        fortune.sendline("end")
 
15
    # else:
 
16
    #     fortune.sendeof()
 
17
 
 
18
def sell_land():
 
19
    fortune.expect(" >")    
 
20
    fortune.sendline("sell all land")
 
21
    # fortune.expect(" >")
 
22
    # fortune.sendline("show wealth")
 
23
 
 
24
def buy_spices():
 
25
    fortune.expect(" >")
 
26
    fortune.sendline("buy spices")
 
27
    fortune.expect("max = (\d+)")
 
28
    max_to_buy = fortune.match.groups()[0]
 
29
    fortune.sendline(str(int(max_to_buy) / 2))
 
30
 
 
31
def end_turn():
 
32
    # if not fortune.dont_wait:
 
33
    #     fortune.expect(" >")
 
34
    #     fortune.dont_wait = False
 
35
    fortune.expect(" >")
 
36
    fortune.sendline("end")
 
37
 
 
38
def sell_spices():
 
39
    fortune.expect(" >")
 
40
    fortune.sendline("sell all spices")
 
41
    fortune.sendline(["end",""][fortune.expect([" >","max"])])
 
42
    
 
43
def quit(): 
 
44
    fortune.expect(" >")    
 
45
    fortune.sendline("show wealth")
 
46
    fortune.expect(" >") 
 
47
    fortune.sendline("q")
 
48
    fortune.wait()
 
49
    fortune.close()
 
50
    print
 
51
 
 
52
def main():
 
53
    """
 
54
    After 2 years, buy all land
 
55
    After 7 years :
 
56
     - sell all land
 
57
     - buy spices for half the cash
 
58
    After 9 years :
 
59
     - sell all spices
 
60
     - buy all land
 
61
    """
 
62
 
 
63
    sequence = [ (4*2,[buy_land]),(4*7,[sell_land,buy_spices]),(4*9,[sell_spices,buy_land]) ]
 
64
    i        = 0
 
65
    modulo   = sequence[i][0]
 
66
    for x in xrange(1,4*64): # 64 years
 
67
        if not x % modulo:
 
68
            # print "%d pourcent %d = %d" % (x,modulo, x%modulo)
 
69
            for action in sequence[i][1]:
 
70
                action()
 
71
            i = (i + 1) % len(sequence)
 
72
            modulo = sequence[i][0]
 
73
        end_turn()
 
74
    quit()
 
75
main()
 
76
 
 
77
    
 
78