public MancalaScript Spawn(Mancala mancala) { var mancalaScript = Instantiate(this); mancalaScript.ID = mancala.ID; mancalaScript.Mancala = mancala; mancala.UnsortedBowls.ForEach(x => GameResources.Bowls[x.Owner][x.Position].Spawn(x, mancalaScript.transform)); return(mancalaScript); }
private void New() { var mancala = Mancala.NewGame(GameResources.Settings.StartingStones); GameResources.Game = mancala; var mancalaScript = MancalaScript.Spawn(mancala); mancalaScript.transform.SetParent(transform); var plunder = new PlunderData(Guid.NewGuid().ToString(), GameResources.Settings.IsPlunder, GameResources.Settings.GameType); GameResources.Plunder = new Plunder(plunder.ID); new PlunderBodyData(plunder.ID, PlunderScript.WorldScript.transform.position, PlunderScript.WorldScript.transform.rotation); var plunderScript = PlunderScript.Spawn(new PlunderBody(plunder.ID)); plunderScript.transform.SetParent(transform); }
public void Continue() { var save = JsonSerializer.Deserialize <Save>(PlayerPrefs.GetString(GameResources.Settings.GameType.ToString())); save.AddToAccess(); var mancala = new Mancala(save.MancalaData.ID); GameResources.Game = mancala; var mancalaScript = MancalaScript.Spawn(mancala); mancalaScript.transform.SetParent(transform); GameResources.Plunder = new Plunder(save.PlunderData.ID); var plunderScript = PlunderScript.Spawn(new PlunderBody(save.PlunderData.ID)); plunderScript.transform.SetParent(transform); GameResources.Queue.Load(save.Queue); }
public double AplhaBeta(Mancala currentInstance, int depth, bool max, bool emptyMove, double alpha, double beta) { if (currentInstance.CheckEnd() || depth == 0) { return(currentInstance.BoardPoints(player)); } int bestMove = 0; // If there is empty move, we move -1, which doesnt change game state int[] possibleMoves = new int[1] { -1 }; if (!emptyMove) { possibleMoves = currentInstance.GetAvaibleHoles(); } double result; if (max) { foreach (var move in possibleMoves) { Mancala newInstance = new Mancala(currentInstance); bool change = newInstance.Move(move); bool maxNext = !max; newInstance.player1Turn = !newInstance.player1Turn; if (change) { emptyMove = false; } else { emptyMove = true; } // Empty move doesn't change board situation result = AplhaBeta(newInstance, depth - 1, maxNext, emptyMove, alpha, beta); // Aplha-Beta part if (result > alpha) { alpha = result; // Found a better best move bestMove = move; } if (alpha >= beta && depth != ChosenDepth) { return(alpha); } } if (depth != ChosenDepth) { return(alpha); } } else { foreach (var move in possibleMoves) { Mancala newInstance = new Mancala(currentInstance); bool change = newInstance.Move(move); bool maxNext = !max; newInstance.player1Turn = !newInstance.player1Turn; if (change) { emptyMove = false; } else { emptyMove = true; } // Empty move doesn't change board situation result = AplhaBeta(newInstance, depth - 1, maxNext, emptyMove, alpha, beta); // Aplha-Beta part if (result < beta) { beta = result; // Found a better best move bestMove = move; } if (alpha >= beta && depth != ChosenDepth) { return(beta); } } if (depth != ChosenDepth) { return(beta); } } return(bestMove); }
public double MinmaxAlg(Mancala currentInstance, int depth, bool max, bool emptyMove) { if (currentInstance.CheckEnd() || depth == 0) { return(currentInstance.BoardPoints(player)); } int bestMove = 0; double score; if (max) { score = double.MinValue; } else { score = double.MaxValue; } int[] possibleMoves = new int[1] { -1 }; if (!emptyMove) { possibleMoves = currentInstance.GetAvaibleHoles(); } if (!player) { possibleMoves = possibleMoves.Reverse().ToArray(); } double result; if (max) { foreach (var move in possibleMoves) { Mancala newInstance = new Mancala(currentInstance); bool change = newInstance.Move(move); bool maxNext = !max; newInstance.player1Turn = !newInstance.player1Turn; if (change) { emptyMove = false; } else { emptyMove = true; } // Empty move doesn't change board situation result = MinmaxAlg(newInstance, depth - 1, maxNext, emptyMove); if (result > score) { bestMove = move; score = result; } } } // Min else { foreach (var move in possibleMoves) { Mancala newInstance = new Mancala(currentInstance); bool change = newInstance.Move(move); bool maxNext = !max; newInstance.player1Turn = !newInstance.player1Turn; if (change) { emptyMove = false; } else { emptyMove = true; } // Empty move doesn't change board situation result = MinmaxAlg(newInstance, depth - 1, maxNext, emptyMove); if (result < score) { bestMove = move; score = result; } } } if (depth != ChosenDepth) { return(score); } return(bestMove); }
public void MakeAMove(Mancala _mancala) { _mancala.Pick(_mancala.Bowls[Player.Two].Bowls.Where(x => x.StoneIDs.Count > 0).Random().Position); }
static void Main(string[] args) { bool allow = false; IEvaluatePoints simpleGetPoints = new SimpleGetPoints(); IEvaluatePoints expandedGetPoints = new ExpandedGetPoints(); ////IEvaluatePoints simpleGetPoints2 = new SimpleGetPoints(); ////IEvaluatePoints simpleGetPoints2 = new ExpandedGetPoints(); Mancala mancala1 = new Mancala(ref expandedGetPoints, ref simpleGetPoints, 5, player1Turn: false, 8, 8); mancala1.PrintText = true; mancala1.StartGame(); if (allow) { Console.WriteLine(double.MinValue + 1 - double.MinValue); int maxDepth = 12; int numberOfGames = 50; Console.WriteLine("Hello Mancala!"); FileStream fs = new FileStream("data.txt", FileMode.Create); using StreamWriter writeText = new StreamWriter(fs); List <double> avgTimes = new List <double>(); List <double> avgMoves = new List <double>(); List <double> wrP1 = new List <double>(); List <double> avgLead1 = new List <double>(); List <double> wrP2 = new List <double>(); List <double> avgLead2 = new List <double>(); // From depth to depth for (int i = 1; i <= maxDepth; i++) { double avgTime = 0; double avgNumberM = 0; double avgTimeTotal = 0; double avgNumberMTotal = 0; double winsP1 = 0; double leadP1 = 0; double winsP2 = 0; double leadP2 = 0; //writeText.WriteLine("\nIteration\tDepth\tAverage Time(ms)\tNumber of moves"); // Every stage is repeated 6 times for (int j = 0; j < numberOfGames; j++) { Console.WriteLine("Current Game: " + j); Mancala mancala = new Mancala(ref simpleGetPoints, ref expandedGetPoints, 5, player1Turn: false, i, i); mancala.PrintText = false; mancala.StartGame(); // Draw, winns the player, that started second, in this case player1 wins if (mancala.Player1Well[0] > mancala.Player2Well[0]) { leadP1 += mancala.Player1Well[0] - mancala.Player2Well[0]; avgTime = mancala.TimesOfPlayer1.Sum(x => x.TotalMilliseconds) / mancala.TimesOfPlayer1.Count; avgNumberM = mancala.MovesOfPlayer1; winsP1++; } else if (mancala.Player1Well[0] < mancala.Player2Well[0]) { leadP2 += mancala.Player2Well[0] - mancala.Player1Well[0]; avgTime = mancala.TimesOfPlayer2.Sum(x => x.TotalMilliseconds) / mancala.TimesOfPlayer2.Count; avgNumberM = mancala.MovesOfPlayer2; winsP2++; } //writeText.WriteLine($"{j}\t{i}\t{avgTime}\t{avgNumberM}"); avgTimeTotal += avgTime; avgNumberMTotal += avgNumberM; } avgTimeTotal = avgTimeTotal / 8; avgNumberMTotal = avgNumberMTotal / 8; //writeText.WriteLine($"AvgTimeTotal:\t{avgTimeTotal}"); //writeText.WriteLine($"avgNumberMTotal:\t{avgNumberMTotal}"); avgTimes.Add(avgTimeTotal); avgMoves.Add(avgNumberMTotal); wrP1.Add(winsP1 / numberOfGames); wrP2.Add(winsP2 / numberOfGames); avgLead1.Add(leadP1 / winsP1); avgLead2.Add(leadP2 / winsP2); } //writeText.WriteLine("\n\n\nDepth\tAverage Time(ms)\tNumber of moves"); //for (int i = 0; i < maxDepth; i++) //{ // writeText.WriteLine($"{i + 1}\t{avgTimes[i]}\t{avgMoves[i]}"); //} writeText.WriteLine("\n\n\nDepth\tPlayer 1 simple WR %\tPlayer 2 Expanded WR %\t AvgLead P1 Simple\t AvgLead P2 Expanded"); for (int i = 0; i < maxDepth; i++) { writeText.WriteLine($"{i + 1}\t{wrP1[i]}\t{wrP2[i]}\t{avgLead1[i]}\t{avgLead2[i]}"); } } }