First of all let me introduce myself seeing that this is my first post here. Hello! My name is Zack!
Alright enough chit chat, let's get to work:
So I'm trying to make a simple text game in Python. If you guys feel like this code looks familiar, that's most likely because you've seen something similar to it before. I've only been using Python for about 4 days now and have been programming overall for less than a year, and I'm trying to modify this game I found in a tutorial into my own version. I'm very close (I think), but I can't quite get it to do something I want.
Background on the Game:
Well since it's a long code I figure I should firstly let you know what is going on. Basically, there's a game board and there's random "caves" throughout the game board. I drop "nodes" that tell the player how close a cave is. Now, in the tutorial there was only one type of "cave(actually in the tutorial he was using treasure)". But I want there to be different "caves" with different things in them. The problem that is occurring is, when I play the game it runs fine, but only one type of "cave" seems to be in the gameboard. I can't get it to include all the "caves"
- I know a lot of that probably sounds like jibberish, but I'm sure you'll be able to understand what I'm talking about when you read the code. So without further ado, here's my elite level coding skills

:
CODE
import random
import sys
import time
def drawBoard(board):
hline = ' '
for i in range(1,6):
hline += (' ' * 9) + str(i)
print hline
print ' ' + ('0123456789' * 6)
print
for i in range(15):
if i < 10:
extraSpace = ' '
else:
extraSpace = ''
print '%s%s %s %s' %(extraSpace, i, getRow(board, i), i )
print
print ' ' + ('0123456789' * 6)
print hline
def getRow(board, row):
boardRow = ''
for i in range(60):
boardRow += board[i][row]
return boardRow
def getNewBoard():
board = []
for x in range(60):
board.append([])
for y in range(15):
if random.randint(0,1) == 0:
board[x].append('`')
else:
board[x].append('^')
return board
def getRandomDragons(numDragons):
dragons = []
for i in range(numDragons):
dragons.append([random.randint(0,59), random.randint(0,14)])
return dragons
# could be wrong =======================
def getRandomTenCaves(numTenCaves):
tenCaves = []
for i in range(numTenCaves):
tenCaves.append([random.randint(0,59), random.randint(0,14)])
return tenCaves
# could be wrong =======================
def getRandomThreeCaves(numThreeCaves):
threeCaves = []
for i in range(numThreeCaves):
threeCaves.append([random.randint(0,59), random.randint(0,14)])
return threeCaves
# could be wrong =======================
def getRandomHolyGrail(numHolyGrails):
holyGrail = []
for i in range(numHolyGrails):
holyGrail.append([random.randint(0,59), random.randint(0,14)])
return holyGrail
def isValidMove(x,y):
return x >= 0 and x <= 59 and y >= 0 and y <= 14
def makeMove(board, dragons, tenCaves, threeCaves, holyGrail, x, y):
if not isValidMove(x,y):
return False
smallestDistance = 100
for dx, dy in dragons:
if abs(dx-x) > abs(dy-y):
distance = abs(dx-x)
else:
distance = abs(dy-y)
if distance < smallestDistance:
smallestDistance = distance
if smallestDistance == 0:
print 'You have found a Dragon, prepare for Battle!'
time.sleep(5)
liveOrDie = random.choice('1 2 3 4'.split())
if liveOrDie != '2':
dragons.remove([x,y])
return 'You beat the dragon and took his hide!'
else:
return 'The Dragon is stronger than you thought!'
return 'You died and lost a node! But the Dragon remains in the same spot.'
else:
if smallestDistance < 10:
board[x][y] = str(smallestDistance)
return 'Dragon\'s cave detected at a distance of %s miles from the Magical Node.' %(smallestDistance)
else:
board[x][y] = '|'
return 'Magical Node did not detect anything. All Dragons out of range.'
for tx, ty in tenCaves:
if abs(tx-x) > abs(ty-y):
distance = abs(tx-x)
else:
distance = abs(ty-y)
if distance < smallestDistance:
smallestDistance = distance
if smallestDistance == 0:
print 'You step into the cave.....'
time.sleep(2)
friendOrFoe = random.choice('1'.split())
if friendOrFoe == '1':
tenCaves.remove([x,y])
return 'This Cave doesn\'t have a Dragon in it. Instead you find 10 Magical Nodes!'
else:
return 'This code will never be seen by anyone!'
else:
if smallestDistance < 10:
board[x][y] = str(smallestDistance)
return 'Dragon\'s cave detected at a distance of %s miles from the Magical Node.' %(smallestDistance)
else:
board[x][y] = '|'
return 'Magical Node did not detect anything. All Dragons out of range.'
for thx, thy in threeCaves:
if abs(thx-x) > abs(thy-y):
distance = abs(thx-x)
else:
distance = abs(thy-y)
if distance < smallestDistance:
smallestDistance = distance
if smallestDistance == 0:
print 'You step into the cave....'
time.sleep(2)
friendOrFoe = random.choice('1'.split())
if friendOrFoe == '1':
threeCaves.remove([x,y])
return 'This Cave doesn\'t have a Dragon in it. Instead you find 3 Magical Nodes!'
else:
return 'This code will never be seen by anyone!'
else:
if smallestDistance < 10:
board[x][y] = str(smallestDistance)
return 'Dragon\'s cave detected at a distance of %s miles from the Magical Node.' %(smallestDistance)
else:
board[x][y] = '|'
return 'Magical Node did not detect anything. All Dragons out of range.'
for hx, hy in holyGrail:
if abs(hx-x) > abs(hy-y):
distance = abs(hx-x)
else:
distance = abs(hy - y)
if distance < smallestDistance:
smallestDistance = distance
if smallesDistance == 0:
print 'You step into the cave......'
time.sleep(2)
friendOrFoe = random.choice('1'.split())
if friendOrFoe == '1':
holyGrail.remove([x,y])
return 'This Cave doesn\'t have a Dragon in it. Instead you find the Holy Grail!'
else:
return 'This code will never be seen by anyone!'
else:
if smallestDistance < 10:
board[x][y] = str(smallestDistance)
return 'Dragon\'s cave detected at a distance of %s miles from the Magical Node.' %(smallestDistance)
else:
board[x][y] = '|'
return 'Magical Node did not detect anything. All Dragons out of range.'
def enterPlayerMove():
print 'Where do you want to drop the next Magical Node? (0-59 0-14) (or type quit)'
while True:
move = raw_input()
if move.lower() == 'quit':
print 'Thanks for playing!'
sys.exit()
move = move.split()
if len(move) == 2 and move[0].isdigit() and move[1].isdigit() and isValidMove(int(move[0]), int(move[1])):
return [int(move[0]), int(move[1])]
print 'Enter a number from 0 to 59, a space, then a number from 0 to 14.'
def playAgain():
print 'Do you want to play again? (yes or no)'
return raw_input().lower().startswith('y')
def showInstructions():
print '''Instructions:
You are a brave Knight from Camelot who was sent on a dangerous quest to rid
the world of three Dragons. You know the dragons are in the land of Camelot,
but not exactly sure where. Luckily for you, Merlin has given you 16 Magical
Nodes that are capable of telling you if a Dragon is near by.
To play, enter the coordinates of the point in Camelot you wish to drop a
Magical Node. The Node can find out how far away the closest Dragon is to it.
For example, the n below marks where the node was dropped, and the 2's
represent distances of 2 away from the node. The 4's represent
distances of 4 away from the node.
444444444
4 4
4 22222 4
4 2 2 4
4 2 n 2 4
4 2 2 4
4 22222 4
4 4
444444444
Press enter to continue...'''
raw_input()
print '''For example, here is a dragon (the d) located a distance of 2 away
from the magical node (the n):
22222
d 2
2 n 2
2 2
22222
The point where the node was dropped will be marked with a 2.
The Dragons stay in their caves, so you don't have to worry abou them moving
around. Magical Nodes can detect Dragons up to a distance of 9 miles. If all
Dragons are out of range, the point will be marked with |
If a node is directly dropped on a cave that has a Dragon in it, you have
discovered the location of the Dragon, and you will have to fight it. Once
you defeat the Dragon, you will collect his hide for proof and the Magical
Node will remain there.
Press enter to continue...'''
raw_input()
print
print 'D R A G O N ~ H U N T !'
print
print 'Would you like to view the instructions? (yes/no)'
if raw_input().lower().startswith('y'):
showInstructions()
while True:
magicalNodes = 16
theBoard = getNewBoard()
theDragons = getRandomDragons(3)
drawBoard(theBoard)
previousMoves = []
#could be wrong===============
tenNodes = getRandomTenCaves(20)
threeNodes = getRandomThreeCaves(20)
grail = getRandomHolyGrail(10)
while magicalNodes > 0:
if magicalNodes > 1: extraSnode = 's'
else: extraSnode = ''
if len(theDragons) > 1: extraSdrag = 's'
else: extraSdrag = ''
print 'You have %s Magical Node%s left. %s Dragon%s remaining.' %(magicalNodes, extraSnode, len(theDragons), extraSdrag)
x, y = enterPlayerMove()
previousMoves.append([x,y])
moveResult = makeMove(theBoard, theDragons, tenNodes, threeNodes, grail, x, y)
if moveResult == False:
continue
elif moveResult == 'You have found a Dragon, prepare for Battle!':
for x, y in previousMoves:
makeMove(theBoard, theDragons, tenNodes, threeNodes, grail, x, y)
drawBoard(theBoard)
print moveResult
if moveResult == 'The Dragon is stronger than you thought!':
magicalNodes -= 1
time.sleep(5)
print 'You died and lost a node!'
time.sleep(3)
print 'Merlin revived you.'
time.sleep(3)
print 'The Dragon remains in the same spot.'
elif moveResult == 'You step into the cave.....':
for x, y in previousMoves:
makeMove(theBoard, theDragons, tenNodes, threeNodes, grail, x, y)
drawBoard(theBoard)
print moveResult
if moveResult == 'This Cave doesn\'t have a Dragon in it. Instead you find 10 Magical Nodes!':
magicalNodes += 10
time.sleep(5)
print 'You now have 10 more nodes to help you in your search!'
elif moveResult == 'You step into the cave....':
for x, y in previousMoves:
makeMove(theBoard, theDragons, tenNodes, threeNodes, grail, x, y)
drawBoard(theBoard)
print moveResult
if moveResult == 'This Cave doesn\'t have a Dragon in it. Instead you find 3 Magical Nodes!':
magicalNodes += 3
time.sleep(5)
print 'You now have 3 more nodes to help you in your search!'
else:
if moveResult == 'You step into the cave......':
for x, y in previousMoves:
makeMove(theBoard, theDragons, tenNodes, threeNodes, grail, x, y)
drawBoard(theBoard)
print moveResult
if moveResult == 'This Cave doesn\'t have a Dragon in it. Instead you find the Holy Grail!':
print 'You pick up the glass......'
time.sleep(5)
print 'You feel weightless for a second, and then all you see is a flash of white!'
time.sleep(5)
print 'The Holy Grail teleported you to the Cave of the King Dragon!'
if len(theDragons) == 0:
print 'You found and killed all the Dragons! Congratulations and good game!'
break
magicalNodes -= 1
if magicalNodes == 0:
print 'We\'ve run out of Magical Nodes! You now must report your'
print 'misfortune to King Arthur!'
print 'He will not be happy. It\'ll probably be off with your head!'
print 'GAME OVER.'
print ' The remaining Dragons were here:'
for x, y in theDragons:
print ' %s, %s' %(x,y)
if not playAgain():
sys.exit()
I'm pretty sure the problem is somewhere in here:
CODE
def makeMove(board, dragons, tenCaves, threeCaves, holyGrail, x, y):
if not isValidMove(x,y):
return False
smallestDistance = 100
for dx, dy in dragons:
if abs(dx-x) > abs(dy-y):
distance = abs(dx-x)
else:
distance = abs(dy-y)
if distance < smallestDistance:
smallestDistance = distance
if smallestDistance == 0:
print 'You have found a Dragon, prepare for Battle!'
time.sleep(5)
liveOrDie = random.choice('1 2 3 4'.split())
if liveOrDie != '2':
dragons.remove([x,y])
return 'You beat the dragon and took his hide!'
else:
return 'The Dragon is stronger than you thought!'
return 'You died and lost a node! But the Dragon remains in the same spot.'
else:
if smallestDistance < 10:
board[x][y] = str(smallestDistance)
return 'Dragon\'s cave detected at a distance of %s miles from the Magical Node.' %(smallestDistance)
else:
board[x][y] = '|'
return 'Magical Node did not detect anything. All Dragons out of range.'
for tx, ty in tenCaves:
if abs(tx-x) > abs(ty-y):
distance = abs(tx-x)
else:
distance = abs(ty-y)
if distance < smallestDistance:
smallestDistance = distance
if smallestDistance == 0:
print 'You step into the cave.....'
time.sleep(2)
friendOrFoe = random.choice('1'.split())
if friendOrFoe == '1':
tenCaves.remove([x,y])
return 'This Cave doesn\'t have a Dragon in it. Instead you find 10 Magical Nodes!'
else:
return 'This code will never be seen by anyone!'
else:
if smallestDistance < 10:
board[x][y] = str(smallestDistance)
return 'Dragon\'s cave detected at a distance of %s miles from the Magical Node.' %(smallestDistance)
else:
board[x][y] = '|'
return 'Magical Node did not detect anything. All Dragons out of range.'
for thx, thy in threeCaves:
if abs(thx-x) > abs(thy-y):
distance = abs(thx-x)
else:
distance = abs(thy-y)
if distance < smallestDistance:
smallestDistance = distance
if smallestDistance == 0:
print 'You step into the cave....'
time.sleep(2)
friendOrFoe = random.choice('1'.split())
if friendOrFoe == '1':
threeCaves.remove([x,y])
return 'This Cave doesn\'t have a Dragon in it. Instead you find 3 Magical Nodes!'
else:
return 'This code will never be seen by anyone!'
else:
if smallestDistance < 10:
board[x][y] = str(smallestDistance)
return 'Dragon\'s cave detected at a distance of %s miles from the Magical Node.' %(smallestDistance)
else:
board[x][y] = '|'
return 'Magical Node did not detect anything. All Dragons out of range.'
for hx, hy in holyGrail:
if abs(hx-x) > abs(hy-y):
distance = abs(hx-x)
else:
distance = abs(hy - y)
if distance < smallestDistance:
smallestDistance = distance
if smallesDistance == 0:
print 'You step into the cave......'
time.sleep(2)
friendOrFoe = random.choice('1'.split())
if friendOrFoe == '1':
holyGrail.remove([x,y])
return 'This Cave doesn\'t have a Dragon in it. Instead you find the Holy Grail!'
else:
return 'This code will never be seen by anyone!'
else:
if smallestDistance < 10:
board[x][y] = str(smallestDistance)
return 'Dragon\'s cave detected at a distance of %s miles from the Magical Node.' %(smallestDistance)
else:
board[x][y] = '|'
return 'Magical Node did not detect anything. All Dragons out of range.'
Or in the part underneath the "instructions".
If you guys could find the problem it would be much appreciated! Like I said, I'm a new programmer and even newer to Python, and this is my very first game.
#Thanks!
^haha... programming joke.. get it?!
This post has been edited by eZACKe: 1 Jun, 2009 - 08:04 PM