game_matrix_menu.py

#! /usr/bin/python3
# ==================================================================
# 3D Matrix Game - Menus
# ==================================================================

import os

import game_matrix_config   as cfg
import game_matrix01        as gm1
import game_matrix_def_file as dfl


# ---- global data -------------------------------------------------
# CurrentNode  matrix current (working) node  
# Debug        flag - print debug messages
# EndNode      matrix path end node
# MatrixEdge   nodes per matrix dimensions (x,y,z)
# StartNode    matrix path start node
# Verbose      flag - print verbose messages
# ------------------------------------------------------------------

global CurrentNode
global Debug
global EndNode
global MatrixEdge
global StartNode
global Verbose


# ------------------------------------------------------------------
# ---- helper functions --------------------------------------------
# ------------------------------------------------------------------

# ---- get user input

def get_user_input(prompt):
    return input(prompt)

# ---- pause program

def pause_program():
    get_user_input('\nPress enter to continue ')

# ---- clear terminal screen

def clear_screen():
    os.system('clear')

# ---- display movement error message

def error_message_movement():
    clear_screen()
    print('=======================================================')
    print('Error: Illegal movement')
    print('=======================================================')

    pause_program()

# ---- display file exists error message

def error_file_exists(f):
    clear_screen()
    print('=======================================================')
    print('Error: file {} does not exist'.format(f))
    print('=======================================================')

    pause_program()


# ------------------------------------------------------------------
# --- debug menu ---------------------------------------------------
# ------------------------------------------------------------------

def debug_menu(mtrx):

    while True:

        # --- display menu

        clear_screen()

        print('===================================================')
        print('Debug Menu')
        print('===================================================')
        print('Option  Description ')
        print('------  -------------------------------------------')
        if cfg.Debug:
            print('   1    debug flag off')
        else:
            print('   1    debug flag on')
        if cfg.Verbose:
            print('   2    verbose flag off')
        else:
            print('   2    verbose flag on')

        print('   3    display matrix')
        print('   4    get currrent node nav information')
        print('  99    return to main menu')

        val = get_user_input('\n  Select option: ')

        sval = val.strip()

        if sval == '':
            break

        if sval.isdigit() != True:
            print('\nIllegal value entered ({})'.format(sval))
            pause_program()
            continue

        ival = int(sval)

        if   ival == 1:        # ---- toggle debug flag
            if cfg.Debug:
                cfg.Debug = False
            else:
                cfg.Debug = True
        if   ival == 2:        # ---- toggle debug flag
            if cfg.Verbose:
                cfg.Verbose = False
            else:
                cfg.Verbose = True
        elif ival == 3:        # ---- display matrix
            gm1.display_matrix(mtrx,cfg.MatrixEdge)
            pause_program()
        elif ival == 4:        # ---- current node navigation into
            nav = cfg.CurrentNode.get_nav_info()
            print(nav)
            pause_program()
        elif ival == 99:       # ---- return to main menu
            break 

    return


# ------------------------------------------------------------------
# ---- main menu ---------------------------------------------------
# ------------------------------------------------------------------

