Skip to content

Commit 186aecd

Browse files
committed
Better randomization.
1 parent 2b36d43 commit 186aecd

File tree

7 files changed

+91
-56
lines changed

7 files changed

+91
-56
lines changed

AbsShip.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import Glyphs
55
from Quips import Quips
6+
from Quadrant import Quadrant
67

78
class AbsShip(abc.ABC):
89
''' The first step, into a much larger universe ... '''
@@ -14,7 +15,7 @@ def __init__(self):
1415
def get_glyph(self):
1516
pass
1617

17-
class StarBase(AbsShip):
18+
class ShipStarbase(AbsShip):
1819

1920
def __init__(self):
2021
super().__init__()
@@ -41,7 +42,7 @@ def launch_enterprise(ship):
4142
ship.docked = False
4243

4344

44-
class Enterprise(AbsShip):
45+
class ShipEnterprise(AbsShip):
4546

4647
def __init__(self):
4748
super().__init__()
@@ -56,14 +57,16 @@ def __init__(self):
5657
self.photon_damage = 0
5758
self.phaser_damage = 0
5859
self.photon_torpedoes = 0
59-
StarBase.dock_enterprise(self)
60-
StarBase.launch_enterprise(self)
60+
ShipStarbase.dock_enterprise(self)
61+
ShipStarbase.launch_enterprise(self)
6162

6263
def get_glyph(self):
6364
return Glyphs.ENTERPRISE
6465

65-
6666
def damage(self, game, item):
67+
'''
68+
Damage the Enterprise.
69+
'''
6770
if game.is_testing:
6871
return
6972
if random.randint(0, 6) > 0:
@@ -94,8 +97,10 @@ def damage(self, game, item):
9497
game.display(Quips.jibe_damage('Phasers'))
9598
game.display()
9699

97-
98100
def repair(self, game):
101+
'''
102+
Rapair damage to the Enterprise.
103+
'''
99104
if self.navigation_damage > 0:
100105
self.navigation_damage -= 1
101106
if self.navigation_damage == 0:
@@ -140,9 +145,7 @@ def repair(self, game):
140145
return True
141146
return False
142147

143-
144148
def short_range_scan(self, game):
145-
146149
if self.short_range_scan_damage > 0:
147150
game.display(Quips.jibe_damage('Short Ranged Scanners'))
148151
game.display()
@@ -151,38 +154,43 @@ def short_range_scan(self, game):
151154
Quadrant.display_area(game, quad)
152155
game.display()
153156

154-
155157
def long_range_scan(self, game):
156158
if self.long_range_scan_damage > 0:
157159
game.display(Quips.jibe_damage('Long Ranged Scanners'))
158160
game.display()
159161
return
160162
sb = ""
161-
game.display("-------------------")
162163
pw_sector = game.game_map.sector
163164
if pw_sector < 5:
164165
pw_sector = 5
165166
elif pw_sector > 59:
166167
pw_sector = 59
168+
dots = None
167169
for peek in range(pw_sector-5, pw_sector + 6):
168170
quad = game.game_map.scan_quad(peek)
169-
f"{quad.klingons}{quad.starbases}{quad.stars} "
170-
sb += "|"
171+
lines = \
172+
(f"Sector: {quad.number:>02}",
173+
f"Enemies:{quad.klingons:>02}",
174+
f"Bases:{quad.starbases:>02}",
175+
f"Stars: {quad.stars:>03}")
176+
str_ = ' | '.join(lines)
177+
dots = '-' * len(str_) + "\n"
178+
sb += dots
179+
sb += str_
171180
game.display(sb)
172181
sb = ""
173-
game.display("-------------------")
182+
game.display(dots)
174183
game.display()
175184

176185

177-
class KlingonShip(AbsShip):
186+
class ShipKlingon(AbsShip):
178187

179188
def __init__(self):
180189
super().__init__()
181190
self.xpos = 0
182191
self.ypos = 0
183192
self.shield_level = 0
184193

185-
186194
def get_glyph(self):
187195
return Glyphs.KLINGON
188196

Calculators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from math import atan2, pi, sqrt, cos, sin
1+
from math import sqrt
22
import random
33

44
from MapGame import *
@@ -53,7 +53,7 @@ def navigation(game):
5353
game.display()
5454
else:
5555
if game.game_map.klingons > 0:
56-
KlingonShip.attack(game)
56+
ShipKlingon.attack(game)
5757
game.display()
5858
elif not game.enterprise.repair(game):
5959
game.enterprise.damage(game, -1)

