Search code examples
pythonpython-2.7text-based

How would I create a text-based board for a scrabble game in python?


I am currently making a text-based scrabble game using python, and I was wondering how to make a board for such a game. Do I have to individually draw out each tile, or is there a better way?

Thanks


Solution

  • Use an array:

    board = [4, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 4, 5,
        0, 3, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 3, 0, 5,
        0, 0, 3, 0, 0, 0, 1, 0, 1, 0, 0, 0, 3, 0, 0, 5,
        1, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 1, 5,
        0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 5,
        0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 5,
        0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 5,
        4, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 4, 5,
        0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 5,
        0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 5,
        0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 5,
        1, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 1, 5,
        0, 0, 3, 0, 0, 0, 1, 0, 1, 0, 0, 0, 3, 0, 0, 5,
        0, 3, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 3, 0, 5,
        4, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 4] #This is the real board
    

    To loop over it, use the following code. I use termcolor.cprint() to make the text different colors, matching the background color on a true scrabble board

    for k in board:
        if k == 5:
            print'\n\n',
        elif k == 4:
            cprint(' TW', 'red', attrs=[], end = ' ')
        #print 'TW',
        elif k == 3:
            cprint(' DW', 'magenta', attrs=[], end = ' ')
        #print 'DW',
        elif k == 2:
            cprint(' TL', 'blue', attrs=[], end = ' ')
        #print 'TL',
        elif k == 1:
            cprint(' DL', 'cyan', attrs=[], end = ' ')
        #print 'DL',
        elif k == 0:
            print '___',
    

    This prints:

    Scrabble Board