def __init__(self, *args, **kwargs): super(BlocksBoard, self).__init__(*args, **kwargs) neighbours = self.neighbour_cross_locs for tile in self: tile.maxnum = len( [self.valid(nbloc) for nbloc in neighbours(tile)] ) tile.num = Loop(range1(tile.maxnum))
def display(self): print(nl*5) for n, word in enumerate1(self.words): print(lettertpl % n, space, word, nl) lnumbers = sjoin(range1(len(word)), space * word.spacing(), lettertpl) print(space*4, lnumbers, nl*2) print(self.stattpl % (self.random_reveals, self.guesses), nl)
def __init__(self, *args, **kwargs): super(BlocksBoard, self).__init__(*args, **kwargs) self.current = Loc(0,0) self.hl_visible = False neighbours = self.neighbour_cross_locs for tile in self: tile.maxnum = len( [self.valid(nbloc) for nbloc in neighbours(tile)] ) tile.num = Loop(range1(tile.maxnum)) if randomize_option: for _ in range(randrange(0, tile.maxnum)): tile.num.next()
def __init__(self, num): """Create player's board and randomly place `num_ships` ships on it.""" self.num = num self.ai = bool(num in ai_players) self.board = BattleshipBoard(size, Blank, num_grid=True, padding=padding, pause_time=0, screen_sep=0) B = self.board for ship in range1(num_ships): for loc in B.random_placement(ship): B[loc] = Ship(loc) if not self.ai: for tile in B: tile.revealed = True
def __init__(self, num): """Create player's board and randomly place `num_ships` ships on it.""" self.num = num self.ai = bool(num in ai_players) self.board = BattleshipBoard(size, Blank, num_grid=False, padding=padding, pause_time=0, screen_sep=0) B = self.board for ship in range1(num_ships): for loc in B.random_placement(ship): B[loc] = Ship(loc) if not self.ai: for tile in B: tile.revealed = True
def run(self, pause_time, display=True): """Run the machine, return tuple of (symbol line, win_amount).""" rotations = [randint(1,4) for _ in range(num_reels)] # reel rotations per cycle rd = reel_delay total_cycles = [randint(x, x+rd) for x in range(first_stop, first_stop + rd*num_reels, rd)] reels = [Reel(rotations, max_cycle) for rotations, max_cycle in zip(rotations, total_cycles)] for cycle in range1(max(total_cycles)): line = sjoin( [reel.symbol(cycle) for reel in reels] ) if display: print(nl*5, line) sleep(pause_time) return self.done(reels, display, line)
def draw(self, pause=None): pause = pause or self.pause_time print(nl * self.screen_sep) if self.num_grid: print(space, space*(self.xpad + 1), ujoin( range1(self.width), space, self.tiletpl ), nl * self.ypad) for n, row in enumerate1(self.board): args = [self.tiletpl % n] if self.num_grid else [] if self.stackable: row = (tile[-1] for tile in row) args = [space] + args + [ujoin(row, space, self.tiletpl), nl * self.ypad] print(*args) self.status() sleep(pause)
def draw(self, pause=None): pause = pause or self.pause_time print(nl * self.screen_sep) if self.num_grid: print(space, space * (self.xpad + 1), ujoin(range1(self.width), space, self.tiletpl), nl * self.ypad) for n, row in enumerate1(self.board): args = [self.tiletpl % n] if self.num_grid else [] if self.stackable: row = (tile[-1] for tile in row) args = [space] + args + [ ujoin(row, space, self.tiletpl), nl * self.ypad ] print(*args) self.status() sleep(pause)
def get_move(self, player): while True: try: return self._get_move(player) except (IndexError, AssertionError): print(self.textinput.invalid_move) def _get_move(self, player): cmd = self.textinput.getinput() if not cmd: return src, goal, ships = cmd src, goal = stars[src], stars[goal] assert src == player and src.ships >= ships return src, goal, ships if __name__ == "__main__": board = BetelgeuseBoard(size, Blank, padding=padding, pause_time=pause_time) betelgeuse = Betelgeuse() fleets = [] stars = [Star(board.random_blank(), n) for n in range1(num_stars)] players = [Player(c) for c in player_chars] for n, player in enumerate(players): stars[n].char = player.char try: BasicInterface().run() except KeyboardInterrupt: sys.exit()