Need help for assignment

This is the place for queries that don't fit in any of the other categories.

Need help for assignment

Postby chennyowww » Sun Sep 25, 2016 3:58 am

Hello everyone. I just need a little help from you with my assignment. For this assignment, I have to create a 2D Minesweeper. I am using Python 3. The problem is during the third try (last try), when I try to hit the mine, all of the printed output is '!' instead of printing another signs which are '@', 'X' and 'O'. It should only print one '!' on the mine that I hit and the rest should be 'O'.

Code: Select all
import random

#Ask the user whether they want to random or not
response = input("Do you want a random mine location?")
if response.lower() == 'yes':
       mineRow = random.randint(0, 4)
    mineCol = random.randint(0, 4)
else:
    mineRow = int(input("Which row do you want to put the mine? (Please input an integer between 0 and 4)"))
    mineCol = int(input("Which column do you want to put the mine? (Please input an integer between 0 and 4)"))

print("")

#Location of the mine
print("Location of The Mine")
for i in range(5):
    for m in range(5):
        if (m == mineCol and i == mineRow):
            print("X ", end = '')
        else:
            print("O ", end = '')
    print()

print()
#First guess
print("Try 1:")
guessRow = int(input("Guess a ROW number between 0 and 4:"))
guessCol = int(input("Guess a COLUMN number between 0 and 4:"))

done = 1 # Indicates that the user guesses once

#Print the map
for j in range(5):
    for n in range(5):
        if guessRow == mineRow and guessCol == mineCol:
            print("! ", end = '')
        elif n == guessCol and j == guessRow:
            print("@ ", end = '')
        elif n == mineCol and j == mineRow:
            print("X ", end = '')
        else:
            print("O ", end = '')
    print()
print()

#Second and third guess
while done < 3 and ((guessRow != mineRow and guessCol != mineCol) or (guessRow == mineRow and guessCol!= mineCol) or (guessRow != mineRow and guessCol == mineCol)):
    print("Congratulations. You missed.")
    print()
    done += 1
    print()
    print("Try", done, ":")
    guessRow = int(input("Guess the Row number again:"))
    guessCol = int(input("Guess the Column number again"))
    for k in range(5):
        for o in range(5):
            if guessRow == mineRow and guessCol == mineCol:
                print("! ", end='')
            elif o == guessCol and k == guessRow :
                print("@ ", end='')
            elif o == mineCol and k == mineRow:
                print("X ", end='')
            else:
                print("O ", end='')
        print()
if done <= 3 and (guessRow == mineRow and guessCol == mineCol):
    print("You hit the mine! Game over")
if done == 3 and ((guessRow != mineRow and guessCol == mineCol) or (guessRow == mineRow and guessCol != mineCol) or (guessRow != mineRow and guessCol != mineCol)):
    print("You win!")
Last edited by Yoriz on Sun Sep 25, 2016 8:39 am, edited 2 times in total.
Reason: Initial post lock. Formating.
chennyowww
 
Posts: 1
Joined: Sun Sep 25, 2016 3:46 am

Re: Need help for assignment

Postby Yoriz » Sun Sep 25, 2016 8:32 am

The first if statement that the codes come to in the for loops of the guesses
Code: Select all
if guessRow == mineRow and guessCol == mineCol:

will always be true and therefore always print '!'
because it is only checking if the guess and the actual row & col are the same but does not check if the j and n or K & o values are also the same row & col as the actual.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Yoriz
 
Posts: 1672
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests