hamngan
This commit is contained in:
parent
788ac7c2fa
commit
99215a91e4
|
@ -26,32 +26,66 @@ namespace dotbot.Commands
|
|||
{
|
||||
var gameId = Context.Channel.Id;
|
||||
_games.Add(gameId, new HangmanSession(secret));
|
||||
await Context.Message.DeleteAsync();
|
||||
await ReplyAsync($"{_games[gameId]}");
|
||||
}
|
||||
|
||||
|
||||
[Command("stop")]
|
||||
[Priority(1)]
|
||||
[Summary("ends a hangman session")]
|
||||
public async Task StopGame()
|
||||
{
|
||||
var gameId = Context.Channel.Id;
|
||||
if (_games.ContainsKey(gameId))
|
||||
{
|
||||
var game = _games[gameId];
|
||||
_games.Remove(gameId);
|
||||
await ReplyAsync($"game stopped. the secret word was {game.SecretWord}");
|
||||
}
|
||||
else
|
||||
{
|
||||
await ReplyAsync($"no game started in this channel...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class HangmanSession
|
||||
{
|
||||
internal string SecretWord;
|
||||
internal char[] SecretWordLetters => SecretWord.ToCharArray().Distinct().OrderBy(a => a).ToArray();
|
||||
internal char[] GuessedLetters;
|
||||
internal IEnumerable<char> SecretWordLetters => SecretWord.ToCharArray().Distinct().OrderBy(a => a);
|
||||
internal List<char> GuessedLetters;
|
||||
private int Guesses;
|
||||
public bool GameOver => SecretWordLetters.SequenceEqual(GuessedLetters.OrderBy(c => c)) && Guesses < Hangman.Gallows.Length;
|
||||
public bool GameOver => SecretWordLetters.SequenceEqual(GuessedLetters.OrderBy(c => c)) && Guesses >= Hangman.Gallows.Length;
|
||||
|
||||
public HangmanSession(string secretWord)
|
||||
{
|
||||
SecretWord = secretWord;
|
||||
GuessedLetters = new List<char>();
|
||||
Guesses = 0;
|
||||
}
|
||||
|
||||
|
||||
public bool Guess(char guess)
|
||||
{
|
||||
if (GuessedLetters.Contains(guess)) return false;
|
||||
GuessedLetters.Add(guess);
|
||||
if (!SecretWordLetters.Contains(guess)) Guesses++;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var Incorrects = GuessedLetters.Except(SecretWordLetters);
|
||||
return $"```{Hangman.Gallows[Guesses]}\n{ShowSecretWord()}\n\nGuessed Letters: {string.Join(' ', GuessedLetters)}\nIncorrect Letters: {string.Join(' ', Incorrects)}```";
|
||||
}
|
||||
|
||||
public string ShowSecretWord() => $"Word: {SecretWord.ToCharArray().Select<char, string>(c => $"{(GuessedLetters.Contains(c) ? c : '_')} ")}";
|
||||
public string ShowSecretWord()
|
||||
{
|
||||
var disp = SecretWord.ToCharArray().Select(c => $"{(GuessedLetters.Contains(c) ? c : '_')} ");
|
||||
return $"Word: {string.Join("", disp)}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ namespace dotbot.Services
|
|||
private readonly CommandService _commands;
|
||||
private readonly IConfigurationRoot _config;
|
||||
private readonly IServiceProvider _provider;
|
||||
private readonly HangmanService _hangman;
|
||||
private Dictionary<ulong, Poll> _polls;
|
||||
|
||||
public CommandHandlerService(
|
||||
|
@ -25,7 +26,8 @@ namespace dotbot.Services
|
|||
IConfigurationRoot config,
|
||||
IServiceProvider provider,
|
||||
DotbotDb db,
|
||||
PollService polls
|
||||
PollService polls,
|
||||
HangmanService hangman
|
||||
) {
|
||||
_discord = discord;
|
||||
_commands = commands;
|
||||
|
@ -33,6 +35,7 @@ namespace dotbot.Services
|
|||
_provider = provider;
|
||||
_db = db;
|
||||
_polls = polls.currentPolls;
|
||||
_hangman = hangman;
|
||||
|
||||
_discord.MessageReceived += OnMessageReceivedAsync;
|
||||
}
|
||||
|
@ -77,13 +80,39 @@ namespace dotbot.Services
|
|||
}
|
||||
}
|
||||
else
|
||||
{ // add poll options
|
||||
{
|
||||
// add poll options
|
||||
var id = context.Channel.Id;
|
||||
if (_polls.ContainsKey(id) && _polls[id].Owner == context.User && !_polls[id].IsOpen)
|
||||
{
|
||||
_polls[id].Options.Add(new PollOption { Text = msg.Content });
|
||||
await context.Channel.SendMessageAsync($"{msg.Content} added!");
|
||||
}
|
||||
|
||||
// handle hangman guess
|
||||
if (_hangman._activeGames.ContainsKey(id))
|
||||
{
|
||||
var game = _hangman._activeGames[id];
|
||||
if (msg.Content.Length != 1)
|
||||
{
|
||||
await context.Channel.SendMessageAsync($"send one letter at a time.\n\n{game}");
|
||||
return;
|
||||
}
|
||||
|
||||
if (game.Guess(msg.Content[0]))
|
||||
{ // proper guess
|
||||
if (game.GameOver)
|
||||
{
|
||||
await context.Channel.SendMessageAsync($"game over!\n\n{game}");
|
||||
return;
|
||||
}
|
||||
await context.Channel.SendMessageAsync($"{msg.Content[0]} guessed:\n\n{game}");
|
||||
}
|
||||
else
|
||||
{
|
||||
await context.Channel.SendMessageAsync($"letter already guessed... try again!\n\n{game}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue