Exemplo n.º 1
0
def check_if_game_over(l1, l2, options, game, rounds):
    if options.single_player_mode:
        return check_if_game_over_single_player(l1, l2, options, game, rounds)
    if l1 > 0 and l2 > 0:
        return False
    if options.display:
        game_interface.curses_init_round(game)
        winner = 0
        if l1 < l2:
            winner = 2
        elif l1 > l2:
            winner = 1
        game_interface.curses_declare_winner(winner)
    else:
        if l1 == l2:
            print 'Tie, remaining life: %d v. %d' % (l1, l2)
        elif l1 < l2:
            print 'Player 2 wins: %d v. %d' % (l1, l2)
        else:
            print 'Player 1 wins: %d v. %d' % (l1, l2)
    # Wait for input
    if options.display:
        sys.stdin.read(1)
        game_interface.curses_close()
    return True
Exemplo n.º 2
0
def check_if_game_over(l1, l2, options, game, rounds):
    if options.single_player_mode:
        return check_if_game_over_single_player(l1, l2, options, game, rounds)
    if l1 > 0 and l2 > 0:
        return False
    if options.display:
        game_interface.curses_init_round(game)
        winner = 0
        if l1 < l2:
            winner = 2
        elif l1 > l2:
            winner = 1
        game_interface.curses_declare_winner(winner)
    else:
        if l1 == l2:
            print "Tie, remaining life: %d v. %d" % (l1, l2)
        elif l1 < l2:
            print "Player 2 wins: %d v. %d" % (l1, l2)
        else:
            print "Player 1 wins: %d v. %d" % (l1, l2)
    # Wait for input
    if options.display:
        sys.stdin.read(1)
        game_interface.curses_close()
    return True
Exemplo n.º 3
0
def run(options):
  game = game_interface.GameInterface(options.plant_bonus,
                                      options.plant_penalty,
                                      options.observation_cost,
                                      options.starting_life,
                                      options.life_per_turn)
  player1_view = game.GetPlayer1View()
  player2_view = game.GetPlayer2View()

  if options.display:
    if game_interface.curses_init() < 0:
      return
    game_interface.curses_draw_board(game)
  
  # Keep running until one player runs out of life.
  x = 10000
  while x>0:
    (mv1, eat1) = get_move(player1_view, player1.mover.get_move, options, 1)
    (mv2, eat2) = get_move(player2_view, player2.mover.get_move, options, 2)

    game.ExecuteMoves(mv1, eat1, mv2, eat2)
    if options.display:
      game_interface.curses_draw_board(game)
      game_interface.curses_init_round(game)
        #else:
        #print mv1, eat1, mv2, eat2
      #print player1_view.GetLife(), player2_view.GetLife()
    # Check whether someone's life is negative.
      #l1 = player1_view.GetLife()
      #l2 = player2_view.GetLife()
    x -= 1
  return (player1.mover.move_hist, player2.mover.move_hist)
Exemplo n.º 4
0
def run(options):
  game = game_interface.GameInterface(options.plant_bonus,
                                      options.plant_penalty,
                                      options.observation_cost,
                                      options.starting_life,
                                      options.life_per_turn)
  player1_view = game.GetPlayer1View()
  player2_view = game.GetPlayer2View()

  if options.display:
    if game_interface.curses_init() < 0:
      return
    game_interface.curses_draw_board(game)
  
  # Keep running until one player runs out of life.
  while True:
    (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1)
    (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2)

    game.ExecuteMoves(mv1, eat1, mv2, eat2)
    if options.display:
      game_interface.curses_draw_board(game)
      game_interface.curses_init_round(game)
    #else:
      #print mv1, eat1, mv2, eat2
      #print player1_view.GetLife(), player2_view.GetLife()
    # Check whether someone's life is negative.
    l1 = player1_view.GetLife()
    l2 = player2_view.GetLife()
  
    if l1 <= 0 or l2 <= 0:
      if options.display:
        winner = 0
        if l1 < l2:
          winner = 2
        else:
          winner = 1
        game_interface.curses_declare_winner(winner)
      else:
        with open('score.txt', 'r') as file:
          wins, losses = map(int, file.readline().split()[:2])

        if l1 == l2:
          print 'Tie, remaining life: %d v. %d' % (l1, l2)
        elif l1 < l2:
          print 'Player 2 wins: %d v. %d' % (l1, l2)
          wins += 1
        else:
          print 'Player 1 wins: %d v. %d' % (l1, l2)
          losses += 1

        with open('score.txt', 'w') as file:
          file.write(str(wins) + ' ' + str(losses) + ' ' + str(1.0 * wins / (wins + losses)) + '\n')
      # Wait for input
      #sys.stdin.read(1)
      if options.display:
        game_interface.curses_close()
      break
