import random

MAP1 = [
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 3],
    [1, 7, 0, 0, 0, 1, 1, 6, 1, 3], #y2, x7
    [1, 1, 1, 0, 0, 0, 1, 0, 1, 3],
    [1, 6, 1, 0, 0, 0, 1, 0, 1, 3], #y3, x1
    [1, 0, 1, 1, 0, 0, 1, 0, 1, 3],
    [1, 0, 0, 0, 0, 0, 0, 0, 1, 3],
    [1, 0, 0, 1, 1, 0, 1, 1, 1, 3],
    [1, 2, 0, 7, 1, 0, 0, 5, 1, 3],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 3]
    ]

MAP2 = [
    [1, 1, 1, 1, 1, 3],
    [1, 6, 1, 7, 1, 3], #y1, x1
    [1, 0, 1, 0, 1, 3],
    [1, 0, 0, 0, 1, 3],
    [1, 1, 1, 1, 1, 3]
    ]

MAP3 = [
    [1, 1, 1, 1, 1, 0, 0, 0, 3],
    [1, 6, 1, 7, 1, 0, 0, 0, 3], #y1, x1
    [1, 0, 1, 0, 1, 0, 0, 0, 3],
    [1, 0, 1, 0, 1, 1, 1, 1, 3],
    [1, 0, 0, 0, 0, 0, 6, 1, 3], #y4, x6
    [1, 1, 1, 1, 1, 1, 1, 1, 3]
    ]

MAP4 = [
    [1, 1, 1, 1, 1, 0, 0, 0, 3],
    [1, 6, 0, 0, 1, 1, 1, 1, 3], #y1, x1
    [1, 1, 1, 0, 0, 0, 7, 1, 3],
    [1, 0, 0, 0, 0, 1, 1, 1, 3],
    [1, 6, 1, 0, 0, 0, 6, 1, 3], #y4, x6; #y3, x1
    [1, 1, 1, 1, 1, 1, 1, 1, 3]
    ]

MAP5 = [
    [1, 1, 1, 1, 3],
    [1, 6, 0, 1, 3], #y1, x1
    [1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 3],
    [1, 1, 0, 1, 0, 0, 0, 1, 1, 6, 1, 3], #y3, x9
    [0, 1, 7, 1, 0, 0, 0, 1, 0, 0, 1, 3],
    [0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 3],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 6, 1, 3], #y6, x11
    [0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 7, 1, 3],
    [0, 0, 0, 1, 6, 1, 0, 0, 1, 1, 1, 1, 1, 3], #y7, x4
    [0, 1, 1, 1, 0, 1, 1, 1, 3],
    [0, 1, 6, 0, 0, 0, 0, 1, 3], #y10, x2
    [0, 1, 1, 0, 0, 0, 7, 1, 3],
    [0, 0, 1, 1, 1, 1, 1, 1, 3]
    ]
    
MAP6 = [
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3],
    [1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 3],
    [1, 0, 6, 1, 0, 0, 0, 1, 0, 1, 3], #y2, x2
    [1, 0, 1, 0, 0, 0, 1, 7, 0, 1, 3],
    [1, 0, 0, 0, 0, 0, 1, 5, 0, 1, 3],
    [1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 3],
    [1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 3],
    [1, 0, 1, 0, 0, 0, 0, 1, 7, 1, 3],
    [1, 7, 1, 6, 1, 0, 0, 0, 1, 1, 3], #y8, x3
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3],
    ]
    
MAP7 = [
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3],
    [1, 6, 0, 0, 0, 0, 0, 0, 6, 1, 3], #y1, x1;  #y1, x8
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3],
    ]
    
MAP8 = [
    [1, 1, 1, 1, 1, 1, 1, 1, 3],
    [1, 7, 0, 0, 0, 0, 6, 1, 3], #y1, x6
    [1, 1, 1, 1, 1, 1, 1, 1, 3],
    ]
    
MAP9 = [
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3],
    [1, 6, 0, 1, 0, 0, 0, 1, 0, 0, 7, 1, 3], #y1, x1
    [1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 3],
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 3],
    [1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 3],
    [1, 0, 0, 1, 7, 0, 0, 1, 0, 0, 0, 0, 1, 3],
    [1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 3],
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 7, 1, 0, 1, 3],
    [1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 3],
    [1, 0, 0, 0, 0, 1, 0, 1, 7, 0, 0, 0, 1, 3],
    [1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 3],
    [1, 9, 7, 1, 6, 0, 0, 1, 6, 0, 0, 0, 1, 3], #y11, x3; #y11, x8
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3]
    ]

