Skip to content

Commit cef8473

Browse files
Add Hangman code
1 parent 553eac6 commit cef8473

File tree

7 files changed

+8187
-0
lines changed

7 files changed

+8187
-0
lines changed

Hangman/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Hangman-OpenCV
2+
This repository contains the code for Hangman game made using OpenCV library in Python.
3+
4+
## Outline
5+
1. Use **pandas** library to process the **IMDb database**.
6+
- 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.
7+
- All the rows having at least one missing value are removed using `dropna` function from Pandas.
8+
- We only keep the rows which have english alpha numerical movie title.
9+
- 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.
10+
- We only keep movies with titles less than or equal to 20 and tagline length less than or equal to 30.
11+
- Save the final database to a CSV file using `to_csv` function from Pandas.
12+
13+
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`**
14+
15+
3. Get a random movie from the list of movies and get all the information (5 features) for that movie - **`get_movie_info`**
16+
17+
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`**
18+
19+
5. Read the hangman template - **`get_canvas`**
20+
21+
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`**
22+
23+
7. Draw the blank rectangular boxes - **`draw_blank_rects`**
24+
25+
8. While the number of incorrect attempts is less than 6 and the game hasn't been won or lost yet:
26+
- Take character input from user
27+
- If the character is invalid (not an alphabet), display `INVALID CHARACTER`
28+
- Else if the character has already been entered, display `ALREADY USED`
29+
- 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`
30+
- Else, display `CORRECT`, display the letters in the blank rectangles and display the character in `Letters used`
31+
- If all characters in movie have been guessed, break the loop and display `YOU WON`
32+
- If number of incorrect attempts are equal to or more than 6, break the loop and display `YOU LOST`
33+
34+
9. Reveal the movie name at the end of the game.
35+
36+
10. Wait for the user to press a key and quit.
37+
38+
**The entire game will run on full screen by default**.
39+
40+
## Instructions
41+
`python main.py`
42+
43+
## Requirements
44+
1. OpenCV 3.4
45+
2. Pandas
46+
3. Numpy
47+

Hangman/blank-canvas.png

1.42 KB
Loading

Hangman/main.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import cv2
2+
import numpy as np
3+
from utils import *
4+
5+
movie_csv = "movies-list-short.csv"
6+
canvas = "blank-canvas.png"
7+
8+
movies_data = read_from_csv(movie_csv)
9+
10+
movie, movie_info = get_movie_info(movies_data)
11+
12+
print(movie)
13+
14+
hints,labels = select_hints(movie_info)
15+
16+
img = get_canvas(canvas)
17+
18+
char_rects = get_char_coords(movie)
19+
20+
img = draw_blank_rects(movie,char_rects,img)
21+
22+
cv2.namedWindow("Hangman", cv2.WND_PROP_FULLSCREEN)
23+
cv2.setWindowProperty("Hangman",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
24+
25+
cv2.imshow("Hangman",img)
26+
27+
chars_entered = []
28+
29+
incorrect_attempts = 0
30+
31+
img_copy = img.copy()
32+
33+
while 1:
34+
img = img_copy.copy()
35+
img = draw_hint(img,hints,labels,incorrect_attempts)
36+
if incorrect_attempts >= 6:
37+
img = draw_lost(img)
38+
break
39+
elif check_all_chars_found(movie, chars_entered):
40+
img = draw_won(img)
41+
break
42+
else:
43+
letter = cv2.waitKey(0) & 0xFF
44+
if letter < 65 or letter > 122 or (letter > 90 and letter < 97):
45+
img = draw_invalid(img)
46+
cv2.imshow("Hangman",img)
47+
continue
48+
else:
49+
letter = chr(letter).upper()
50+
if letter in chars_entered:
51+
img = draw_reuse(img)
52+
img = draw_used_chars(img,chars_entered,letter)
53+
cv2.imshow("Hangman",img)
54+
continue
55+
else:
56+
chars_entered.append(letter)
57+
if letter in movie:
58+
img = draw_right(img)
59+
img = displayLetter(img,letter,movie,char_rects)
60+
img_copy = displayLetter(img_copy,letter,movie,\
61+
char_rects)
62+
else:
63+
img = draw_wrong(img,incorrect_attempts)
64+
incorrect_attempts += 1
65+
img = draw_used_chars(img,chars_entered,letter)
66+
img = draw_hangman(img,incorrect_attempts)
67+
img_copy = draw_used_chars(img_copy,chars_entered,letter)
68+
img_copy = draw_hangman(img_copy,incorrect_attempts)
69+
cv2.imshow("Hangman",img)
70+
#cv2.waitKey(0)
71+
72+
img = revealMovie(movie,img,char_rects)
73+
cv2.imshow("Hangman",img)
74+
cv2.waitKey(0)
75+
76+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)