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.
#8
You going have to go deeper into math to create a full puzzle or create them manually.

example a test. Haven't block 3x3 match within blocks yet.
The zeros had no choices to pick from.
import sys
import random
from PyQt4 import QtCore, QtGui

class Main(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.resize(519, 594)
        self.setMouseTracking(True)
        self.font = QtGui.QFont()
        self.font.setPointSize(22)
        self.make_labels()
        self.create()

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_N:
            self.create()

    def make_labels(self):
        self.labels = []
        self.values = []
        for y in range(9):
            l_row = []
            v_row = []
            yy = 50 * y + 30 + int(y / 3) * 10
            for x in range(9):
                label = QtGui.QLabel(self)
                xx = 50 * x + 30 + int(x / 3) * 10
                label.setGeometry(QtCore.QRect(xx, yy, 25, 25))
                label.setFont(self.font)
                label.setText('N')
                l_row.append(label)
                v_row.append(0)

            self.labels.append(l_row)
            self.values.append(v_row)

    def create(self):
        possible_across = []
        possible_down = []
        for y in range(9):
            possible_across.append([True for i in range(10)])
            possible_down.append([True for i in range(10)])

        for y in range(9):
            for x in range(9):
                rlist = []
                for n in range(1, 10):
                    if possible_across[y][n] and possible_down[x][n]:
                        rlist.append(n)

                if len(rlist) > 1:
                    rnd = random.choice(rlist)
                else:
                    try:
                        rnd = rlist[0]
                    except:
                        rnd = 0

                possible_across[y][rnd] = False
                possible_down[x][rnd] = False
                self.values[y][x] = rnd
                self.labels[y][x].setText(str(rnd))

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = Main()
    win.show()
    app.exec_()

example a test. Added block checks. Zeros appear more often.
import sys
import random
from PyQt4 import QtCore, QtGui

class Main(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.resize(519, 594)
        self.setMouseTracking(True)
        self.font = QtGui.QFont()
        self.font.setPointSize(22)
        self.make_labels()
        self.create()

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_N:
            self.create()

    def make_labels(self):
        self.labels = []
        self.values = []
        for y in range(9):
            l_row = []
            v_row = []
            yy = 50 * y + 30 + int(y / 3) * 10
            for x in range(9):
                label = QtGui.QLabel(self)
                xx = 50 * x + 30 + int(x / 3) * 10
                label.setGeometry(QtCore.QRect(xx, yy, 25, 25))
                label.setFont(self.font)
                label.setText('N')
                l_row.append(label)
                v_row.append(0)

            self.labels.append(l_row)
            self.values.append(v_row)

    def create(self):
        possible_across = []
        possible_block = []
        possible_down = []
        for y in range(9):
            possible_across.append([True for i in range(10)])
            possible_block.append([True for i in range(10)])
            possible_down.append([True for i in range(10)])

        for y in range(9):
            for x in range(9):
                rlist = []
                zone = int(y / 3) * 3 + int(x / 3)
                for n in range(1, 10):
                    if possible_across[y][n] and possible_down[x][n] and possible_block[zone][n]:
                        rlist.append(n)

                if len(rlist) > 1:
                    rnd = random.choice(rlist)
                else:
                    try:
                        rnd = rlist[0]
                    except:
                        rnd = 0

                possible_block[zone][rnd] = False
                possible_across[y][rnd] = False
                possible_down[x][rnd] = False
                self.values[y][x] = rnd
                self.labels[y][x].setText(str(rnd))

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    win = Main()
    win.show()
    app.exec_()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
RE: Python not correctly choosing numbers for my game. - by Windspar - Mar-30-2018, 08:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  From python game in terminal to a website game using bottle Njanez 0 4,526 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