Created
September 28, 2019 01:25
-
-
Save cmikaiti/296229033a59e1ab59a1819b668f3788 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Project.py | |
| import os, sys | |
| import random | |
| import kivy | |
| from kivy.app import App | |
| from kivy.uix.gridlayout import GridLayout | |
| from kivy.uix.button import Button | |
| from kivy.uix.label import Label | |
| from kivy.uix.screenmanager import ScreenManager, Screen | |
| from kivy.lang import Builder | |
| from kivy.factory import Factory | |
| kv = """ | |
| <SolverLayout>: | |
| rows: 2 | |
| cols: 2 | |
| padding: 10 | |
| <StoryLayout>: | |
| cols: 1 | |
| <MenuLayout>: | |
| padding: 10 | |
| <MenuButton>: | |
| text: 'Menu' | |
| on_press: root.buttonpressed() | |
| <HintButton>: | |
| text: 'Hint' | |
| #padding: 10 | |
| on_press: root.buttonpressed() | |
| <TopRowLayout>: | |
| rows: 1 | |
| <TopRowSubLayout>: | |
| <-TopRowButton>: | |
| state_image: self.background_normal if self.state == 'normal' else self.background_down | |
| disabled_image: self.background_disabled_normal if self.state == 'normal' else self.background_disabled_down | |
| canvas: | |
| Color: | |
| rgba: self.background_color | |
| BorderImage: | |
| border: self.border | |
| pos: self.pos | |
| size: self.size | |
| source: self.disabled_image if self.disabled else self.state_image | |
| PushMatrix: | |
| Rotate: | |
| origin: self.center | |
| angle: 90 | |
| Color: | |
| rgba: 1, 1, 1, 1 | |
| Rectangle: | |
| texture: self.texture | |
| size: self.texture_size | |
| pos: int(self.center_x - self.texture_size[0] / 2.), int(self.center_y - self.texture_size[1] / 2.) | |
| PopMatrix: | |
| <SideColumnLayout>: | |
| cols: 1 | |
| <SideColumnSubLayout>: | |
| <SideColumnButton>: | |
| #text: 'Side Column Items' | |
| <MainGrid>: | |
| #rows: 40 | |
| # cols: 40 | |
| <SubGrid>: | |
| <SubGridButton>: | |
| text: '' | |
| checkedstate: 0 | |
| #background_normal: '' | |
| #background_color: (1,.3,.4,.85) | |
| on_press: root.buttonpressed() | |
| <BlankGrid>: | |
| <BlankSquare>: | |
| """ | |
| black = (0, 0, 0) | |
| white = (255, 255, 255) | |
| green = (34, 18, 0, 0) | |
| blue = (0, 0, 128) | |
| background = (255, 228, 181) | |
| checkedcolor = green | |
| currentcolor = blue | |
| NumofItems = 4 | |
| NumofCategories = 5 | |
| People = ["Brit", "Swede", "Dane", "Norwegian", "German"][:NumofItems] | |
| Places = ["Red", "Green", "Yellow", "White", "Blue"][:NumofItems] | |
| Weapons = ["Dogs", "Birds", "Cats", "Fish", "Lizards"][:NumofItems] | |
| Motives = ["1", "2", "3", "4", "5"][:NumofItems] | |
| Food = ["Tea", "Coffee", "Milk", "Beer", "Water"][:NumofItems] | |
| Alibis = ["Pall Mall", "Dunhill", "Blends", "Bluemasters", "Cigars"][:NumofItems] | |
| AllItems = [People, Places, Weapons, Motives, Food, Alibis][:NumofCategories] | |
| TopRow = [] | |
| SideColumn = [] | |
| GridList = [] | |
| for i in range(len(AllItems)-1): #1, 2, 3, 4, ..., N-1 | |
| TopRow.append(AllItems[i]) | |
| for i in range(-1, -len(AllItems), -1): #N, N-1, N-2, ..., 2 | |
| SideColumn.append(AllItems[i]) | |
| TopRowCombined = sum(TopRow, []) #Collapses the list. | |
| SideColumnCombined = sum(SideColumn, []) #Collapses the list. | |
| # class screenmanager(ScreenManager): | |
| # def __init__(self, **kwargs): | |
| class MenuLayout(GridLayout): | |
| def __init__(self, **kwargs): | |
| GridLayout.__init__(self, **kwargs) | |
| self.padding=5 | |
| self.add_widget(MenuButton()) | |
| class MenuButton(Button): | |
| def __init__(self, **kwargs): | |
| Button.__init__(self, **kwargs) | |
| def buttonpressed(self): | |
| print("OK") | |
| screenmanager.current = "story" | |
| class HintButton(Button): | |
| def __init__(self, **kwargs): | |
| Button.__init__(self, **kwargs) | |
| def buttonpressed(self): | |
| print("Pressed") | |
| for i in range(len(GridList)): | |
| if GridList[i].checkedstate == 2: | |
| for j in range(len(GridList)): # Crosses out the other X's in the box. | |
| if GridList[i].CoTop == GridList[j].CoTop: # Crosses out vertical. | |
| for k in range(len(SideColumn)): | |
| if {GridList[i].CoSide, GridList[j].CoSide}.issubset(set(SideColumn[k])): | |
| if GridList[j].checkedstate == 0: | |
| GridList[j].checkedstate = 1 | |
| #GridList[j].background_normal = '' | |
| GridList[j].background_color = green | |
| GridList[j].text = 'X' | |
| elif GridList[i].CoSide == GridList[j].CoSide: # Crosses out horizontal. | |
| for k in range(len(TopRow)): | |
| if {GridList[i].CoTop, GridList[j].CoTop}.issubset(set(TopRow[k])): | |
| if GridList[j].checkedstate == 0: | |
| GridList[j].checkedstate = 1 | |
| #GridList[j].background_normal = '' | |
| GridList[j].background_color = green | |
| GridList[j].text = 'X' | |
| for j in range(len(GridList)): # i is current grid, j is all other grids. | |
| if GridList[j].checkedstate == 2: # Find other grids that have an O. | |
| oddcouple = {GridList[i].CoTop, GridList[i].CoSide} ^ {GridList[j].CoTop, GridList[j].CoSide} # Find only unique values between the 2 lists. | |
| for k in range(len(GridList)): | |
| if set(oddcouple) == {GridList[k].CoTop, GridList[k].CoSide}: # Find the grid that matches the remaining pair. | |
| if GridList[k].checkedstate == 0: | |
| GridList[k].checkedstate = 2 | |
| #GridList[k].background_normal = '' | |
| GridList[k].background_color = green | |
| GridList[k].text = 'O' | |
| elif GridList[j].checkedstate == 1: | |
| oddcouple = {GridList[i].CoTop, GridList[i].CoSide} ^ {GridList[j].CoTop, GridList[j].CoSide} # Find only unique values between the 2 lists. | |
| for k in range(len(GridList)): | |
| if set(oddcouple) == {GridList[k].CoTop, GridList[k].CoSide}: | |
| if GridList[k].checkedstate == 0: | |
| GridList[k].checkedstate = 1 | |
| #GridList[k].background_normal = '' | |
| GridList[k].background_color = green | |
| GridList[k].text = 'X' | |
| class TopRowLayout(GridLayout): | |
| def __init__(self, **kwargs): | |
| GridLayout.__init__(self, **kwargs) | |
| for i in range(len(TopRow)): | |
| self.toprowsublayout = TopRowSubLayout(RowCount=len(TopRow[0])*i, cols = len(TopRow[0])) | |
| self.add_widget(self.toprowsublayout) | |
| class TopRowSubLayout(GridLayout): | |
| def __init__(self, RowCount, **kwargs): | |
| GridLayout.__init__(self, **kwargs) | |
| self.padding = 5 | |
| self.rowcount = RowCount | |
| for i in range(len(TopRow[0])): | |
| self.add_widget(TopRowButton(text=TopRowCombined[i+self.rowcount])) | |
| class TopRowButton(Button): | |
| def __init__(self, **kwargs): | |
| Button.__init__(self, **kwargs) | |
| class SideColumnLayout(GridLayout): | |
| def __init__(self, **kwargs): | |
| GridLayout.__init__(self, **kwargs) | |
| for i in range(len(SideColumn)): | |
| self.sidecolumnsublayout = SideColumnSubLayout(ColCount=len(SideColumn[0])*i, rows = len(SideColumn[0])) | |
| self.add_widget(self.sidecolumnsublayout) | |
| class SideColumnSubLayout(GridLayout): | |
| def __init__(self, ColCount, **kwargs): | |
| GridLayout.__init__(self, **kwargs) | |
| self.padding = 5 | |
| self.colcount = ColCount | |
| for i in range(len(SideColumn[0])): | |
| self.add_widget(SideColumnButton(text=SideColumnCombined[i+self.colcount])) | |
| class SideColumnButton(Button): | |
| def __init__(self, **kwargs): | |
| Button.__init__(self, **kwargs) | |
| class MainGridLayout(GridLayout): | |
| def __init__(self, **kwargs): | |
| GridLayout.__init__(self, **kwargs) | |
| gridcount = len(TopRow)-1 | |
| for i in range(len(TopRow)): | |
| for j in range(len(SideColumn)): | |
| self.MainGridSubLayout = MainGridSubLayout(RowNum=i, ColNum=j, cols = len(TopRow[0]), rows = len(SideColumn[0])) | |
| self.blankgrid = BlankGrid(cols=1) | |
| self.hintbutton = HintButton(text="Hint") | |
| if j > gridcount: | |
| if i == len(TopRow)-1 and j == len(SideColumn)-1: | |
| self.add_widget(self.hintbutton) | |
| else: | |
| self.add_widget(self.blankgrid) | |
| else: | |
| self.add_widget(self.MainGridSubLayout) | |
| gridcount -= 1 | |
| class MainGridSubLayout(GridLayout): | |
| def __init__(self, RowNum, ColNum, **kwargs): | |
| GridLayout.__init__(self, **kwargs) | |
| self.padding = 5 | |
| self.RowNum = RowNum | |
| self.ColNum = ColNum | |
| for i in range(len(TopRow[0])): | |
| for j in range(len(SideColumn[0])): | |
| self.add_widget(SubGridButton(TopRowCombined[j+(ColNum*NumofItems)], SideColumnCombined[i+(RowNum*NumofItems)], 0)) | |
| class SubGridButton(Button): | |
| def __init__(self, CoTop, CoSide, checkedstate, **kwargs): | |
| Button.__init__(self, **kwargs) | |
| self.CoTop = CoTop | |
| self.CoSide = CoSide | |
| self.checkedstate = checkedstate | |
| GridList.append(self) | |
| def buttonpressed(self): | |
| if self.checkedstate == 0: | |
| self.text = 'X' | |
| self.underline = 1 | |
| self.background_normal = "" | |
| self.background_color = green | |
| self.checkedstate=1 | |
| elif self.checkedstate == 1: | |
| self.text = 'O' | |
| self.checkedstate = 2 | |
| elif self.checkedstate == 2: | |
| self.text = 'X' | |
| self.checkedstate = 1 | |
| class BlankGrid(GridLayout): | |
| def __init__(self, **kwargs): | |
| GridLayout.__init__(self, **kwargs) | |
| self.add_widget(BlankSquare()) | |
| class BlankSquare(Label): | |
| def __init__(self, **kwargs): | |
| Label.__init__(self, **kwargs) | |
| class SolverLayout(GridLayout): | |
| def __init__(self, **kwargs): | |
| GridLayout.__init__(self, **kwargs) | |
| self.menulayout = MenuLayout(size_hint_x = 0.2, size_hint_y=0.2, cols=1) | |
| self.toprowlayout = TopRowLayout(size_hint_y=0.2) | |
| self.sidecolumnlayout = SideColumnLayout(size_hint_x=0.2) | |
| self.MainGridLayout = MainGridLayout(cols = len(TopRow), rows = len(SideColumn)) | |
| self.add_widget(self.menulayout) | |
| self.add_widget(self.toprowlayout) | |
| self.add_widget(self.sidecolumnlayout) | |
| self.add_widget(self.MainGridLayout) | |
| class StoryLayout(GridLayout): | |
| def __init__(self, **kwargs): | |
| GridLayout.__init__(self, **kwargs) | |
| self.add_widget(Button(text='OK')) | |
| class MainApp(App): | |
| def build(self): | |
| screenmanager = ScreenManager() | |
| solvescreen = Screen(name='solve') | |
| storyscreen = Screen(name='story') | |
| self.solverlayout = SolverLayout() | |
| self.storylayout = StoryLayout() | |
| solvescreen.add_widget(self.solverlayout) | |
| storyscreen.add_widget(self.storylayout) | |
| screenmanager.add_widget(solvescreen) | |
| screenmanager.add_widget(storyscreen) | |
| # screenmanager.current = "story" | |
| return screenmanager | |
| if __name__ == '__main__': | |
| Builder.load_string(kv) | |
| MainApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment