def test_get_row(self):
        file_path = "../data/commonwords.txt"
        g = state.GameState(file_path)
        row = g.get_row()

        self.assertEqual(len(set(row)), len(row))  #check no dupes
        self.assertEqual(10, len(row))
示例#2
0
文件: main.py 项目: Decin/stabbybot
def main():
    parser = get_parser()
    args = vars(parser.parse_args())
    server = args['server']
    username = args['username']

    ws = websocket.WebSocket()
    ws.settimeout(1)
    ws.connect('ws://%s:443' % server, origin=GAME_URL)

    # instantiate classes
    gs = state.GameState()
    outgoing = comm.Outgoing(ws)
    bot = brain.GenTwo(outgoing)

    # init comms
    outgoing.begin(GAME_VER)
    outgoing.setname(username)

    try:
        while True:
            raw_data = ws.recv()
            comm.incoming(gs, raw_data)

            if gs.game_state['dead']:
                break

            bot.main(gs.game_state)
    except KeyboardInterrupt:
        pass

    ws.close()
    def test_update_row(self):
        file_path = "../data/commonwords.txt"
        g = state.GameState(file_path)

        old_next_row = g.next_row
        g.update_row()

        self.assertEqual(old_next_row, g.current_row)
示例#4
0
 def __init__(self, resolution):
     self.resolution = resolution
     self.engine = engine.GfxEngine(self.resolution)
     self.game_state = state.GameState([])
     self.history = []
     # one player by default
     self.player_list = [
         players.Human("white"),
         players.AI("black", "random")
     ]
    def test_update_current_word(self):
        file_path = "../data/commonwords.txt"
        g = state.GameState(file_path)

        next_word = g.current_row[0] + " "
        next_next_word = g.current_row[1] + " "
        g.update_current_word()
        self.assertEqual(next_word, g.current_word)

        g.update_current_word()
        self.assertEqual(next_next_word, g.current_word)
    def test_get_words(self):
        file_path = "../data/commonwords.txt"
        g = state.GameState(file_path)

        words = g.get_words()

        file = open(file_path)
        from_file = file.read()
        file.close()

        self.assertTrue(all((w in from_file) for w in words))
        self.assertTrue(all(len(w) > 2 for w in words))
示例#7
0
def state_maker(piece_list):
    output_state = state.GameState([])
    output_state.piece_list = piece_list
    output_state.pieces_by_position = dict(zip([piece.position 
                                for piece in output_state.piece_list], 
                                output_state.piece_list))
    output_state.captured_pieces = []
    output_state.whose_turn = "white"
    output_state.can_castle = state.can_castle(output_state)
    output_state.move_history = []

    return output_state   
    def test_update_user_row(self):
        file_path = "../data/commonwords.txt"
        g = state.GameState(file_path)

        self.assertEqual("", g.user_word)

        g.update_user_word("h")

        self.assertEqual("h", g.user_word)

        g.update_user_word("e")
        g.update_user_word("l")
        g.update_user_word("l")
        g.update_user_word("o")

        self.assertEqual("hello", g.user_word)
示例#9
0
    def __init__(self, hs_filepath, output_filepath):
        """Create a new game parser.
        
        Args:
            - hs_filepath: The source of the Hearthstone logs
            - output_filepath: A destination filepath to store lines that are parsed
        """
        self.hs_filepath = hs_filepath
        self.logfile = file(self.hs_filepath, 'r')

        self.output_filepath = output_filepath
        self.outfile = file(self.output_filepath, 'w')

        # Position in the HS log
        self.pos = 0
        # The state of the game
        self.gstate = state.GameState()
示例#10
0
def main():
  screen_width, screen_height = 80, 50
  map_width, map_height = 80, 45

  tcod.console_set_custom_font('data/arial10x10.png', tcod.FONT_LAYOUT_TCOD)

  with tcod.console_init_root(
    screen_width,
    screen_height,
    'Forest',
    renderer=tcod.RENDERER_SDL2,
    vsync=True,
    order='F'
  ) as console:
    session_ = Session()
    session_.active_map = map_generator.generate(map_width, map_height)
    session_.active_map.session = session_
    current_state = state.GameState(session_)
    current_state.run(console)
示例#11
0
    def test_accuracy(self):
        file_path = "../data/commonwords.txt"
        g = state.GameState(file_path)
        g.current_word = "malnutrition "  # 12

        g.user_word = "malnutrition "
        self.assertEqual(0, g.accuracy())

        g.user_word = "maln "
        self.assertEqual(8, g.accuracy())

        g.user_word = "manutrition "
        self.assertEqual(10, g.accuracy())

        g.user_word = "malnutritiob "
        self.assertEqual(1, g.accuracy())

        g.user_word = "malnutritio "
        self.assertEqual(1, g.accuracy())

        g.user_word = "malnutritional "
        self.assertEqual(2, g.accuracy())
示例#12
0
# Miyagi bot

import discord
import config
import quest
import state
import user
import shlex
import threading
import datetime
import asyncio

client = discord.Client()
gameState = state.GameState()
lastTimedActionTime = datetime.datetime.now()
asyncTimer = None


