Skip to content

Commit

Permalink
Added the common letter test and refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
redg25 committed Mar 13, 2022
1 parent fac91c3 commit 7026240
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 18 deletions.
15 changes: 12 additions & 3 deletions common_letters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
from typing import List

letters = {'a':[],
'b':['d','p','q','g'],
Expand Down Expand Up @@ -39,7 +40,7 @@ def letter_choice():
letter_pair = random.choice(choice_for_pair)
return [letter,letter_pair]

def question():
def get_question():
up=[]
down=[]
while len(up)<4:
Expand All @@ -52,16 +53,24 @@ def question():
up = [x.upper() for x in up]
else:
down = [x.upper() for x in down]
return up,down
return [up, down]

def get_nb_of_pairs(up, down):
def get_nb_of_pairs(lines:List[list]):
score = 0
up = lines[0]
down = lines[1]
for u,d in zip(up,down):
if u.lower() == d.lower():
score += 1
return score


def get_question_and_answer():
question = get_question()
all_letters = question[0]+ question[1]
answer = get_nb_of_pairs(question)
return all_letters, answer




93 changes: 81 additions & 12 deletions game_gia.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from kivy.app import App, Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.core.window import Window

import abc
import threading
import number_exam
import common_letters


class Menu(BoxLayout, Screen):
Expand All @@ -17,7 +22,11 @@ def go_to_a_screen_test(self, screen_name: str):
""""""
app.root.current = screen_name
screen = app.root.current_screen
screen.design()
ratio_boxtest = screen.ids.boxtest.size_hint
boxtest_real_size = [a*b for a,b in zip(screen.ids.boxtest.size_hint,Window.size)]
# boxtest_x = ratio_boxtest[0] * Window.size[0]
# boxtest_y = ratio_boxtest[1] * Window.size[1]
screen.design(boxtest_real_size)
screen.update_layout_with_new_question()
screen.start_timer()

Expand All @@ -30,8 +39,9 @@ def __init__(self, **kwargs):
self.question: str = ''
self.answer: str = ''
self.score: int = 0
self.number_of_questions: int = 0
self.widgets: dict = {'buttons': {}}
self.number_of_questions: int = 1
self.widgets: dict = {'buttons': {}, 'labels':{}, 'grid':{}}
self.timer: threading.Timer = None

@classmethod
def __subclasshook__(cls, subclass):
Expand All @@ -42,7 +52,7 @@ def __subclasshook__(cls, subclass):
NotImplemented)

@abc.abstractmethod
def design(self):
def design(self,size):
"""Designs specific layout for a given test"""
pass

Expand All @@ -58,6 +68,8 @@ def check_answer_update_score_and_add_new_question(self, button: Button):
else:
self.score -= 1
self.update_layout_with_new_question()
self.number_of_questions += 1


def stop_game(self):
""" When the timer is done, disable all buttons and show the user score"""
Expand All @@ -68,44 +80,101 @@ def stop_game(self):

def start_timer(self):
"""Timer to start when a test is starting"""
timer = threading.Timer(180, self.stop_game)
timer.start()
self.timer = threading.Timer(3, self.stop_game)
self.timer.start()

def remove_test_layout(self):
"""
Reset the screen and variables to their original layout/values
Set the 'menu' screen as the current one
"""
self.ids.boxtest.remove_widget(self.ids.boxtest.children[0])
self.timer.cancel()
# Loop through the range of the length of children because
# when looping through the children, some were missed and
# I couldn't figure out why

for n in range(len(self.ids.boxtest.children)):
self.ids.boxtest.remove_widget(self.ids.boxtest.children[0])
self.ids.score_lbl.text = ''
self.score = 0
self.number_of_questions = 0
self.number_of_questions = 1
app.root.current = 'menu'


class NumbersTest(SingleTestInterface):

def design(self):
h_layout = BoxLayout(orientation='horizontal')
def design(self,boxtest_real_size):
h_layout_x = boxtest_real_size[0]/3*2
h_layout_y = boxtest_real_size[1]/3*2
h_layout_pos_x = boxtest_real_size[0]/6
h_layout_pos_y = boxtest_real_size[1]/6