MAP10 = [
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3],
    [1, 7, 0, 0, 0, 0, 0, 0, 7, 1, 3],
    [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 3],
    [1, 0, 1, 0, 0, 6, 1, 1, 0, 1, 3], #y3, x5
    [1, 0, 0, 0, 1, 1, 6, 1, 0, 1, 3], #y4, x6
    [1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 3],
    [1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 3],
    [1, 6, 0, 1, 0, 0, 1, 0, 0, 1, 3], #y7, x1
    [1, 7, 0, 0, 0, 0, 0, 0, 7, 1, 3],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3]
    ]

MAP11 = [
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3],
    [1, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3],
    [1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 7, 0, 1, 3],
    [1, 0, 1, 6, 1, 0, 0, 0, 0, 7, 1, 0, 1, 0, 1, 3], #y3, x3
    [1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 3],
    [1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 3],
    [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 3],
    [1, 0, 0, 0, 1, 0, 1, 7, 0, 0, 1, 0, 1, 0, 1, 3],
    [1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 3],
    [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 3],
    [1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 3],
    [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3],
    [1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 3],
    [1, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 1, 3],
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3]
    ]

MAP12 = [
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3],
    [1, 6, 1, 0, 1, 0, 0, 0, 0, 1, 1, 3], #y1, x1
    [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 3],
    [1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 3],
    [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 6, 1, 3], #y4, x11
    [1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 7, 1, 3],
    [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 3],
    [1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 3],
    [1, 6, 1, 0, 0, 0, 1, 0, 0, 1, 1, 3], #y8, x1
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3]
    ]

MAP13 = [
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3],
    [1, 6, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 3], #y1, x1
    [1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 3],
    [1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 3],
    [1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 3],
    [1, 7, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3],
    [1, 1, 1, 0, 0, 0, 6, 1, 6, 0, 0, 0, 1, 3], #y6, x6; #y6, x8
    [1, 7, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 3],
    [1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 3],
    [1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 3],
    [1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 3],
    [1, 6, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 3], #y3, x11
    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3]
    ]

MAP_STAIRWELL_1 = [
    [1, 1, 1, 1, 1, 3],
    [1, 0, 0, 0, 1, 3],
    [1, 6, 1, 0, 1, 3], #y2, x1
    [1, 1, 6, 0, 1, 3], #y3, x2
    [1, 1, 1, 1, 1, 3]
    ]

MAP_STAIRWELL_2 = [
    [1, 1, 1, 1, 1, 3],
    [1, 1, 6, 0, 1, 3], #y1, x2
    [1, 6, 1, 0, 1, 3], #y2, x1
    [1, 0, 0, 0, 1, 3],
    [1, 1, 1, 1, 1, 3]
    ]

MAP_STAIRWELL_3 = [
    [1, 1, 1, 1, 1, 3],
    [1, 0, 6, 1, 1, 3], #y1, x2
    [1, 0, 1, 6, 1, 3], #y2, x3
    [1, 0, 0, 0, 1, 3],
    [1, 1, 1, 1, 1, 3]
    ]

MAP_STAIRWELL_4 = [
    [1, 1, 1, 1, 1, 3],
    [1, 0, 0, 0, 1, 3],
    [1, 0, 1, 6, 1, 3], #y2, x3
    [1, 0, 6, 1, 1, 3], #y3, x2
    [1, 1, 1, 1, 1, 3]
    ]

MAP_TOWER_SUMMIT = [
    [1, 1, 1, 1, 1, 1, 1, 3],
    [1, 1, 0, 7, 0, 6, 1, 3], #y1, x5
    [1, 0, 0, 0, 0, 0, 1, 3],
    [1, 7, 0, 6, 0, 7, 1, 3], #y3, x3
    [1, 0, 0, 0, 0, 0, 1, 3],
    [1, 1, 0, 7, 0, 1, 1, 3],
    [1, 1, 1, 1, 1, 1, 1, 3]
    ]

MAP_TOWER_SUMMIT_ROOF = [
    [1, 1, 1, 1, 1, 3],
    [1, 0, 0, 6, 1, 3], #y1, x3
    [1, 0, 0, 0, 1, 3],
    [1, 7, 0, 0, 1, 3],
    [1, 1, 1, 1, 1, 3]
    ]


class gameField:
    def __init__(self, mapData=[0]):
	self.mapData = mapData
	self.string = ''
	self.blocks = []

	"""For now, this part of the program creates "blocks" for the map. Currently, this is outdated
	and a new method needs to be coded for creating blocks for each map when the program starts."""
	
	for i in range(len(self.mapData)):
	    for j in range(len(self.mapData[i])):
		if self.mapData[i][j] == 2:
		    r = random.randrange(0, 4)+1
		    if r == 1:
			self.blocks.append(' v ')
		    if r == 2:
			self.blocks.append(' x ')
		    if r == 3:
			self.blocks.append(' ^ ')
		    if r == 4:
			self.blocks.append(' * ')
		    if r == 5:
			self.blocks.append(' ~ ')

class character:
    def __init__(self, x=0, y=0, field=None, startwep=None):
	self.crds = [x, y] #crds = coordinates
	self.x = self.crds[0]
	self.y = self.crds[1]
	self.field = field #This links the current playing field with the character. The purpose was to make is easy to manipulate the player's coordinates in reference to the current game field. field would be an instance of the gameField class.
	self.inventory = {'Slot1':None, 'Slot2':None, 'Slot3':None, 'Equipped':None, 'Gold':0,}
	self.hp = 100
	self.inventory['Equipped'] = startwep
	self.spells = ["teleport"]
    def __str__(self):
	s = ''
	r = 0
	for i in range(len(self.field.mapData)):
	    for j in range(len(self.field.mapData[i])):
		if self.field.mapData[i][j] == 4:
		    self.field.mapData[i][j] = 0
	self.field.mapData[self.y][self.x] = 4
	for i in range(len(self.field.mapData)):
	    for j in range(len(self.field.mapData[i])):
		if self.field.mapData[i][j] == 0:
		    s += '   '
		elif self.field.mapData[i][j] == 1:
		    s += ' / '
		elif self.field.mapData[i][j] == 2:
		    r += 1
		    s += self.field.blocks[r-1]
		elif self.field.mapData[i][j] == 3:
		    s += '\n'
		elif self.field.mapData[i][j] == 4:
		    s += ' O '
		elif self.field.mapData[i][j] == 5:
		    s += ' S '
		elif self.field.mapData[i][j] == 6:
		    s += '[/]'
		elif self.field.mapData[i][j] == 7:
		    s += '{"}'
		elif self.field.mapData[i][j] == 8:
		    s += '{_}'
		elif self.field.mapData[i][j] == 9:
		    s += ' & '
	self.field.string = s
	return self.field.string

    def isValidMove(self, move):
	'''This method just checks to see if a desired move is valid.
	The coordinates must be referenced backwards due to the way I use
	lists of lists to store coordinates. It would actually be more
	efficient if I converted the lists to tuples after instantiation.
	Oh well.'''
	
	if move == 'a':
	    if self.field.mapData[self.y][self.x - 1] != 0:
		return False
	if move == 'a':
	    if self.field.mapData[self.y][self.x - 1] == 0:
		return True
	if move == 'w':
	    if self.field.mapData[self.y - 1][self.x] != 0:
		return False
	if move == 'w':
	    if self.field.mapData[self.y - 1][self.x] == 0:
		return True
	if move == 's':
	    if self.field.mapData[self.y + 1][self.x] != 0:
		return False
	if move == 's':
	    if self.field.mapData[self.y + 1][self.x] == 0:
		return True
	if move == 'd':
	    if self.field.mapData[self.y][self.x + 1] != 0:
		return False
	if move == 'd':
	    if self.field.mapData[self.y][self.x + 1] == 0:
		return True

class item:
    def __init__(self, name='', drange=[1, 4], itype='', price=0):
	self.name = name
	self.drange = drange
	self.itype = itype
	self.price = price

    def attackRoll(self):
	#Ranges in the if comparisons are for hit/miss.
	#Ranges of drange[0]-[1] are for the weapon's damage ranges.
	
	if self.itype == 'Small':
	    if random.randrange(1, 12) > 1:
		return random.randrange(self.drange[0], self.drange[1]+1)
	    else:
		return 0

	if self.itype == 'Medium':
	    if random.randrange(1, 9) > 1:
		return random.randrange(self.drange[0], self.drange[1]+1)
	    else:
		return 0

	if self.itype == 'Large':
	    if random.randrange(1, 5) > 1:
		return random.randrange(self.drange[0], self.drange[1]+1)
	    else:
		return 0

class monster:
    def __init__(self, name='', hp=5, weapon=[0], armor=None, battleLevel=1):
	self.hp = hp
	self.name = name
	self.armor = armor
	self.battleLevel = battleLevel
	
	if weapon[0] != 0:
	    self.weapon = item(weapon[0], weapon[1], weapon[2])
	else:
	    self.weapon = item('Small Claws', [1, 4], 'Small')

