'I made a game and I would like to know if the game will end when won
I made a game where once the game board doesnt have any numbers left, the player wins. This is a dice game so it is random. I cant test a win because of that. Would my code end if there is no numbers left on the board. If the board has no numbers it should print 'You Win'
import random
Board = set(range(1, 11))
def displayBoard(Board):
for n in Board:
print(n, end='|')
print()
def playgame():
print('Welcome! Here is your board, let\'s get started.')
displayBoard(Board)
while True:
print('To begin, roll the pair of dice by typing \'r\' then press Enter''.')
rollOne = input()
if rollOne == 'r':
diceRoll = random.randint(1, 6) + random.randint(1,6)
print('The sum of your two rolls is ' + str(diceRoll) + '. Now choose which \
numbers to remove from the board.')
print('Here\'s the board again, you can type a single number, or multiple \
numbers separated by a commma to remove them from the board. Then press Enter.')
displayBoard(Board)
newBoard = input()
if newBoard == 'n':
print('Game Over! You Lose!')
break
numbers = [int(i) for i in newBoard.split(',')]
if sum(numbers) != diceRoll:
print("Your numbers don't add up to " + diceRoll)
else:
for n in numbers:
if n not in Board:
print(n + " is not in board.")
return
for i in numbers:
Board.discard(i)
print("Your new board is: ")
displayBoard(Board)
if len(Board) == 0:
print('You Win')
return
if __name__ == "__main__":
playgame()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