Exemplo n.º 5
0
Arquivo: rg.py Projeto: mxw/cs181
def run(options):
  game = game_interface.GameInterface(options.plant_bonus,
                                      options.plant_penalty,
                                      options.observation_cost,
                                      options.starting_life,
                                      options.life_per_turn)
  player1_view = game.GetPlayer1View()
  player2_view = game.GetPlayer2View()

  if options.display:
    if game_interface.curses_init() < 0:
      return
    game_interface.curses_draw_board(game)
  
  # Keep running until one player runs out of life.
  while True:
    (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1)
    (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2)
    # time.sleep(0.1)

    game.ExecuteMoves(mv1, eat1, mv2, eat2)
    if options.display:
      game_interface.curses_draw_board(game)
      game_interface.curses_init_round(game)
    else:
      pass
      print mv1, eat1, mv2, eat2
      print 'Life:', player1_view.GetLife(), player2_view.GetLife()
    # Check whether someone's life is negative.
    l1 = player1_view.GetLife()
    l2 = player2_view.GetLife()
  
    if l1 <= 0 or l2 <= 0:
      player1.player.print_board((player1_view.GetXPos(), player1_view.GetYPos()))
      if options.display:
        winner = 0
        if l1 < l2:
          winner = 2
        else:
          winner = 1
        game_interface.curses_declare_winner(winner)
      else:
        if l1 == l2:
          print 'Tie, remaining life: %d v. %d' % (l1, l2)
        elif l1 < l2:
          print 'Player 2 wins: %d v. %d' % (l1, l2)
        else:
          print 'Player 1 wins: %d v. %d' % (l1, l2)
      # Wait for input
      
      if options.display:
        sys.stdin.read(1)
        game_interface.curses_close()

      
      break
Exemplo n.º 6
0
def run(options):
    game = game_interface.GameInterface(options.plant_bonus,
                                        options.plant_penalty,
                                        options.observation_cost,
                                        options.starting_life,
                                        options.life_per_turn)
    player1_view = game.GetPlayer1View()
    player2_view = game.GetPlayer2View()

    if options.display:
        if game_interface.curses_init() < 0:
            return
        game_interface.curses_draw_board(game)

    # Keep running until one player runs out of life.
    while True:
        (mv1, eat1) = get_move(player1_view, player.player.get_move, options,
                               1)
        (mv2, eat2) = get_move(player2_view, player2.player.get_move, options,
                               2)

        # hack to break early
        if mv1 == None:
            break

        game.ExecuteMoves(mv1, eat1, mv2, eat2)
        if options.display:
            game_interface.curses_draw_board(game)
            game_interface.curses_init_round(game)
        else:
            print mv1, eat1, mv2, eat2
            print player1_view.GetLife(), player2_view.GetLife()
        # Check whether someone's life is negative.
        l1 = player1_view.GetLife()
        l2 = player2_view.GetLife()

        if l1 <= 0 or l2 <= 0:
            if options.display:
                winner = 0
                if l1 < l2:
                    winner = 2
                else:
                    winner = 1
                game_interface.curses_declare_winner(winner)
            else:
                if l1 == l2:
                    print 'Tie, remaining life: %d v. %d' % (l1, l2)
                elif l1 < l2:
                    print 'Player 2 wins: %d v. %d' % (l1, l2)
                else:
                    print 'Player 1 wins: %d v. %d' % (l1, l2)
            # Wait for input
            # sys.stdin.read(1)
            if options.display:
                game_interface.curses_close()
            break
