From ef2e811b47623129b7de7217c77b69846d00c6dc Mon Sep 17 00:00:00 2001 From: deadvey Date: Thu, 14 Nov 2024 23:52:12 +0000 Subject: [PATCH] initial commit :D --- README.md | 18 ++++++- bases-game.py | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 bases-game.py diff --git a/README.md b/README.md index c393647..c95ce0c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,19 @@ # bases-game -An game for revision base 2, 10 and 16 (for my a level) \ No newline at end of file +An educational game for revision base 2, 10 and 16 (for my a level)
+See chapters 28 - 32 (Section 6) of the PG OCR revision guide for Computer Science AS and A level for the "rules"
+https://www.pgonline.co.uk/resources/computer-science/a-level-ocr/ocr-a-level-textbook/
+ +# What can you "play" currently? + - Conversions between bases (integers)
+ - Binary addition
+ - Floating point binary to denery conversion
+ + # TODO + - Two's complement
+ - Sign and magnitude
+ - Negative binary
+ - Complex binary*
+ +# Why the fuck is this a thing? +I was fucking revision my fucking as levels and I wanted a program to auto make binary and other bases questions up so I could just rattle through them so yeah. diff --git a/bases-game.py b/bases-game.py new file mode 100644 index 0000000..9531023 --- /dev/null +++ b/bases-game.py @@ -0,0 +1,143 @@ +print("") +import random + +while True: + ''' + Conversion ----- 2 -> 10 + |- 2 -> 16 + |- 10 -> 2 + |- 10 -> 16 + |- 16 -> 2 + |- 16 -> 10 + + Binary Addition + Floating point binary + Twos compliment, Sign & Magnitude, Negative binary, Complex binary + ''' + type_int = random.randint(0,2) + + if type_int == 2: + print("What is this binary floating point number in denery?") + int_length = random.randint(0,7) + decimal_length = random.randint(1,7) + answer = float(random.randint(0,2**int_length)) + start_int = bin(int(answer))[2:] + start_frac = "" + for x in range(1,decimal_length): + rand = random.randint(0,1) + answer+=(float(rand) * 2**(0-x)) + start_frac = start_frac + str(rand) + exponent = bin(len(start_int) - 1)[2:] + + print(start_int+start_frac," exp:", exponent) + attempt = input() + if float(attempt) == answer: + print("Correct") + else: + print(f"No, the answer was {answer}") + if type_int == 1: + answer_in_bin_or_den = random.randint(0,1) + if answer_in_bin_or_den == 1: + print("Answer in Base 10") + if answer_in_bin_or_den == 0: + print("Answer in Base 2") + numbers_to_add = random.randint(2,2) + numbers = [] + answer = 0 + for x in range(numbers_to_add): + current_number = random.randint(0,127) + numbers.append(current_number) + answer = answer + current_number + print(format(current_number, '#010b')[2:],end="") + if (x > 0): + print("+") + else: + print("") + + attempt = input() + if answer_in_bin_or_den == 1: + if int(attempt) == answer: + print("Correct") + else: + print(f"No, the answer was {answer}") + if answer_in_bin_or_den == 0: + if str(bin(int(attempt,2)))[2:] == str(bin(answer))[2:]: + print("Correct") + else: + print(f"No, the answer was {bin(answer)}") + if type_int == 0: + initial_base_int = random.randint(0,2) + match initial_base_int: + case 0: + initial_base = 2 + case 1: + initial_base = 10 + case 2: + initial_base = 16 + case _: + print("Error") + + final_base_int = random.randint(0,2) + while final_base_int == initial_base_int: + final_base_int = random.randint(0,2) + match final_base_int: + case 0: + final_base = 2 + case 1: + final_base = 10 + case 2: + final_base = 16 + case _: + print("Error") + + print(f"Base {initial_base} to Base {final_base}\n") + + start_number = random.randint(0,255) + if initial_base == 2: + print(str(bin(start_number))[2:]) + if initial_base == 10: + print(start_number) + if initial_base == 16: + print(str(hex(start_number))[2:]) + + if final_base == 2: + print("") + print("128") + print("|64") + print("||32") + print("|||16") + print("||||8421") + print("||||||||") + print("VVVVVVVV") + if final_base == 10: + print("") + print("100") + print("|10") + print("||1") + print("|||") + print("VVV") + if final_base == 16: + print("") + print("16") + print("|1") + print("||") + print("VV") + + attempt = input() + if final_base == 16: + if str(hex(int(attempt,16)))[2:] == str(hex(start_number))[2:]: + print("Correct") + else: + print(f"No, the answer was {hex(int(start_number))[2:]}") + if final_base == 10: + if attempt == str(int(start_number)): + print("Correct") + else: + print(f"No, the answer was {int(start_number)}") + if final_base == 2: + if str(bin(int(attempt,2)))[2:] == str(bin(start_number))[2:]: + print("Correct") + else: + print(f"No, the answer was {bin(start_number)[2:]}") + + print("------------------------------------")