Best Python code snippet using hypothesis
kids_terminal_game.py
Source:kids_terminal_game.py
1from typing import List, Optional, Tuple, Union2Playground = List[List[str]]3def new_playground(size: int) -> Playground:4 return [[" " for _ in range(size)] for _ in range(size)]5def init_playground(playground: Playground) -> None:6 half_index = len(playground) // 27 playground[half_index - 1][half_index - 1] = "X"8 playground[half_index][half_index] = "X"9 playground[half_index][half_index - 1] = "O"10 playground[half_index - 1][half_index] = "O"11def get(playground: Playground, row: int, col: int) -> str:12 return playground[row][col]13def set_symbol(playground: Playground, row: int,14 col: int, symbol: str) -> None:15 playground[row][col] = symbol16def draw_header(length: int) -> None:17 print(" ", end="")18 for i in range(length):19 print("{:<4}".format(i), end="")20 print("")21def draw_line(length: int) -> None:22 print(" +", end="")23 for _ in range(length):24 print("---+", end="")25 print("")26def draw_values(length: int, char: str,27 playground: Playground, row_num: int) -> None:28 print(" {} |".format(char), end="")29 for i in range(length):30 print(" {} |".format(playground[row_num][i]), end="")31 print("")32def draw(playground: Playground) -> None:33 length = len(playground)34 draw_header(length)35 draw_line(length)36 char = ord('A')37 for i in range(length):38 draw_values(length, chr(char), playground, i)39 draw_line(length)40 char += 141def valid_coordinates(playground: Playground, row: int, col: int) -> bool:42 return (0 <= row < len(playground)) and (0 <= col < len(playground))43def pick_enemy_symbol(symbol: str) -> str:44 symbols = ["X", "O"]45 symbols.remove(symbol)46 enemy_symbol = symbols[0]47 return enemy_symbol48def move_coordinates(row_move: int, col_move: int, course_row: int,49 course_col: int, forward: bool) -> Tuple[int, int]:50 if forward:51 row_move += course_row52 col_move += course_col53 else:54 row_move -= course_row55 col_move -= course_col56 return row_move, col_move57def move_forward(playground: Playground, row_move: int, col_move: int,58 course_row: int, course_col: int,59 symbol: str) -> Tuple[int, int]:60 while valid_coordinates(playground, row_move + course_row,61 col_move + course_col) \62 and playground[row_move][col_move] not in (" ", symbol):63 row_move, col_move = move_coordinates(row_move, col_move, course_row,64 course_col, forward=True)65 return row_move, col_move66def switch_stones(playground: Playground, row_move: int, col_move: int,67 course_row: int, course_col: int, symbol: str, row: int,68 col: int, enemy_symbol: str, switched_stones: int) -> int:69 while (row_move != row) or (col_move != col):70 row_move, col_move = move_coordinates(row_move, col_move, course_row,71 course_col, forward=False)72 if playground[row_move][col_move] == enemy_symbol:73 switched_stones += 174 playground[row_move][col_move] = symbol75 return switched_stones76def make_play(playground: Playground, symbol: str, row: int, col: int,77 enemy_symbol: str, just_strategy: bool) \78 -> Optional[Union[int, bool]]:79 switched: int = 080 directions: List[List[int]] = [[-1, -1], [0, -1], [1, -1], [-1, 0],81 [1, 0], [-1, 1], [0, 1], [1, 1]]82 for course_row, course_col in directions:83 row_move: int = row + course_row84 col_move: int = col + course_col85 if not valid_coordinates(playground, row_move, col_move) \86 or playground[row_move][col_move] != enemy_symbol:87 continue88 row_move, col_move = move_forward(playground, row_move, col_move,89 course_row, course_col, symbol)90 if playground[row_move][col_move] != symbol:91 continue92 if just_strategy:93 return True94 switched = switch_stones(playground, row_move, col_move,95 course_row, course_col, symbol, row, col,96 enemy_symbol, switched)97 if switched == 0:98 return None99 return switched100def play(playground: Playground, row: int,101 col: int, symbol: str) -> Optional[int]:102 if playground[row][col] != " ":103 return None104 enemy_symbol = pick_enemy_symbol(symbol)105 return make_play(playground, symbol, row, col,106 enemy_symbol, just_strategy=False)107def strategy(playground: Playground, symbol: str) -> Optional[Tuple[int, int]]:108 enemy_symbol = pick_enemy_symbol(symbol)109 for row in range(len(playground)):110 for col in range(len(playground[row])):111 if playground[row][col] != " ":112 continue113 if make_play(playground, symbol, row, col,114 enemy_symbol, just_strategy=True):115 return row, col116 return None117def count(playground: Playground) -> Tuple[int, int]:118 x_stones = 0119 o_stones = 0120 for row in playground:121 x_stones += row.count("X")122 o_stones += row.count("O")123 return x_stones, o_stones124def asks_for_starter() -> bool:125 start = input("DO YOU WANT TO START FIRST? [Y]/[N]: \n")126 if start.upper() == "Y":127 return True128 if start.upper() == "N":129 return False130 print("\n[WRONG CHOICE, TRY AGAIN]")131 return asks_for_starter()132def pick_symbol() -> Tuple[str, str]:133 symbol = input("PICK YOUR SYMBOL [X]/[O]: \n")134 symbol = symbol.upper()135 if symbol not in ("X", "O"):136 print("\n[WRONG SYMBOL, TRY AGAIN]")137 return pick_symbol()138 return symbol, pick_enemy_symbol(symbol)139def pick_coordinates(playground: Playground) -> Tuple[int, int]:140 print("\n[BOTH ROW AND COLLUM ARE NUMBERED FROM 0]")141 row = input("PICK NUMBER OF ROW YOU WANT TO PLAY ON: \n")142 col = input("PICK NUMBER OF COLLUM YOU WANT TO PLAY ON: \n")143 if not (row.isdecimal() and col.isdecimal()):144 print("\n[WRONG FORMAT OF COORDINATES, TRY AGAIN]")145 return pick_coordinates(playground)146 num_row = int(row)147 num_col = int(col)148 if not valid_coordinates(playground, num_row, num_col):149 print("\n[COORDINATES NOT ON PLAYGROUND, TRY AGAIN]")150 return pick_coordinates(playground)151 return num_row, num_col152def print_results(playground: Playground) -> None:153 x, o = count(playground)154 if x > o:155 print("PLAYER X WON")156 elif x < o:157 print("PLAYER O WON")158 else:159 print("DRAW")160def player_play(playground: Playground, symbol: str,161 enemy_turn: bool) -> Optional[bool]:162 draw(playground)163 row, col = pick_coordinates(playground)164 if play(playground, row, col, symbol) is None:165 print("\n[INVALID MOVE TRY AGAIN]\n")166 return pick_and_play(playground, symbol, enemy_turn)167 play(playground, row, col, symbol)168 return None169def enemy_play(playground: Playground, symbol: str) -> None:170 draw(playground)171 row_and_col = strategy(playground, symbol)172 if row_and_col is not None:173 row, col = row_and_col174 play(playground, row, col, symbol)175def end_of_the_game(playground: Playground, num_x: int, num_o: int) -> None:176 print("\n\nEND OF THE GAME")177 print("NUMBER OF STONES X:{} O:{}".format(num_x, num_o))178 print_results(playground)179 print("FINAL PLAYGROUND\n")180 draw(playground)181def pick_and_play(playground: Playground, symbol: str,182 enemy_turn: bool) -> bool:183 num_x, num_o = count(playground)184 if (num_x + num_o) == len(playground) ** 2:185 end_of_the_game(playground, num_x, num_o)186 return False187 print("\nNUMBER OF STONES X:{} O:{}\n".format(num_x, num_o))188 if strategy(playground, symbol) is None:189 print("SKIPPING ROUND, NOWHERE TO PLAY")190 return True191 if enemy_turn:192 enemy_play(playground, symbol)193 else:194 player_play(playground, symbol, enemy_turn)195 return True196def game(size: int) -> None:197 print("\n {}\n".format("*** REVERSI ***"))198 field = new_playground(size)199 init_playground(field)200 symbol, enemy_symbol = pick_symbol()201 next_turn = True202 if asks_for_starter():203 while next_turn:204 next_turn = (pick_and_play(field, symbol, enemy_turn=False) and205 pick_and_play(field, enemy_symbol, enemy_turn=True))206 else:207 while next_turn:208 next_turn = (pick_and_play(field, enemy_symbol, enemy_turn=True)...
test_strategy_state.py
Source:test_strategy_state.py
...138 else:139 return result2140 return source.flatmap(do_map)141 @rule(target=strategies, value=objects)142 def just_strategy(self, value):143 return just(value)144 @rule(target=strategy_tuples, source=strategies)145 def single_tuple(self, source):146 return (source,)147 @rule(target=strategy_tuples, left=strategy_tuples, right=strategy_tuples)148 def cat_tuples(self, left, right):149 return left + right150 @rule(target=objects, strat=strategies, data=data())151 def get_example(self, strat, data):152 data.draw(strat)153 @rule(target=strategies, left=integers(), right=integers())154 def integer_range(self, left, right):155 left, right = sorted((left, right))156 return integers(left, right)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!