int patternBModulo; //D^k (ahol D legyen az összes különböző összértékű dobás, k pedig a patternB hossza) //Default konstruktor, amely egy egy elöre megadott Penney’s game játékot hoz létre. public Penney() { int[] defaultDice = new int[1]; defaultDice[0] = 2; dicePool = new Dice(ref defaultDice); patternA = new int[3]; patternA[0] = 0; patternA[1] = 0; patternA[2] = 1; patternACode = 1; patternAModulo = 8; patternB = new int[3]; patternB[0] = 0; patternB[1] = 1; patternB[2] = 0; patternBCode = 2; patternBModulo = 8; gameStates = new List <int[]>(); gameStateBreakpoints = new int[4]; CreateStates(0, 3, 2); CreateGraph(2); actualGraph = new Graph(ref gameStates, ref gameGraph, 0, ref dicePool); actualMarkov = null; }
//Metódus, ami a megadott taktika és az analysisGraph segítségével létrehozza az actualMarkov elemet. public void CreateMarkov(int tacticUsed) { //create transition matrix from the game graph + dice pool -> use it to initialize the actualMarkov double[,] trMat = new double[gameStates.Count * 2, gameStates.Count * 2]; double[] odds = dicePool.GetOdds(); //create the analyzeGameGraph array if needed if (lastTacticUsed != tacticUsed) { CreateAnalysisGraph(tacticUsed); int startFrom = playerStarts ? 0 : gameStates.Count; analysisGraph = new Graph(ref gameStates, ref analysisGameGraph, startFrom, ref dicePool); } for (int i = 0; i < analysisGameGraph.Length; i++) { for (int j = 0; j < analysisGameGraph[i].Count; j++) { trMat[i, analysisGameGraph[i][j]] += odds[j]; } if (analysisGameGraph[i].Count == 0) { trMat[i, i] = 1.0; } } analysisMarkov = new Markov(ref trMat); }
private Markov actualMarkov; //A játék elemzéséhez használt példánya a Markov osztálynak. //Default konstruktor, amely egy előre megadott Kígyók és Létrák játékot hoz létre. public SnakesAndLadders() { int boardLength = 100; List <int[]> ladders = new List <int[]>(); List <int[]> snakes = new List <int[]>(); int[] s1 = { 20, 11 }; int[] s2 = { 34, 1 }; int[] s3 = { 53, 32 }; int[] s4 = { 60, 50 }; int[] s5 = { 88, 42 }; snakes.Add(s1); snakes.Add(s2); snakes.Add(s3); snakes.Add(s4); snakes.Add(s5); int[] l1 = { 2, 10 }; int[] l2 = { 17, 30 }; int[] l3 = { 36, 64 }; int[] l4 = { 58, 62 }; int[] l5 = { 70, 86 }; ladders.Add(l1); ladders.Add(l2); ladders.Add(l3); ladders.Add(l4); ladders.Add(l5); dicePool = new Dice(); int[] helpArray = new int[boardLength]; CreateHelpArray(ref helpArray, ref ladders, ref snakes); gameStates = new List <int[]>(); CreateStates(ref helpArray); gameGraph = new List <int> [gameStates.Count]; CreateGraph(ref helpArray, 6); actualGraph = new Graph(ref gameStates, ref gameGraph, 0, ref dicePool); actualMarkov = null; }
//Alternatív konstruktor, amely lehetővé teszi tetszőleges kockák és játékosok által használt listák megadását. public Penney(ref int[] diceArray, ref int[] inputPatternA, ref int[] inputPatternB) { dicePool = new Dice(ref diceArray); int possibleRolls = dicePool.MaxRoll() - dicePool.MinRoll() + 1; patternA = inputPatternA; patternACode = 0; patternAModulo = 1; for (int i = patternA.Length - 1; i >= 0; i--) { patternACode += patternA[patternA.Length - 1 - i] * patternAModulo; patternAModulo *= possibleRolls; } patternB = inputPatternB; patternBCode = 0; patternBModulo = 1; for (int i = patternB.Length - 1; i >= 0; i--) { patternBCode += patternB[patternB.Length - 1 - i] * patternBModulo; patternBModulo *= possibleRolls; } int maxLength = patternA.Length; if (patternB.Length > maxLength) { maxLength = patternB.Length; } gameStates = new List <int[]>(); gameStateBreakpoints = new int[maxLength + 1]; CreateStates(0, maxLength, possibleRolls); CreateGraph(possibleRolls); actualGraph = new Graph(ref gameStates, ref gameGraph, 0, ref dicePool); actualMarkov = null; }
//Függvény, ami létrehozza a játékhoz tartozó Markov osztály példányt, amennyiben az még nem létezett, //majd Markov osztály transitionMatrix változójának másolatát (steps-1)-ik hatványára emeli. //Ennek adja vissza a startingStateIndex állapotnak megfelelő sorát //(Emlékeztető: Markov létrehozásakor a sorok és oszlopok sorrendje változott!) public double[] Markov1(int startingStateIndex, int steps) { //check if too big? double[] resultVector = new double[gameStates.Count]; if (actualMarkov == null) { CreateMarkov(); } double[,] currentMatrix = new double[gameStates.Count, gameStates.Count]; for (int i = 0; i < gameStates.Count; i++) { currentMatrix[i, i] = 1.0; } for (int i = 0; i < steps; i++) { currentMatrix = Markov.MultiplyMatrices(ref currentMatrix, ref actualMarkov.transitionMatrix); } //try to find where our starting state ended up after the markov class switched rows and columns int startingStateNewIndex = -1; for (int i = 0; i < gameStates.Count; i++) { if (actualMarkov.originalOrder[i] == startingStateIndex) { startingStateNewIndex = i; break; } } for (int i = 0; i < gameStates.Count; i++) { resultVector[actualMarkov.originalOrder[i]] = currentMatrix[startingStateNewIndex, i]; } return(resultVector); }
//Metódus, ami ténylegesen létrehozza az elemző Markov osztály példányát. public void CreateMarkov() { //create transition matrix from the game graph + dice pool -> use it to initialize the actualMarkov double[,] trMat = new double[gameStates.Count, gameStates.Count]; double[] odds = dicePool.GetOdds(); for (int i = 0; i < gameGraph.Length; i++) { for (int j = 0; j < gameGraph[i].Count; j++) { trMat[i, gameGraph[i][j]] += odds[j]; } if (gameGraph[i].Count == 0) { trMat[i, i] = 1.0; } } actualMarkov = new Markov(ref trMat); }
//Függvény, ami létrehozza a játékhoz és a tacticUsed-hoz tartozó Markov osztály példányt, amennyiben az még nem létezett, //majd Markov osztály transitionMatrix változójának másolatát (steps-1)-ik hatványára emeli. //Ennek adja vissza a startingStateIndex állapotnak megfelelő sorát //(Emlékeztető: Markov létrehozásakor a sorok és oszlopok sorrendje változott!) public double[] Markov1(int startingStateIndex, int steps, int tacticUsed) { double[] resultVector = new double[gameStates.Count * 2]; if (analysisMarkov == null || lastTacticUsed != tacticUsed) { CreateMarkov(tacticUsed); } double[,] currentMatrix = new double[gameStates.Count * 2, gameStates.Count * 2]; for (int i = 0; i < gameStates.Count * 2; i++) { currentMatrix[i, i] = 1.0; } for (int i = 0; i < steps; i++) { currentMatrix = Markov.MultiplyMatrices(ref currentMatrix, ref analysisMarkov.transitionMatrix); } //try to find where our starting state ended up after the markov class switched rows and columns int startingStateNewIndex = -1; for (int i = 0; i < gameStates.Count; i++) { if (analysisMarkov.originalOrder[i] == startingStateIndex) { startingStateNewIndex = i; break; } } for (int i = 0; i < gameStates.Count; i++) { resultVector[analysisMarkov.originalOrder[i]] = currentMatrix[startingStateNewIndex, i]; } return(resultVector); }
//Alternatív konstruktor, amely lehetővé teszi tetszőleges kockák, játéktábla hossz, kígyók és létrák megadását. //A konstruktor ezen kívül rendezi a snakes és ladders listákat kezdőpozíció szerint növekvő sorrendbe. public SnakesAndLadders(ref int[] diceArray, int boardLength, ref List <int[]> ladders, ref List <int[]> snakes) { dicePool = new Dice(ref diceArray); int possibleRolls = dicePool.MaxRoll() - dicePool.MinRoll() + 1; int[] helpArray = new int[boardLength]; //order ladders for (int i = 0; i < ladders.Count - 1; i++) { int min = i; for (int j = i + 1; j < ladders.Count; j++) { if (ladders[j][0] < ladders[min][0]) { min = j; } } if (min != i) { int[] swi = ladders[i]; ladders[i] = ladders[min]; ladders[min] = swi; } } //order snakes for (int i = 0; i < snakes.Count - 1; i++) { int min = i; for (int j = i + 1; j < snakes.Count; j++) { if (snakes[j][0] < snakes[min][0]) { min = j; } } if (min != i) { int[] swi = snakes[i]; snakes[i] = snakes[min]; snakes[min] = swi; } } CreateHelpArray(ref helpArray, ref ladders, ref snakes); gameStates = new List <int[]>(); CreateStates(ref helpArray); gameGraph = new List <int> [gameStates.Count]; CreateGraph(ref helpArray, possibleRolls); actualGraph = new Graph(ref gameStates, ref gameGraph, 0, ref dicePool); actualMarkov = null; }