Controls.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from math import pi, sqrt, cos, sin
21
import random
32

43
import TrekStrings
@@ -72,7 +71,7 @@ def phasers(game):
7271
game.game_map.remove_items(destroyed_ships)
7372
if game.game_map.klingons > 0:
7473
game.display()
75-
KlingonShip.attack(game)
74+
ShipKlingon.attack(game)
7675
game.display()
7776

7877

@@ -162,7 +161,7 @@ def torpedos(game):
162161
game.display("Torpedo missed.")
163162
if len(game.game_map.get_area_klingons()) > 0:
164163
game.display()
165-
KlingonShip.attack(game)
164+
ShipKlingon.attack(game)
166165
game.display()
167166

168167

MapGame.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import TrekStrings
33

44
import Glyphs
5-
from AbsShip import KlingonShip
5+
from AbsShip import ShipKlingon
66
from Points import Destination
77
from Quadrant import Quadrant
88

@@ -36,18 +36,22 @@ def place(self, takers):
3636
continue
3737
taken = 0
3838
while taken != to_take:
39-
for ss, area in enumerate(self.areas()):
40-
should_take = random.randrange(1, 8)
41-
if should_take % (ss + 2) == 0:
42-
if which is 0:
43-
area.place_glyph(Glyphs.STARBASE)
44-
elif which is 1:
45-
area.place_glyph(Glyphs.STAR)
46-
elif which is 2:
47-
area.place_glyph(Glyphs.KLINGON)
48-
taken += 1
49-
if taken == to_take:
50-
break;
39+
ss = random.randrange(1, 64)
40+
area = self.get_area(ss)
41+
should_take = random.randrange(1, 8)
42+
if which is 0:
43+
if area.count_glyphs(Glyphs.STARBASE) != 0:
44+
continue
45+
area.place_glyph(Glyphs.STARBASE)
46+
elif which is 1:
47+
area.place_glyph(Glyphs.STAR)
48+
elif which is 2:
49+
if area.count_glyphs(Glyphs.KLINGON) > 3:
50+
continue
51+
area.place_glyph(Glyphs.KLINGON)
52+
taken += 1
53+
if taken == to_take:
54+
break;
5155
takers[which] -= taken
5256
return tuple(takers)
5357