def battle(playerCharacter, battleLevel=1, enemy=None):

    turn = random.randrange(0, 2) + 1
    print('\n' + 'Battle Initiated.')
    b = raw_input()
    
    if battleLevel == 1 and enemy == None:
	a = random.randrange(0, 3) + 1
	if a == 1:
	    enemy = monster('Little Creature', 5, ['Small Claws', [1, 2], 'Small'], 5)
	if a == 2:
	    enemy = monster('Sewer Rat', 10, ['Teeth', [1, 5], 'Small'], 7)
	if a == 3:
	    enemy = monster('Groping Ooze', 10, ['Psuedopod', [1, 10], 'Small'], 10)
    elif battleLevel == 2 and enemy == None:
	a = random.randrange(0, 3) + 1
	if a == 1:
	    enemy = monster('Lesser Nighwalker', 5, ['Psionic Caress', [3, 15], 'Medium'], 5)
	if a == 2:
	    enemy = monster('Spirit Cube', 10, ['Mind Pulse', [-5, 5], 'Small'], 3)
	if a == 3:
	    enemy = monster('Mummy', 25, ['Gnarl', [5, 15], 'Small'], 15)
    elif battleLevel == 3 and enemy == None:
	a = 1
	if a == 1:
	    enemy = monster('Death Hawk', 5, ['Sonic Dive', [0, 20], 'Large'], 15)
    else:
	print('Fuck. This text should not appear. Something is wrong with mV.')

    while enemy.hp > 0 and playerCharacter.hp > 0:
	print ('Player    |    ' + enemy.name)
	if playerCharacter.hp == 100:
	    print ('   ' + str(playerCharacter.hp) + '    |    ' + str(enemy.hp))
	else:
	    print ('    ' + str(playerCharacter.hp) + '    |    ' + str(enemy.hp))
	print ('     o    |    x')
	print ('')
	if turn == 1:
	    print ('The ' + enemy.name + ' is attacking you! (press enter)')
	    b = raw_input()
	    attackRoll = enemy.weapon.attackRoll()
	    playerCharacter.hp -= attackRoll
	    print ('The ' + enemy.name + ' hit you with its ' + enemy.weapon.name + ' dealing ' + str(attackRoll) + ' damage! (press enter)')
	    b = raw_input()
	    if enemy.hp <= 0:
		print ('You have been defeated by the ' + enemy.name + '! (press enter)')
		b = raw_input()
	    turn = 2
	    
	elif turn == 2:
	    print ('It is your turn to fight! Press enter to attack!')
	    b = raw_input()
	    if playerCharacter.inventory['Equipped'] != None:
		attackRoll = playerCharacter.inventory['Equipped'].attackRoll()
	    else:
		attackRoll = 1
	    enemy.hp -= attackRoll
	    if playerCharacter.inventory['Equipped'] != None: 
		print ('You hit the ' + enemy.name + ' with your ' + playerCharacter.inventory['Equipped'].name + ' dealing ' + str(attackRoll) + ' damage! (press enter)')
	    else:
		print ('You hit the ' + enemy.name + ' with your fists dealing 1 damage!')
	    b = raw_input()
	    if enemy.hp <= 0:
		print ('You have defeated the ' + enemy.name + '! (press enter)')
		b = raw_input()
		return enemy.battleLevel
	    turn = 1
	else:
	    return

def spells(currentPlayer):
	while True:
		index = 1
		print('Your available spells. Type a number and a direction to use a spell in the selected direction, or enter to cancel.')
		for option in currentPlayer.spells:
			print(str(index) + '. ' + option)
			index = index + 1
		spellCommand = raw_input()
		toCast = int(spellCommand[0]) - 1
		dir = spellCommand[1]
		if(currentPlayer.spells[toCast] == 'teleport'):
			zappx = 0
			zappy = 0
			if(dir == "a"):
				zappx = -1
			if(dir == "d"):
				zappx = 1
			if(dir == "w"):
				zappy = -1
			if(dir == "s"):
				zappy = 1
			offsetMag = 0
			while(offsetMag < 3):
				offsetMag += 1
				if(currentCharacter.isValidMove(dir)):
					currentCharacter.x += zappx
					currentCharacter.y += zappy
		return 0

