|
1 | 1 | """
|
2 |
| -hey everyone it is a basic game code using random . in this game computer will randomly chose an number from 1 to 100 and players will have |
3 |
| -to guess that which number it is and game will tell him on every guss whether his/her guess is smaller or bigger than the chosen number. it is |
4 |
| -a multi player game so it can be played with many players there is no such limitations of user till the size of list. if any one wants to modify |
5 |
| -this game he/she is most welcomed. |
6 |
| - Thank you |
| 2 | +Random Number Guessing Game |
| 3 | +--------------------------- |
| 4 | +This is a simple multiplayer game where each player tries to guess a number |
| 5 | +chosen randomly by the computer between 1 and 100. After each guess, the game |
| 6 | +provides feedback whether the guess is higher or lower than the target number. |
| 7 | +The winner is the player who guesses the number in the fewest attempts. |
| 8 | +
|
| 9 | +Example: |
| 10 | + >>> import builtins, random |
| 11 | + >>> random.seed(0) |
| 12 | + >>> inputs = iter(["1", "Alice", "50", "49"]) |
| 13 | + >>> builtins.input = lambda prompt="": next(inputs) |
| 14 | + >>> from game import play_game |
| 15 | + >>> players, scores, winners = play_game() |
| 16 | + >>> players |
| 17 | + ['Alice'] |
| 18 | + >>> scores # doctest: +ELLIPSIS |
| 19 | + [2] |
| 20 | + >>> winners |
| 21 | + ['Alice'] |
7 | 22 | """
|
8 | 23 |
|
9 |
| -import os |
10 | 24 | import random
|
| 25 | +from typing import List, Tuple |
| 26 | + |
| 27 | + |
| 28 | +def get_players(n: int) -> List[str]: |
| 29 | + """ |
| 30 | + Prompt to enter `n` player names. |
11 | 31 |
|
12 |
| -players = [] |
13 |
| -score = [] |
| 32 | + Args: |
| 33 | + n (int): number of players |
14 | 34 |
|
15 |
| -print( |
16 |
| - "\n\tRandom Number Game\n\nHello Everyone ! it is just a game of chance in which you have to guess a number" |
17 |
| - " from 0 to 100 and computer will tell whether your guess is smaller or bigger than the acctual number chossen by the computer . " |
18 |
| - "the person with less attempts in guessing the number will be winner ." |
19 |
| -) |
20 |
| -x = input() |
21 |
| -os.system("cls") |
| 35 | + Returns: |
| 36 | + List[str]: list of player names |
22 | 37 |
|
23 |
| -n = int(input("Enter number of players : ")) |
24 |
| -print() |
| 38 | + Example: |
| 39 | + >>> import builtins |
| 40 | + >>> inputs = iter(["Alice", "Bob"]) |
| 41 | + >>> builtins.input = lambda prompt="": next(inputs) |
| 42 | + >>> get_players(2) |
| 43 | + ['Alice', 'Bob'] |
| 44 | + """ |
| 45 | + return [input("Enter name of player: ") for _ in range(n)] |
25 | 46 |
|
26 |
| -for i in range(0, n): |
27 |
| - name = input("Enter name of player : ") |
28 |
| - players.append(name) |
29 | 47 |
|
30 |
| -os.system("cls") |
| 48 | +def play_turn(player: str) -> int: |
| 49 | + """ |
| 50 | + Let a player try to guess a random number. |
31 | 51 |
|
32 |
| -for i in range(0, n): |
33 |
| - orignum = random.randint(1, 100) |
34 |
| - print(players[i], "your turn :", end="\n\n") |
35 |
| - count = 0 |
| 52 | + Args: |
| 53 | + player (str): player name |
| 54 | +
|
| 55 | + Returns: |
| 56 | + int: number of attempts taken |
| 57 | +
|
| 58 | + Example: |
| 59 | + >>> import builtins, random |
| 60 | + >>> random.seed(1) |
| 61 | + >>> inputs = iter(["30", "15", "9"]) |
| 62 | + >>> builtins.input = lambda prompt="": next(inputs) |
| 63 | + >>> play_turn("Alice") # doctest: +ELLIPSIS |
| 64 | + 3 |
| 65 | + """ |
| 66 | + target = random.randint(1, 100) |
| 67 | + print(f"\n{player}, it's your turn!") |
| 68 | + attempts = 0 |
36 | 69 | while True:
|
37 |
| - ch = int(input("Please enter your guess : ")) |
38 |
| - if ch > orignum: |
39 |
| - print("no! number is smaller...") |
40 |
| - count += 1 |
41 |
| - elif ch == orignum: |
42 |
| - print("\n\n\tcongrats you won") |
43 |
| - break |
| 70 | + guess = int(input("Please enter your guess: ")) |
| 71 | + attempts += 1 |
| 72 | + if guess > target: |
| 73 | + print("Too high, try smaller...") |
| 74 | + elif guess < target: |
| 75 | + print("Too low, try bigger...") |
44 | 76 | else:
|
45 |
| - print("nope ! number is large dude...") |
46 |
| - count += 1 |
47 |
| - print(" you have taken", count + 1, "attempts") |
48 |
| - x = input() |
49 |
| - score.append(count + 1) |
50 |
| - os.system("cls") |
51 |
| -print("players :\n") |
52 |
| -for i in range(0, n): |
53 |
| - print(players[i], "-", score[i]) |
54 |
| - |
55 |
| -print("\n\nwinner is :\n") |
56 |
| -for i in range(0, n): |
57 |
| - if score[i] == min(score): |
58 |
| - print(players[i]) |
59 |
| -x = input() |
| 77 | + print("Congratulations! You guessed it!") |
| 78 | + return attempts |
| 79 | + |
| 80 | + |
| 81 | +def play_game() -> Tuple[List[str], List[int], List[str]]: |
| 82 | + """ |
| 83 | + Run the multiplayer game. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + Tuple[List[str], List[int], List[str]]: (players, scores, winners) |
| 87 | +
|
| 88 | + Example: |
| 89 | + >>> import builtins, random |
| 90 | + >>> random.seed(2) |
| 91 | + >>> inputs = iter(["1", "Eve", "30", "13"]) |
| 92 | + >>> builtins.input = lambda prompt="": next(inputs) |
| 93 | + >>> players, scores, winners = play_game() |
| 94 | + >>> players |
| 95 | + ['Eve'] |
| 96 | + >>> scores # doctest: +ELLIPSIS |
| 97 | + [2] |
| 98 | + >>> winners |
| 99 | + ['Eve'] |
| 100 | + """ |
| 101 | + n = int(input("Enter number of players: ")) |
| 102 | + players = get_players(n) |
| 103 | + scores = [play_turn(p) for p in players] |
| 104 | + min_score = min(scores) |
| 105 | + winners = [p for p, s in zip(players, scores) if s == min_score] |
| 106 | + print("\nResults:") |
| 107 | + for p, s in zip(players, scores): |
| 108 | + print(f"{p}: {s} attempts") |
| 109 | + print("\nWinner(s):", ", ".join(winners)) |
| 110 | + return players, scores, winners |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + import doctest |
| 115 | + |
| 116 | + doctest.testmod() |
| 117 | + play_game() |
0 commit comments