def main_menu(mtrx,title):

    while True:

        # ---- are we there yet (at the end node)?

        if cfg.CurrentNode == cfg.EndNode:
            clear_screen()
            print('=======================================================')
            print('Hooray!! You have reached the end node!')
            print('=======================================================')
            pause_program()
            return

        # --- display menu

        clear_screen()

        print('===================================================')
        print(title)
        print('===================================================')
        print('Option  Description ')
        print('------  -------------------------------------------')
        print('   1    move forward   ( z axis, z=z+1 )')
        print('   2    move backward  ( z axis, z=z-1 )')
        print('   3    move up        ( y axis, y=y+1 )')
        print('   4    move down      ( y axis, y=y-1 )')
        print('   5    move left      ( x axis, x=x-1 )')
        print('   6    move right     ( x axis, x=x+1 )')
        print('   7    wormhole jump')
        print('')
        print('   9    display current node info')
        print('')
        print('  20    load matrix definition file')
        print('')
        print('  30    go to start node')
        print('')
        print('  40    display matrix info')
        print('')
        print('  90    create new random matrix')
        print('  98    debug menu')
        print('  99    exit program')

        val = get_user_input('\n  Select option: ')

        sval = val.strip()

        if sval == '':
            break

        if sval.isdigit() != True:
            print('\nIllegal value entered ({})'.format(sval))
            pause_program()
            continue

        ival = int(sval)

        if   ival == 1:        # ---- forward ------------------

            if cfg.CurrentNode           == None or \
                cfg.CurrentNode.forward  == None or \
                cfg.CurrentNode.z        >= cfg.MatrixEdge-1:
                error_message_movement()
            else:
                cfg.CurrentNode = cfg.CurrentNode.forward

        elif ival == 2:        # ---- backward -----------------

            if cfg.CurrentNode           == None or \
                cfg.CurrentNode.backward == None or \
                cfg.CurrentNode.z        <= 0:
                error_message_movement()
            else:
                cfg.CurrentNode = cfg.CurrentNode.backward

        elif ival == 3:        # ---- up -----------------------

            if cfg.CurrentNode          == None or \
                cfg.CurrentNode.up      == None or \
                cfg.CurrentNode.y       >= cfg.MatrixEdge-1:
                error_message_movement()
            else:
                cfg.CurrentNode = cfg.CurrentNode.up

        elif ival == 4:        # ---- down ---------------------

            if cfg.CurrentNode          == None or \
                cfg.CurrentNode.down    == None or \
                cfg.CurrentNode.y       <= 0:
                error_message_movement()
            else:
                cfg.CurrentNode = cfg.CurrentNode.down

        elif ival == 5:        # ---- left ---------------------

            if cfg.CurrentNode          == None or \
                cfg.CurrentNode.left    == None or \
                cfg.CurrentNode.x       <= 0:
                error_message_movement()
            else:
                cfg.CurrentNode = cfg.CurrentNode.left

        elif ival == 6:        # ---- right --------------------

            if cfg.CurrentNode          == None or \
                cfg.CurrentNode.right   == None or \
                cfg.CurrentNode.x       >= cfg.MatrixEdge-1:
                error_message_movement()
            else:
                cfg.CurrentNode = cfg.CurrentNode.right

        elif ival == 7:        # ---- wormhole jump ------------

            if cfg.CurrentNode == None or \
                cfg.CurrentNode.wormhole == None:
                error_message_movement()
            else:
                cfg.CurrentNode = cfg.CurrentNode.wormhole

        elif ival == 9:        # ---- node navigation info -----

            clear_screen()
            print('-----------------------------------------------')
            print('Current Node Navigation Information')
            print('-----------------------------------------------')
            gm1.display_node_nav_info(cfg.CurrentNode)
            pause_program()

        elif ival == 20:       # ---- load matrix def file ------

            filename = get_user_input(
               '\n  Enter matrix definition file name: ')
            if filename:
                if not os.path.isfile(filename):
                    error_file_exists(filename)
                else:
                    mtrx = dfl.load_matrix_definition(filename)


        elif ival == 30:       # ---- go to start node ---------

            cfg.CurrentNode = cfg.StartNode

        elif ival == 40:       # ---- display matrix info ------

            clear_screen()
            print('-----------------------------------------------')
            print('Matrix Information')
            print('-----------------------------------------------')
            gm1.display_matrix_info()
            pause_program()

        elif ival == 90:       # ---- create new matrix --------
            clear_screen()
            print('=======================================================')
            print('Random matrix generation not implemented yet')
            print('=======================================================')
            pause_program()

        elif ival == 98:       # ---- debug menu ---------------

            debug_menu(mtrx)

        elif ival == 99:       # ---- exit ---------------------

            break

        else:                  # ---- error --------------------

            print('\nUnknown menu option entered ({})'.format(ival))
            pause_program()

    return