python - Breaking and resuming in the middle of a function? -
i working on final programming project , attempting create battleship. thing have yet program automatic intelligence. way works far guessing 2 random integers 1 11 pick random space on grid. once hits something, have rules created check on sides until has sunk entire ship. issue this, computer take 8 turns @ time. need somehow jump out of function, main run function, resume left off next turn. there way this, or there better , entirely different way take turns?
this turns function looks like. user_guess function works fine, checks see if have won game. thinking() purely aesthetic, comp_guess function have questions about, checks see if have lost. recalls itself.
def turns(): user_guess() if win_game==true: return ('you win!') thinking() comp_guess() if lose_game==true: return ('you lose!') turns()
then far, have comp_guess function is:
def comp_guess(): comp_in1=random.randrange(1,11) comp_in2=random.randrange(1,11) if (grid_two[comp_in1])[comp_in2]=='s': (grid_two[comp_in1])[comp_in2]='x' (grid_four[comp_in1])[comp_in2]='x' else: (grid_two[comp_in1])[comp_in2]='*' (grid_four[comp_in1])[comp_in2]='*' f in grid_four: print(' '.join(f))
grid_two stored grid of ship locations. grid_four 1 gets displayed , keeps track of computers moves , whether hit or misses.
you need loop random int until finds slot not taken. set if statements computer can tell if has hit ship or not. after should go main function.
i think reason computer taking more 1 turn because looping until kills ship? main loop swaps player goes, should have condition ends game.
Comments
Post a Comment