Skip to content

Commit

Permalink
Add Hangman code
Browse files Browse the repository at this point in the history
  • Loading branch information
lipi17dpatnaik committed Apr 23, 2019
1 parent 553eac6 commit cef8473
Show file tree
Hide file tree
Showing 7 changed files with 8,187 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Hangman/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Hangman-OpenCV
This repository contains the code for Hangman game made using OpenCV library in Python.

## Outline
1. Use **pandas** library to process the **IMDb database**.
- Remove the columns which are not required. This has been done by removing the columns which are required from the list of all column headings. We are keeping **`release_year`**, **`cast`**, **`director`**, **`keywords`** and **`tagline`** as 5 features of a movie.
- All the rows having at least one missing value are removed using `dropna` function from Pandas.
- We only keep the rows which have english alpha numerical movie title.
- We remove all the rows which have `,` in the movie title or any of the 5 features. This has been done to make sure that the commas are present only to separate the data across columns in CSV file.
- We only keep movies with titles less than or equal to 20 and tagline length less than or equal to 30.
- Save the final database to a CSV file using `to_csv` function from Pandas.

2. Read the dataset from the CSV file and store it in dictionary format: `movie_title:[year,list of keywords, tagline, director, list of cast]`- **`read_from_csv`**

3. Get a random movie from the list of movies and get all the information (5 features) for that movie - **`get_movie_info`**

4. From the 5 movie features, select any 3 features. If the features have list of keywords and/or list of cast, randomly select one from the list - **`select_hints`**

5. Read the hangman template - **`get_canvas`**

6. Get the points where each blank rectanglular box will be drawn depending on the maximum width and height among all the characters present in the movie name - **`get_char_coords`**

7. Draw the blank rectangular boxes - **`draw_blank_rects`**

8. While the number of incorrect attempts is less than 6 and the game hasn't been won or lost yet:
- Take character input from user
- If the character is invalid (not an alphabet), display `INVALID CHARACTER`
- Else if the character has already been entered, display `ALREADY USED`
- Else if the character is NOT present in movie title, display `WRONG`, increment number of incorrect attempts, display another body part in Hangman, and display the character in `Letters used`
- Else, display `CORRECT`, display the letters in the blank rectangles and display the character in `Letters used`
- If all characters in movie have been guessed, break the loop and display `YOU WON`
- If number of incorrect attempts are equal to or more than 6, break the loop and display `YOU LOST`

9. Reveal the movie name at the end of the game.

10. Wait for the user to press a key and quit.

**The entire game will run on full screen by default**.

## Instructions
`python main.py`

## Requirements
1. OpenCV 3.4
2. Pandas
3. Numpy

Binary file added Hangman/blank-canvas.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions Hangman/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import cv2
import numpy as np
from utils import *

movie_csv = "movies-list-short.csv"
canvas = "blank-canvas.png"

movies_data = read_from_csv(movie_csv)

movie, movie_info = get_movie_info(movies_data)

print(movie)

hints,labels = select_hints(movie_info)

img = get_canvas(canvas)

char_rects = get_char_coords(movie)

img = draw_blank_rects(movie,char_rects,img)

cv2.namedWindow("Hangman", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("Hangman",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

cv2.imshow("Hangman",img)

chars_entered = []

incorrect_attempts = 0

img_copy = img.copy()

while 1:
img = img_copy.copy()
img = draw_hint(img,hints,labels,incorrect_attempts)
if incorrect_attempts >= 6:
img = draw_lost(img)
break
elif check_all_chars_found(movie, chars_entered):
img = draw_won(img)
break
else:
letter = cv2.waitKey(0) & 0xFF
if letter < 65 or letter > 122 or (letter > 90 and letter < 97):
img = draw_invalid(img)
cv2.imshow("Hangman",img)
continue
else:
letter = chr(letter).upper()
if letter in chars_entered:
img = draw_reuse(img)
img = draw_used_chars(img,chars_entered,letter)
cv2.imshow("Hangman",img)
continue
else:
chars_entered.append(letter)
if letter in movie:
img = draw_right(img)
img = displayLetter(img,letter,movie,char_rects)
img_copy = displayLetter(img_copy,letter,movie,\
char_rects)
else:
img = draw_wrong(img,incorrect_attempts)
incorrect_attempts += 1
img = draw_used_chars(img,chars_entered,letter)
img = draw_hangman(img,incorrect_attempts)
img_copy = draw_used_chars(img_copy,chars_entered,letter)
img_copy = draw_hangman(img_copy,incorrect_attempts)
cv2.imshow("Hangman",img)
#cv2.waitKey(0)

img = revealMovie(movie,img,char_rects)
cv2.imshow("Hangman",img)
cv2.waitKey(0)

cv2.destroyAllWindows()
Loading

0 comments on commit cef8473

Please sign in to comment.