Using the newest discord.net , fixes, improvements
If you draw 5 cards now, you get a hand value.
This commit is contained in:
parent
e0ff0b4447
commit
485783d146
@ -20,6 +20,9 @@ public class Cards
|
|||||||
{ 13, "King" },
|
{ 13, "King" },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static Dictionary<string, Func<List<Card>, bool>> handValues;
|
||||||
|
|
||||||
|
|
||||||
public enum CARD_SUIT
|
public enum CARD_SUIT
|
||||||
{
|
{
|
||||||
Spades = 1,
|
Spades = 1,
|
||||||
@ -28,7 +31,7 @@ public class Cards
|
|||||||
Clubs = 4
|
Clubs = 4
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Card
|
public class Card : IComparable
|
||||||
{
|
{
|
||||||
public CARD_SUIT suit;
|
public CARD_SUIT suit;
|
||||||
public int number;
|
public int number;
|
||||||
@ -63,6 +66,13 @@ public class Cards
|
|||||||
{
|
{
|
||||||
return cardNames[number] + " Of " + suit;
|
return cardNames[number] + " Of " + suit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int CompareTo(object obj)
|
||||||
|
{
|
||||||
|
if (!(obj is Card)) return 0;
|
||||||
|
var c = (Card)obj;
|
||||||
|
return this.number - c.number;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Card> cardPool;
|
private List<Card> cardPool;
|
||||||
@ -79,6 +89,7 @@ public class Cards
|
|||||||
{
|
{
|
||||||
cardPool = new List<Card>(52);
|
cardPool = new List<Card>(52);
|
||||||
RefillPool();
|
RefillPool();
|
||||||
|
InitHandValues();
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Restart the game of blackjack. It will only refill the pool for now. Probably wont be used, unless you want to have only 1 bjg running at one time,
|
/// Restart the game of blackjack. It will only refill the pool for now. Probably wont be used, unless you want to have only 1 bjg running at one time,
|
||||||
@ -116,28 +127,21 @@ public class Cards
|
|||||||
/// <returns>A card from the pool</returns>
|
/// <returns>A card from the pool</returns>
|
||||||
public Card DrawACard()
|
public Card DrawACard()
|
||||||
{
|
{
|
||||||
if (CardPool.Count > 0)
|
if (CardPool.Count == 0)
|
||||||
{
|
|
||||||
//you can either do this if your deck is not shuffled
|
|
||||||
Random r = new Random((int)DateTime.Now.Ticks);
|
|
||||||
int num = r.Next(0, cardPool.Count);
|
|
||||||
Card c = cardPool[num];
|
|
||||||
cardPool.RemoveAt(num);
|
|
||||||
return c;
|
|
||||||
|
|
||||||
// if you want to shuffle when you fill, then take the first one
|
|
||||||
/*
|
|
||||||
Card c = cardPool[0];
|
|
||||||
cardPool.RemoveAt(0);
|
|
||||||
return c;
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
|
|
||||||
//for now return null
|
|
||||||
Restart();
|
Restart();
|
||||||
return null;
|
//you can either do this if your deck is not shuffled
|
||||||
}
|
Random r = new Random((int)DateTime.Now.Ticks);
|
||||||
|
int num = r.Next(0, cardPool.Count);
|
||||||
|
Card c = cardPool[num];
|
||||||
|
cardPool.RemoveAt(num);
|
||||||
|
return c;
|
||||||
|
|
||||||
|
// if you want to shuffle when you fill, then take the first one
|
||||||
|
/*
|
||||||
|
Card c = cardPool[0];
|
||||||
|
cardPool.RemoveAt(0);
|
||||||
|
return c;
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Shuffles the deck. Use this if you want to take cards from the top of the deck, instead of randomly. See DrawACard method.
|
/// Shuffles the deck. Use this if you want to take cards from the top of the deck, instead of randomly. See DrawACard method.
|
||||||
@ -149,7 +153,77 @@ public class Cards
|
|||||||
cardPool.OrderBy(x => r.Next());
|
cardPool.OrderBy(x => r.Next());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//public override string ToString() => string.Join("", cardPool.Select(c => c.ToString())) + Environment.NewLine;
|
|
||||||
public override string ToString() => string.Join("", cardPool.Select(c => c.ToString())) + Environment.NewLine;
|
public override string ToString() => string.Join("", cardPool.Select(c => c.ToString())) + Environment.NewLine;
|
||||||
|
|
||||||
|
public void InitHandValues() {
|
||||||
|
Func<List<Card>, bool> hasPair =
|
||||||
|
cards => cards.GroupBy(card => card.number)
|
||||||
|
.Count(group => group.Count() == 2) == 1;
|
||||||
|
Func<List<Card>, bool> isPair =
|
||||||
|
cards => cards.GroupBy(card => card.number)
|
||||||
|
.Count(group => group.Count() == 3) == 0
|
||||||
|
&& hasPair(cards);
|
||||||
|
|
||||||
|
Func<List<Card>, bool> isTwoPair =
|
||||||
|
cards => cards.GroupBy(card => card.number)
|
||||||
|
.Count(group => group.Count() >= 2) == 2;
|
||||||
|
|
||||||
|
Func<List<Card>, bool> isStraight =
|
||||||
|
cards => cards.GroupBy(card => card.number)
|
||||||
|
.Count() == cards.Count()
|
||||||
|
&& cards.Max(card => (int)card.number)
|
||||||
|
- cards.Min(card => (int)card.number) == 4;
|
||||||
|
|
||||||
|
Func<List<Card>, bool> hasThreeOfKind =
|
||||||
|
cards => cards.GroupBy(card => card.number)
|
||||||
|
.Any(group => group.Count() == 3);
|
||||||
|
|
||||||
|
Func<List<Card>, bool> isThreeOfKind =
|
||||||
|
cards => hasThreeOfKind(cards) && !hasPair(cards);
|
||||||
|
|
||||||
|
Func<List<Card>, bool> isFlush =
|
||||||
|
cards => cards.GroupBy(card => card.suit).Count() == 1;
|
||||||
|
|
||||||
|
Func<List<Card>, bool> isFourOfKind =
|
||||||
|
cards => cards.GroupBy(card => card.number)
|
||||||
|
.Any(group => group.Count() == 4);
|
||||||
|
|
||||||
|
Func<List<Card>, bool> isFullHouse =
|
||||||
|
cards => hasPair(cards) && hasThreeOfKind(cards);
|
||||||
|
|
||||||
|
Func<List<Card>, bool> hasStraightFlush =
|
||||||
|
cards => isFlush(cards) && isStraight(cards);
|
||||||
|
|
||||||
|
Func<List<Card>, bool> isRoyalFlush =
|
||||||
|
cards => cards.Min(card => (int)card.number) == 10
|
||||||
|
&& hasStraightFlush(cards);
|
||||||
|
|
||||||
|
Func<List<Card>, bool> isStraightFlush =
|
||||||
|
cards => hasStraightFlush(cards) && !isRoyalFlush(cards);
|
||||||
|
|
||||||
|
handValues = new Dictionary<string, Func<List<Card>, bool>>
|
||||||
|
{
|
||||||
|
{ "A Pair", isPair },
|
||||||
|
{ "Two Pairs", isTwoPair },
|
||||||
|
{ "Three Of A Kind", isThreeOfKind },
|
||||||
|
{ "Straight", isStraight },
|
||||||
|
{ "Flush", isFlush },
|
||||||
|
{ "Full House", isFullHouse },
|
||||||
|
{ "Four Of A Kind", isFourOfKind },
|
||||||
|
{ "Straight Flush", isStraightFlush },
|
||||||
|
{ "Royal Flush", isRoyalFlush }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetHandValue(List<Card> cards)
|
||||||
|
{
|
||||||
|
foreach (var KVP in handValues)
|
||||||
|
{
|
||||||
|
if (KVP.Value(cards)) {
|
||||||
|
return KVP.Key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "High card "+cards.Max().GetName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Drawing.Imaging;
|
using System.Drawing.Imaging;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
@ -13,10 +14,11 @@ namespace NadekoBot
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="images">The Images you want to merge.</param>
|
/// <param name="images">The Images you want to merge.</param>
|
||||||
/// <returns>Merged bitmap</returns>
|
/// <returns>Merged bitmap</returns>
|
||||||
public static Bitmap MergeImages(Image[] images)
|
public static Bitmap MergeImages(IEnumerable<Image> images)
|
||||||
{
|
{
|
||||||
|
if (images.Count() == 0) return null;
|
||||||
int width = images.Sum(i => i.Width);
|
int width = images.Sum(i => i.Width);
|
||||||
int height = images[0].Height;
|
int height = images.First().Height;
|
||||||
Bitmap bitmap = new Bitmap(width, height);
|
Bitmap bitmap = new Bitmap(width, height);
|
||||||
var r = new Random();
|
var r = new Random();
|
||||||
int offsetx = 0;
|
int offsetx = 0;
|
||||||
|
@ -3,6 +3,7 @@ using System.Threading.Tasks;
|
|||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Drawing.Imaging;
|
using System.Drawing.Imaging;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace NadekoBot
|
namespace NadekoBot
|
||||||
{
|
{
|
||||||
@ -17,8 +18,8 @@ namespace NadekoBot
|
|||||||
{
|
{
|
||||||
if (cards == null)
|
if (cards == null)
|
||||||
{
|
{
|
||||||
|
await e.Send("Shuffling cards...");
|
||||||
cards = new Cards();
|
cards = new Cards();
|
||||||
await e.Send( "Shuffling cards...");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
@ -33,17 +34,24 @@ namespace NadekoBot
|
|||||||
if (num > 5)
|
if (num > 5)
|
||||||
num = 5;
|
num = 5;
|
||||||
|
|
||||||
Image[] images = new Image[num];
|
List<Image> images = new List<Image>();
|
||||||
|
List<Cards.Card> cardObjects = new List<Cards.Card>();
|
||||||
for (int i = 0; i < num; i++)
|
for (int i = 0; i < num; i++)
|
||||||
{
|
{
|
||||||
if (cards.CardPool.Count == 0)
|
if (cards.CardPool.Count == 0 && i!= 0)
|
||||||
{
|
{
|
||||||
await e.Send( "No more cards in a deck...\nGetting a new deck...\nShuffling cards...");
|
await e.Send( "No more cards in a deck.");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
images[i] = Image.FromFile(cards.DrawACard().Path);
|
var currentCard = cards.DrawACard();
|
||||||
|
cardObjects.Add(currentCard);
|
||||||
|
images.Add(Image.FromFile(currentCard.Path));
|
||||||
}
|
}
|
||||||
Bitmap bitmap = ImageHandler.MergeImages(images);
|
Bitmap bitmap = ImageHandler.MergeImages(images);
|
||||||
await client.SendFile(e.Channel, num+" cards.jpg",ImageHandler.ImageToStream(bitmap,ImageFormat.Jpeg));
|
await client.SendFile(e.Channel, images.Count+" cards.jpg",ImageHandler.ImageToStream(bitmap, ImageFormat.Jpeg));
|
||||||
|
if (cardObjects.Count == 5) {
|
||||||
|
await e.Send(Cards.GetHandValue(cardObjects));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
Console.WriteLine("Error drawing (a) card(s) "+ex.ToString());
|
Console.WriteLine("Error drawing (a) card(s) "+ex.ToString());
|
||||||
|
@ -46,7 +46,7 @@ namespace NadekoBot.Modules
|
|||||||
.Description("Nadeko replies with /o/")
|
.Description("Nadeko replies with /o/")
|
||||||
.Do(async e =>
|
.Do(async e =>
|
||||||
{
|
{
|
||||||
await e.Send( e.User.Mention + "/o/");
|
await e.Send(e.User.Mention + "/o/");
|
||||||
});
|
});
|
||||||
|
|
||||||
cgb.CreateCommand("/o/")
|
cgb.CreateCommand("/o/")
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
dlls/WebSocket4Net.dll
Normal file
BIN
dlls/WebSocket4Net.dll
Normal file
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user