Я играю в поиск слов с помощью «python» и «kivy».Я сделал функцию PlotName (LabelObj, word, direction = 0, plot = -1).Эта функция создает слово в сетке слов 10x10 в произвольном месте.
Ex.PlotName (LabelObg, 'ORANGE', 1) -LabelObg - объект метки kivy для графиков сетки 10x10.направление = 0 = горизонтальное, 1 = вертикальное, 2 = перекрестное, 3 = создание перекрестного слова.
Когда ABC и AXY-подобные слова пытались поместить в одни и те же графики, эта функция сначала проверяет и исправляет его.
Но иногда у меня возникают проблемы, как показано на картинке ниже.
![enter image description here](https://i.stack.imgur.com/GZXo0.png)
Новое слово перезаписывает предыдущее слово.
Я не знаюНе знаю, почему у меня возникает эта проблема. (Я думаю, что это проблема вложенных циклов.)
Здесь моя полная игра.https://drive.google.com/open?id=1m0W0KDsvmKcTg68LHfVsFS4P_m2d-jn_
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
import random
def PlotName(LabelObj,word,direction=0,plot=-1):
word = word
direction = direction # 0=horizontal , 1=vertical , 2=crossup , 3=crossdown
if(plot!=-1):Rand=plot
else:Rand = random.randint(0 ,99)
newRand=Rand
#Checking of previous occupied places
while (True and WordBox.Occupied_plots!=[]):
bool=True
if (direction == 0):
while True:
if ((10 - int(str(Rand / 10).split('.')[1])) >= len(word)):
for i in word:
if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
bool = False
Rand += 1
break
elif ((10 - int(str((99 - Rand) / 10).split('.')[1])) >= len(word)):
for i in word:
if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
bool = False
Rand -= 1
break
Rand = random.randint(0 ,99)
if (direction == 1):
while True:
if ((10 - int(Rand / 10)) >= len(word)):
for i in word:
if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
bool = False
Rand += 10
break
elif ((10 - int((99 - Rand) / 10)) >= len(word)):
for i in word:
if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
bool = False
Rand -= 10
break
Rand = random.randint(0, 99)
if (direction == 2):
while True:
if ((10 - int(Rand / 10)) >= len(word)) and ((10 - int(str(Rand / 10).split('.')[1])) >= len(word)):
for i in word:
if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
bool = False
Rand += 10
Rand += 1
break
elif ((10 - int((99 - Rand) / 10)) >= len(word)) and (
(10 - int(str((99 - Rand) / 10).split('.')[1])) >= len(word)):
for i in word:
if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
bool = False
Rand -= 10
Rand -= 1
break
Rand = random.randint(0, 99)
if (direction == 3):
while True:
if ((int(Rand / 10)) >= len(word)) and ((10 - int(str(Rand / 10).split('.')[1])) >= len(word)):
for i in word:
if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
bool = False
Rand -= 10
Rand += 1
break
elif ((int((99 - Rand) / 10)) >= len(word)) and (
(10 - int(str((99 - Rand) / 10).split('.')[1])) >= len(word)):
for i in word:
if (LabelObj[Rand].text!=i and LabelObj[Rand].text!='%'):
bool = False
Rand += 10
Rand -= 1
break
Rand = random.randint(0, 99)
if(bool):
break
else:
Rand = random.randint(0 ,99)
newRand=Rand
#After Checking -- Put Word --
Rand=newRand
if (direction == 0):
while True:
if ((10 - int(str(Rand / 10).split('.')[1])) >= len(word)):
for i in word:
LabelObj[Rand].text = i
LabelObj[Rand].color = (1, 0, 0, 1)
WordBox.Occupied_plots.append(Rand)
Rand += 1
break
elif ((10 - int(str((99 - Rand) / 10).split('.')[1])) >= len(word)):
for i in word:
LabelObj[Rand].text = i
LabelObj[Rand].color = (1, 0, 0, 1)
WordBox.Occupied_plots.append(Rand)
Rand -= 1
break
Rand = random.randint(0 ,99)
if (direction == 1):
while True:
if ((10 - int(Rand / 10)) >= len(word)):
for i in word:
LabelObj[Rand].text = i
LabelObj[Rand].color = (1, 0, 0, 1)
WordBox.Occupied_plots.append(Rand)
Rand += 10
break
elif ((10 - int((99 - Rand) / 10)) >= len(word)):
for i in word:
LabelObj[Rand].text = i
LabelObj[Rand].color = (1, 0, 0, 1)
WordBox.Occupied_plots.append(Rand)
Rand -= 10
break
Rand = random.randint(0 ,99)
if (direction == 2):
while True:
if ((10 - int(Rand / 10)) >= len(word)) and ((10 - int(str(Rand / 10).split('.')[1])) >= len(word)):
for i in word:
LabelObj[Rand].text = i
LabelObj[Rand].color = (1, 0, 0, 1)
WordBox.Occupied_plots.append(Rand)
Rand += 10
Rand += 1
break
elif ((10 - int((99 - Rand) / 10)) >= len(word)) and (
(10 - int(str((99 - Rand) / 10).split('.')[1])) >= len(word)):
for i in word:
LabelObj[Rand].text = i
LabelObj[Rand].color = (1, 0, 0, 1)
WordBox.Occupied_plots.append(Rand)
Rand -= 10
Rand -= 1
break
Rand = random.randint(0 ,99)
if (direction == 3):
while True:
if ((int(Rand / 10)) >= len(word)) and ((10 - int(str(Rand / 10).split('.')[1])) >= len(word)):
for i in word:
LabelObj[Rand].text = i
LabelObj[Rand].color = (1, 0, 0, 1)
WordBox.Occupied_plots.append(Rand)
Rand -= 10
Rand += 1
break
elif ((int((99 - Rand) / 10)) >= len(word)) and (
(10 - int(str((99 - Rand) / 10).split('.')[1])) >= len(word)):
for i in word:
LabelObj[Rand].text = i
LabelObj[Rand].color = (1, 0, 0, 1)
WordBox.Occupied_plots.append(Rand)
Rand += 10
Rand -= 1
break
Rand = random.randint(0 ,99)
class MainScreen(Screen):
def switch(self,direction,screen):
self.manager.current=screen
class GameScreen(Screen):
pass
class Manager(ScreenManager):
pass
class RootWid(FloatLayout):
player='player'
def inpChange1(self,x):
if(x.text=='player'):
x.text=''
elif(x.text==''):
x.text='player'
def ok_click(self):
self.parent.switch('left','game_scr')
RootWid.player=self.ids['inp1']
class RootWid2(FloatLayout):
pass
class WordBox(FloatLayout):
Myself=object
Occupied_plots=[]
def myself(self):
WordBox.Myself=self
original_Dictionary = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
LabelObj=[]
y=0
x=0
for i in range(100):
if(x>0.99):
x=0
y+=0.1
Latter=random.randint(0,25)
labelobj=Label(text='%',pos_hint={'x':x,'y':y},size_hint=(0.1,0.1))
x+=0.1
self.add_widget(labelobj)
LabelObj.append(labelobj)
PlotName(LabelObj,'BANANA',0,13)
PlotName(LabelObj,'ORANGE',3,17)
return ''
class NameBox(FloatLayout):
names=['','','','','','','','','','']
Myself=object
def myself(self):
NameBox.Myself=self
return ''
@staticmethod
def get_names():
for i in range(10):
NameBox.names[i]=NameBox.Myself.ids['lbl_'+str(i+1)].text
return NameBox.names
@staticmethod
def set_names(name,index=1):
NameBox.Myself.ids['lbl_'+str(index)].text=name
class MyCanvas(FloatLayout):
pass
class Credit(FloatLayout):
pass
class Mygame(App):
def build(self):
return Manager()
Mygame().run()