diff --git a/.gitignore b/.gitignore index b2f3687..bdc906d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ lecture_slides pictures old_lessons Getting Started.docx - +.DS_Store # settings and other directoreis __pycache__/ .ipynb_checkpoints/ diff --git a/02-IDE_exercises/solutions/02b-project/temp_conversion.py b/02-IDE_exercises/solutions/02b-project/temp_conversion.py index 8dac2a8..4e4ec2e 100644 --- a/02-IDE_exercises/solutions/02b-project/temp_conversion.py +++ b/02-IDE_exercises/solutions/02b-project/temp_conversion.py @@ -5,8 +5,8 @@ print("Please enter the temperature in Celsius.") -temp_c = float(input()) -temp_f = (9/5) * temp_c + 32 +temp_c: float = float(input()) +temp_f: float = (9/5) * temp_c + 32 print("The corresponding temperature in Fahrenheit is:") print(temp_f) diff --git a/02-IDE_exercises/solutions/02c-project/coin_change.py b/02-IDE_exercises/solutions/02c-project/coin_change.py index 97721f5..d474f5d 100644 --- a/02-IDE_exercises/solutions/02c-project/coin_change.py +++ b/02-IDE_exercises/solutions/02c-project/coin_change.py @@ -2,20 +2,22 @@ # Date: 09/24/2020 # Description: Asks the user for int of cents less than $1 and returns # breakout of coin denominations with fewest number of coins +# Hint: the floor division // could be useful where it rounds the result down to the nearest whole number +# or the modulo operator can also be used. print("Please enter an amount between 0 and 99 cents.") -coins = int(input()) +coins: int = int(input()) print("Your change will be:") -quarters = coins // 25 +quarters: int = coins // 25 coins -= quarters * 25 -dimes = coins // 10 +dimes: int = coins // 10 coins -= dimes * 10 -nickels = coins // 5 +nickels: int = coins // 5 coins -= nickels * 5 -pennies = coins +pennies: int = coins print("Q:", quarters) print("D:", dimes) diff --git a/02-IDE_exercises/solutions/03a-project/min_max_number.py b/02-IDE_exercises/solutions/03a-project/min_max_number.py index 8014ae3..845c7b9 100644 --- a/02-IDE_exercises/solutions/03a-project/min_max_number.py +++ b/02-IDE_exercises/solutions/03a-project/min_max_number.py @@ -4,26 +4,26 @@ # integers from user print("How many numbers would you like to enter?") -num_int = int(input()) +num_int: int = int(input()) # The first while loop ensures that the user enters at least one number # if, the user enters anything less than 1, it will prompt the user to enter something greater than one while num_int < 1: print("Please enter at least one number") - num_int = int(input()) + num_int: int = int(input()) else: print("Please enter", num_int, "numbers.") - first_int = int(input()) + first_int: int = int(input()) # the first number is the largest as well as the smallest number - min = first_int - max = first_int + min: int = first_int + max: int = first_int # if the number of ints requested by user is greater than one, we'll initialize # a while loop while num_int > 1: - n = int(input()) + n: int = int(input()) if n < min: min = n if n > max: diff --git a/02-IDE_exercises/solutions/03b-project/find_factors.py b/02-IDE_exercises/solutions/03b-project/find_factors.py index 845fd69..08bb0b4 100644 --- a/02-IDE_exercises/solutions/03b-project/find_factors.py +++ b/02-IDE_exercises/solutions/03b-project/find_factors.py @@ -2,9 +2,10 @@ # Date: 10/05/2020 # Description: Take in positive integer and print list of factors -integer = int(input("Please enter a positive integer:")) +integer: int = int(input("Please enter a positive integer:")) print("The factors of", integer, "are:") -for i in range(1, integer+1): +i: int +for i in range(1, integer + 1): if integer % i == 0: print(i) \ No newline at end of file diff --git a/02-IDE_exercises/solutions/03c-project/guess_num.py b/02-IDE_exercises/solutions/03c-project/guess_num.py index 81527fa..69a6401 100644 --- a/02-IDE_exercises/solutions/03c-project/guess_num.py +++ b/02-IDE_exercises/solutions/03c-project/guess_num.py @@ -1,15 +1,15 @@ # Author: Brandon Hoffman # Date: 10/05/2020 -# Description: Guessing game that takes a number to guess and has user contiously +# Description: Guessing game that takes a number to guess and has user continuously # make guesses till they get the answer correct print("Enter the number for the player to guess.") -solution = int(input()) +solution: int = int(input()) print("Enter your guess.") -guess = int(input()) +guess: int = int(input()) -number_of_guesses = 1 +number_of_guesses: int = 1 while guess != solution: if guess > solution: @@ -19,8 +19,6 @@ print("Too low - try again:") guess = int(input()) - number_of_guesses += 1 - -number_of_guesses +=1 +number_of_guesses += 1 print("You guessed it in", number_of_guesses, "tries.") \ No newline at end of file diff --git a/02-IDE_exercises/solutions/04a-project/distance_fallen.py b/02-IDE_exercises/solutions/04a-project/distance_fallen.py index 3615881..a41762c 100644 --- a/02-IDE_exercises/solutions/04a-project/distance_fallen.py +++ b/02-IDE_exercises/solutions/04a-project/distance_fallen.py @@ -3,10 +3,14 @@ # Description: created a function called "fall_distance() that takes an input of seconds and returns # the distance an object has fallen." +time: int = int(input("Please enter an amount of time in second(s): ")) -def fall_distance(t): - '''takes input in seconds and returns distance object has fallen''' - g = 9.8 # velocity - return 0.5 * g * t*t +def fall_distance(t:int) -> float: + """takes input in seconds and returns distance object has fallen""" + g: float = 9.8 # velocity + return 0.5 * g * t * t + + +print("The distance the object has fallen is : ", fall_distance(time), "meter:") diff --git a/02-IDE_exercises/solutions/04b-project/fibonacci.py b/02-IDE_exercises/solutions/04b-project/fibonacci.py index 779b046..838f366 100644 --- a/02-IDE_exercises/solutions/04b-project/fibonacci.py +++ b/02-IDE_exercises/solutions/04b-project/fibonacci.py @@ -1,18 +1,22 @@ -# Author: Brandon Hoffman -# Date: 10/17/2020 -# Description: created a function called fib() that calculate a fibonnaci number for a given 'n' without recursion or the golden ratio. +# Author: Brandon Hoffman Date: 10/17/2020 Description: created a function called fib() that calculate a Fibonacci +# number for a given 'n' without recursion or the golden ratio. -def fib(n): - '''calculate a fibonnaci number given the input 'n' without recursion using the golden ratio''' - prev_num = 0 - curr_num = 1 - sol = 1 - for i in range(n-1): +def fib(n: int) -> int: + """calculate a fibonnaci number given the input 'n' without recursion using the golden ratio""" + + prev_num: int = 0 + curr_num: int = 1 + sol: int = 1 + print("The numbers in fibonacci series are : ") + i: int + for i in range(n - 1): sol = curr_num + prev_num prev_num = curr_num curr_num = sol - - + print(sol) return sol + +number: int = int(input("Please enter a number to calculate its Fibonacci number: ")) +print("The Fibonacci number is: ", fib(number)) diff --git a/02-IDE_exercises/solutions/04c-project/hailstone_step.py b/02-IDE_exercises/solutions/04c-project/hailstone_step.py index a5599a9..30877b1 100644 --- a/02-IDE_exercises/solutions/04c-project/hailstone_step.py +++ b/02-IDE_exercises/solutions/04c-project/hailstone_step.py @@ -4,17 +4,21 @@ # same process until the result terminates at a value of 1. -def hailstone(n): - '''takes an integer, 'n' and if even divides by 2 and if odd multiples by 3 and adds 1 - the result continues until the value reaches 1 and the function terminates and returns the count the number of steps''' - count = 0 +def hailstone(n: int) -> int: + """takes an integer, 'n' and if even divides by 2 and if odd multiples by 3 and adds 1 the result continues until + the value reaches 1 and the function terminates and returns the count the number of steps """ + count: int = 0 + n: int while n != 1: print(count, n) if n % 2 == 0: n //= 2 else: n = (n * 3) + 1 - + count += 1 + return count + - return count \ No newline at end of file +number: int = int(input("Please enter a number: ")) +print("The count is: ", hailstone(number)) diff --git a/02-IDE_exercises/solutions/05a-project/multiply_recur.py b/02-IDE_exercises/solutions/05a-project/multiply_recur.py index 1fc6cf8..4492b94 100644 --- a/02-IDE_exercises/solutions/05a-project/multiply_recur.py +++ b/02-IDE_exercises/solutions/05a-project/multiply_recur.py @@ -3,11 +3,16 @@ # Description: write a function that uses recursive iteration to multiply two positive integers # with addition -def multiply(multiplier, multiplicand): +def multiply(multiplier: int, multiplicand: int) -> int: """ Takes in two positive integers and multiplies them by recursively adding the int multiplicand int multiplier times. """ if multiplier == 1: return multiplicand else: - return multiply(multiplier-1, multiplicand) + multiplicand \ No newline at end of file + return multiply(multiplier - 1, multiplicand) + multiplicand + + +num1: int = int(input("Please enter a multiplier: ")) +num2: int = int(input("Please enter a multiplicand: ")) +print("The result is: ", multiply(num1, num2)) diff --git a/02-IDE_exercises/solutions/05b-project/Taxicab.py b/02-IDE_exercises/solutions/05b-project/Taxicab.py index 160503f..43425bc 100644 --- a/02-IDE_exercises/solutions/05b-project/Taxicab.py +++ b/02-IDE_exercises/solutions/05b-project/Taxicab.py @@ -7,7 +7,7 @@ class Taxicab: """ Represents a taxicab that tracks the distance traveled when given x and y coordinates """ - def __init__(self, x, y): + def __init__(self, x: float, y: float) -> None: """ Creates a taxicab object with x and y coordiantes and intializes an odometer to 0 """ @@ -16,32 +16,32 @@ def __init__(self, x, y): self._odometer = 0 - def get_x_coord(self): + def get_x_coord(self) -> float: """ returns the current x coordinate """ return self._x - def get_y_coord(self): + def get_y_coord(self) -> float: """ returns the current y coordinate """ return self._y - def get_odometer(self): + def get_odometer(self) -> float: """ sums the abs value of the change in x and y coordinates """ return self._odometer - def move_x(self, x_vector): + def move_x(self, x_vector: float) -> None: """ takes in a one-dimensional vector and adds it to the x coordiante as its original value and the odometer as an absolute value """ self._x += x_vector self._odometer += abs(x_vector) - def move_y(self, y_vector): + def move_y(self, y_vector: float) -> None: """ takes in a one-dimensional vector and adds it to the x coordiante as its original value and the odometer as an absolute value """ diff --git a/02-IDE_exercises/solutions/05b-project/test.py b/02-IDE_exercises/solutions/05b-project/test.py new file mode 100644 index 0000000..6162f2d --- /dev/null +++ b/02-IDE_exercises/solutions/05b-project/test.py @@ -0,0 +1,7 @@ +from Taxicab import Taxicab + +cab = Taxicab(3, -8) # creates a Taxicab object at coordinates (3, -8) +cab.move_x(4) # moves cab 4 units "right" +cab.move_y(-5) # moves cab 5 units "down" +cab.move_x(-2) # moves cab 2 unit "left" +print(cab.get_odometer()) # prints the current odometer reading \ No newline at end of file diff --git a/02-IDE_exercises/solutions/06a-project/find_median.py b/02-IDE_exercises/solutions/06a-project/find_median.py index 6c74469..2f2e46c 100644 --- a/02-IDE_exercises/solutions/06a-project/find_median.py +++ b/02-IDE_exercises/solutions/06a-project/find_median.py @@ -2,18 +2,21 @@ # Date: 10/25/2020 # Description: Sorts a list of numbers and returns the median number in the list -def find_median(some_nums): +def find_median(some_nums: list[int]) -> float: """ Sorts a list of numbers and returns the median value """ some_nums.sort() - array_length = len(some_nums) + array_length: int = len(some_nums) - mid_index = int(array_length/2) + mid_index: int = int(array_length / 2) if array_length % 2 == 0: - mid_index_alt = int((array_length/2)-1) + mid_index_alt = int((array_length / 2) - 1) return (some_nums[mid_index] + some_nums[mid_index_alt]) / 2 else: return some_nums[mid_index] - \ No newline at end of file + + +list_of_nums: list[int] = [13, 7, -3, 82, 4] +result: float = find_median(list_of_nums) diff --git a/02-IDE_exercises/solutions/06b-project/add_surname.py b/02-IDE_exercises/solutions/06b-project/add_surname.py index 64a7c42..ae61f07 100644 --- a/02-IDE_exercises/solutions/06b-project/add_surname.py +++ b/02-IDE_exercises/solutions/06b-project/add_surname.py @@ -1,9 +1,22 @@ -# Author: Brandon Hoffman -# Date: 10/25/2020 -# Description: create a function that takes a list of names and stores any value that starts with the letter 'K' in a new list concatenated also with the string ' Hoffman' +# Author: Brandon Hoffman Date: 10/25/2020 Description: create a function that takes a list of names and stores any +# value that starts with the letter 'K' in a new list concatenated also with the string ' Hoffman' -def add_surname(name_list): +def add_surname(name_list: list[str]) -> list[str]: """ - takes a list of names and stores any value that starts with the letter 'K' in a new list concatenated also with the string ' Hoffman' + takes a list of names and stores any value that starts with the letter 'K' in a new list concatenated also with + the string ' Hoffman' """ - return [name + " Hoffman" for name in name_list if name[0] == "K"] \ No newline at end of file + new_name_list: list[str] = [] + for name_in_list in name_list: + if name_in_list[0] == 'K': + new_name_list += [name_in_list + ' Hoffman'] + + return new_name_list + + +# Testing the function +names: list[str] = ["Kiki", "Krystal", "Pavel", "MaryKay", "Annie", "Koala", "Killer"] +changed_name_lists: list[str] = add_surname(names) +name: str +for name in changed_name_lists: + print(name) diff --git a/02-IDE_exercises/solutions/06c-project/std_dev.py b/02-IDE_exercises/solutions/06c-project/std_dev.py index 7c8b568..94b93bd 100644 --- a/02-IDE_exercises/solutions/06c-project/std_dev.py +++ b/02-IDE_exercises/solutions/06c-project/std_dev.py @@ -1,6 +1,6 @@ # Author: Brandon Hoffman # Date: 10/25/2020 -# Description: Creates a Person class to intialize a name and age. +# Description: Creates a Person class to initialize a name and age. # Then creates a std_dev function that can take a list of person # objects and calculate a standard deviation on the age @@ -9,34 +9,42 @@ class Person: Represents a person """ - def __init__(self, name, age): + def __init__(self, name: str, age: int) -> None: """ Intializes a person's name and age """ self._name = name self._age = age - def get_age(self): + def get_age(self) -> int: """ returns the age of a person object """ return self._age - -def std_dev(person_list): + +def std_dev(person_list: list[str]) -> float: """ Takes in a list of 'Person' objects and returns the standard deviation of their ages """ - age_sum = 0 + age_sum: int = 0 + person: str for person in person_list: age_sum += person.get_age() - avg = age_sum / len(person_list) + avg: float = age_sum / len(person_list) - variance = 0 + variance: float = 0 for person in person_list: - variance += (person.get_age() - avg)**2 + variance += (person.get_age() - avg) ** 2 + + stdv: float = (variance / len(person_list)) ** 0.5 + + return stdv - stdv = (variance / len(person_list))**0.5 - return stdv \ No newline at end of file +person1: Person = Person("Katie", 73) +person2: Person = Person("Tran", 24) +person3: Person = Person("Hanna", 48) +person_list: list[Person] = [person1, person2, person3] +print(std_dev(person_list)) diff --git a/02-IDE_exercises/solutions/07a-project/square_list.py b/02-IDE_exercises/solutions/07a-project/square_list.py index 901174e..c2d779a 100644 --- a/02-IDE_exercises/solutions/07a-project/square_list.py +++ b/02-IDE_exercises/solutions/07a-project/square_list.py @@ -2,11 +2,18 @@ # Date: 10/29/2020 # Description: Mutates a given list and squares the items in the list -def square_list(lst): +def square_list(lst: list[int]) -> None: """ Input: a list of numbers Doesn't return anything, mutates given list """ - - for i in range(len(lst)-1): + i: int + for i in range(len(lst)): lst[i] **= 2 + + +# Testing + +numb_lists: list[int] = [1, 2, 3, 4] +square_list(numb_lists) +print(numb_lists) # This should print [1, 4, 9, 4] diff --git a/02-IDE_exercises/solutions/07b-project/reverse_list.py b/02-IDE_exercises/solutions/07b-project/reverse_list.py index f6125a0..6259bb1 100644 --- a/02-IDE_exercises/solutions/07b-project/reverse_list.py +++ b/02-IDE_exercises/solutions/07b-project/reverse_list.py @@ -2,13 +2,20 @@ # Date: 10/29/2020 # Description: Mutates a given list and by the order of the elements of teh list -def reverse_list(lst): +def reverse_list(lst: object) -> None: """ Input: list Takes in a list and reverses the order of the elements of the list """ - temp_lst = [] - for i in range(len(lst)-1,-1, -1): + temp_lst: object = [] + i: int + for i in range(len(lst) - 1, -1, -1): temp_lst.append(lst[i]) - - lst[:] = temp_lst \ No newline at end of file + lst: object + lst[:] = temp_lst + + +# Testing +vals: list[int] = [2, -5, 10, 9] +reverse_list(vals) +print(vals) # This should print [9, 10, -5, 2] diff --git a/02-IDE_exercises/solutions/08a-project/count_letters.py b/02-IDE_exercises/solutions/08a-project/count_letters.py index 9f6d30c..c6028f2 100644 --- a/02-IDE_exercises/solutions/08a-project/count_letters.py +++ b/02-IDE_exercises/solutions/08a-project/count_letters.py @@ -3,17 +3,17 @@ # Description: creates a function called count_letters that takes as a parameter a string # and returns a dictionary that tabulates how many of each letter is in that string. -def count_letters(string): +def count_letters(string: str) -> dict[str, int]: """ Input: a string Output: a dictionary with the key as a capital letter and the value as the count of occurrences of the upper and lower case occurences of that letter - """ - - letter_counts = {} + """ + letter_counts: dict[str, int] = {} + c: str for c in string: - + if 'A' <= c.upper() <= 'Z': if c.upper() not in letter_counts: @@ -21,5 +21,11 @@ def count_letters(string): else: letter_counts[c.upper()] += 1 - - return letter_counts \ No newline at end of file + + return letter_counts + + +# Testing + +word: str = "AaBb" +print(count_letters(word)) # should print {'A': 2, 'B': 2} diff --git a/02-IDE_exercises/solutions/08b-project/words_in_both.py b/02-IDE_exercises/solutions/08b-project/words_in_both.py index 5f4a063..ca52d4d 100644 --- a/02-IDE_exercises/solutions/08b-project/words_in_both.py +++ b/02-IDE_exercises/solutions/08b-project/words_in_both.py @@ -2,12 +2,18 @@ # Date: 11/14/2020 # Description: Takes in two strings and returns a set of common words -def words_in_both(string_1, string_2): +def words_in_both(string_1: str, string_2: str) -> set[str]: """ Input: two strings Output: a set of common words between the two strings """ - set_1 = set(string_1.lower().split()) - set_2 = set(string_2.lower().split()) + set_1: set[str] = set(string_1.lower().split()) + set_2: set[str] = set(string_2.lower().split()) - return set_1 & set_2 \ No newline at end of file + return set_1 & set_2 + + +# Testing + +common_words: str = words_in_both("She's a jack of all trades", 'Jack was tallest of all') +print(common_words) diff --git a/02-IDE_exercises/solutions/08c-project/make_employee_dict.py b/02-IDE_exercises/solutions/08c-project/make_employee_dict.py index 754b5e5..63c8d6d 100644 --- a/02-IDE_exercises/solutions/08c-project/make_employee_dict.py +++ b/02-IDE_exercises/solutions/08c-project/make_employee_dict.py @@ -9,7 +9,7 @@ class Employee: Represents fundamental characteristics of an employee of a company """ - def __init__(self, name, ID_number, salary, email_address): + def __init__(self, name: str, ID_number: int, salary: float, email_address: str) -> None: """ Creates an Employeee object with name, ID_number, salary, and email_address values """ @@ -18,41 +18,43 @@ def __init__(self, name, ID_number, salary, email_address): self._salary = salary self._email_address = email_address - def get_name(self): + def get_name(self) -> str: """ returns employee name """ return self._name - - def get_ID_number(self): + + def get_ID_number(self) -> int: """ returns employee ID number """ return self._ID_number - def get_salary(self): + def get_salary(self) -> float: """ returns employee salary """ return self._salary - def get_email_address(self): + def get_email_address(self) -> str: """ returns employee email_address """ return self._email_address -def make_employee_dict(names, id_nums, salaries, email_addresses): + +def make_employee_dict(names: str, id_nums: int, salaries: float, email_addresses: str) -> dict[int, Employee]: """ Input: 4 lists of names, id numbers, salaries, and email_addresses of equal length Output: a dictionary where the id num is the key and the Employee object the value """ - employee_dict = {} + employee_dict: dict[int, Employee] = {} - i = 0 + i: int = 0 + id_: int for id_ in id_nums: employee_dict[id_] = Employee(names[i], id_nums[i], salaries[i], email_addresses[i]) i += 1 - return employee_dict \ No newline at end of file + return employee_dict diff --git a/02-IDE_exercises/solutions/09-project/AddThreeGame.py b/02-IDE_exercises/solutions/09-project/AddThreeGame.py index 9afaa4c..f2af4ba 100644 --- a/02-IDE_exercises/solutions/09-project/AddThreeGame.py +++ b/02-IDE_exercises/solutions/09-project/AddThreeGame.py @@ -18,17 +18,16 @@ class AddThreeGame: then the game ends in a draw. """ - def __init__(self): + def __init__(self) -> None: """ intializes 4 private variables to be used throughout the Class """ - self._nums_chosen = [] - self._current_state = "UNFINISHED" - self._player_turn = "first" - self._player_score = 0 + self._nums_chosen: list[int] = [] + self._current_state: str = "UNFINISHED" + self._player_turn: str = "first" + self._player_score: int = 0 - - def get_current_state(self): + def get_current_state(self) -> None: """ get method to allow user to see the state of the game. Input: VOID @@ -36,20 +35,20 @@ def get_current_state(self): """ return self._current_state - def get_player_turn(self): + def get_player_turn(self) -> str: """ returns player turn private variable """ return self._player_turn - - def get_player_choices(self): + + def get_player_choices(self) -> None: """ returns array of player choices for first or second player, depending on instance of player turn """ if self._player_turn == "first": - n = 0 + n: int = 0 elif self._player_turn == "second": - n = 1 + n: int = 1 return self._nums_chosen[n::2] @@ -60,7 +59,7 @@ def _set_current_state(self): """ if self._check_score(self.get_player_choices()): self._current_state = self.get_player_turn().upper() + '_' + 'WON' - + elif len(self._nums_chosen) == 9: self._current_state = 'DRAW' @@ -70,36 +69,39 @@ def _set_player_turn(self): """ if self._player_turn == "first": self._player_turn = "second" - + else: self._player_turn = "first" - def _check_score(self, arr): + def _check_score(self, arr: list[int]) -> bool: if len(arr) < 3: return False - - choices = set(arr) - for i in range(len(arr) -1): + + choices: list[int] = set(arr) + i: int + for i in range(len(arr) - 1): + j: int for j in range(i + 1, len(arr)): - solution = 15 - (arr[i] + arr[j]) - + solution: int = 15 - (arr[i] + arr[j]) + if solution in choices and solution not in [arr[i], arr[j]]: return True else: return False - def make_move(self, player, num_choice): + def make_move(self, player: str, num_choice: int) -> bool: """ public method that allows players to interact with the game Input: takes a player variable either "first" or "second" and an integer from 1-9 Output: Returns True if rules of game are met. """ + self._current_state: str if self._current_state != "UNFINISHED": return False - + if player != self._player_turn: return False - + if 9 < num_choice or num_choice < 1: return False @@ -110,5 +112,5 @@ def make_move(self, player, num_choice): self._nums_chosen.append(num_choice) self._set_current_state() self._set_player_turn() - - return True \ No newline at end of file + + return True diff --git a/02-IDE_exercises/solutions/10-project/BuildersGame.py b/02-IDE_exercises/solutions/10-project/BuildersGame.py index 4a5e325..1f1dec3 100644 --- a/02-IDE_exercises/solutions/10-project/BuildersGame.py +++ b/02-IDE_exercises/solutions/10-project/BuildersGame.py @@ -1,8 +1,7 @@ -# Author: Brandon Hoffman -# Date: 11/25/2020 -# Description: A class called BuildersGame that represents the board for a two-player game (Player X and Player O) that is played on a 5x5 grid. -# During the game, each players' builders will move around the board and add levels to towers. The winner is the -# first one to move a builder on top of a 3-story tower. +# Author: Brandon Hoffman Date: 11/25/2020 Description: A class called BuildersGame that represents the board for +# a two-player game (Player X and Player O) that is played on a 5x5 grid. During the game, each players' builders +# will move around the board and add levels to towers. The winner is the first one to move a builder on top of a +# 3-story tower. class BuildersGame: @@ -12,7 +11,7 @@ class BuildersGame: first one to move a builder on top of a 3-story tower. """ - def __init__(self): + def __init__(self) -> None: """ intializes 12 private variables to be used throughout the Class _current_state represents the state game as "UNFINISHED", "X_WON, "O_WON" @@ -28,36 +27,38 @@ def __init__(self): pieces or just do their regular turns """ - self._current_state = "UNFINISHED" - self._board = [[0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0]] + self._current_state: str = "UNFINISHED" + self._board: list[list[int]] = [[0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0]] - self._x1_position = (0, 0) - self._x2_position = (0, 0) - self._o1_position = (0, 0) - self._o2_position = (0, 0) + self._x1_position: tuple[int, int] = (0, 0) + self._x2_position: tuple[int, int] = (0, 0) + self._o1_position: tuple[int, int] = (0, 0) + self._o2_position: tuple[int, int] = (0, 0) - self._x1_height = self._board[self._x1_position[0]][self._x1_position[1]] - self._x2_height = self._board[self._x2_position[0]][self._x2_position[1]] - self._o1_height = self._board[self._o1_position[0]][self._o1_position[1]] - self._o2_height = self._board[self._o2_position[0]][self._o2_position[1]] + self._x1_height: int = self._board[self._x1_position[0]][self._x1_position[1]] + self._x2_height: int = self._board[self._x2_position[0]][self._x2_position[1]] + self._o1_height: int = self._board[self._o1_position[0]][self._o1_position[1]] + self._o2_height: int = self._board[self._o2_position[0]][self._o2_position[1]] - self._player_turn = "x" - self._turn_count = 1 + self._player_turn: str = "x" + self._turn_count: int = 1 - def print_board(self): + def print_board(self) -> None: """ displays board for testing purposes """ + row: int row = 0 - print(" ", 0, "", 1, "", 2, "", 3, "", 4,) + print(" ", 0, "", 1, "", 2, "", 3, "", 4, ) + line: int for line in self._board: print(row, line) row += 1 - + self._set_player_height() print() @@ -66,32 +67,30 @@ def print_board(self): print("o1 piece:", self._o1_position, "height:", self._o1_height) print("o2 piece:", self._o2_position, "height:", self._o2_height) - - def _set_player_turn(self): + def _set_player_turn(self) -> None: """ private method to change the _player_turn variable to the next player """ if self._player_turn == "x": self._player_turn = "o" - + else: self._player_turn = "x" - def _set_player_height(self): + def _set_player_height(self) -> None: """ private method that updates the private player height variables """ - self._x1_height = self._board[self._x1_position[0]][self._x1_position[1]] - self._x2_height = self._board[self._x2_position[0]][self._x2_position[1]] - self._o1_height = self._board[self._o1_position[0]][self._o1_position[1]] - self._o2_height = self._board[self._o2_position[0]][self._o2_position[1]] - + self._x1_height: int = self._board[self._x1_position[0]][self._x1_position[1]] + self._x2_height: int = self._board[self._x2_position[0]][self._x2_position[1]] + self._o1_height: int = self._board[self._o1_position[0]][self._o1_position[1]] + self._o2_height: int = self._board[self._o2_position[0]][self._o2_position[1]] - def _check_player_turn(self, from_row, from_col): + def _check_player_turn(self, from_row: int, from_col: int) -> bool: """ returns a boolean of False if it's not the appropriate players turn """ - player_referenced = None + player_referenced: str | None = None if (from_row, from_col) in [self._x1_position, self._x2_position]: player_referenced = 'x' @@ -101,13 +100,14 @@ def _check_player_turn(self, from_row, from_col): if player_referenced != self._player_turn: return False - def get_current_state(self): + def get_current_state(self) -> str: """ returns private _current_state variable """ return self._current_state - def _check_legal_range(self, from_row, from_col, to_row, to_col, build_row, build_col): + def _check_legal_range(self, from_row: int, from_col: int, to_row: int, to_col: int, build_row: int, + build_col: int) -> None: """ returns False if row or column variable is less than 0 or greater than 4 """ @@ -129,7 +129,8 @@ def _check_legal_range(self, from_row, from_col, to_row, to_col, build_row, buil elif 4 < build_col or build_col < 0: return False - def _check_legal_move(self, from_row, from_col, to_row, to_col, build_row, build_col): + def _check_legal_move(self, from_row: int, from_col: int, to_row: int, to_col: int, build_row: int, + build_col: int) -> None: """ returns False if a move taken is greater than 1 in the respective column or row variable """ @@ -145,36 +146,41 @@ def _check_legal_move(self, from_row, from_col, to_row, to_col, build_row, build elif abs(to_col - build_col) > 1: return False - def _check_player_occupied(self, from_row, from_col, to_row, to_col, build_row, build_col): + def _check_player_occupied(self, from_row: int, from_col: int, to_row: int, to_col: int, build_row: int, + build_col: int) -> None: """ returns True if a space being moved to is occupied by a different piece """ - player_positions = [self._x1_position, self._x2_position, self._o1_position, self._o2_position] + player_positions: list[tuple[int, int]] = [self._x1_position, self._x2_position, self._o1_position, + self._o2_position] if (to_row, to_col) in player_positions: return True - player_positions = [self._x1_position, self._x2_position, self._o1_position, self._o2_position] + player_positions: list[tuple[int, int]] = [self._x1_position, self._x2_position, self._o1_position, + self._o2_position] - # If space is not occupied by other player, we need to temporarily imagine moving our piece, so that our original location is available to build on - # So we'll mutate the player_positions list with an updated position variable without actually changing a position variable - i = 0 + # If space is not occupied by other player, we need to temporarily imagine moving our piece, so that our + # original location is available to build on So we'll mutate the player_positions list with an updated + # position variable without actually changing a position variable + i: int = 0 + position: int for position in player_positions: if position == (from_row, from_col): player_positions[i] = (to_row, to_col) break - + i += 1 if (build_row, build_col) in player_positions: return True - def _check_tower_level(self, from_row, from_col, to_row, to_col): + def _check_tower_level(self, from_row: int, from_col: int, to_row: int, to_col: int) -> bool: """ returns True if the height you're moving to less than your the tower height you're moving from is less than 2. returns False if diff is 2 or greater """ - from_sq_height = self._board[from_row][from_col] - to_sq_height = self._board[to_row][to_col] + from_sq_height: int = self._board[from_row][from_col] + to_sq_height: int = self._board[to_row][to_col] if to_sq_height - from_sq_height < 2: return True @@ -182,12 +188,12 @@ def _check_tower_level(self, from_row, from_col, to_row, to_col): else: return False - def _can_build_tower(self, build_row, build_col): + def _can_build_tower(self, build_row: int, build_col: int) -> bool: """ Returns True if square player wants to build on is less than True False otherwise """ - build_sq_height = self._board[build_row][build_col] + build_sq_height: int = self._board[build_row][build_col] if build_sq_height < 4: return True @@ -195,7 +201,7 @@ def _can_build_tower(self, build_row, build_col): else: return False - def _set_current_state(self, from_row, from_col, to_row, to_col, build_row, build_col): + def _set_current_state(self, from_row: int, from_col: int, to_row: int, to_col: int, build_row: int, build_col: int) -> None: """ sets the current state to X_WON or O_WON if the respective player piece lands on a tower level 3 """ @@ -211,7 +217,6 @@ def _set_current_state(self, from_row, from_col, to_row, to_col, build_row, buil elif (from_row, from_col) == self._o2_position: self._o2_position = (to_row, to_col) - if self._board[to_row][to_col] >= 3: if self._player_turn == 'x': self._current_state = "X_WON" @@ -221,7 +226,7 @@ def _set_current_state(self, from_row, from_col, to_row, to_col, build_row, buil self._board[build_row][build_col] += 1 - def _check_legal_inputs(self, from_row, from_col, to_row, to_col, build_row, build_col): + def _check_legal_inputs(self, from_row: int, from_col: int, to_row: int, to_col: int, build_row: int, build_col: int) -> bool: """ Compiles conditional methods above. Returns False if inputs are illegal """ @@ -236,7 +241,7 @@ def _check_legal_inputs(self, from_row, from_col, to_row, to_col, build_row, bui elif self._check_player_turn(from_row, from_col) == False: return False - + elif self._check_legal_move(from_row, from_col, to_row, to_col, build_row, build_col) == False: return False @@ -249,8 +254,7 @@ def _check_legal_inputs(self, from_row, from_col, to_row, to_col, build_row, bui elif self._can_build_tower(build_row, build_col) == False: return False - - def initial_placement(self, t1_row, t1_col, t2_row, t2_col, player): + def initial_placement(self, t1_row: int, t1_col: int, t2_row: int, t2_col: int, player: str) -> bool: """ Takes in a row and col input for both pieces a player intially places as well as a player value of x or o If conditions for intial placement are not met returns False @@ -280,7 +284,7 @@ def initial_placement(self, t1_row, t1_col, t2_row, t2_col, player): if 4 < t1_row or t1_row < 0: return False - + if 4 < t1_col or t1_col < 0: return False @@ -293,19 +297,19 @@ def initial_placement(self, t1_row, t1_col, t2_row, t2_col, player): else: - if player == "x": - self._x1_position = (t1_row, t1_col) - self._x2_position = (t2_row, t2_col) + if player == "x": + self._x1_position: tuple[int, int] = (t1_row, t1_col) + self._x2_position: tuple[int, int] = (t2_row, t2_col) elif player == "o": - self._o1_position = (t1_row, t1_col) - self._o2_position = (t2_row, t2_col) + self._o1_position: tuple[int, int] = (t1_row, t1_col) + self._o2_position: tuple[int, int] = (t2_row, t2_col) self._set_player_turn() self._turn_count += 1 return True - def make_move(self, from_row, from_col, to_row, to_col, build_row, build_col): + def make_move(self, from_row: int, from_col: int, to_row: int, to_col: int, build_row: int, build_col: int) -> bool: """ first calls _check_legal_inputs variable. If inputs are legal, calls the set_current state method, set_player_turn method and increase _turn_count variable """ @@ -313,13 +317,9 @@ def make_move(self, from_row, from_col, to_row, to_col, build_row, build_col): return False else: - + self._set_current_state(from_row, from_col, to_row, to_col, build_row, build_col) self._set_player_turn() self._turn_count += 1 return True - - - - diff --git a/IDE_exercises/.DS_Store b/IDE_exercises/.DS_Store new file mode 100644 index 0000000..791b325 Binary files /dev/null and b/IDE_exercises/.DS_Store differ