User Tools

Site Tools


pybog

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

pybog [2023/11/10 04:16] – created appledogpybog [2023/11/10 04:28] (current) appledog
Line 71: Line 71:
 == bog.py == bog.py
 This is where all the real magic happens. This could have been a class, but, since it's just a small, specialized project, I decided to write it using functions for kicks. Instead of self, I just use a bog variable. It's basically the same as if I wrote it like a class. This is where all the real magic happens. This could have been a class, but, since it's just a small, specialized project, I decided to write it using functions for kicks. Instead of self, I just use a bog variable. It's basically the same as if I wrote it like a class.
 +
 +If this seems a bit hard to follow, please keep in mind it is only about 50 lines of code, if you don't count function definitions!
  
 <Code:Python> <Code:Python>
 +import random
 +
 +# Define bog size (5x5).
 +bog_x = 5
 +bog_y = 5
 +
 +# Make a new bog game board. It's a 2-d array (list) of characters.
 +def make_bog():
 +    bog = [[" " for x in range(bog_x)] for y in range(bog_y)]
 +    return bog
 +
 +# display the bog. Some may say this doesn't need to be a function, but it might be, especially if this was a class.
 +def print_bog(bog):
 +    for row in bog:
 +        print(" ".join(row))
 +
 +# fill the remaining empty spaces with letters.
 +def fill_bog(bog):
 +    letters = 'qwertyuioplkjhgfdsazxcvbnm'
 +    for y in range(bog_y):
 +        for x in range(bog_x):
 +            # If it's an empty space replace it with a random letter.
 +            if bog[y][x] == ' ':
 +                bog[y][x] = random.choice(letters)
 +
 +# Try to put a word on the board a certain number of times.
 +def try_put_bog(bog, word, n):
 +    while n > 0:
 +        if put_bog(bog, word):
 +            return True
 +        else:
 +            n = n - 1
 +
 +    return False
 +
 +# Try to put a word on the board in a random position.
 +# This will take two passes. One to test and one to put.
 +def put_bog(bog, word):
 +    # Choose a random spot and a random direction for the word.
 +    x = random.randint(0, bog_x - 1)
 +    y = random.randint(0, bog_y - 1)
 +    d = random.randint(1, 3)
 +
 +    # Set dx and dy for 'walking' on the board in the specified direction
 +    if d == 1:
 +        # go up
 +        dx = 0
 +        dy = -1
 +    elif d == 2:
 +        # go to the right
 +        dx = 1
 +        dy = 0
 +    else:
 +        # go down
 +        dx = 0
 +        dy = 1
 +
 +    # PASS 1: Test if we can add the word
 +    tx = x  # test-x
 +    ty = y  # test-y
 +
 +    for i in range(len(word)):
 +        # Fail because outside
 +        if tx < 0 or ty < 0 or tx >= bog_x or ty >= bog_y:
 +            return False
 +
 +        # Fail because there is already a letter.
 +        if bog[ty][tx] != ' ':
 +            return False
 +
 +        tx = tx + dx    #step to next letter position
 +        ty = ty + dy    #step to next letter position
 +
 +    # Fall-through pass 2: add the word.
 +    # For every letter, step through and add it.
 +    for c in word:
 +        bog[y][x] = c
 +        x = x + dx
 +        y = y + dy
 +
 +    # Since we added the word, return true.
 +    return True    
 </Code> </Code>
 +
 +With this, the game is finished. It's a very simple concept.
 +
 +== Commentary
 +The big logic here is how the words are put on the board. Using dx and dy to walk accross a 2d landscape is a common theme. fill_bog() is the other interesting function (to a point), with the rest of the code being rather ordinary.
 +
 +What most people are intened to remember from this example is how the list was filtered, "list = [x for x in list if try_put_bog(bog, x, 1000)]", as well as the dx:dy stepping method. It also serves as a reminder for some basic programming concepts, like always put your variables at the start of a program, and don't necessarily need classes.
 +
 +If your program plans to instantiate a class and then call it, and the class is only ever intended to be instantiated or created once, it is highly likely it does not need to be a class. If there is no overhead, then the use of a class is fine. But plesae do not use this archetype to promote callback-based programming. That just sucks,
pybog.1699589812.txt.gz · Last modified: 2023/11/10 04:16 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki