Visit cambolc.co.uk to learn more!
Twitter: @cambolc
Blog written on a Raspberry Pi!

A little bit of Hit and Miss!


A mile-stone has been reached with Codecademy.  A "real" program has been written.


In a previous blog I started documenting my attempts to tame the programming language Python using the online  Codecademy course.  A whole lot of persistence and about 35 hours and I have reached the stage where I understand enough of Python to write my own text based game.  Admittedly there has been a lot scratching of head during the 3 hours.  The course is only 60% completed so a little way to go mastering the basics fully.  Of course this is the basics. 

The course is introducing a methodology of breaking down  the problem into steps.  Having programmed ZX81s in my distant youth this is not a new concept, but is a muscle that is starting to become stronger agin with practice.  A bit like Fencing (of which I was a coach many years ago) where you it takes weeks to learn the basics before you can put them  into something which vaguely looks like proficiency.  However, each new technique when viewed as a still frame from a video gives the impression of a d'artagnan moment.  (Small aside, just looked up d'artagnan on Wikipedia, and apparently there was a real person of that name who was a Musketeer).  So with Codecademy acting as the Mongoose, I have subdued the Python, a little.

Batlleships is a classic computer game.  It is understood by most people (probably of a certain age now) and can be played easily with pen and paper.  The drawing of a grid requires a knowledge of the rules of how to draw the grid and populate them with battleships.  This is quite a complex operation involving the ability to place items in a 2D grid.    The questioning and answering that occurs when people play this game are like the if statements found in programming languages. 

So the first program!

from random import randint

board = []

for x in range(5):
    board.append(["O"] * 5)

def print_board(board):
    for row in board:
        print " ".join(row)

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

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)
print ship_row
print ship_col


for i in range(4):
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    turn = i
    if turn > 4:
        print "Game Over"
        break
    else:
        if guess_row == ship_row and guess_col == ship_col:
            print "Congratulations! You sunk my battleship!"
            break
        else:
            if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
                print "Oops, that's not even in the ocean."
            elif(board[guess_row][guess_col] == "X"):
                print "You guessed that one already."
            else:
                print "You missed my battleship!"
            board[guess_row][guess_col] = "X"
        print (turn + 1) 
        print_board(board)

The above does work and passes the Codecademy checker.  The code is as but,  there are a couple of bugs such as out of range inputs for column and row causing an error that was not addressed through the Codecademy checker.  The code does need the correct indenting.  Now here comes the CAMBOLC bit.  To obtain a copy of the Battleship program visit https://sites.google.com/a/cambolc.co.uk/cambridge-online-learning-community/tasks/a-little-bit-of-raspberry-pi/mongoose-tales-and-python where the files are available for download.  The instructions on how to use the files can be seen on the webpage.

The principles involved in making other text print out  based games are similar to that of the above.  Two similar games that involve grids are Noughts and Crosses (ot Tic Tac Toe for the American speakers) and Connect 4.  I will be having a go at these at another time! Or better still if you have some coding your self why not shrae via the Google + Group of the Cambridge Online Learning Community!


0

Add a comment

    Loading