Skip to content

Commit a3b785f

Browse files
authored
Create tictactoe.py
1 parent 68e7459 commit a3b785f

1 file changed

Lines changed: 151 additions & 0 deletions

File tree

tictactoe.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
from random import randint
2+
from os import system, name
3+
def pick_random_word():
4+
word_list = [ "about", "above", "across", "act", "active", "activity", "add", "afraid", "after", "again", "age", "ago", "agree", "air", "all", "alone", "along", "already", "always", "am", "amount", "an", "and", "angry", "another", "answer", "any", "anyone", "anything", "anytime", "appear", "apple", "are", "area", "arm", "army", "around", "arrive", "art", "as", "ask", "at", "attack", "aunt", "autumn", "away","baby", "back", "bad", "bag", "ball", "bank", "base", "basket", "bath", "be", "bean", "bear", "beautiful", "bed", "bedroom", "beer", "behave", "before", "begin", "behind", "bell", "below", "besides", "best", "better", "between", "big", "bird", "birth", "birthday", "bit", "bite", "black", "bleed", "block", "blood", "blow", "blue", "board", "boat", "body", "boil", "bone", "book", "border", "born", "borrow", "both", "bottle", "bottom", "bowl", "box", "boy", "branch", "brave", "bread", "break", "breakfast", "breathe", "bridge", "bright", "bring", "brother", "brown", "brush", "build", "burn", "business", "bus", "busy", "but", "buy", "by", "cake", "call", "can", "candle", "cap", "car", "card", "care", "careful", "careless", "carry", "case", "cat", "catch", "central", "century", "certain", "chair", "chance", "change", "chase", "cheap", "cheese", "chicken", "child", "children", "chocolate", "choice", "choose", "circle", "city", "class", "clever", "clean", "clear", "climb", "clock", "cloth", "clothes", "cloud", "cloudy", "close", "coffee", "coat", "coin", "cold", "collect", "colour", "comb", "comfortable", "common", "compare", "come", "complete", "computer", "condition", "continue", "control", "cook", "cool", "copper", "corn", "corner", "correct", "cost", "contain", "count", "country", "course", "cover", "crash", "cross", "cry", "cup", "cupboard", "cut","dance", "dangerous", "dark", "daughter", "day", "dead", "decide", "decrease", "deep", "deer", "depend", "desk", "destroy", "develop", "die", "different", "difficult", "dinner", "direction", "dirty", "discover", "dish", "do", "dog", "door", "double", "down", "draw", "dream", "dress", "drink", "drive", "drop", "dry", "duck", "dust", "duty" "each", "ear", "early", "earn", "earth", "east", "easy", "eat", "education", "effect", "egg", "eight", "either", "electric", "elephant", "else", "empty", "end", "enemy", "enjoy", "enough", "enter", "equal", "entrance", "escape", "even", "evening", "event", "ever", "every", "everyone", "exact", "everybody", "examination", "example", "except", "excited", "exercise", "expect", "expensive", "explain", "extremely", "eye", "face", "fact", "fail", "fall", "false", "family", "famous", "far", "farm", "father", "fast", "fat", "fault", "fear", "feed", "feel", "female", "fever", "few", "fight", "fill", "film", "find", "fine", "finger", "finish", "fire", "first", "fish", "fit", "five", "fix", "flag", "flat", "float", "floor", "flour", "flower", "fly", "fold", "food", "fool", "foot", "football", "for", "force", "foreign", "forest", "forget", "forgive", "fork", "form", "fox", "four", "free", "freedom", "freeze", "fresh", "friend", "friendly", "from", "front", "fruit", "full", "fun", "funny", "furniture", "further", "future", "game", "garden", "gate", "general", "gentleman", "get", "gift", "give", "glad", "glass", "go", "goat", "god", "gold", "good", "goodbye", "grandfather", "grandmother", "grass", "grave", "great", "green", "gray", "ground", "group", "grow", "gun", "hair", "half", "hall", "hammer", "hand", "happen", "happy", "hard", "hat", "hate", "have", "he", "head", "healthy", "hear", "heavy", "heart", "heaven", "height", "hello", "help", "hen", "her", "here", "hers", "hide", "high", "hill", "him", "his", "hit", "hobby", "hold", "hole", "holiday", "home", "hope", "horse", "hospital", "hot", "hotel", "house", "how", "hundred", "hungry", "hour", "hurry", "husband", "hurt", "ice", "idea", "if", "important", "in", "increase", "inside", "into", "introduce", "invent", "iron", "invite", "is", "island", "it", "its", "jelly", "job", "join", "juice", "jump", "just", "keep", "key", "kill", "kind", "king", "kitchen", "knee", "knife", "knock", "know", "ladder", "lady", "lamp", "land", "large", "last", "late", "lately", "laugh", "lazy", "lead", "leaf", "learn", "leave", "leg", "left", "lend", "length", "less", "lesson", "let", "letter", "library", "lie", "life", "light", "like", "lion", "lip", "list", "listen", "little", "live", "lock", "lonely", "long", "look", "lose", "lot", "love", "low", "lower", "luck", "machine", "main", "make", "male", "man", "many", "map", "mark", "market", "marry", "matter", "may", "me", "meal", "mean", "measure", "meat", "medicine", "meet", "member", "mention", "method", "middle", "milk", "million", "mind", "minute", "miss", "mistake", "mix", "model", "modern", "moment", "money", "monkey", "month", "moon", "more", "morning", "most", "mother", "mountain", "mouth", "move", "much", "music", "must", "my", "name", "narrow", "nation", "nature", "near", "nearly", "neck", "need", "needle", "neighbour", "neither", "net", "never", "new", "news", "newspaper", "next", "nice", "night", "nine", "no", "noble", "noise", "none", "nor", "north", "nose", "not", "nothing", "notice", "now", "number", "obey", "object", "ocean", "of", "off", "offer", "office", "often", "oil", "old", "on", "one", "only", "open", "opposite", "or", "orange", "order", "other", "our", "out", "outside", "over", "own", "page", "pain", "paint", "pair", "pan", "paper", "parent", "park", "part", "partner", "party", "pass", "past", "path", "pay", "peace", "pen", "pencil", "people", "pepper", "per", "perfect", "period", "person", "petrol", "photograph", "piano", "pick", "picture", "piece", "pig", "pin", "pink", "place", "plane", "plant", "plastic", "plate", "play", "please", "pleased", "plenty", "pocket", "point", "poison", "police", "polite", "pool", "poor", "popular", "position", "possible", "potato", "pour", "power", "present", "press", "pretty", "prevent", "price", "prince", "prison", "private", "prize", "probably", "problem", "produce", "promise", "proper", "protect", "provide", "public", "pull", "punish", "pupil", "push", "put", "queen", "question", "quick", "quiet", "quite", "radio", "rain", "rainy", "raise", "reach", "read", "ready", "real", "really", "receive", "record", "red", "remember", "remind", "remove", "rent", "repair", "repeat", "reply", "report", "rest", "restaurant", "result", "return", "rice", "rich", "ride", "right", "ring", "rise", "road", "rob", "rock", "room", "round", "rubber", "rude", "rule", "ruler", "run", "rush", "sad", "safe", "sail", "salt", "same", "sand", "save", "say", "school", "science", "scissors", "search", "seat", "second", "see", "seem", "sell", "send", "sentence", "serve", "seven", "several", "sex", "shade", "shadow", "shake", "shape", "share", "sharp", "she", "sheep", "sheet", "shelf", "shine", "ship", "shirt", "shoe", "shoot", "shop", "short", "should", "shoulder", "shout", "show", "sick", "side", "signal", "silence", "silly", "silver", "similar", "simple", "single", "since", "sing", "sink", "sister", "sit", "six", "size", "skill", "skin", "skirt", "sky", "sleep", "slip", "slow", "small", "smell", "smile", "smoke", "snow", "so", "soap", "sock", "soft", "some", "someone", "something", "sometimes", "son", "soon", "sorry", "sound", "soup", "south", "space", "speak", "special", "speed", "spell", "spend", "spoon", "sport", "spread", "spring", "square", "stamp", "stand", "star", "start", "station", "stay", "steal", "steam", "step", "still", "stomach", "stone", "stop", "store", "storm", "story", "strange", "street", "strong", "structure", "student", "study", "stupid", "subject", "substance", "successful", "such", "sudden", "sugar", "suitable", "summer", "sun", "sunny", "support", "sure", "surprise", "sweet", "swim", "sword", "table", "take", "talk", "tall", "taste", "taxi", "tea", "teach", "team", "tear", "telephone", "television", "tell", "ten", "tennis", "terrible", "test", "than", "that", "the", "their", "then", "there", "therefore", "these", "thick", "thin", "thing", "think", "third", "this", "though", "threat", "three", "tidy", "tie", "title", "to", "today", "toe", "together", "tomorrow", "tonight", "too", "tool", "tooth", "top", "total", "touch", "town", "train", "tram", "travel", "tree", "trouble", "true", "trust", "twice", "try", "turn", "type", "ugly", "uncle", "under", "understand", "unit", "until", "up", "use", "useful", "usual", "usually", "vegetable", "very", "village", "voice", "visit", "wait", "wake", "walk", "want", "warm", "was", "wash", "waste", "watch", "water", "way", "we", "weak", "wear", "weather", "wedding", "week", "weight", "welcome", "were", "well", "west", "wet", "what", "wheel", "when", "where", "which", "while", "white", "who", "why", "wide", "wife", "wild", "will", "win", "wind", "window", "wine", "winter", "wire", "wise", "wish", "with", "without", "woman", "wonder", "word", "work", "world", "worry", "yard", "yell", "yesterday", "yet", "you", "young", "your", "zero", "zoo"]
5+
selete_index = randint(0, len(word_list)-1)
6+
return word_list[selete_index]
7+
8+
def change_the_state(current_state, input_char, selected_word):
9+
modified_word = ""
10+
11+
for i in range(len(selected_word)):
12+
if current_state[i] == "_" and selected_word[i] == input_char:
13+
modified_word+=selected_word[i]
14+
15+
else:
16+
modified_word+=current_state[i]
17+
18+
return modified_word
19+
20+
def draw_hangman(attempts_remaining):
21+
if(attempts_remaining == 5):
22+
print ("_________")
23+
print ("| |")
24+
print ("|")
25+
print ("|")
26+
print ("|")
27+
print ("|")
28+
print ("|________")
29+
elif(attempts_remaining == 4):
30+
print ("_________")
31+
print ("| |")
32+
print ("| O")
33+
print ("|")
34+
print ("|")
35+
print ("|")
36+
print ("|________")
37+
elif(attempts_remaining == 3):
38+
print ("_________")
39+
print ("| |")
40+
print ("| O")
41+
print ("| |")
42+
print ("| |")
43+
print ("|")
44+
print ("|________")
45+
elif(attempts_remaining == 2):
46+
print ("_________")
47+
print ("| |")
48+
print ("| O")
49+
print ("| \|/")
50+
print ("| |")
51+
print ("|")
52+
print ("|________")
53+
elif(attempts_remaining == 1):
54+
print ("_________")
55+
print ("| |")
56+
print ("| O")
57+
print ("| \|/")
58+
print ("| |")
59+
print ("| |")
60+
print ("|________")
61+
elif(attempts_remaining == 0):
62+
print ("_________")
63+
print ("| |")
64+
print ("| O")
65+
print ("| \|/")
66+
print ("| |")
67+
print ("| / \\")
68+
print ("|________")
69+
70+
def print_current_state(current_state, attempts_remaining):
71+
72+
clear()
73+
draw_hangman(attempts_remaining)
74+
print("current word state :", end=" ")
75+
76+
for i in current_state:
77+
print(i, end=" ")
78+
79+
print("\t Attempts Remaining :",attempts_remaining)
80+
81+
82+
def check_in_word(selected_word, input_char, attempts_remaining, current_state):
83+
84+
if input_char in selected_word:
85+
current_state = change_the_state(current_state, input_char, selected_word)
86+
else:
87+
attempts_remaining-=1
88+
89+
return attempts_remaining, current_state
90+
91+
92+
def check_the_game(selected_word, current_state, attempts_remaining):
93+
94+
if(attempts_remaining<=0):
95+
print("Sorry You Lost! :( Try Again!)")
96+
print("The word was:",selected_word)
97+
return False
98+
99+
if selected_word==current_state:
100+
print("Congratulation!! Winner winner chicken dinner")
101+
return False
102+
return True
103+
104+
105+
106+
def play_game(attempts=5):
107+
selected_word = pick_random_word()
108+
current_state = ""
109+
for i in selected_word:
110+
if i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u':
111+
current_state+=i
112+
else:
113+
current_state+='_'
114+
115+
attempts_remaining = attempts
116+
117+
print_current_state(current_state, attempts_remaining)
118+
119+
while True:
120+
121+
input_char = input("Guess the character : ")
122+
123+
attempts_remaining, current_state = check_in_word(selected_word, input_char, attempts_remaining, current_state)
124+
125+
print_current_state(current_state, attempts_remaining)
126+
127+
128+
check_game = check_the_game(selected_word, current_state, attempts_remaining)
129+
130+
if check_game==False :
131+
132+
print("Do You Want to Play Again (Y/N) ")
133+
c=input()
134+
if c=="Y" or c=="y":
135+
play_game()
136+
137+
break
138+
139+
def clear():
140+
141+
# for windows
142+
if name == 'nt':
143+
_ = system('cls')
144+
145+
# for mac and linux
146+
else:
147+
_ = system('clear')
148+
149+
150+
if __name__ == "__main__":
151+
play_game()

0 commit comments

Comments
 (0)