def battleTreasure(val, currentPlayer):
    
    diceRoll1 = random.randrange(0, 20) + 1
    diceRoll2 = random.randrange(0, 5) + 1
    diceRoll3 = random.randrange(0, 5) + 1

    if  diceRoll1 > 10:
	currentPlayer.inventory['Gold'] += (diceRoll2 + diceRoll3 + val)
	print('You gained ' + str(diceRoll2 + diceRoll3 + val) + ' gold from winning the battle!')
	b = raw_input()
	print('You now have ' + str(currentPlayer.inventory['Gold']) + ' gold.')
	b = raw_input()

def shop(currentPlayer, shopx=0):

    while shopx == 0:
	print('Welcome to the shop! What would you like to buy? (1-4)')
	print('Current Gold: ' + str(currentPlayer.inventory['Gold']))
	print('1. Short Sword : Small  : 20 Gold')
	print('2. Mace	: Medium : 40 Gold')
	print('3. Halberd     : Large  : 70 Gold')
	print('4. Potion      : 1 use  : 5  Gold')
	print('Exit: Any other key.')
	choice = raw_input()

	slot = findEmptyInventorySlot(currentPlayer)

	if choice == '1':
	    buy(currentPlayer, SHORTSWORD, slot)

	if choice == '2':
	    buy(currentPlayer, MACE, slot)

	if choice == '3':
	    buy(currentPlayer, HALBERD, slot)

	if choice == '4':
	    buy(currentPlayer, POTION, slot)
	    
	else:
	    return

    while shopx == 1:
	print('Welcome to the shop! What would you like to buy? (1-4)')
	print('Current Gold: ' + str(currentPlayer.inventory['Gold']))
	print('1. Metronome	  : Small  : 5   Gold')
	print('3. Nullifier	  : Large  : 40  Gold')
	print('2. Pitchfork of Hell  : Medium : 100 Gold')
	print('4. Potion	     : 1 use  : 5   Gold')
	print('Exit: Any other key.')
	choice = raw_input()

	slot = findEmptyInventorySlot(currentPlayer)

	if choice == '1':
	    buy(currentPlayer, METRONOME, slot)

	if choice == '2':
	    buy(currentPlayer, NULLIFIER, slot)

	if choice == '3':
	    buy(currentPlayer, PITCHFORK_OF_HELL, slot)

	if choice == '4':
	    buy(currentPlayer, POTION, slot)
	    
	else:
	    return

def buy(currentPlayer, item, slot):
    if currentPlayer.inventory['Gold'] >= item.price:
	slot = findEmptyInventorySlot(currentPlayer)
	if slot != None:
	    currentPlayer.inventory[slot] = item
	    currentPlayer.inventory['Gold'] -= item.price
	    print('You purchased a ' + currentPlayer.inventory[slot].name + '!')
	    b = raw_input()
	    choice = 0
	    return
	
    elif currentPlayer.inventory['Gold'] < item.price:
	print('That is not enough money! You need ' + str(item.price) + ' Gold. (press enter)')
	b = raw_input()
	return
    
    else:
	    print('Your inventory is full!')
	    b = raw_input()
	    return

def inventory(currentPlayer):
    
    while True:
	print('Your current inventory, pick a number to use/equip an item:')
	s = inventoryDisplay('1.', currentPlayer.inventory['Slot1'])
	print(s)
	s = inventoryDisplay('2.', currentPlayer.inventory['Slot2'])
	print(s)
	s = inventoryDisplay('3.', currentPlayer.inventory['Slot3'])
	print(s) 
	s = inventoryDisplay('4.', currentPlayer.inventory['Equipped'], 'Equipped')
	print(s)
	print('5. Enter Discard Menu')
	print('Press any other key to exit.')
	choice = raw_input()
	
	if choice != '1' and choice != '2' and choice != '3' and choice != '4' and choice != '5':
	    return
	if choice == '1':
	    useItem('Slot1', currentPlayer)
	if choice == '2':
	    useItem('Slot2', currentPlayer)
	if choice == '3':
	    useItem('Slot3', currentPlayer)
	if choice == '5':
	    discard(currentPlayer)

