-4

So I'm making a simple game in which the user attacks a nation from a bordering nation, but I have run into an issue in that I can't think of an easy way to expand on this code because I am planning on adding more nations to the game. The end product will be similar to Risk but, if all goes as planned, more complex. This code works fine, but I'm looking to make it easier to expand. Think of this as a rough draft.

countries=['USA','MEXICO','CANADA']

#ignore MILITARYGROWTHRATE, that will be put to use later in production, I just added it in early

USA_REGION_DATA={'Bordering':['MEXICO','CANADA'],'MILITARYGROWTHRATE':1.03}
MEXICO_REGION_DATA={'Bordering':['USA'],'MILITARYGROWTHRATE':1.01}
CANADA_REGION_DATA={'Bordering':['USA'],'MILITARYGROWTHRATE':1.01}

def attack(origin,target):
    '''Origin is where you are attacking from,
    target is who you are attacking'''
    x=origin.upper()
    y=target.upper()
    if x not in countries:
        print("You must attack from somewhere!")
    elif x=='USA':
        if y not in USA_REGION_DATA['Bordering']:
            print("Not a valid target")
        else:
            print("Attack is underway!")
    elif x=='MEXICO':
        if y not in MEXICO_REGION_DATA['Bordering']:
            print("Not a valid target")
        else:
            print("Attack is underway!")
    elif x=='Canada':
        if y not in CANADA_REGION_DATA['Bordering']:
            print("Not a valid target")
        else:
            print("Attack is underway!")

print("Are you attacking from the USA, Mexico, or Canada?")
origin=raw_input()
if origin.upper()=='USA':
    print("Are you attacking Mexico or Canada?")
    target=raw_input()
    print("Are you sure you want to attack "+target+"? (Yes or No)")
    answer=raw_input()
    if answer.upper()=='YES':
        attack(origin,target)
    else:
        print("You'll never get anything done by sitting around...")
else:
    print("Are you sure you want to attack the USA?(Yes or No)")
    if raw_input().upper()=='YES':
        target='USA'
        attack(origin,target)
    else:
        print("You'll never get anything done by sitting around...")
Jackson
  • 15
  • 4

1 Answers1

3

You almost certainly want to replace your specific variables for each nation with a data structure (such as a dictionary) that uses the nation's name as a key. So, rather than referring to USA_REGION_DATA you'd look up REGION_DATA["USA"]. This is extensible to any number of nations, as you can simply add new values to the dictionary.

You can initialize it with something like:

REGION_DATA = { "USA": {'Bordering':['MEXICO','CANADA'],'MILITARYGROWTHRATE':1.03},
                "MEXICO": {'Bordering':['USA'],'MILITARYGROWTHRATE':1.01},
                "CANADA": {'Bordering':['USA'],'MILITARYGROWTHRATE':1.01}
              }

Your attack function (and others) would be generic, with no special casing for individual nations:

def attack(origin,target):
    '''Origin is where you are attacking from,
    target is who you are attacking'''
    x=origin.upper()
    y=target.upper()
    if x not in REGION_DATA:
        print("You must attack from somewhere!")
    elif y not in REGION_DATA[x]['Bordering']:  # extra indexing by x here
        print("Not a valid target")
    else:
        print("Attack is underway!")
Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • This is perfect! I just started coding so I was hoping for a well explained solution and this is spot on. Thanks! – Jackson Jun 07 '14 at 07:42