Exemplo n.º 7
0
def run(options):
    game = game_interface.GameInterface(
        options.plant_bonus,
        options.plant_penalty,
        options.observation_cost,
        options.starting_life,
        options.life_per_turn,
    )
    # Give the players a chance to customize according to the parameters of the
    # game.
    player1.player.init_point_settings(
        options.plant_bonus,
        options.plant_penalty,
        options.observation_cost,
        options.starting_life,
        options.life_per_turn,
    )
    player2.player.init_point_settings(
        options.plant_bonus,
        options.plant_penalty,
        options.observation_cost,
        options.starting_life,
        options.life_per_turn,
    )
    player1_view = game.GetPlayer1View()
    player2_view = game.GetPlayer2View()

    if options.display:
        if game_interface.curses_init() < 0:
            return
        game_interface.curses_draw_board(game)

    # Keep running until one player runs out of life.
    while True:
        (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1)
        (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2)
        # Players might have exhausted all of their energy eating.
        l1 = player1_view.GetLife()
        l2 = player2_view.GetLife()
        if check_if_game_over(l1, l2, options, game, player1_view.GetRound()):
            break

        game.ExecuteMoves(mv1, eat1, mv2, eat2)
        if options.display:
            if player1_view.GetRound() % DISP == 0:
                game_interface.curses_draw_board(game)
                game_interface.curses_init_round(game)
        else:
            pass
            # print mv1, eat1, mv2, eat2
            # print player1_view.GetLife(), player2_view.GetLife()

        if check_if_game_over(l1, l2, options, game, player1_view.GetRound()):
            break
Exemplo n.º 8
0
def run(options):
    game = game_interface.GameInterface(options.plant_bonus,
                                        options.plant_penalty,
                                        options.observation_cost,
                                        options.starting_life,
                                        options.life_per_turn)
    # Give the players a chance to customize according to the parameters of the
    # game.
    player1.player.init_point_settings(options.plant_bonus,
                                       options.plant_penalty,
                                       options.observation_cost,
                                       options.starting_life,
                                       options.life_per_turn)
    player2.player.init_point_settings(options.plant_bonus,
                                       options.plant_penalty,
                                       options.observation_cost,
                                       options.starting_life,
                                       options.life_per_turn)
    player1_view = game.GetPlayer1View()
    player2_view = game.GetPlayer2View()

    if options.display:
        if game_interface.curses_init() < 0:
            return
        game_interface.curses_draw_board(game)

    # Keep running until one player runs out of life.
    while True:
        (mv1, eat1) = get_move(player1_view, player1.player.get_move, options,
                               1)
        (mv2, eat2) = get_move(player2_view, player2.player.get_move, options,
                               2)
        # Players might have exhausted all of their energy eating.
        l1 = player1_view.GetLife()
        l2 = player2_view.GetLife()
        if check_if_game_over(l1, l2, options, game, player1_view.GetRound()):
            break

        game.ExecuteMoves(mv1, eat1, mv2, eat2)
        if options.display:
            if player1_view.GetRound() % DISP == 0:
                game_interface.curses_draw_board(game)
                game_interface.curses_init_round(game)
        else:
            pass
            # print mv1, eat1, mv2, eat2
            # print player1_view.GetLife(), player2_view.GetLife()

        if check_if_game_over(l1, l2, options, game, player1_view.GetRound()):
            break
Exemplo n.º 9
0
def check_if_game_over_single_player(l1, l2, options, game, rounds):
  if options.display:
    game_interface.curses_init_round(game)
  if l1 <= 0:
    debug_str = 'Single player mode: lasted %d rounds' % rounds
    # Need to end the game
    if options.display:
      game_interface.curses_debug(1, debug_str)
    else:
      print debug_str
    sys.stdin.read(1)
    if options.display:
      game_interface.curses_close()
    return True
  return False
