Mini Project: Aim: To Make An AI Game - Hangman Using Python (Pygame) Lab Objective
Mini Project: Aim: To Make An AI Game - Hangman Using Python (Pygame) Lab Objective
Mini Project: Aim: To Make An AI Game - Hangman Using Python (Pygame) Lab Objective
Lab Objective:
To explain to students the basic issues of knowledge represetation and Logic so as to build
inference engines.
Course Objective:
To review the different stages of developement of the AI field from human like behavior to
Rational Agents.
Theory:
At each stage, we are guessing the letter (not previously guessed) which occurs in the largest
number of remaining possible words. There is some motivation to like this algorithm - we are
always minimally likely to lose a life. But, it strikes me that this isn't necessarily the best solution:
if we're trying to guess the word (within a certain number of lives), it's not necessarily always the
case that the most frequently occurring letter is the most useful letter to distinguish between the
remaining available words.
Alpha-beta Pruning:
Alpha–beta pruning is a search algorithm that seeks to decrease the number of nodes that are
evaluated by the minimax algorithm in its search tree. It is an adversarial search algorithm used
commonly for machine playing of two-player games. It stops evaluating a move when at least one
possibility has been found that proves the move to be worse than a previously examined move.
Such moves need not be evaluated further. When applied to a standard minimax tree, it returns the
same move as minimax would, but prunes away branches that cannot possibly influence the final
decision.
Alpha-Beta pruning is not actually a new algorithm, rather an optimization technique for minimax
algorithm. It reduces the computation time by a huge factor. This allows us to search much faster
and even go into deeper levels in the game tree. It cuts off branches in the game tree which need
not be searched because there already exists a better move available. It is called Alpha-Beta
pruning because it passes 2 extra parameters in the minimax function, namely alpha and beta.
Output:
When executed on command prompt
1
2
3
4
Source Code:-
import random
import os
hangmanpics = ['''
+---+
| |
|
|
|
|
|
=========== ''','''
+---+
| |
O |
|
|
|
|
=========== ''','''
+---+
| |
O |
| |
|
|
|
=========== ''','''
+---+
| |
O |
/| |
|
|
|
=========== ''','''
+---+
| |
O |
/|\ |
|
|
|
=========== ''','''
+---+
| |
O |
/|\ |
/ |
|
|
=========== ''','''
+---+
| |
O |
/|\ |
/\ |
|
|
=========== ''']
Country = 'india australia africa russia england pakistan afghanistan bhutan myannmar'
City = 'bangkok bangalore hyderabad newyork shangai berlin calcutta mumbai'
Famousp = 'Shahrukh Khan Gandhi Hitler Nehru Jackie Brucelee Salman khan'
def Countries():
print('You will have to guess the name of a Country')
return Country
def Cities():
print('You will have to guess the name of a City')
return City
def Famous():
print('You will have to guess this Famous Personality')
return Famousp
def err():
print('You will get a random word from the above 3')
return Country + City + Famousp
def Welcomenote():
choice = {
"1": Countries,
"2": Cities,
"3": Famous }
choose = input()
return choice.get(choose,err)().split()
def getRandomword(wordlist):
wordindex = random.randint(0,len(wordlist)-1)
return wordlist[wordindex]
def display(hangmanpics,missedletters,correctletters,secretword):
os.system('cls')
print(hangmanpics[len(missedletters)])
print()
for i in range(len(secretword)):
if secretword[i] in correctletters:
blanks = blanks[:i] + secretword[i] + blanks[i+1:]
print()
print()
def getguess(alreadyguessed):
while True:
print(' Guess a Letter ')
guess = input()
guess = guess.lower()
if len(guess) != 1:
print('Please enter single letters')
elif guess in alreadyguessed:
print('This letter is already guessed,choose a new letter')
else:
return guess
def playagain():
print('Do you wanna play again? (Yes or No)')
return input().lower().startswith('y')
done = False
while True:
display(hangmanpics,missedletters,correctletters,secretword)
guess = getguess(missedletters + correctletters)
if guess in secretword:
correctletters = correctletters + guess
found = True
for i in range(len(secretword)):
if secretword[i] not in correctletters:
found = False
break
if found:
print('You won the game ')
print('The secret word was -----> ' +secretword.upper())
done = True
else:
missedletters = missedletters + guess
if len(missedletters) == len(hangmanpics)-1 :
display(hangmanpics,missedletters,correctletters,secretword)
print('You have lost the game, the word was :' +secretword)
done = True
if done:
if playagain():
os.system('cls')
words = Welcomenote()
missedletters = ''
correctletters = ''
done = False
secretword = getRandomword(words)
else:
break
Conclusion:
We were able to execute the code and prepare a game using Python IDLE and Alpha-beta pruning
algorithm where computer is made to configure each move and thereby win the game.
Lab Outcomes:
Attain the capability to represent various real-life problem domains using logic-based techniques
Course Outcomes:
Attain the capability to represent various real-life problem domains using logic-based techniques