-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
150 lines (140 loc) · 5.23 KB
/
Copy pathProgram.cs
File metadata and controls
150 lines (140 loc) · 5.23 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
namespace Rubik
{
class Program
{
private static long AnzahlZuege = -1;
private static CardSet[] Spielfeld;
private static CardSet[] Cards;
private static DateTime CalcStart;
private static DateTime AppStart;
private static BigInteger MaxAnzahlZüge;
private static BigInteger AnzahlZügeÜbersprungen;
private static int AnzahlCards = 0;
static void Main(string[] args)
{
Cards = CardSet.CreateCards();
AnzahlCards = Cards.Count();
PrintCards(Cards);
MaxAnzahlZüge = CalculatePossibilities(25);
Console.WriteLine("MaxAnzahlZüge: " + MaxAnzahlZüge);
Spielfeld = new CardSet[AnzahlCards];
CalcStart = DateTime.Now;
AppStart = DateTime.Now;
LegeKarte(0);
Console.WriteLine("#####################################################################################");
Console.WriteLine("Alles durchprobiert.");
PrintFooter();
Console.ReadLine();
}
private static BigInteger CalculatePossibilities(int cardCount)
{
var result = new BigInteger(1);
for (int i = cardCount * 4; i != 0; i-=4)
{
result *= i;
}
return result;
}
private static void LegeKarte(int spielfeldPos)
{
if (spielfeldPos == AnzahlCards)
{
//Lösung gefunden?
Console.WriteLine("#####################################################################################");
Console.WriteLine("Lösung gefunden:");
PrintCards(Spielfeld);
PrintFooter();
CalcStart = DateTime.Now;
return;
}
for (int i = 0; i < AnzahlCards; i++)
{
if (Cards[i].Used)
continue;
for (int rotations = 0; rotations < 4; rotations++)
{
AnzahlZuege += 1;
bool recurse = false;
if (Compare(spielfeldPos, Cards[i]))
{
Spielfeld[spielfeldPos] = Cards[i];
Cards[i].Used = true;
recurse = true;
LegeKarte(spielfeldPos + 1);
Cards[i].Used = false;
}
if (!recurse)
{
int unused = GetUnusedCardCount() - 1;
AnzahlZügeÜbersprungen += CalculatePossibilities(unused);
}
Cards[i].Rotate();
}
}
}
private static void PrintFooter()
{
Console.WriteLine();
Console.WriteLine(string.Format("Dauer Calc : {0:mmss}", CalcStart - DateTime.Now));
Console.WriteLine(string.Format("Dauer App : {0:mmss}", AppStart - DateTime.Now));
Console.WriteLine(string.Format("Anzahl Züge : {0,45}", AnzahlZuege));
Console.WriteLine(string.Format("Züge Übersprungen: {0,45}", AnzahlZügeÜbersprungen));
Console.WriteLine(string.Format("Max Züge : {0,45}", MaxAnzahlZüge));
Console.WriteLine(string.Format("Züge offen : {0,45}", MaxAnzahlZüge - AnzahlZügeÜbersprungen - AnzahlZuege));
}
private static int GetUnusedCardCount()
{
return Cards.Where(c => !c.Used).Count();
}
private static void PrintCards(CardSet[] cards)
{
for (int row = 0; row < 25; row += 5)
{
for (int col = 0; col < 5; col++)
{
cards[row+col].Print(0);
}
Console.WriteLine();
for (int col = 0; col < 5; col++)
{
cards[row + col].Print(1);
}
Console.WriteLine();
for (int col = 0; col < 5; col++)
{
cards[row + col].Print(2);
}
Console.WriteLine();
for (int col = 0; col < 5; col++)
{
cards[row + col].Print(3);
}
Console.WriteLine();
Console.WriteLine("-------------------------");
}
}
private static bool Compare(int pos, CardSet cardSet)
{
int posLinks = pos - 1;
bool vglLinks = true;
bool vglOben = true;
if (pos % 5 != 0 && posLinks >= 0)
{
var links = Spielfeld[posLinks];
vglLinks = links.Card[2] == cardSet.Card[7] && links.Card[3] == cardSet.Card[6];
}
int posOben = pos - 5;
if (posOben >= 0)
{
var oben = Spielfeld[posOben];
vglOben = oben.Card[5] == cardSet.Card[0] && oben.Card[4] == cardSet.Card[1];
}
return vglLinks && vglOben;
}
}
}