Exemplo n.º 10
0
def run(options):
  game = game_interface.GameInterface(20,10,1, 100,1)
  """
  game = game_interface.GameInterface(options.plant_bonus,
                                      options.plant_penalty,
                                      options.observation_cost,
                                      options.starting_life,
                                      options.life_per_turn)
  if options.display:
    if game_interface.curses_init() < 0:
      return
    game_interface.curses_draw_board(game)"""
  player1_view = game.GetPlayer1View()
  player2_view = game.GetPlayer2View()


  # Keep running until one player runs out of life.
  roundNum = 0
  while True:
    (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1)
    (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2)

    old_life1 = player1_view.GetLife()
    old_life2 = player2_view.GetLife()
    game.ExecuteMoves(mv1, eat1, mv2, eat2)
    
    old_xpos1 = player1_view.GetXPos()
    old_xpos2 = player2_view.GetXPos()
    old_ypos1 = player1_view.GetYPos()
    old_ypos2 = player2_view.GetYPos()

    if False: #OPTIONS.DISPLAY
      game_interface.curses_draw_board(game)
      game_interface.curses_init_round(game)
      
    else:
      #print mv1, eat1, mv2, eat2
      #print player1_view.GetLife(), player2_view.GetLife()
      #print "Location1: (" + str(player1_view.GetXPos()) + ", " + str(player1_view.GetYPos()) + ")\n"
      #print "Location2: (" + str(player2_view.GetXPos()) + ", " + str(player2_view.GetYPos()) + ")\n"
      #print "RoundNum: " + str(player1_view.GetRound()) + " Coords: (" + str(player1_view.GetXPos()) + ", " + str(player1_view.GetYPos()) + ")"
      # if eat1:
      #   if player1_view.GetLife() - old_life1 > 0:
      #     #print "Location1: (" + str(player1_view.GetXPos()) + ", " + str(player1_view.GetYPos()) + "): Nutritious" + "\n"
      #     print "1 " + str(player1_view.GetXPos()) + " " + str(player1_view.GetYPos()) + " N" + "\n"
      #   else: 
      #     #print "Location1: (" + str(player1_view.GetXPos()) + ", " + str(player1_view.GetYPos()) + "): Poisonous" + "\n"
      #     print "1 " + str(player1_view.GetXPos()) + " " + str(player1_view.GetYPos()) + " P" + "\n"
      
      # if eat2:
      #   if player2_view.GetLife() - old_life2 > 0:
      #     #print "Location2: (" + str(player2_view.GetXPos()) + ", " + str(player2_view.GetYPos()) + "): Nutritious" + "\n"
      #     print "2 " + str(player2_view.GetXPos()) + " " + str(player2_view.GetYPos()) + " N" + "\n"
        
      #   else: 
      #     #print "Location2: (" + str(player2_view.GetXPos()) + ", " + str(player2_view.GetYPos()) + "): Poisonous" + "\n"
      #     print "2 " + str(player2_view.GetXPos()) + " " + str(player2_view.GetYPos()) + " P" + "\n"
      
      if eat1:
        if player1_view.GetLife() - old_life1 > 0:
          #print "Location1: (" + str(player1_view.GetXPos()) + ", " + str(player1_view.GetYPos()) + "): Nutritious" + "\n"
          print "N  1 " + str(old_xpos1) + " " + str(old_ypos1) + " N" + "\n"
        else: 
          #print "Location1: (" + str(player1_view.GetXPos()) + ", " + str(player1_view.GetYPos()) + "): Poisonous" + "\n"
          print "P  1 " + str(old_xpos1) + " " + str(old_ypos1) + " P" + "\n"
      #else: 
      #  print "1 " + str(old_xpos1) + " " + str(old_ypos1) + "\n"
      if eat2:
        if player2_view.GetLife() - old_life2 > 0:
          #print "Location2: (" + str(player2_view.GetXPos()) + ", " + str(player2_view.GetYPos()) + "): Nutritious" + "\n"
          print "N  2 " + str(old_xpos2) + " " + str(old_ypos2) + " N" + "\n"
        else: 
          #print "Location2: (" + str(player2_view.GetXPos()) + ", " + str(player2_view.GetYPos()) + "): Poisonous" + "\n"
          print "P  2 " + str(old_xpos2) + " " + str(old_ypos2) + " P" + "\n"
      #else: 
      #  print "2 " + str(old_xpos2) + " " + str(old_ypos2) + "\n"


    # Check whether someone's life is negative.
    l1 = player1_view.GetLife()
    l2 = player2_view.GetLife()

    if l1 <= 0 or l2 <= 0:
      if False: # OPTIONS.DISPLAY
        winner = 0
        if l1 < l2:
          winner = 2
        else:
          winner = 1
        game_interface.curses_declare_winner(winner)
      else:
        if l1 == l2:
          print 'Tie, remaining life: %d v. %d' % (l1, l2)
          return 2
        elif l1 < l2:
          print 'Player 2 wins: %d v. %d' % (l1, l2)
          return 0
        else:
          print 'Player 1 wins: %d v. %d' % (l1, l2)
          return 1
      # Wait for input
      sys.stdin.read(1)
      if options.display:
        game_interface.curses_close()
      break
