My Second Python Program – BattleShip Game

programming

Started Programming less than two weeks ago through Codecademy.com,made my first Python program, A PygLatin Translator about a week ago and just yesterday completed a small simple game called BattleShip, Thanks to Codecademy.com

Here is the source code for the game that I completed yesterday as a part of my Python Learning Track on Codecademy.com.

The game is like the famous Minesweeper game in Windows, of course it is not that complicated, rather a simpler version of that game.

import random

board = []

#Adds 5 rows to list board[]
for x in range(0,5):
	board.append(["O"] * 5) #adds 5 'O's to each row in list board[]

def print_board(board):
	for row in board:		#for loop puts the rows in a grid format
		print " ".join(row) #deletes unnecessary formatting of the grid and makes it a clear list of 2 dimensional 'O's

print "Let's play Battleship!"
print_board(board)

def random_row(board):		#radomint function randomly creates a number where the battleship is stored.
  return random.randint(0,len(board)-1)

def random_col(board):
  return random.randint(0,len(board[0])-1) #board[0] represents first row, so board[0] - 1 represents the length of column in first row.

ship_row = random_row(board) #ship_row stores the row number where the battleship is located.
ship_col = random_col(board) #ship_col stores the col number where the battleship is located.

print 'The correct row is: %d' % ship_row
print 'The correct Column is: %d' % ship_col

#Everything from here on should go in your for loop, to ensure turns.
#Be sure to indent!
for turn in range(4): #gives 4 turns to the user to correct the right location of battleship
	guess_row = input("Guess Row:")
	guess_col = input("Guess Col:")

	if guess_row == ship_row and guess_col == ship_col:
		print "Congratulations! You sunk my battleship!\n"
		break	#End of the program if user correctly finds the location of battleship
	else:
		if (turn == 3): #if all turns have passed, game is over, end of program!
			print 'Game Over, you have used your all turns.\n'
			break
		elif (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
			print "Oops, that's not even in the ocean.\n"
		elif(board[guess_row][guess_col] == "X"):
			print "You guessed that one already.\n"
		else:
			print "You missed my battleship!\n"
  			board[guess_row][guess_col] = "X"
	print 'You have used %d turn(s)' % (turn + 1) #turn+1 because user must not get turn 0,1,2 and 3 instead the user must get turn 1,2,3 and 4.
	print_board(board)

print 'The correct row is: %d\n' % ship_row
print 'The correct Column is: %d\n' % ship_col

For many, this might not be a very interesting game, but trust me, from knowing nothing about Python less than 2 weeks ago, to creating a small game all by myself without anyone’s help, sitting in my bedroom, this is a miracle for me and has given me such an immense level of confidence.

I am full set on to go ahead and complete my Python Learning Track, currently I am on while and while / else Loops in Python.

Wish me luck guys, not to be able to code, but to stay seriously motivated for the rest of the learning experience just like I am now.

Advertisements
Aside | This entry was posted in Code Academy, Fun with Python and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

3 Responses to My Second Python Program – BattleShip Game

  1. Laura says:

    Holaaa saludes de casualidad no tienes el juego de batalla naval en java modo grafico saludes

  2. jason says:

    Hey,
    can you please explain
    return random.randint(0,len(board[0])-1) #the way I see it is that len board[0] which is the first entry is [O O O O O] the length is 5. But why is the [0] necessary?
    and
    board[guess_row][guess_col] == “X” # how does it know how to differentiate between row and column? if my guess row is 3 and guess column is 2, then wouldn’t it be board[2][1]? so it will be calling the second list entry and the first list entry. but how does it make it into row and column?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s