-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
123 lines (109 loc) · 3.63 KB
/
Program.cs
File metadata and controls
123 lines (109 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Text.Json;
using RestSharp;
namespace WordleGame
{
class Program
{
private static string[] words;
private static string _tryWord;
private static char[] _currentTry;
private static string _wordToFind;
private static bool end = false;
static Random random = new Random();
static void Main ()
{
// First message
Console.Clear();
Console.WriteLine("Starting game, loading words...");
words = LoadWords();
// Palavra aleatória
_wordToFind = words[random.Next(0, words.Length-1)];
// Colocar a tamanho da palavra para a char array
_currentTry = new string('_', _wordToFind.Length).ToCharArray();
// Loop game
do
{
Start();
if (end){
EndGame();
}
}while (!end);
}
static void Start ()
{
Console.Clear();
Console.WriteLine("\n\nGuess the word! The Game\n\n");
Console.WriteLine($"Guess this word: {new string(_currentTry)} \nType 'help' for a tip\nType 'exit' to quit");
Console.Write("\nType: ");
_tryWord = Console.ReadLine();
// Show help
if (_tryWord == "help")
{
Console.WriteLine($"Word to find: {new string(_wordToFind.Reverse().ToArray())}");
Console.Read();
}
if (_tryWord == "exit")
{
end = true;
}
CheckAnswer();
}
static void EndGame()
{
Console.Clear();
Console.WriteLine($"\n\nCongratulations, you Win!!!\n\nThe word was *{_wordToFind}* \n\n");
string playAgain;
Console.WriteLine("Want to play again? \n(Y) for yes (N) for no");
playAgain = Console.ReadLine();
if (playAgain == "y"){
Console.Clear();
Console.WriteLine("Loading...");
// Reset word
_wordToFind = words[random.Next(0, words.Length-1)];
_currentTry = new string('_', _wordToFind.Length).ToCharArray();
end = false;
playAgain = "";
}
}
static void CheckAnswer ()
{
for (int i = 0; i < _wordToFind.Length; i++)
{
if (i < _tryWord.Length && _tryWord[i] == _wordToFind[i])
{
_currentTry[i] = _tryWord[i];
}
}
if (new string(_currentTry) == _tryWord)
{
end = true;
}
}
static string[] LoadWords ()
{
string _url = "https://random-word-api.herokuapp.com/all";
var _client = new RestClient(_url);
var _request = new RestRequest();
var _response = _client.Get(_request);
if (_response.IsSuccessful)
{
try
{
// Tratamento da resposta Json para string
string[] loadedWords = JsonSerializer.Deserialize<string[]>(_response.Content);
return loadedWords;
}
catch (Exception e)
{
Console.WriteLine("Erro: " + e.Message);
throw new Exception ("Erro" + e.Message);
}
}
else
{
throw new Exception ("Erro to load API");
}
}
}
}