Exemplo n.º 11
0
def run(options):
  game = game_interface.GameInterface(options.plant_bonus,
                                      options.plant_penalty,
                                      options.observation_cost,
                                      options.starting_life,
                                      options.life_per_turn)
  player1_view = game.GetPlayer1View()
  player2_view = game.GetPlayer2View()

  if options.display:
    if game_interface.curses_init() < 0:
      return
    game_interface.curses_draw_board(game)

  d = {}
  img_file = open('image_map_startlife=100.txt', 'r')
  img = pickle.load(img_file)
  img_file.close()
  
  # Keep running until one player runs out of life.
  while True:
    (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1)
    (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2)

# ---------------------------------
# Experiment Code

    # Get the coordinates of each run
    player1_coords = (player1_view.GetXPos(), player1_view.GetYPos())
    player2_coords = (player2_view.GetXPos(), player2_view.GetYPos()) 

    
    player1_status = player1_view.GetPlantInfo()
    player2_status = player2_view.GetPlantInfo()
    # d[player1_coords] = player1_status
    # d[player2_coords] = player2_status

    if (player1_status == game_interface.STATUS_NUTRITIOUS_PLANT) | (player1_status == game_interface.STATUS_POISONOUS_PLANT):
      img[player1_view.GetImage()] = player1_status
    if (player2_status == game_interface.STATUS_NUTRITIOUS_PLANT) | (player2_status == game_interface.STATUS_POISONOUS_PLANT):
      img[player2_view.GetImage()] = player2_status


# ----------------------------------

    game.ExecuteMoves(mv1, eat1, mv2, eat2)
    if options.display:
      game_interface.curses_draw_board(game)
      game_interface.curses_init_round(game)
    else:
      print mv1, eat1, mv2, eat2
      print player1_view.GetLife(), player2_view.GetLife()
    # Check whether someone's life is negative.
    l1 = player1_view.GetLife()
    l2 = player2_view.GetLife()
  
    if l1 <= 0 or l2 <= 0:
      if options.display:
        winner = 0
        if l1 < l2:
          winner = 2
        else:
          winner = 1
        game_interface.curses_declare_winner(winner)
      else:
        if l1 == l2:
          print 'Tie, remaining life: %d v. %d' % (l1, l2)
        elif l1 < l2:
          print 'Player 2 wins: %d v. %d' % (l1, l2)
        else:
          print 'Player 1 wins: %d v. %d' % (l1, l2)
      # Wait for input
      #sys.stdin.read(1)
      if options.display:
        game_interface.curses_close()
      break

  # distri_file = open('plant_distribution_startlife=100.txt', 'w')
  image_map_file = open('image_map_startlife=100.txt', 'w')
  #print img
  # print d
  # pickle.dump(d, distri_file)
  pickle.dump(img, image_map_file)
  # distri_file.close()

  image_map_file.close()
