repl.it
@pyelias/

Farming Game verifier

Python

testing

fork
loading
Files
  • main.py
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""
its a bot, but it still plays following the normal rules
the strategy is kinda dumb but it works well
each turn it:
  buys enough seeds to fill all the unused land
  buys enough workers to harvest the land
  buys more land
in that order (technically repeats some stuff to be clever)
"""

from replit import clear
import random
from readchar import readkey
from sys import stdout
from time import sleep
import requests

CHEATING = False # looking at you haaruun

scores = 'https://farming-game-database-1.pyelias.repl.co'

nscores = []

def getscore(p):
  return p[0]

def getdb(p, params={}):
  # why is this being evaled
  inf = float("inf")
  try: return eval(str(requests.get(scores+'/'+p, params=params).text))
  except: return 'Error'

def add_score(name, score, l, log):
  data = {
    "name": name,
    "score": score,
    "l": l,
    "log": log
  }
  requests.post(scores + "/add", json=data)

for player in getdb('lead'):
  nscores.append((player['s'], player['n'].rstrip()))

nscores.sort(key=getscore)
nscores.reverse()

stscores = []
for s in nscores:
  stscores.append(s[1] + ':   ' + str(s[0]) + 'pts')

def getavg():
  return float(str(getdb('avg'))[:8])

def newinput(prompt): 
  stdout.write(prompt)
  stdout.flush()
  return str(readkey())

times = {
  'a': 7,
  'b': 31,
  'c': 92,
  'd': 182,
  'e': 365
}

mults = {
  'a': 45,
  'b': 8,
  'c': 3,
  'd': 1.75,
  'e': 1
}

def intro():
  print('Hello and welcome to THE FARMING GAME! By PYer.')
  print(f'\n\tLEADERBOARD: (Avg Score: {getavg()})')
  print('\n'.join(stscores))
  newinput("press key")
  
  clear()
  print('''How long do you want to play? Of course these are only days in the game, not real life.

        A) One Week   [ \u001b[1mx45.0 pts \u001b[0m]
        B) One Month  [ \u001b[1mx8.00 pts \u001b[0m] [ \u001b[1mSUGGESTED \u001b[0m]
        C) Trimester  [ \u001b[1mx3.00 pts \u001b[0m]
        D) Half Year  [ \u001b[1mx1.75 pts \u001b[0m]
        E) One Year   [ \u001b[1mx1.00 pts \u001b[0m]
''')
  a = newinput('How long do you want to play? (A, B, C, D, E)? ').lower()
  try: 
    return a, times[a], mults[a]
  except: 
    return "b", times['b'], mults['b']


prices = {
  'people': 10,
  'land': 30,
  'seeds': 1
}

def div_up(a, b):
  return (a + b - 1) // b

class Game():
  def __init__(self, days, mult, rand=random):
    self.life = 0
    self.crop = 50
    self.people = 6
    self.land = 5*5
    self.seeds = 10
    self.unused_land = self.land
    self.gain = 0
    self.days = days
    self.mult = mult
    self.prices = prices.copy()
    self.extrabanner = 'Nothing extraordinary happened today. (Welcome, btw)'
    self.rand = rand

    self.log = []
  
  def r(self, a, b):
    return self.rand.randint(a, b)
  
  def choice(self, l):
    return self.rand.choice(l)

  def new_day(self):
    print(f'''THE FARMING GAME    ||
      By PYer        ||
  ======================
  Days Left    [ \u001b[1m{self.days} \u001b[0m] 
  Crops        [ \u001b[1m{self.crop} \u001b[0m]
  Seeds        [ \u001b[1m{self.seeds} \u001b[0m]
  Total Land   [ \u001b[1m{self.land} \u001b[0m] feet
  Unused Land  [ \u001b[1m{self.unused_land} \u001b[0m] feet
  Workers      [ \u001b[1m{self.people} \u001b[0m]
  Harvested    [ \u001b[1m{self.gain} \u001b[0m] crops!
  ===================================''')


  def score(self):
    s = 0
    s += self.life/5*4
    s += (self.crop-50)/5
    s += (self.land-25)*3
    s += self.people - 6
    s += (self.seeds - 10)/50
    s *= self.mult
    return int(s)

  def benefit(self):
    n = 1 if CHEATING else self.r(1, 4)
    if n == 1:
      o = self.r(1, 2)
      if o == 1:
        self.extrabanner = 'The god\'s have granted you a plentiful harvest!'
        self.gain *= self.r(2,4)
      elif o == 2:
        self.extrabanner = 'Aliens deposited a bunch of crops into your fields!'
        self.gain += self.r(self.gain, self.gain*2)
    elif n == 2:
      o = self.r(1, 2)
      if o == 1:
        self.extrabanner = 'A disease has plagued your workers!'
        self.people -= int(self.people/self.r(3, 6))
      elif o == 2:
        self.extrabanner = 'Some of your crops have rotted!'
        self.crop -= int(self.crop/self.r(3, 6))
    # original code didnt actually use this
    elif (n == 3 or n == 4) and 0:
      item = self.choice(('people', 'land', 'seeds'))
      uorn = self.r(1,2)
      change = int(self.prices[item]/self.r(2,10))
      if uorn == 1:
        self.prices[item] += change
        self.extrabanner = 'Higher Price on ' + item + '. New cost ' + str(self.prices[item]) + ' crops.'
      elif uorn == 2:
        self.prices[item] -= change
        self.extrabanner = 'Lower Price on ' + item + '. New cost ' + str(self.prices[item]) + ' crops.'

  def shop_info(self):
    clear()
    self.new_day()
    print(f'''SHOP:
    (1) Land        - 25ft [ \u001b[1m{self.get_available("land")} \u001b[0m] avalible [ \u001b[1mCost: {self.prices["land"]} \u001b[0m] crops
    (2) Workers     - 1 Person [ \u001b[1m{self.get_available("people")} \u001b[0m] avalible [ \u001b[1mCost: {self.prices["people"]} \u001b[0m] crops
    (3) Seed Packet - 5 seeds [ \u001b[1m{self.get_available("seeds")} \u001b[0m] avalible [ \u001b[1mCost: {self.prices["seeds"]} \u001b[0m] crops
    (0) EXIT
    ''')

  def shop(self):
    self.shop_info()
    while True:
      try:
        opt = int(newinput('What would you like to buy? [0, 1, 2, or 3] '))
        break
      except: pass

    things = {
      1: "land",
      2: "people", # slaves??
      3: "seeds"
    }
    if opt not in things:
      return
    thing = things[opt]

    # you spelled it wrong my dude
    avalible = self.get_available(thing)
    if avalible == 0:
      print(f'You can\'t buy any {thing}!')
      newinput('Press any key...')
      return
    else:
      print('\nYou can buy', avalible, f'{thing}.')
      while True:
        try:
          am = int(input('How many do you buy? '))
          break
        except: pass
      if am > avalible:
        print('Wow there thats too much!')
        newinput('Press any key...')
        return
    
    self.buy(thing, am)

  def get_available(self, thing):
    return self.crop // self.prices[thing]

  def buy(self, thing, count):
    self.log.append(("buy", thing, count))
    # easier for ai
    count = min(count, self.get_available(thing))
    self.crop -= count * self.prices[thing]
    if thing == "land":
      self.land += count*(5*5)
      self.unused_land += count*(5*5)
    elif thing == "people":
      self.people += count
    elif thing == "seeds":
      self.seeds += count*5

  def plant(self):
    self.log.append(("plant",))
    #if self.unused_land == 0:
    #  print('You have already used all your land!')
    #else:
      # bruh have you not heard of an elif
      # seriously wtf
      # if self.seeds == 0:

    if self.unused_land <= self.seeds:
      self.seeds -= self.unused_land
      self.unused_land = 0
    else:
      self.unused_land -= self.seeds
      self.seeds = 0
  
  def next_day(self):
    self.log.append(("next",))
    # what even was going through your head
    # when you wrote this line
    #self.gain = int((self.land-self.unused_land)- ((self.land-self.unused_land)-(self.people*2)))

    self.gain = min(self.people * 2, self.land-self.unused_land)

    self.unused_land += self.gain
    if self.r(1,5) == 2 or CHEATING: # why 2 tho
      self.benefit()
    self.crop += self.gain
    self.life += self.gain
    self.days -= 1

  # shake it off
  def player_play(self):
    while self.days >= 0:
      clear()
      self.new_day()
      print(self.extrabanner)
      self.extrabanner = 'Nothing extraordinary happened today.'
      print('===================================')
      while True:
        try:
          opt = int(newinput('Would you like to (1) enter the shop, (2) plant seeds, or (3) end the day? '))
          break
        except: pass
      if opt == 1:
        self.shop()
      elif opt==2:
        self.plant()
      elif opt==3:
        self.next_day()
    
    print('Game over! In one year, you managed to harvest a total of', self.life, ' crops, ending the game with', self.crop, 'crops!')
    print('\nThis gives you a total score of [', self.score(), '] pts!')
    return

  def ai_play(self, show=True, land_fac=1, stock_at=5):
    while self.days >= 0:
      if show:
        clear()
        self.new_day()
        sleep(max(0.1, 1 / (self.days + 1)))

      while True:
        # fill all land
        seeds_to_buy = self.unused_land - self.seeds
        self.buy("seeds", div_up(seeds_to_buy, 5))
        self.plant()

        #stock crops for last days
        if self.days < stock_at:
          break

        # harvest all land
        workers_to_buy = div_up(self.land, 2) - self.people
        self.buy("people", workers_to_buy)

        # get more land
        avail_land = self.get_available("land")
        land_to_buy = div_up(avail_land, land_fac)
        self.buy("land", land_to_buy)
        if not land_to_buy:
          break
      self.next_day()

length, days, mult = intro()
rand_state = random.getstate()
game = Game(days, mult)
game.player_play()

full_log = [length, rand_state, game.log]

score = game.score()
print("bot scored", float(score))

perc = ((score - getavg()) / getavg())*100

print('Lets look at the data! You seem to do ' + str(perc)[:6] + '% better than an average player!')

winner = 1 or score > nscores[4][0]

if winner:
  print('This is a winning score!')
  add_score(input("name: "), score, 1, full_log)
  print('Score saved!')