class AsyncTimer:
    def __init__(self, timeout, callback):
        self._timeout = timeout
        self._callback = callback
        self._task = asyncio.ensure_future(self._job())

    async def _job(self):
        await asyncio.sleep(self._timeout)
        await self._callback()

    def cancel(self):
        self._task.cancel()
示例#13
0
        end_time = time.time() + 60

        while time.time() < end_time:
            self.get_user_in()
            gui.highlight(self.word_counter, correct)
            gui.time.set(str(int(end_time - time.time())) + "s")
            gui.disable_button(gui.start_timer)
            if g.user_word and g.user_word[-1] == " ":
                correct = self.is_correct()
                self.update_state()

            gui.root.update()
            gui.root.update_idletasks()

        print(str(g.correct_words) + " words per minute!")
        if g.total_chars:
            print(str((g.total_chars - g.wrong_chars) / g.total_chars)[2:4] + "% accuracy!")


if __name__ == "__main__":
    # source: https://gist.github.com/deekayen/4148741
    file_path = "data/commonwords.txt"
    g = state.GameState(file_path)

    ui = user_interface.Gui(g)
    ui.build_gui()

    game = Game(g, ui)
    game.run_game()
    exit(0)
示例#14
0
    if selected_tile:
        st_pixel_coordinates = util.get_screen_coords(map_xy[0], map_xy[1])
        st_screen_coordinates = st_pixel_coordinates[
            0] + game_state.background_x_middle + (
                tile_width /
                2) - 20, st_pixel_coordinates[1] + game_state.background_top
        screen.blit(art.selected_tile_image,
                    [st_screen_coordinates[0], st_screen_coordinates[1]])

    pygame.display.flip()


screen_width = 800
screen_height = 600
game_state = state.GameState(screen_width, screen_height)
game_state.active_map = game_map.Map([25, 25], [screen_width, screen_height])
game_state.active_map.mapgen()
new_tree = plant.Tree(0, 0, game_state.active_map.game_tile_rows[0][0])
game_state.active_map.plants.append(new_tree)


def main():
    active_map = game_state.active_map
    active_map.x_shift = game_state.screen_width / 2 - (
        game_state.background_width / 2)
    active_map.y_shift = game_state.screen_height / 2 - (
        game_state.background_height / 2)
    while True:
        pos = pygame.mouse.get_pos()
        map_xy = util.get_map_coords(pos, game_state.active_map.x_shift,
示例#15
0
    def run(self):
        self.last_action = 'Stop'

        while True:
            received_message = self.receive_message()

            if received_message.msg_type == messages.STATE:
                game_state = self.game_states[received_message.agent_id]
                game_state.set_walls(received_message.wall_positions)
                game_state.set_food_positions(received_message.food_positions)

                agent_action = self.choose_action(received_message)
                reply_message = self.create_action_message(
                    received_message.agent_id, agent_action)
                self.send_message(reply_message)

                self.last_action = agent_action
            elif received_message.msg_type == messages.INIT:
                agent_id = received_message.agent_id
                ally_ids = self.get_agent_allies(agent_id)
                enemy_ids = self.get_agent_enemies(agent_id)

                if agent_id in self.agents:
                    del self.agents[agent_id]

                self.game_number[agent_id] = 0
                self.agents[agent_id] = self.agent_classes[agent_id](agent_id,
                                                                     ally_ids,
                                                                     enemy_ids)
                self.send_message(self.create_ack_message())
                print 'Initialized %s\tID: %d\tClass: %s' % (
                    self.agent_teams[agent_id], agent_id,
                    self.agent_classes[agent_id].__name__)
            elif received_message.msg_type == messages.START:
                width = received_message.map_width
                height = received_message.map_height
                agent_id = received_message.agent_id
                ally_ids = self.get_agent_allies(agent_id)
                enemy_ids = self.get_agent_enemies(agent_id)

                if self.agent_teams[agent_id] == 'pacman':
                    eater = True
                else:
                    eater = False

                if agent_id in self.game_states:
                    del self.game_states[agent_id]

                self.game_states[agent_id] = state.GameState(
                    width,
                    height, [],
                    agent_id=agent_id,
                    ally_ids=ally_ids,
                    enemy_ids=enemy_ids,
                    eater=eater,
                    iteration=self.game_number[agent_id])
                self.send_message(self.create_ack_message())
                print 'Started game #%d  \tID: %d\tClass: %s' % (
                    self.game_number[agent_id], agent_id,
                    self.agent_classes[agent_id].__name__)

                self.game_number[agent_id] += 1
            elif received_message.msg_type == messages.REGISTER:
                self.register_agent(received_message)
                self.send_message(self.create_ack_message())
            elif received_message.msg_type == messages.REQUEST_BEHAVIOR_COUNT:
                self.send_message(
                    self.create_behavior_count_message(
                        received_message.agent_id))
                self.reset_behavior_count(received_message.agent_id)
            elif received_message.msg_type == messages.REQUEST_POLICY:
                self.send_message(
                    self.create_policy_message(received_message.agent_id))
            elif received_message.msg_type == messages.POLICY:
                self.agents[received_message.agent_id].set_policy(
                    received_message.policy)
                self.send_message(self.create_ack_message())