@@ -107,10 +111,9 @@ def scan_quad(self, sector):
107111
Return a scan (LRS?) for a specific quadrant.
108112
Return empty quadrant, on error.
109113
'''
110-
if sector > 0 and sector < 65:
111-
for area in self.areas():
112-
if area.number == self.sector:
113-
return Quadrant.from_area(area)
114+
area = self.get_area(sector)
115+
if area:
116+
return Quadrant.from_area(area)
114117
return Quadrant()
115118

116119
def _count(self, glyph):
@@ -141,7 +144,7 @@ def get_area_klingons(self):
141144
results = []
142145
area = self.area()
143146
for data in area.get_data(Glyphs.KLINGON):
144-
ship = KlingonShip()
147+
ship = ShipKlingon()
145148
ship.from_map(data.xpos, data.ypos)
146149
results.append(ship)
147150
return results
@@ -222,7 +225,7 @@ def randomize(self, bases=None, stars=None, aliens=None):
222225
self.init()
223226
takers = bases, stars, aliens
224227
while takers:
225-
for lrs in self.map:
228+
for lrs in self._map:
226229
takers = self.place(takers)
227230
if not takers:
228231
break

MapSparse.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def remove(self, xpos, ypos):
6767

6868
def get_map(self):
6969
'''
70-
Generate a map of this sector. Map is full
70+
Generate a map of this AREA. Map is full
7171
of Glyphs.SPACE on error.
7272
'''
7373
results = [[Glyphs.SPACE for _ in range(8)] for _ in range(8)]
@@ -142,31 +142,52 @@ def clone(piece):
142142

143143
def __init__(self):
144144
self.initalized = False
145-
self.map = [[[y,x] for y in range(8)] for x in range(8)]
145+
self._map = [[[y,x] for y in range(8)] for x in range(8)]
146146

147147
def init(self, reset=False):
148148
if not reset and self.initalized:
149149
return
150-
for xx, row in enumerate(self.map):
150+
for xx, row in enumerate(self._map):
151151
lrs = SparseMap.Region()
152152
for yy, col in enumerate(row):
153-
self.map[xx][yy] = [lrs, SparseMap.Area()]
153+
self._map[xx][yy] = [lrs, SparseMap.Area()]
154154
self.name_areas()
155155
self.initalized = True
156156

157+
def get_area(self, which):
158+
'''
159+
Get an AREA from the map.
160+
Area identifiers are 1's based.
161+
Returns False on under / over flows.
162+
'''
163+
if which < 1:
164+
return False
165+
if which >= 65:
166+
return False
167+
which -= 1 # ASSURED
168+
fid = which / 8
169+
ypos = 0
170+
xpos = int(fid)
171+
if not fid.is_integer():
172+
ypos = which %8
173+
return self._map[xpos][ypos][1]
174+
157175
def data(self):
158176
''' Enumerate thru every [Region, Area] in the Map '''
159-
for row in self.map:
177+
for row in self._map:
160178
for col in row:
161179
yield col
162180

163181
def areas(self):
164182
''' Enumerare thru every Area on the Map '''
165-
for row in self.map:
183+
for row in self._map:
166184
for col in row:
167185
yield col[1]
168186

169187
def name_areas(self):
188+
'''
189+
Assign / re-assign 'Trekian names.
190+
'''
170191
names = list(TrekStrings.AREA_NAMES)
171192
for num, area in enumerate(self.areas(),1):
172193
index = random.randint(0, len(names) - 1)
@@ -175,6 +196,9 @@ def name_areas(self):
175196
del names[index]
176197

177198
def get_sector_names(self):
199+
'''
200+
Return a list of [secor numbers, sector_name] pairs.
201+
'''
178202
results = []
179203
temp = []
180204
for ss, col in enumerate(self.areas(),1):

Quadrant.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import Glyphs
22

33
class Quadrant():
4-
def __init__(self, num=-1, name='', lines=[],
5-
aliens=-1, stars=-1, starbases=-1):
4+
def __init__(self, num=-1, name='',
5+
aliens=-1, stars=-1,
6+
starbases=-1, lines=[]):
67
self.name = name
78
self.number = num
89
self.lines = lines
@@ -21,10 +22,11 @@ def from_area(area):
2122
name = area.name
2223
num = area.number
2324
map = area.get_map()
24-
return Quadrant(num, name, map,
25-
area.get_data(Glyphs.KLINGON),
25+
return Quadrant(num, name,
26+
area.count_glyphs(Glyphs.KLINGON),
2627
area.count_glyphs(Glyphs.STAR),
27-
area.count_glyphs(Glyphs.STARBASE))
28+
area.count_glyphs(Glyphs.STARBASE),
29+
map)
2830

2931

3032
@staticmethod
@@ -39,7 +41,7 @@ def display_area(game, quad):
3941
sb += f" -=--=--=--=--=--=--=--=- Quadrant: {quad.name}\n"
4042
info = list()
4143
info.append(f" Area: [{quad.number}]\n")
42-
info.append(f" Hazzards: [{quad.stars + len(quad.klingons)}]\n")
44+
info.append(f" Hazzards: [{quad.stars + quad.klingons}]\n")
4345
info.append(f" Stardate: {game.star_date}\n")
4446
info.append(f" Condition: {game.enterprise.condition}\n")
4547
info.append(f" Energy: {game.enterprise.energy}\n")
@@ -54,7 +56,7 @@ def display_area(game, quad):
5456
sb += f" -=--=--=--=--=--=--=--=- Docked: {game.enterprise.docked}\n"
5557
print(sb, end='')
5658

57-
if len(quad.klingons) > 0:
59+
if quad.klingons > 0:
5860
game.display()
5961
game.display("Condition RED: Klingon ship{0} detected.".format("" if quad.klingons == 1 else "s"))
6062
if game.enterprise.shield_level == 0 and not game.enterprise.docked:

StarTrek2020.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from math import pi, sqrt, cos, sin
2-
import random
1+
import random
32

43
import TrekStrings
54

@@ -17,7 +16,7 @@ class Game(Con):
1716
def __init__(self):
1817
self.is_testing = False
1918
self.game_map = GameMap()
20-
self.enterprise = Enterprise()
19+
self.enterprise = ShipEnterprise()
2120
self.star_date = 0
2221
self.time_remaining = 0
2322
self.destroyed = False
@@ -27,7 +26,7 @@ def run(self):
2726
game.star_date = random.randint(0, 50) + 2250
2827
game.time_remaining = 40 + random.randint(0, 9)
2928
game.destroyed = False
30-
stars = random.randrange(32, 64)
29+
stars = random.randrange(400, 600) # 4096 = ALL
3130
aliens = random.randrange(12, 16)
3231
starbases = random.randrange(6, 8)
3332
game.game_map.randomize(starbases, stars, aliens)

0 commit comments

Comments
 (0)