def discard(currentPlayer):
    while True:
	print('Your current inventory, pick a number to discard an item:')
	s = inventoryDisplay('1.', currentPlayer.inventory['Slot1'])
	print(s)
	s = inventoryDisplay('2.', currentPlayer.inventory['Slot2'])
	print(s)
	s = inventoryDisplay('3.', currentPlayer.inventory['Slot3'])
	print(s) 
	s = inventoryDisplay('4.', currentPlayer.inventory['Equipped'], 'Equipped')
	print(s)
	print('Press any other key to enter the inventory menu.')
	choice = raw_input()

	if choice != '1' and choice != '2' and choice != '3' and choice != '4' and choice != '5':
	    return
	if choice == '1':
	    currentPlayer.inventory['Slot1'] = None
	if choice == '2':
	    currentPlayer.inventory['Slot2'] = None
	if choice == '3':
	    currentPlayer.inventory['Slot3'] = None
	if choice == '4':
	    currentPlayer.inventory['Equipped'] = None
	    print('You discared an equipped item! Be sure to equip another for battle.')
	    b = raw_input()
    

def useItem(slot, currentPlayer):
    if currentPlayer.inventory[slot] == POTION:
	currentPlayer.hp += 20
	currentPlayer.inventory[slot] = None
	print('You used a potion! You have been healed by 20 hp. (press enter)')
	b = raw_input()
    if currentPlayer.inventory[slot] != None:
	if currentPlayer.inventory['Equipped'] != None:
	    c = currentPlayer.inventory['Equipped']
	    currentPlayer.inventory['Equipped'] = currentPlayer.inventory[slot]
	    currentPlayer.inventory[slot] = c
	    del(c)
	    print(currentPlayer.inventory['Equipped'].name + ' has been equipped! (press enter)')
	    b = raw_input()
	else:
	    currentPlayer.inventory['Equipped'] = currentPlayer.inventory[slot]
	    currentPlayer.inventory[slot] = None
	    print(currentPlayer.inventory['Equipped'].name + ' has been equipped! (press enter)')
	    b = raw_input()
    else:
	print('There is nothing in this slot to use! (press enter)')
	b = raw_input
	
	

def inventoryDisplay(number, item, slotType=''):
    if item == DAGGER and slotType != 'Equipped':
	s = number + ' ' + item.name + '      : ' + item.itype + '  : ' + '1 - 4  Damage'
	return s
    elif item == SHORTSWORD and slotType != 'Equipped':
	s = number + ' ' + item.name + ' : ' + item.itype + '	: ' + '1 - 8  Damage'
	return s
    elif item == MACE and slotType != 'Equipped':
	s = number + ' ' + item.name + '       : ' + item.itype + ' : ' + '3 - 10 Damage'
	return s
    elif item == POTION and slotType != 'Equipped':
	s = number + ' ' + item.name + '     : ' + item.itype + '  : ' + 'Heals 20 damage'
	return s
    elif item == HALBERD and slotType != 'Equipped':
	s = number + ' ' + item.name + '    : ' + item.itype + '  : ' + '1-25 Damage'
	return s
    elif slotType == 'Equipped':
	'4. Potion      : 1 use  : 5  Gold'
	'Equipped Item  : '
	if item == DAGGER:
	    s = number + ' Equipped Item : ' + item.name + ' : ' + item.itype + ' : ' + '1 - 4 Damage'
	    return s
	elif item == SHORTSWORD:
	    s = number + ' Equipped Item : ' + item.name + ' : ' + item.itype + ' : ' + '1 - 8 Damage'
	    return s
	elif item == MACE:
	    s = number + ' Equipped Item : ' + item.name + ' : ' + item.itype + ' : ' + '3 - 10 Damage'
	    return s
	elif item == POTION:
	    s = number + ' Equipped Item : ' + item.name + ' : ' + item.itype + ' : ' + 'Heals 20 damage'
	    return s
	elif item == HALBERD:
	    s = number + ' Equipped Item : ' + item.name + ' : ' + item.itype + ' : ' + '1-25 Damage'
	    return s
	else:
	    return ' Nothing Equipped'

    else:
	s = number + ' Empty Slot :	:'
	return s

def findEmptyInventorySlot(currentPlayer):
    if currentPlayer.inventory['Slot1'] == None:
	return 'Slot1'
    elif currentPlayer.inventory['Slot2'] == None:
	return 'Slot2'
    elif currentPlayer.inventory['Slot3'] == None:
	return 'Slot3'
    else:
	return None

