From 5798d2ff933daca37dbed69b1abc740c9a06ea84 Mon Sep 17 00:00:00 2001 From: RustyRaptor Date: Sun, 28 May 2017 01:32:33 -0600 Subject: [PATCH] fixed --- .idea/vcs.xml | 6 ++++ battleshyp.py | 89 +++++++++++++++++++++++++++------------------------ 2 files changed, 54 insertions(+), 41 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/battleshyp.py b/battleshyp.py index 900737f..7dad1b1 100644 --- a/battleshyp.py +++ b/battleshyp.py @@ -1,76 +1,83 @@ - from random import randint + board = [] -#prompt player to set difficulty +# prompt player to set difficulty difficulty = input('Set difficulty. Must be 1, 2, or 3:') -#check to make sure difficulty is in the valid range +# check to make sure difficulty is in the valid range if difficulty < 1 or difficulty > 3: - difficulty = input('that number is too big or too small please enter a number 1, 2, or 3') -#check if the entered difficulty is a number and not a string/bool -if isinstance( difficulty , int ) == False: - difficulty = input('that is not a number please use one of the numbers 1, 2, or 3 as your difficulty:') -#here it appends the grid in the form of '0' strings to the array "board" depending on the difficulty. -#I am working on a better way to do this as this is a bit messy and not very modular -elif difficulty == 1 : - for x in range(3): - board.append(['O'] * 3) - a = 3 -elif difficulty == 2: - for x in range(5): - board.append(['0'] * 5) - a = 5 -elif difficulty == 3: - for x in range(7): - board.append(['0'] * 7) - a = 7 + difficulty = input('that number is too big or too small please enter a number 1, 2, or 3') -#this function will let you print the board at any part of the program. +# check if the entered difficulty is a number and not a string/bool +if isinstance(difficulty, int) == False: + difficulty = input('that is not a number please use one of the numbers 1, 2, or 3 as your difficulty:') + +# here it appends the grid in the form of '0' strings to the array "board" depending on the difficulty. +# I am working on a better way to do this as this is a bit messy and not very modular +elif difficulty == 1: + for x in range(3): + board.append(['O'] * 3) + a = 3 +elif difficulty == 2: + for x in range(5): + board.append(['0'] * 5) + a = 5 +elif difficulty == 3: + for x in range(7): + board.append(['0'] * 7) + a = 7 + + +# this function will let you print the board at any part of the program. def print_board(board): for row in board: print ' '.join(row) -print "Let's play Battleship!" + +print 'Let\'s play Battleship!' print_board(board) -#This function will allow us to generate a random coordinate for the ship's location + +# This function will allow us to generate a random coordinate for the ship's location def random_row(board): return randint(0, len(board) - 1) + def random_col(board): return randint(0, len(board[0]) - 1) + ship_row = random_row(board) ship_col = random_col(board) -#prompts the user to guess where the ship it on the grid +# prompts the user to guess where the ship it on the grid for turn in range(9): guess_row = int(raw_input('Guess Row:')) guess_col = int(raw_input('Guess Collumn:')) - #if the player hits it... + # if the player hits it... if guess_row == ship_row and guess_col == ship_col: print 'Target Neutralized' - #we turn the "0" into a "+" when the player hits the ship + # we turn the "0" into a "+" when the player hits the ship board[guess_row][guess_col] = '+' break else: - #if the player picks a location off the grid... - if (guess_row < 0 or guess_row > a) or (guess_col < 0 or guess_col > a): + # if the player picks a location off the grid... + if (guess_row < 0 or guess_row >= a) or (guess_col < 0 or guess_col >= a): print 'Coordinates undefined' - #if the player picks a location that's already been picked - elif(board[guess_row][guess_col] == 'X'): - print 'coordinates already struck' - print_board(board) - #if the player misses the ship + # if the player picks a location that's already been picked + elif (board[guess_row][guess_col] == 'X'): + print 'coordinates already struck' + print_board(board) + # if the player misses the ship else: - print "Target Missed" - #we turn the "0" into an "X" when it is hit and misses - board[guess_row][guess_col] = "X" - #This is the turn system. - print "Turn", turn + 1 + print 'Target Missed' + # we turn the "0" into an "X" when it is hit and misses + board[guess_row][guess_col] = 'X' + # This is the turn system. + print 'Turn', turn + 1 if turn == 8: - print "Defeat" - print "the target location was", str(ship_row), " , ", str(ship_col) + print 'Defeat' + print 'the target location was', str(ship_row), ' , ', str(ship_col) print_board(board)