Python Forum
Python not correctly choosing numbers for my game.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python not correctly choosing numbers for my game.
#1
So there is this game, the name of it starts with an S, I don't know how to spell it. The game is a number game where you have so many rows and columns of numbers. Each column and row only has one of each number, and you have to solve it by filling in all the numbers properly. I am trying to re-create that game but I just can't get the number choosing part to work right. In my game there are buttons. Each button is on a column and row 1-9. I need the program to choose numbers for each of the buttons while making sure each column and row has only one of each number 1-9. I can't seem to get this to work properly. I am able to get the program to choose numbers for each button, but there are some numbers that are getting numbers that have already been taken by another button in that column or row. The way I am attempting to do this is by giving each button a column and row and placing them into a list. The program then uses a for loop to get each button then another for loop to check for all the buttons in the selected buttion's column and row. It then gets the value of the buttons and creates a list of available options. Then I have it select a random number from the list to give to the selected button and it will then reset the list and move on to the next button. But some how it does not seem to be working properly. Idon't know what to do. I have created some code that allows me to check if the buttons are in their proper columns and rows and they are all placed properly, so it does not look to be a problem with placement the choosing code just does not seem to be working properly.

def Choose(self):
        #choose numbers.
        for b in self.nb:
            self.n1 = True#n1-n9 are used to create the list of possible numbers.
            self.n2 = True
            self.n3 = True
            self.n4 = True
            self.n5 = True
            self.n6 = True
            self.n7 = True
            self.n8 = True
            self.n9 = True
            nlist = []#list used to get possible numbers.
            n = 0 #used to get how many numbers there are to choose from. If only one, the button gets what ever number is in the list.
            if b.value == 0:
                for i in self.nb:
                    if i.r == b.r:
                        self.Clist(i)#Clist changes n1-n9 to false if number is already in column or row.
                    if i.c == b.c:
                        self.Clist(i)
                if self.n1 == True:#Gets what numbers are true and adds then to nlist.
                    nlist.append('1')
                if self.n2 == True:
                    nlist.append('2')
                if self.n3 == True:
                    nlist.append('3')
                if self.n4 == True:
                    nlist.append('4')
                if self.n5 == True:
                    nlist.append('5')
                if self.n6 == True:
                    nlist.append('6')
                if self.n7 == True:
                    nlist.append('7')
                if self.n8 == True:
                    nlist.append('8')
                if self.n9 == True:
                    nlist.append('9')
                for item in nlist:#checks how many items are in nlist.
                    n += 1
                if n == 1:
                    N = nlist[0]
                if n > 1:
                    N = random.choice(nlist)
                if N == '1':#Get number and set the value of the current button to the number.
                    b.value = 1
                if N == '2':
                    b.value = 2
                if N == '3':
                    b.value = 3
                if N == '4':
                    b.value = 4
                if N == '5':
                    b.value = 5
                if N == '6':
                    b.value = 6
                if N == '7':
                    b.value = 7
                if N == '8':
                    b.value = 8
                if N == '9':
                    b.value = 9
def Clist(self,i):
        if i.value == 1:
            self.n1 = False
        if i.value == 2:
            self.n2 = False
        if i.value == 3:
            self.n3 = False
        if i.value == 4:
            self.n4 = False
        if i.value == 5:
            self.n5 = False
        if i.value == 6:
            self.n6 = False
        if i.value == 7:
            self.n7 = False
        if i.value == 8:
            self.n8 = False
        if i.value == 9:
            self.n9 = False


I just don't understand what is going on. I can't see anything wrong. Does anyone have any ideas of what I should to to make this work?
Reply


Messages In This Thread
Python not correctly choosing numbers for my game. - by LavaCreeperKing - Mar-28-2018, 07:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  From python game in terminal to a website game using bottle Njanez 0 3,894 Aug-13-2021, 01:11 PM
Last Post: Njanez

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020