-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSimpleArranger.cs
73 lines (65 loc) · 2.61 KB
/
SimpleArranger.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace GauntletPrinter
{
public class SimpleArranger
{
internal class CardInDeck
{
public Card Card { get; set; }
public int DeckNumber { get; set; }
}
public List<List<Card>> ArrangeCards(List<List<Card>> decks)
{
var data = new List<CardInDeck>();
for(int i = 0; i < decks.Count; i++)
{
foreach(var card in decks[i])
{
data.Add(new CardInDeck { Card = card, DeckNumber = i });
}
}
// Put the cards into the proxy deck starting with the cards with the longest text
var orderedData = data.OrderByDescending(p => p.Card.TextLength);
int deckSize = decks[0].Count;
var generatedDecks = new Card[decks.Count, deckSize];
foreach(var cardInDeck in orderedData)
{
// Find deck slot with the most empty room AND empty slot for the card
int foundSlot = 0;
int foundFullness = int.MaxValue;
for (int i = 0; i < deckSize; i++)
{
if (generatedDecks[cardInDeck.DeckNumber, i] != null) continue;
int currentFullness = Enumerable.Range(0, decks.Count).Sum(p => generatedDecks[p, i] != null ? (generatedDecks[p, i].TextLength) : 0);
if(currentFullness == 0)
{
foundFullness = 0;
foundSlot = i;
break;
}
else if(currentFullness < foundFullness)
{
foundSlot = i;
foundFullness = currentFullness;
}
}
if (generatedDecks[cardInDeck.DeckNumber, foundSlot] != null) throw new ApplicationException("Could not arrange cards.");
generatedDecks[cardInDeck.DeckNumber, foundSlot] = cardInDeck.Card;
}
// Reformat the deck array into the output format
var outputDecks = new List<List<Card>>();
for (int i = 0; i < decks.Count; i++)
{
var deck = new List<Card>();
for (int j = 0; j < deckSize; j++)
{
deck.Add(generatedDecks[i, j]);
}
outputDecks.Add(deck);
}
return outputDecks;
}
}
}