h_layout = BoxLayout(size=(h_layout_x,h_layout_y),
pos=(h_layout_pos_x,h_layout_pos_y),
size_hint=(None,None))
for n in range(3):
b_name = f'button_{n+1}'
button = Button(text='', on_release=self.check_answer_update_score_and_add_new_question,
font_size=80, disabled=False)
font_size=30, disabled=False)
self.widgets['buttons'][b_name] = button
h_layout.add_widget(button)
self.ids.boxtest.add_widget(h_layout)

def update_layout_with_new_question(self):
self.question, self.answer = number_exam.get_question_and_answer()
self.number_of_questions += 1
# Assign the 3 numbers from the question to the text of the 3 buttons
for i, value in enumerate(self.widgets['buttons'].values()):
value.text = str(self.question[i])

class LettersTest(SingleTestInterface):

def design(self,boxtest_real_size):
h_layout_x = boxtest_real_size[0]/3*2
h_layout_y = boxtest_real_size[1]/8*7
h_layout_pos_x = boxtest_real_size[0]/6
h_layout_pos_y = boxtest_real_size[1]/8
h_layout = BoxLayout(orientation='vertical',
size=(h_layout_x,h_layout_y),
pos=(h_layout_pos_x,h_layout_pos_y),
size_hint=(None,None))
grid = GridLayout(cols=4, spacing=20)
for n in range(8):
lbl = Label(text='',font_size=60, padding_y = 20)
grid.add_widget(lbl)
self.widgets['grid'][f'letter_{str(n)}']=lbl
answers_line = BoxLayout(orientation='horizontal',size = self.parent.size )
for n in range(5):
a_btn = Button(text=str(n),
on_release=self.check_answer_update_score_and_add_new_question,
font_size=50,
disabled=False)
answers_line.add_widget(a_btn)
self.widgets['buttons'][f'answer_{str(n)}']=a_btn
h_layout.add_widget(grid)
h_layout.add_widget(Label(size=(boxtest_real_size[0],boxtest_real_size[1]/9),
size_hint=(None,None)))
h_layout.add_widget(answers_line)
self.ids.boxtest.add_widget(h_layout)

def update_layout_with_new_question(self):
self.question, self.answer = common_letters.get_question_and_answer()
for label, letter in zip(self.widgets['grid'],self.question):
self.widgets['grid'][label].text=letter
# for letter in self.question[0]:
# grid.add_widget(Label(text=letter,font_size=50))
# for letter in self.question[1]:
# grid.add_widget(Label(text=letter,font_size=50))
# grid.add_widget(Label())

class LabelInputs:
pass



class MainApp(App):

def build(self):
print(Window.size)
file = Builder.load_file('gia_screens.kv')
return file

Expand Down
20 changes: 17 additions & 3 deletions gia_screens.kv
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ ScreenManager:
id: numbers
name: 'numbers'
manager: 'screen_manager'
LettersTest:
id: letters
name: 'letters'
manager: 'screen_manager'


<Menu>:
orientation: "vertical"
Expand All @@ -26,6 +31,10 @@ ScreenManager:
text: 'Numbers'
size_hint_y: 0.2
on_release: root.go_to_a_screen_test('numbers')
Button:
text: 'Letters'
size_hint_y: 0.2
on_release: root.go_to_a_screen_test('letters')

<SingleTestInterface>:
orientation: "vertical"
Expand All @@ -34,12 +43,12 @@ ScreenManager:
halign: 'center'
valign: 'middle'
font_size: 24
size_hint_y: 0.3
size_hint_y: 0.2
text: ''
BoxLayout:
RelativeLayout:
id: boxtest
orientation: "horizontal"
size_hint_y: 0.5
size_hint_y: 0.6
Button:
id: to_menu
font_size: 30
Expand All @@ -50,6 +59,11 @@ ScreenManager:
<NumbersTest>:
orientation: "vertical"

<LettersTest>:
orientation: "vertical"






Expand Down

0 comments on commit 7026240

Please sign in to comment.