diff --git a/.GitImages/basegame.png b/.GitImages/basegame.png new file mode 100644 index 0000000..15f657c Binary files /dev/null and b/.GitImages/basegame.png differ diff --git a/BaseGame/README.md b/BaseGame/README.md new file mode 100644 index 0000000..b086b79 --- /dev/null +++ b/BaseGame/README.md @@ -0,0 +1,10 @@ +BaseGame +======= + +Practice your ability to convert numbers between two bases. + +Edit the `DEFAULT_` variables at the top of the script to however you like them. While playing, you can enter "!" to play on different settings. Enter "?" to receive the answer to the current round. + +

+ basegame +

\ No newline at end of file diff --git a/BaseGame/basegame.py b/BaseGame/basegame.py new file mode 100644 index 0000000..0782ba0 --- /dev/null +++ b/BaseGame/basegame.py @@ -0,0 +1,112 @@ +import random +import shutil + +DEFAULT_FROMBASE = 10 +DEFAULT_TOBASE = 16 +DEFAULT_LOWER = 0 +DEFAULT_UPPER = 1000 + + +TERMINALWIDTH = shutil.get_terminal_size().columns + +def basex(number, base, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'): + """Converts an integer to a different base string.""" + alphabet = alphabet[:base] + if not isinstance(number, (int, str)): + raise TypeError('number must be an integer') + number = int(number) + based = '' + sign = '' + if number < 0: + sign = '-' + number = -number + if 0 <= number < len(alphabet): + return sign + alphabet[number] + while number != 0: + number, i = divmod(number, len(alphabet)) + based = alphabet[i] + based + return sign + based + +def changebase(number, frombase, tobase): + number = str(number) + result = int(number, frombase) + return basex(result, tobase) + +def intinput(prompt, greaterthan=None): + ''' Prompt for input until the user enters an int ''' + while True: + result = input(prompt) + try: + i = int(result) + if greaterthan is None or i > greaterthan: + return i + except ValueError: + pass + +def configuration(): + frombase = intinput(' From base: ', 1) + tobase = intinput(' To base: ', 1) + lower = intinput('Lower bound: ') + upper = intinput('Upper bound: ', lower) + return { + 'frombase': frombase, + 'tobase': tobase, + 'lower': lower, + 'upper': upper, + } + +def calcpoints(solution, lower, upper, maxpoints): + ''' + 1/2 of the maxpoints are granted automatically. + The other 1/2 points depends on how large the number + you had to guess was. + ''' + basescore = int(maxpoints / 2) + 1 + bonus = (solution - lower) / (upper - lower) + bonus *= basescore + bonus = int(bonus) + return basescore + bonus + +def playgame(): + c= { + 'frombase': DEFAULT_FROMBASE, + 'tobase': DEFAULT_TOBASE, + 'lower': DEFAULT_LOWER, + 'upper': DEFAULT_UPPER, + } + print('\n"!" to reconfigure, "?" to give up') + score = 0 + while True: + number = random.randint(c['lower'], c['upper']) + prompt = changebase(number, 10, c['frombase']) + solution = changebase(number, 10, c['tobase']).lower() + prompt = 'b%d -> b%d : %s = ' % (c['frombase'], c['tobase'], prompt) + + cursor = len(prompt) + 1 + scoretext = '(score: %d)' % score + spacer = ' ' * (TERMINALWIDTH - (len(scoretext) + cursor)) + prompt = prompt + spacer + scoretext + prompt += '\b' * (TERMINALWIDTH - cursor) + + correctness = False + while not correctness: + guess = input(prompt).lower() + guess = guess.lstrip('0') + if guess == '!': + c = configuration() + correctness = True + print() + if guess == '?': + if c['frombase'] != 10 and c['tobase'] != 10: + b10 = ' (b10= %d)' % number + else: + b10 = '' + print('%s= %s%s'% (' '*(cursor-3), solution, b10)) + correctness = True + if guess == solution: + maxpoints = int((c['upper'] - c['lower']) ** 0.5) + score += calcpoints(number, c['lower'],c['upper'],maxpoints) + correctness = True + +if __name__ == '__main__': + playgame() \ No newline at end of file diff --git a/Keyboard/keyboard.pyw b/Keyboard/keyboard.pyw new file mode 100644 index 0000000..0999b35 --- /dev/null +++ b/Keyboard/keyboard.pyw @@ -0,0 +1,75 @@ +import tkinter + +class Keyboard: + def __init__(self): + self.windowtitle = 'Keyboard' + + self.keywidth = 30 + self.keyspace = 5 + self.keys = self.build_keydict() + + self.t = tkinter.Tk() + self.t.title(self.windowtitle) + self.w = (self.keywidth + self.keyspace) * 16 + self.h = (self.keywidth + self.keyspace) * 6 + self.screenwidth = self.t.winfo_screenwidth() + self.screenheight = self.t.winfo_screenheight() + self.windowwidth = self.w + self.windowheight = self.h + self.windowx = (self.screenwidth-self.windowwidth) / 2 + self.windowy = ((self.screenheight-self.windowheight) / 2) - 27 + self.geometrystring = '%dx%d+%d+%d' % ( + self.windowwidth, self.windowheight, self.windowx, self.windowy) + self.t.geometry(self.geometrystring) + + self.c = tkinter.Canvas(self.t) + self.c.pack(expand=True, fill='both') + + self.build_qwerty() + + self.t.bind('', self.press) + self.t.bind('', self.release) + + def mainloop(self): + self.t.mainloop() + + def press(self, event): + c = event.char.lower() + if c in self.keys: + self.c.itemconfig(self.keys[c], fill='#f00') + + def release(self, event): + c = event.char.lower() + if c in self.keys: + self.c.itemconfig(self.keys[c], fill='systembuttonface') + + def build_keydict(self): + mainy = self.keywidth + w = self.keywidth + self.keyspace + rows = [ + {'c':'`1234567890-=', 'x':w, 'y':mainy}, + {'c':'qwertyuiop[]\\', 'x':w*1.5, 'y':mainy+w}, + {'c':'asdfghjkl;\'', 'x':w*1.8, 'y':mainy+(w*2)}, + {'c':'zxcvbnm,./', 'x':w*2.4, 'y':mainy+(w*3)}, + {'c':' ', 'x':w, 'y':mainy+(w*4)} + ] + + keys = {} + for row in rows: + for ki in range(len(row['c'])): + k = row['c'][ki] + keys[k] = (row['x'] + ((self.keywidth + self.keyspace) * ki), row['y']) + return keys + + def build_qwerty(self): + w = self.keywidth + for k in self.keys: + x = self.keys[k][0] + y = self.keys[k][1] + self.keys[k] = self.c.create_rectangle(x, y, x+w, y+w) + self.c.create_text(x+(w/2), y+(w/2), text=k) + + + +k = Keyboard() +k.mainloop() \ No newline at end of file diff --git a/TKdraw/README.md b/TKdraw/README.md index 3aca3c7..c611718 100644 --- a/TKdraw/README.md +++ b/TKdraw/README.md @@ -4,5 +4,5 @@ TKDraw Unfortunately, there's no TKErase ...

- ASCII + tkdraw

\ No newline at end of file