1+ // **************************************************************** //
2+ //
3+ // Copyright (c) RimuruDev. All rights reserved.
4+ // Contact me:
5+ // - Gmail: rimuru.dev@gmail.com
6+ // - GitHub: https://github.com/RimuruDev
7+ // - LinkedIn: https://www.linkedin.com/in/rimuru/
8+ // - GitHub Organizations: https://github.com/Rimuru-Dev
9+ //
10+ // **************************************************************** //
11+
12+ #if UNITY_EDITOR
13+ using UnityEngine ;
14+ using UnityEditor ;
15+ using System . Collections . Generic ;
16+
17+ public sealed class HangmanGameInUnityEditorWindow : EditorWindow
18+ {
19+ private const string WindowName = "Hangman" ;
20+ private string secretWord = "UNITY" ;
21+ private char [ ] guessedWord ;
22+ private const int maxAttempts = 6 ;
23+ private int attemptsLeft ;
24+ private readonly List < char > guessedLetters = new ( ) ;
25+ private const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
26+
27+ [ MenuItem ( "RimuruDev Games/Hangman Game" ) ]
28+ public static void ShowWindow ( ) =>
29+ GetWindow < HangmanGameInUnityEditorWindow > ( false , WindowName , true ) ;
30+
31+ private void OnEnable ( ) =>
32+ StartNewGame ( ) ;
33+
34+ private void OnGUI ( )
35+ {
36+ DrawHeader ( ) ;
37+ DrawLetters ( ) ;
38+ CheckedGameState ( ) ;
39+ }
40+
41+ private void DrawHeader ( )
42+ {
43+ GUILayout . Label ( "Welcome to Hangman!" , EditorStyles . boldLabel ) ;
44+ GUILayout . Label ( "Guess the word:" ) ;
45+ GUILayout . Label ( new string ( guessedWord ) ) ;
46+ GUILayout . Label ( "Attempts left: " + attemptsLeft ) ;
47+ }
48+
49+ private void GameState ( string label )
50+ {
51+ GUILayout . Label ( label ) ;
52+
53+ if ( GUILayout . Button ( "Play Again" ) )
54+ StartNewGame ( ) ;
55+ }
56+
57+ private void CheckedGameState ( )
58+ {
59+ if ( attemptsLeft <= 0 )
60+ GameState ( "Game Over!" ) ;
61+ else if ( new string ( guessedWord ) . Equals ( secretWord ) )
62+ GameState ( "You Win!" ) ;
63+ }
64+
65+ private void DrawLetters ( )
66+ {
67+ GUILayout . BeginHorizontal ( ) ;
68+
69+ foreach ( var letter in Alphabet )
70+ {
71+ if ( guessedLetters . Contains ( letter ) )
72+ continue ;
73+
74+ if ( GUILayout . Button ( letter . ToString ( ) ) )
75+ {
76+ guessedLetters . Add ( letter ) ;
77+ GuessLetter ( letter ) ;
78+ }
79+ }
80+
81+ GUILayout . EndHorizontal ( ) ;
82+ }
83+
84+ private void GuessLetter ( char letter )
85+ {
86+ var correctGuess = false ;
87+
88+ for ( var i = 0 ; i < secretWord . Length ; i ++ )
89+ {
90+ if ( secretWord [ i ] == letter )
91+ {
92+ guessedWord [ i ] = letter ;
93+ correctGuess = true ;
94+ }
95+ }
96+
97+ if ( ! correctGuess )
98+ attemptsLeft -- ;
99+ }
100+
101+ private void StartNewGame ( )
102+ {
103+ secretWord = secretWord . ToUpper ( ) ;
104+ guessedWord = new char [ secretWord . Length ] ;
105+
106+ for ( var i = 0 ; i < secretWord . Length ; i ++ )
107+ guessedWord [ i ] = '_' ;
108+
109+ attemptsLeft = maxAttempts ;
110+ guessedLetters . Clear ( ) ;
111+ }
112+ }
113+ #endif
0 commit comments