def play_game(): """ Plays a full game of battleship against an opponent """ board = copy_of(BOARD) comm = util.Communication() initstring = comm.readline() turn, opponent = initstring.split(",") opponent = opponent.strip() # Generate and send my board if opponent in NON_HUMAN_OPPONENTS: genboard = ascii_board else: genboard = generate_playing_board(1.95) for line in genboard.splitlines(): comm.sendline(line) if turn == "0": myturn = True else: myturn = False guesses = set() while True: try: if myturn: # Send a guess guess = fire(board) guessx, guessy = guess guesses.add(guess) comm.sendline("{},{}".format(guessx, guessy)) # Read what happened data = comm.readline().strip() board = update_board(board, guessx, guessy, data) myturn = False else: # Read opponent's guess data = comm.readline() myturn = True except socket.error: # Game is over, we either won or lost print "Game Finished"
if in targetting mode, just pop a random target off the queue and guess it """ import logging import util import os import heapq import socket import random # set up simple logging to a file logging.basicConfig(filename="logs/{}.log".format(os.path.basename(__file__)), level=logging.DEBUG) comm = util.Communication() # first we recieve an init string logging.debug("Recieve the init string...") initstring = comm.readline() turn, opponent = initstring.split(",") if turn == "0": myturn = True else: myturn = False logging.debug("initstring: %s", initstring) board = util.gen_random_board_str() for line in board.splitlines(): comm.sendline(line)