def action(currentPlayer):
    upy = currentPlayer.y - 1
    downy = currentPlayer.y + 1
    leftx = currentCharacter.x - 1
    rightx = currentCharacter.x + 1

    if currentPlayer.field.mapData[currentCharacter.y][leftx] == 5 or currentPlayer.field.mapData[currentCharacter.y][rightx] == 5 or currentPlayer.field.mapData[upy][currentCharacter.x] == 5 or currentPlayer.field.mapData[downy][currentCharacter.x] == 5:
	return 'Store'
    elif currentPlayer.field.mapData[currentCharacter.y][leftx] == 6 or currentPlayer.field.mapData[currentCharacter.y][rightx] == 6 or currentPlayer.field.mapData[upy][currentCharacter.x] == 6 or currentPlayer.field.mapData[downy][currentCharacter.x] == 6:
	return 'Door'
    elif currentPlayer.field.mapData[currentCharacter.y][leftx] == 7 or currentPlayer.field.mapData[currentCharacter.y][rightx] == 7 or currentPlayer.field.mapData[upy][currentCharacter.x] == 7 or currentPlayer.field.mapData[downy][currentCharacter.x] == 7:
	return 'Chest'
    elif currentPlayer.field.mapData[currentCharacter.y][leftx] == 9 or currentPlayer.field.mapData[currentCharacter.y][rightx] == 9 or currentPlayer.field.mapData[upy][currentCharacter.x] == 9 or currentPlayer.field.mapData[downy][currentCharacter.x] == 9:
	return 'CecilSunkure'
    else:
	return None

def chest(currentPlayer, level=0):
    upy = currentPlayer.y - 1
    downy = currentPlayer.y + 1
    leftx = currentCharacter.x - 1
    rightx = currentCharacter.x + 1
    
    if currentPlayer.field.mapData[currentCharacter.y][leftx] == 7:
	chesty = currentCharacter.y
	chestx = leftx
    if currentPlayer.field.mapData[currentCharacter.y][rightx] == 7:
	chesty = currentCharacter.y
	chestx = rightx
    if currentPlayer.field.mapData[upy][currentCharacter.x] == 7:
	chesty = upy
	chestx = currentCharacter.x
    if currentPlayer.field.mapData[downy][currentCharacter.x] == 7:
	chesty = downy
	chestx = currentCharacter.x

    currentPlayer.field.mapData[chesty][chestx] = 8
    slot = findEmptyInventorySlot(currentPlayer)

    if slot != None:
	if level == 0 and random.randrange(0, 2) == 0:
	    currentPlayer.inventory[slot] = POTION
	    print('Acquired one potion from the chest! (press enter)')
	    b = raw_input()
	elif level == 0 and random.randrange(0, 2) == 1:
	    currentPlayer.inventory['Gold'] += 10
	    print('Acquired ten gold from the chest! (press enter)')
	    b = raw_input()
	return

    else:
	print('Your inventory is full! Cannot open chest. (press enter)')
	currentPlayer.field.mapData[chesty][chestx] == 7
	b = raw_input()
	return

#These lines set up the game by creating field, character, and item instances.    
GameField1 = gameField(MAP1)
GameField2 = gameField(MAP2)
DAGGER = item('Small Dagger', [1, 4], 'Small', 5)
SHORTSWORD = item('Short Sword', [1, 8], 'Small', 20)
MACE = item('Mace', [3, 10], 'Medium', 40)
HALBERD = item('Halberd', [1, 25], 'Large', 70)
METRONOME = item('Metronome', [-5, 15], 'Small', 5)
PITCHFORK_OF_HELL = item('Pitchfork of Hell', [5, 35], 'Medium', 100)
NULLIFIER = item('Nullifier', [10, 10], 'Large', 40)
POTION = item('Potion', [0, 0], '1 use', 5)
currentCharacter = character(3, 1, GameField1, DAGGER)


#Cheats! Uncomment these to activate them.
#currentCharacter.inventory['Gold'] = 50