Exemplo n.º 12
0
def run(options):
  game = game_interface.GameInterface(options.plant_bonus,
                                      options.plant_penalty,
                                      options.observation_cost,
                                      options.starting_life,
                                      options.life_per_turn)
  player1_view = game.GetPlayer1View()
  player2_view = game.GetPlayer2View()

  if options.display:
    if game_interface.curses_init() < 0:
      return
    game_interface.curses_draw_board(game)
  

  nlocs1 = [[0 for x in range(100)] for x in range(100)]
  #plocs1 = [[0 for x in range(50)] for x in range(50)]
  plocs2 = [[0 for x in range(100)] for x in range(100)]
  #plocs2 = [[0 for x in range(50)] for x in range(50)]
  # Keep running until one player runs out of life.
  fp = open('locs1.txt', 'a')
  fn = open('locs2.txt', 'a')
  f0 = open('locationsvisited.txt', 'a')
  count = 0
  while count < 5000:
    (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1)
    (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2)

    loc1x = player1_view.GetXPos()
    loc1y = player1_view.GetYPos()
    loc2x = player2_view.GetXPos()
    loc2y = player2_view.GetYPos()
    
    o1 = player1_view.GetLife()
    o2 = player2_view.GetLife()
    game.ExecuteMoves(mv1, eat1, mv2, eat2)
    if options.display:
      game_interface.curses_draw_board(game)
      game_interface.curses_init_round(game)
    else:
      print mv1, eat1, mv2, eat2
      print player1_view.GetLife(), player2_view.GetLife()
    # Check whether someone's life is negative.
    l1 = player1_view.GetLife()
    l2 = player2_view.GetLife()

    #image1 = player1.player.image1
    #image2 = player2.player.image2

    # print image1
    # print image2

    if eat1:
      if l1 == o1 + 19:
        locs1[loc1x+50][loc1y+50] += 1
        #f0.write('n')
        #f0.write(str(loc1x)+str(loc1y))  
      else:
        locs1[loc1x+50][loc1y+50] -= 1
        #f0.write('p')
        #f0.write(str(loc1x) + str(loc1y)) 
    if eat2:
      if l2 == o2 + 19:
        locs2[loc2x+50][loc2y+50] += 1
        #f0.write('n')
        #f0.write(str(loc2x) + str(loc2y)) 
      else:
        locs2[loc2x+50][loc2y+50] -= 1
        #f0.write('p')
        #f0.write(str(loc2x) + str(loc2y)) 
    
    count += 1
    
  fn.write(str(locs1))
  fp.write(str(locs2))
Exemplo n.º 13
0
def run(options):
  game = game_interface.GameInterface(options.plant_bonus,
                                      options.plant_penalty,
                                      options.observation_cost,
                                      options.starting_life,
                                      options.life_per_turn)
  player1_view = game.GetPlayer1View()
  player2_view = game.GetPlayer2View()
  
  # '''BEGIN NEURAL NETWORK ADDITIONS'''
  # # Load in the training data.
  # images = DataReader.GetImages('nutritious_test.txt', -1)
  # #print images0
  # images1 = DataReader.GetImages('poisnous_test.txt', -1)
  # # images.extend(images1)
  # #print 'training', len(images)
  # images=images[:500]+images1[:500]

  # # Load the validation set.
  # validation = DataReader.GetImages('nutritious_valid.txt', -1)
  # validation2 = DataReader.GetImages('poisnous_valid.txt', -1) 
  # # validation.extend(validation2)
  # validation=validation[:500]+validation[:500]
  # #print 'validation', len(validation)

  # # Load the test data.
  # test = DataReader.GetImages('nutritious.txt', -1)
  # test2 = DataReader.GetImages('poisnous.txt', -1)
  # # test.extend(test2)
  # test=test[:500]+test2[:500]
  # #print 'test', len(test)

  # # Initializing network
  # rate = .1
  # epochs = 10
  # network = SimpleNetwork() 

  # # Hooks user-implemented functions to network
  # network.FeedForwardFn = FeedForward
  # network.TrainFn = Train

  # # Initialize network weights
  # network.InitializeWeights()
  

  # '''# Displays information
  # print '* * * * * * * * *'
  # print 'Parameters => Epochs: %d, Learning Rate: %f' % (epochs, rate)
  # print 'Type of network used: %s' % network.__class__.__name__
  # print ('Input Nodes: %d, Hidden Nodes: %d, Output Nodes: %d' %
  #        (len(network.network.inputs), len(network.network.hidden_nodes),
  #         len(network.network.outputs)))
  # print '* * * * * * * * *'''
 
  # # Train the network.
  # network.Train(images, validation, test, rate, epochs)
  # print 'length', len(network.network.weights)
  # # for i in network.network.weights:
  # #   print i.value
    
  # '''END OF NEURAL NETWORK ADDITIONS'''

  if options.display:
    if game_interface.curses_init() < 0:
      return
    game_interface.curses_draw_board(game)
  
  # Keep running until one player runs out of life.
  count = 0
  fp = open('poisnous.txt', 'a')
  fn = open('nutritious.txt', 'a')
  while count < 5000:
    (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1, network)
    (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2, network)

    o1 = player1_view.GetLife()
    o2 = player2_view.GetLife()
    game.ExecuteMoves(mv1, eat1, mv2, eat2)
    if options.display:
      game_interface.curses_draw_board(game)
      game_interface.curses_init_round(game)
    else:
      print mv1, eat1, mv2, eat2
      print player1_view.GetLife(), player2_view.GetLife()
    # Check whether someone's life is negative.
    l1 = player1_view.GetLife()
    l2 = player2_view.GetLife()

    image1 = player1.player.image1
    image2 = player2.player.image2

    # print image1
    # print image2

    if image1 != []:
      print image1
      if l1 == o1 + 19:
        for image in image1:
          fn.write(str(image) + '\n')
        fn.write('\n')
      else:
        for image in image1:
          fp.write(str(image) + '\n')
        fp.write('\n')
    if image2 != []:
      print image2
      if l2 == o2 + 19:
        for image in image2:
          fn.write(str(image) + '\n')
        fn.write('\n')
      else:
        for image in image2:
          fp.write(str(image) + '\n')
        fp.write('\n')


    # if l1 <= 0 or l2 <= 0:
    #   if options.display:
    #     winner = 0
    #     if l1 < l2:
    #       winner = 2
    #     else:
    #       winner = 1
    #     game_interface.curses_declare_winner(winner)
    #   else:
    #     if l1 == l2:
    #       print 'Tie, remaining life: %d v. %d' % (l1, l2)
    #     elif l1 < l2:
    #       print 'Player 2 wins: %d v. %d' % (l1, l2)
    #     else:
    #       print 'Player 1 wins: %d v. %d' % (l1, l2)
    #   # Wait for input
    #   sys.stdin.read(1)
    #   if options.display:
    #     game_interface.curses_close()
    #   break
    count += 1
