I’ve been doing a lot of computer programming lately in Python (http://python.org/), I’m trying to teach the skill to myself. So far I’ve done at the very least a little bit of computer programming every day for eight days.

The main resource I’ve been using to learn is Codecademy (http://www.codecademy.com), while it’s not perfect (sometimes it passes you on an exercise even though your solution didn’t work) I find that its score and streak system keeps me motivated and programming everyday.

Here is a Battleship game I am currently writing in Python. While I do have a little bit of prior experience this is mostly stuff I learned this past week. While it still needs some work to be considered complete (mostly input validation) it already works really well.

#####
# Title: Battleship
# Author: slimnexus
# Date: 10/20/2012
#
# Description: A single player game of battleship.
# The goal is to sink the computers
# battleship.
###

# Import statements
import random as rand

# Constant definitions
GRID_SIZE = 5 # The size of the game board, 5 means 5×5
NUM_OF_BATTLESHIPS = 5 # The number of battleships the computer has, this is not yet implemented

# Function definitions
# Creates a game board of size board_size * board_size and fills it with O’s
def create_board(board_size):
board = []
for i in range(board_size):
board.append([‘O’]*board_size)
return board

# Prints the game board in a way that is more visually pleasing
def print_board(gameboard):
board = gameboard
for row in board:
print ‘ ‘.join(row)

# Designates coordinates for enemy ships
def enemy_gen(num, gameboard):
coordinates = []
x = 0
y = 0
while len(coordinates) GRID_SIZE*GRID_SIZE:
print “You are trying to create more battleships than can fit on the game board.”
running = False
enemys = enemy_gen(NUM_OF_BATTLESHIPS ,board)
num_of_enemys = NUM_OF_BATTLESHIPS

# Main loop
while running:
print “\n”*100
turn += 1
print “Turn: ” + str(turn)
print_board(board)
print “Enter the coordinates of the location to fire upon”
guess_x = input(“What x coordinate? “)
guess_y = input(“What y coordinate? “)

if [guess_x, guess_y] in enemys:
board[guess_y][guess_x] = ‘#’
print_board(board)
print “Enemy ship destroyed!”
num_of_enemys -=1
enemys.remove([guess_x,guess_y])
raw_input(“Press enter to continue”)

elif (((guess_x len(board)-1)) or ((guess_y len(board)-1)) ):
print “Those coordinates are out of range”
turn -= 1 # This is done so that this event doesn’t count as a turn
raw_input(“Press enter to continue”)

elif (board[guess_y][guess_x] == ‘X’) or (board[guess_y][guess_x] == ‘#’):
print “You have already fired at those coordinates”
turn -= 1 # This is done so that this event doesn’t count as a turn
raw_input(“Press enter to continue”)

else:
print “You missed”
board[guess_y][guess_x] = ‘X’
raw_input(“Press enter to continue”)

if num_of_enemys == 0:
print “You sunk all of the enemys battleships!”
print “You win!”
running = False
print ”

It seems that WordPress doesn’t allow indentation. Some parts of this code (conditionals and loops) are supposed to be indented.