public PawnMove TakeTurn(TicTacToeBoard t) { if (Policies.ContainsKey(t.GetBoardState())) { PawnMove moveToTake = Policies[t.GetBoardState()].OrderByDescending(x => x.Utility).ToList()[0]; if (Rnd.Range(1, 101) <= ExplorationRate) { moveToTake = t.GetAvailableMoves()[Rnd.Range(0, t.GetAvailableMoves().Count)]; } TakenMoves.Add(moveToTake); if (Spectation) { string res = "My available moves: \n"; foreach (PawnMove item in Policies[t.GetBoardState()].OrderByDescending(x => x.Utility).ToList()) { res += $"[{item.X},{item.Y}]({item.Utility}) \n"; } Console.WriteLine(res); } return(moveToTake); } else { Policies.Add(t.GetBoardState(), t.GetAvailableMoves()); PawnMove moveToTake; moveToTake = Policies[t.GetBoardState()].OrderByDescending(x => x.Utility).ToList()[0]; TakenMoves.Add(moveToTake); return(moveToTake); } }
public void Reward(float r) { counter++; if (counter == 7500) { Console.WriteLine(""); } MovesMade.Reverse(); for (int i = 0; i < MovesMade.Count; i++) { switch (i) { case 0: MovesMade[i].Utility += 1f * r; break; case 1: MovesMade[i].Utility += .9f * r; break; case 2: MovesMade[i].Utility += .8f * r; break; case 3: MovesMade[i].Utility += .7f * r; break; case 4: MovesMade[i].Utility += .6f * r; break; case 5: MovesMade[i].Utility += .5f * r; break; case 6: MovesMade[i].Utility += .4f * r; break; case 7: MovesMade[i].Utility += .3f * r; break; default: MovesMade[i].Utility += 0.1f * r; break; } } MovesMade.Clear(); if (r > 0) { if (Rnd.Range(1, 100) < ExplorationChance + 5) { // ExplorationChance--; } } }
public PawnMove TakeTurn(Board b, Player player) { string boardstat = b.GetBoardStateString(); // PawnMove PawnMove WinMove; if (Policies.ContainsKey(boardstat)) { WinMove = Policies[boardstat].FirstOrDefault(x => b.IsWinningMove(x)); if (WinMove != null) { MovesMade.Add(WinMove); return(WinMove); } if (Rnd.Range(1, 101) <= ExplorationChance) { WinMove = Policies[boardstat].OrderBy(x => Rnd.Range(0, 10000)).ToList()[0]; MovesMade.Add(WinMove); return(WinMove); } else { WinMove = Policies[boardstat].OrderByDescending(x => x.Utility).ToList()[0]; if (PrintEverything) { string res = ""; foreach (PawnMove item in Policies[boardstat].OrderByDescending(x => x.Utility).ToList()) { res += item.ToString() + "\n"; } Console.WriteLine(res); } MovesMade.Add(WinMove); return(WinMove); } } else { List <PawnMove> moves = b.GetAvailableMoveForPlayer(player).OrderBy(x => Rnd.Range(0, 10000)).ToList(); Policies.Add(boardstat, moves); WinMove = Policies[boardstat].FirstOrDefault(x => b.IsWinningMove(x)); if (WinMove == null) { WinMove = moves[0]; } MovesMade.Add(WinMove); return(WinMove); } }
public void Reward(float reward) { TakenMoves.Reverse(); for (int i = 1; i < TakenMoves.Count + 1; i++) { TakenMoves[i - 1].Utility += reward / i * .1f; } TakenMoves.Clear(); if (reward > 0) { if (Rnd.Range(0, 10) < 3) { ExplorationRate--; if (ExplorationRate < 0) { ExplorationRate = 0; } } } if (reward == 0) { ExplorationRate++; } }
public static PawnMove TakeTurn(List <PawnMove> moves) { return(moves.OrderBy(x => Rnd.Range(0, 10000)).ToList()[0]); }
public PawnMove TakeTurn(TicTacToeBoard t) { List <PawnMove> moves = t.GetAvailableMoves(); return(moves[Rnd.Range(0, moves.Count)]); }
public static void PlaySantorini() { Program p = new Program(); SantoriniQLearn quintin1 = new SantoriniQLearn(); SantoriniQLearn quintin2 = new SantoriniQLearn(); SantoriniPseudoRandom randy = new SantoriniPseudoRandom(Rnd.Range(0, 5000)); SantoriniUtilityAI Ursa = new SantoriniUtilityAI(); SantoriniNAN Nana = new SantoriniNAN(); WinRatio wins = new WinRatio(); WinRatio winsIncremental = new WinRatio(); string ProgressBarMine = ""; bool printstuff = false; //for (int i = 1; i < 1000000; i++) //{ int i = 1; int yx = 0; bool doneTraining = false; bool doneExploring = false; while (!doneTraining) { i++; Player player1 = new Player(); player1.Pawns.Add(new Pawn(0, 0, 1)); Player player2 = new Player(); player2.Pawns.Add(new Pawn(2, 2, 2)); Board b = new Board(); int timesINeedToWin = 3; bool ActivateIntelligentOpponent = false; //Visuals if (i % 1000 == 0) { Console.Clear(); //quintin1.ExplorationChance--; //if (doneExploring) //{ // quintin1.ExplorationChance = 0; // yx++; //} //if (yx > 7) //{ // doneTraining = true; //} //if (i%900000 == 0) //{ // quintin1.ExplorationChance = 0; //} ProgressBarMine += "\n \nPlayer1Wins: " + winsIncremental.Player1 + "\nPlayer2Wins: " + winsIncremental.Player2 + "\nNew Board States: " + MathF.Abs(winsIncremental.Draw - quintin1.Policies.Count); Console.WriteLine(ProgressBarMine); Console.WriteLine("Exploration chance: " + quintin1.ExplorationChance); //Hvor mange nye stadier skal vi over før vi kan gå igang med at spille? //if (MathF.Abs(winsIncremental.Draw - quintin1.Policies.Count) < 500000) //{ // ProgressBarMine += "\nsaving"; // //p.SaveKnowledge(quintin1); // ProgressBarMine += "\n Done saving"; // doneExploring = true; //} // winsIncremental.Draw = quintin1.Policies.Count; winsIncremental.Player2 = 0; winsIncremental.Player1 = 0; if (!ActivateIntelligentOpponent && winsIncremental.Player2 > 7600) { timesINeedToWin--; if (timesINeedToWin == 0) { ActivateIntelligentOpponent = false; } } // quintin1.PrintEverything = true; // printstuff = true; } bool winner = false; while (winner == false) { if (b.GetAvailableMoveForPlayer(player1).Count == 0) { winner = true; // Console.WriteLine("Player 2 killed player 1"); wins.Player2++; winsIncremental.Player2++; //if (ActivateIntelligentOpponent) //{ // quintin2.Reward(-1); //} //quintin1.Reward(-1); } else { b.MakeMove(Nana.TakeTurn(b, player1), player1.Pawns[0]); if (printstuff) { b.DrawBoard(); } foreach (Pawn item in player1.Pawns) { if (b.BoardState[item.X, item.Y, 0] == 3) { winner = true; // Console.WriteLine("Player 1 Climbed the tower"); wins.Player1++; winsIncremental.Player1++; if (ActivateIntelligentOpponent) { quintin2.Reward(1); } quintin1.Reward(1); break; } } } if (winner == false) { if (b.GetAvailableMoveForPlayer(player2).Count == 0) { winner = true; //wins.Player1++; //winsIncremental.Player1++; //if (ActivateIntelligentOpponent) //{ // quintin2.Reward(10); //} //quintin1.Reward(1); // Console.WriteLine("Player 1 killed player 2"); } else { // MAKE DET MOVE MOND! if (doneExploring) { b.MakeMove(randy.TakeTurn(b.GetAvailableMoveForPlayer(player2), b), player2.Pawns[0]); } else { b.MakeMove(randy.TakeTurn(b.GetAvailableMoveForPlayer(player2), b), player2.Pawns[0]); } if (printstuff) { b.DrawBoard(); } foreach (Pawn item in player2.Pawns) { if (b.BoardState[item.X, item.Y, 0] == 3) { winner = true; // Console.WriteLine("Player 2 climbed the tower!"); wins.Player2++; winsIncremental.Player2++; quintin1.Reward(-1); if (ActivateIntelligentOpponent) { quintin2.Reward(-1); } break; } } } } } // b.DrawBoard(); } Console.WriteLine("Player 1 Wins:" + wins.Player1); Console.WriteLine("Player 2 Wins:" + wins.Player2); Console.WriteLine("Board states known: " + quintin1.Policies.Count); Console.WriteLine("Press any key"); // p.SaveKnowledge(quintin1); Console.ReadKey(); for (int hy = 0; hy < 1000; hy++) { Player player1 = new Player(); player1.Pawns.Add(new Pawn(0, 0, 1)); Player player2 = new Player(); player2.Pawns.Add(new Pawn(2, 2, 2)); Board b = new Board(); Console.Clear(); b.DrawBoard(); Console.ReadKey(); bool winner = false; while (winner == false) { if (b.GetAvailableMoveForPlayer(player1).Count == 0) { winner = true; // Console.WriteLine("Player 2 killed player 1"); wins.Player2++; winsIncremental.Player2++; quintin1.Reward(10); } else { b.MakeMove(quintin1.TakeTurn(b, player1), player1.Pawns[0]); //b.GetAvailableMoveForPlayer(player1), b); ; ;, player1.Pawns[0]); b.DrawBoard(); foreach (Pawn item in player1.Pawns) { if (b.BoardState[item.X, item.Y, 0] == 3) { winner = true; Console.WriteLine("Player 1 Climbed the tower"); wins.Player1++; winsIncremental.Player1++; quintin1.Reward(-1); break; } } } if (winner == false) { if (b.GetAvailableMoveForPlayer(player2).Count == 0) { winner = true; wins.Player1++; winsIncremental.Player1++; quintin1.Reward(-1); Console.WriteLine("Player 1 killed player 2"); } else { // MAKE DET MOVE MOND! Console.WriteLine("Make Move Player :D"); PawnMove playermove = new PawnMove(); List <PawnMove> liste = b.GetAvailableMoveForPlayer(player2); A: int count = 0; foreach (var item in liste) { Console.WriteLine(count + ": " + item.ToString()); count++; } int choose = int.Parse(Console.ReadLine()); try { b.MakeMove(liste[choose], player2.Pawns[0]); } catch (Exception) { goto A; } //b.MakeMove(quintin1.TakeTurn(b, player2), player2.Pawns[0]); //b.DrawBoard(); //Console.ReadKey(); foreach (Pawn item in player2.Pawns) { if (b.BoardState[item.X, item.Y, 0] == 3) { winner = true; Console.WriteLine("Player 2 climbed the tower!"); wins.Player2++; winsIncremental.Player2++; break; } } } } } b.DrawBoard(); Console.ReadLine(); } }