Exemplo n.º 14
0
def run(options):
  game = game_interface.GameInterface(options.plant_bonus,
                                      options.plant_penalty,
                                      options.observation_cost,
                                      options.starting_life,
                                      options.life_per_turn)
  player1_view = game.GetPlayer1View()
  player2_view = game.GetPlayer2View()

  if options.display:
    if game_interface.curses_init() < 0:
      return
    game_interface.curses_draw_board(game)
  
  # Keep running until one player runs out of life.
  while True:
    (mv1, eat1) = get_move(player1_view, player1.player.get_move, options, 1)
    if options.train != 0:
      (mv2, eat2) = get_move(player2_view, player2.player.get_move, options, 2)
    else:
      (mv2, eat2) = get_move(player2_view, common.get_move, options, 2)
    game.ExecuteMoves(mv1, eat1, mv2, eat2)

    # No longer new game
    options.new_game = False

    if options.display == 1:
      game_interface.curses_draw_board(game)
      game_interface.curses_init_round(game)
    elif options.display == 2:
      print mv1, eat1, mv2, eat2
      print player1_view.GetLife(), player2_view.GetLife()
    # Check whether someone's life is negative.
    l1 = player1_view.GetLife()
    l2 = player2_view.GetLife()
  
    if l1 <= 0 or l2 <= 0:
      # # Export Q
      # if options.q_out != None:
      #   # print "Exporting Q to", options.q_out
      #   f = open(options.q_out, "w")
      #   cPickle.dump(player1.player.Q, f)
      #   f.close()
      #   f = open(options.t_out, "w")
      #   cPickle.dump(player1.player.t_count, f)
      #   f.close()
      # # Export neural network weights
      # if options.train == 1:
      #   # print "Exporting weights to", options.out_file
      #   player2.player.network.ExportWeights(options.out_file)
      # elif options.train == 2:
      #   print player2.player.correct / player2.player.instances
      # elif options.train == 3:
      #   # print "Exporting plants to", options.out_file
      #   player2.player.ExportPlants(options.out_file)
      if options.display:
        winner = 0
        if l1 < l2:
          winner = 2
        else:
          winner = 1
        game_interface.curses_declare_winner(winner)
      else:
        if l1 == l2:
          print 'Tie, remaining life: %d v. %d' % (l1, l2)
        elif l1 < l2:
          print 'Player 2 wins: %d v. %d' % (l1, l2)
        else:
          print 'Player 1 wins: %d v. %d' % (l1, l2)
      if options.display:
        # Wait for input
        sys.stdin.read(1)
        game_interface.curses_close()
      break