while True:
    print(currentCharacter)
    char = raw_input()

    #Store character's current position.
    oldx = currentCharacter.x
    oldy = currentCharacter.y
    if char == 'q':
	spells(currentCharacter)
    if char == 'a':
	if currentCharacter.isValidMove(char):
	    currentCharacter.x -= 1
    if char == 'd':
	if currentCharacter.isValidMove(char):
	    currentCharacter.x += 1
    if char == 'w':
	if currentCharacter.isValidMove(char):
	    currentCharacter.y -= 1
    if char == 's':
	if currentCharacter.isValidMove(char):
	    currentCharacter.y += 1
    if char == 'e':
	#e is the action button.
	event = action(currentCharacter)
	if event == 'Store' and currentCharacter.field.mapData == MAP1:
	    shop(currentCharacter, 0)
	elif event == 'Store' and currentCharacter.field.mapData == MAP6:
	    shop(currentCharacter, 1)
	elif event == 'Chest':
	    chest(currentCharacter, 0)
	elif event == 'CecilSunkure':
	    print("Well, you've found CecilSunkure! This is all the game has so far. Yay!")
	    b = raw_input()
	elif event == 'Door' and currentCharacter.y == 2 and currentCharacter.x == 7 and MAP1 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP2
	    currentCharacter.x = 1
	    currentCharacter.y = 2
	elif event == 'Door' and currentCharacter.y == 2 and currentCharacter.x == 1 and MAP2 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP1
	    currentCharacter.x = 7
	    currentCharacter.y = 2
	elif event == 'Door' and currentCharacter.y == 4 and currentCharacter.x == 1 and MAP1 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP3
	    currentCharacter.x = 1
	    currentCharacter.y = 2

	    #Map 3
	elif event == 'Door' and currentCharacter.y == 2 and currentCharacter.x == 1 and MAP3 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP1
	    currentCharacter.x = 1
	    currentCharacter.y = 4
	elif event == 'Door' and currentCharacter.y == 4 and currentCharacter.x == 5 and MAP3 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP4
	    currentCharacter.x = 5
	    currentCharacter.y = 4

	    #Map 4
	elif event == 'Door' and currentCharacter.y == 4 and currentCharacter.x == 5 and MAP4 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP3
	    currentCharacter.x = 5
	    currentCharacter.y = 4
	elif event == 'Door' and currentCharacter.y == 1 and currentCharacter.x == 2 and MAP4 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP5
	    currentCharacter.x = 2
	    currentCharacter.y = 1
	elif event == 'Door' and currentCharacter.y == 3 and currentCharacter.x == 1 and MAP4 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP5
	    currentCharacter.x = 4
	    currentCharacter.y = 9

	    #Map 5
	elif event == 'Door' and currentCharacter.y == 1 and currentCharacter.x == 2 and MAP5 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP4
	    currentCharacter.x = 2
	    currentCharacter.y = 1
	elif event == 'Door' and currentCharacter.y == 9 and currentCharacter.x == 4 and MAP5 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP4
	    currentCharacter.x = 1
	    currentCharacter.y = 3
	elif event == 'Door' and currentCharacter.y == 10 and currentCharacter.x == 3 and MAP5 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP6
	    currentCharacter.x = 1
	    currentCharacter.y = 2
	elif event == 'Door' and currentCharacter.y == 5 and currentCharacter.x == 10 and MAP5 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP7
	    currentCharacter.x = 2
	    currentCharacter.y = 1
	elif event == 'Door' and currentCharacter.y == 4 and currentCharacter.x == 9 and MAP5 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP6
	    currentCharacter.x = 3
	    currentCharacter.y = 7
	elif event == 'Door' and currentCharacter.y == 6 and currentCharacter.x == 10 and MAP5 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP7
	    currentCharacter.x = 2
	    currentCharacter.y = 1

	    #Map 6
	elif event == 'Door' and currentCharacter.y == 2 and currentCharacter.x == 1 and MAP6 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP5
	    currentCharacter.x = 3
	    currentCharacter.y = 10
	elif event == 'Door' and currentCharacter.y == 7 and currentCharacter.x == 3 and MAP6 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP5
	    currentCharacter.x = 9
	    currentCharacter.y = 4

	    #Map 7
	elif event == 'Door' and currentCharacter.y == 1 and currentCharacter.x == 7 and MAP6 == currentCharacter.field.mapData:
	    currentCharacter.field.mapData = MAP9
	    currentCharacter.x = 2
	    currentCharacter.y = 1
	    
	else:
	    if event == None:
		print('Nothing interesting here.')
		b = raw_input()
	    else:
		print(str(event))
		b = raw_input()
		
    if char == 'i':
	inventory(currentCharacter)

    if char == 'x':
	print(str(currentCharacter.x) + ', ' + str(currentCharacter.y))

    #Check to see if the character moved.
    if (oldx != currentCharacter.x or oldy != currentCharacter.y) and char != 'e':
	diceRoll = random.randrange(0, 20) + 1
    else:
	diceRoll = 0

    #If dice roll passed (only if character moved last time step), then initiate battle.
    if diceRoll > 18 and diceRoll < 21:
	if currentCharacter.field.mapData == MAP1 or MAP2 or MAP3:
	    mV = 1
	elif currentCharacter.field.mapData == MAP4 or MAP5 or MAP6:
	    mV = 2
	elif currentCharacter.field.mapData == MAP7 or MAP8 or MAP9:
	    mV = 3
	else:
	    mV = 1
	battleValue = battle(currentCharacter, mV)
	battleTreasure(battleValue, currentCharacter)
	
	





