Advertisement
TsmDan3

C# flashback shellshock nightmare

Mar 30th, 2023 (edited)
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 31.13 KB | Source Code | 0 0
  1. using System.Runtime.CompilerServices;
  2. using System.Security.Cryptography.X509Certificates;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace TicTacToeAI
  6. {
  7.     internal class TicTacToe
  8.     {
  9.         struct Move
  10.         {
  11.             public int score;
  12.             public int x;
  13.             public int y;
  14.         }
  15.         int[,] TicTacToeBoard = {
  16.         {0, 0, 0},
  17.         {0, 0, 0},
  18.         {0, 0, 0}
  19.         };
  20.         string[,] TicTacToePrint =
  21.         {
  22.             {" ", " ", " "},
  23.             {" ", " ", " "},
  24.             {" ", " ", " "},
  25.         };
  26.         int playerTurn = 0;
  27.         int playerWon = 0;
  28.         int playerWantToPlay = 1;
  29.         int a1 = 0;
  30.         int a2 = 0;
  31.         int a3 = 0;
  32.         int b1 = 0;
  33.         int b2 = 0;
  34.         int b3 = 0;
  35.         int c1 = 0;
  36.         int c2 = 0;
  37.         int c3 = 0;
  38.         static void Main(string[] args)
  39.         {
  40.             TicTacToe t = new TicTacToe();
  41.             while (t.playerWantToPlay == 1)
  42.             {
  43.                 Regex regex = new Regex(@"^[ABC][123]$");
  44.                 Console.WriteLine("X or O?");
  45.                 string choice = Console.ReadLine();
  46.                 if (choice.ToLower() == "x")
  47.                 {
  48.                     t.playerTurn = 0;
  49.                 }
  50.                 else if (choice.ToLower() == "o")
  51.                 {
  52.                     t.playerTurn = 1;
  53.                 }
  54.                 else
  55.                 {
  56.                     t.playerTurn = 1;
  57.                 }
  58.                 int turn = 0;
  59.                 while (t.playerWon == 0)
  60.                 {
  61.                     t.BoardStateToPrintState();
  62.                     t.PrintPlayableBoard();
  63.                     bool match = false;
  64.                     string loc = "";
  65.                     int valid = 0;
  66.                     while (match == false)
  67.                     {
  68.                         if (t.playerTurn == turn)
  69.                         {
  70.                             if (turn == 0)
  71.                             {
  72.                                 Console.Write("Location to add X: ");
  73.                             }
  74.                             else
  75.                             {
  76.                                 Console.Write("Location to add O: ");
  77.                             }
  78.                             loc = Console.ReadLine();
  79.                             match = regex.IsMatch(loc);
  80.                         }
  81.                         else
  82.                         {
  83.                             t.AdvancedTurn(turn, t.TicTacToeBoard);
  84.                             match = true;
  85.                         }
  86.                         valid = t.ValidateBoardState(loc);
  87.                         if (valid == 0) t.ModBoard(loc, turn);
  88.                         if (t.CheckForTicTacToe(true) != 0)
  89.                         {
  90.                             t.playerWon = t.CheckForTicTacToe();
  91.                         }
  92.                     }
  93.                     Console.WriteLine("Evaluation of position: " + t.Evaluate(t.TicTacToeBoard, turn));
  94.  
  95.                     if (turn == 0 && valid == 0)
  96.                     {
  97.                         turn = 1;
  98.                     }
  99.                     else if (turn == 1 && valid == 0)
  100.                     {
  101.                         turn = 0;
  102.                     }
  103.                     else
  104.                     {
  105.                         turn = turn;
  106.                     }
  107.  
  108.                 }
  109.                 Console.WriteLine("Another round?");
  110.                 if (Console.ReadLine().ToLower() == "y")
  111.                 {
  112.                     Console.Clear();
  113.                     for (int x = 0; x < 3; x++)
  114.                     {
  115.                         for (int y = 0; y < 3; y++)
  116.                         {
  117.                             t.TicTacToeBoard[x, y] = 0;
  118.                             t.TicTacToePrint[x, y] = " ";
  119.                         }
  120.                     }
  121.                     t.playerWon = 0;
  122.                 }
  123.                 else
  124.                 {
  125.                     t.playerWantToPlay = 0;
  126.                 }
  127.             }
  128.         }
  129.         void PlaceVoid(int x, int y)
  130.         {
  131.             TicTacToeBoard[x - 1, y - 1] = 0;
  132.         }
  133.         void PlaceX(int x, int y)
  134.         {
  135.             TicTacToeBoard[x - 1, y - 1] = 1;
  136.         }
  137.         void PlaceO(int x, int y)
  138.         {
  139.             TicTacToeBoard[x - 1, y - 1] = 2;
  140.         }
  141.         void ModBoard(string loc, int turn)
  142.         {
  143.             switch (loc)
  144.             {
  145.                 case "A1":
  146.                     if (turn == 0)
  147.                     {
  148.                         PlaceX(1, 1);
  149.                     }
  150.                     else
  151.                     {
  152.                         PlaceO(1, 1);
  153.                     }
  154.                     break;
  155.                 case "A2":
  156.                     if (turn == 0)
  157.                     {
  158.                         PlaceX(1, 2);
  159.                     }
  160.                     else
  161.                     {
  162.                         PlaceO(1, 2);
  163.                     }
  164.                     break;
  165.                 case "A3":
  166.                     if (turn == 0)
  167.                     {
  168.                         PlaceX(1, 3);
  169.                     }
  170.                     else
  171.                     {
  172.                         PlaceO(1, 3);
  173.                     }
  174.                     break;
  175.                 case "B1":
  176.                     if (turn == 0)
  177.                     {
  178.                         PlaceX(2, 1);
  179.                     }
  180.                     else
  181.                     {
  182.                         PlaceO(2, 1);
  183.                     }
  184.                     break;
  185.                 case "B2":
  186.                     if (turn == 0)
  187.                     {
  188.                         PlaceX(2, 2);
  189.                     }
  190.                     else
  191.                     {
  192.                         PlaceO(2, 2);
  193.                     }
  194.                     break;
  195.                 case "B3":
  196.                     if (turn == 0)
  197.                     {
  198.                         PlaceX(2, 3);
  199.                     }
  200.                     else
  201.                     {
  202.                         PlaceO(2, 3);
  203.                     }
  204.                     break;
  205.                 case "C1":
  206.                     if (turn == 0)
  207.                     {
  208.                         PlaceX(3, 1);
  209.                     }
  210.                     else
  211.                     {
  212.                         PlaceO(3, 1);
  213.                     }
  214.                     break;
  215.                 case "C2":
  216.                     if (turn == 0)
  217.                     {
  218.                         PlaceX(3, 2);
  219.                     }
  220.                     else
  221.                     {
  222.                         PlaceO(3, 2);
  223.                     }
  224.                     break;
  225.                 case "C3":
  226.                     if (turn == 0)
  227.                     {
  228.                         PlaceX(3, 3);
  229.                     }
  230.                     else
  231.                     {
  232.                         PlaceO(3, 3);
  233.                     }
  234.                     break;
  235.             }
  236.         }
  237.         int[,] CopyBoard(int[,] board)
  238.         {
  239.             int[,] newBoard = new int[3, 3];
  240.             for (int x = 0; x < 3; x++)
  241.             {
  242.                 for (int y = 0; y < 3; y++)
  243.                 {
  244.                     newBoard[x, y] = board[x, y];
  245.                 }
  246.             }
  247.             return newBoard;
  248.         }
  249.         int CountPlayerSpaces(int[,] board, int id)
  250.         {
  251.             int c = 0;
  252.             for (int x = 0; x < 2; x++)
  253.             {
  254.                 for (int y = 0; y < 2; y++)
  255.                 {
  256.                     c += board[x, y] == id ? 1 : 0;
  257.                 }
  258.             }
  259.             return c;
  260.         }
  261.         // Returns 0 for None,
  262.         //         1 for X,
  263.         //         2 for O
  264.         int CheckForTicTacToe(bool ChatDisabled = false)
  265.         {
  266.             /*
  267.             * There are eight possible win-states:
  268.             * X B B
  269.             * X B B
  270.             * X B B
  271.             *
  272.             * B X B
  273.             * B X B
  274.             * B X B
  275.             *
  276.             * B B X
  277.             * B B X
  278.             * B B X
  279.             *
  280.             * X X X
  281.             * B B B
  282.             * B B B
  283.             *
  284.             * B B B
  285.             * X X X
  286.             * B B B
  287.             *
  288.             * B B B
  289.             * B B B
  290.             * X X X
  291.             *
  292.             * X B B
  293.             * B X B
  294.             * B B X
  295.             *
  296.             * B B X
  297.             * B X B
  298.             * X B B
  299.             */
  300.             // Really, there are sixteen, but I don't want to get into it.
  301.             int w = 0;
  302.             // Horizontals:
  303.             for (int x = 0; x < 3; x++)
  304.             {
  305.                 if (TicTacToeBoard[x, 0] == TicTacToeBoard[x, 1] && TicTacToeBoard[x, 1] == TicTacToeBoard[x, 2] && TicTacToeBoard[x, 0] != 0)
  306.                 {
  307.                     switch (TicTacToeBoard[x, 0])
  308.                     {
  309.                         case 1:
  310.                             w = 1;
  311.                             if (!ChatDisabled == true)
  312.                             {
  313.                                 Console.WriteLine("X Won At Row " + (x + 1) + ".");
  314.                             }
  315.                             break;
  316.                         case 2:
  317.                             w = 2;
  318.                             if (!ChatDisabled == true)
  319.                             {
  320.                                 Console.WriteLine("O Won At Row " + (x + 1) + ".");
  321.                             }
  322.                             break;
  323.                     }
  324.                 }
  325.             }
  326.             // Verticals:
  327.             for (int y = 0; y < 3; y++)
  328.             {
  329.                 if (TicTacToeBoard[0, y] == TicTacToeBoard[1, y] && TicTacToeBoard[1, y] == TicTacToeBoard[2, y] && TicTacToeBoard[0, y] != 0)
  330.                 {
  331.                     switch (TicTacToeBoard[0, y])
  332.                     {
  333.                         case 1:
  334.                             w = 1;
  335.                             Console.WriteLine("X Won At Column " + (y + 1) + ".");
  336.                             break;
  337.                         case 2:
  338.                             w = 2;
  339.                             Console.WriteLine("O Won At Column " + (y + 1) + ".");
  340.                             break;
  341.                     }
  342.                 }
  343.             }
  344.             // Diagonals:
  345.             if (TicTacToeBoard[0, 0] == TicTacToeBoard[1, 1] && TicTacToeBoard[1, 1] == TicTacToeBoard[2, 2] && TicTacToeBoard[1, 1] != 0)
  346.             {
  347.                 switch (TicTacToeBoard[1, 1])
  348.                 {
  349.                     case 1:
  350.                         w = 1;
  351.                         if (!ChatDisabled == true)
  352.                         {
  353.                             Console.WriteLine("X Won At LR Diagonal");
  354.                         }
  355.                         break;
  356.                     case 2:
  357.                         w = 2;
  358.                         if (!ChatDisabled == true)
  359.                         {
  360.                             Console.WriteLine("O Won At LR Diagonal");
  361.                         }
  362.                         break;
  363.                 }
  364.             }
  365.             if (TicTacToeBoard[0, 2] == TicTacToeBoard[1, 1] && TicTacToeBoard[1, 1] == TicTacToeBoard[2, 0] && TicTacToeBoard[1, 1] != 0)
  366.             {
  367.                 switch (TicTacToeBoard[1, 1])
  368.                 {
  369.                     case 1:
  370.                         w = 1;
  371.                         if (!ChatDisabled == true)
  372.                         {
  373.                             Console.WriteLine("X Won At RL Diagonal");
  374.                         }
  375.                         break;
  376.                     case 2:
  377.                         w = 2;
  378.                         if (!ChatDisabled == true)
  379.                         {
  380.                             Console.WriteLine("O Won At RL Diagonal");
  381.                         }
  382.                         break;
  383.                 }
  384.             }
  385.  
  386.             return w;
  387.         }
  388.         // void PrintBoardState()
  389.         void BoardStateToPrintState()
  390.         {
  391.             for (int x = 0; x < 3; x++)
  392.             {
  393.                 for (int y = 0; y < 3; y++)
  394.                 {
  395.                     switch (TicTacToeBoard[x, y])
  396.                     {
  397.                         case 0:
  398.                             TicTacToePrint[x, y] = " ";
  399.                             break;
  400.                         case 1:
  401.                             TicTacToePrint[x, y] = "X";
  402.                             break;
  403.                         case 2:
  404.                             TicTacToePrint[x, y] = "O";
  405.                             break;
  406.                     }
  407.                 }
  408.             }
  409.         }
  410.         void PrintPlayableBoard()
  411.         {
  412.             Console.WriteLine(" " + TicTacToePrint[0, 0] + " | " + TicTacToePrint[0, 1] + " | " + TicTacToePrint[0, 2]);
  413.             Console.WriteLine("---+---+---");
  414.             Console.WriteLine(" " + TicTacToePrint[1, 0] + " | " + TicTacToePrint[1, 1] + " | " + TicTacToePrint[1, 2]);
  415.             Console.WriteLine("---+---+---");
  416.             Console.WriteLine(" " + TicTacToePrint[2, 0] + " | " + TicTacToePrint[2, 1] + " | " + TicTacToePrint[2, 2]);
  417.         }
  418.  
  419.         int ValidateBoardState(string loc)
  420.         {
  421.             if (loc == "A1")
  422.             {
  423.                 if (TicTacToeBoard[0, 0] != 0) return 1;
  424.             }
  425.             else if (loc == "A2")
  426.             {
  427.                 if (TicTacToeBoard[0, 1] != 0) return 1;
  428.             }
  429.             else if (loc == "A3")
  430.             {
  431.                 if (TicTacToeBoard[0, 2] != 0) return 1;
  432.             }
  433.             else if (loc == "B1")
  434.             {
  435.                 if (TicTacToeBoard[1, 0] != 0) return 1;
  436.             }
  437.             else if (loc == "B2")
  438.             {
  439.                 if (TicTacToeBoard[1, 1] != 0) return 1;
  440.             }
  441.             else if (loc == "B3")
  442.             {
  443.                 if (TicTacToeBoard[1, 2] != 0) return 1;
  444.             }
  445.             else if (loc == "C1")
  446.             {
  447.                 if (TicTacToeBoard[2, 0] != 0) return 1;
  448.             }
  449.             else if (loc == "C2")
  450.             {
  451.                 if (TicTacToeBoard[2, 1] != 0) return 1;
  452.             }
  453.             else if (loc == "C3")
  454.             {
  455.                 if (TicTacToeBoard[2, 2] != 0) return 1;
  456.             }
  457.             return 0;
  458.         }
  459.         void RandomTurn(int turn, int[,] board)
  460.         {
  461.             Random r = new Random();
  462.             int isValid = 1;
  463.             string move = "";
  464.             int loopCap = 0;
  465.             while (loopCap < 1000)
  466.             {
  467.                 loopCap++;
  468.                 move = (char)(r.Next(1, 4) + 64) + r.Next(1, 4).ToString();
  469.                 isValid = ValidateBoardState(move);
  470.  
  471.  
  472.                 // Console.Write(move);
  473.                 // Console.Write(isValid);
  474.                 // Console.Write(loopCap);
  475.                 // Console.Write(" ");
  476.                 if (isValid == 0) break;
  477.             }
  478.             // Console.WriteLine("");
  479.             ModBoard(move, turn);
  480.         }
  481.         void AdvancedTurn(int turn, int[,] board)
  482.         {
  483.             Move eval = new Move();
  484.             int isValid = 1;
  485.             string move = "";
  486.             int loopCap = 0;
  487.             while (loopCap < 1)
  488.             {
  489.                 int _a = 0;
  490.                 _a = CountPlayerSpaces(board, 0);
  491.                 eval = Minimax(board, 9, turn);
  492.                 loopCap++;
  493.                 move = (char)(eval.x + 65) + (eval.y + 1).ToString();
  494.                 isValid = ValidateBoardState(move);
  495.                 if (isValid == 0) break;
  496.             }
  497.             Console.WriteLine("Attempts: " + loopCap);
  498.             Console.WriteLine("Minimax Algorithm Data:");
  499.             Console.WriteLine("\t Score: " + eval.score);
  500.             Console.WriteLine("\t X Value: " + eval.x);
  501.             Console.WriteLine("\t Y Value: " + eval.y);
  502.             Console.WriteLine("Move chosen: " + move);
  503.             ModBoard(move, turn);
  504.  
  505.         }
  506.         int BoardBinary(int[,] board)
  507.         {
  508.             int boardBin = 0;
  509.             for (int x = 0; x < 3; x++)
  510.             {
  511.                 for (int y = 0; y < 3; y++)
  512.                 {
  513.                     boardBin |= board[x, y];
  514.                     boardBin <<= 2;
  515.  
  516.                 }
  517.             }
  518.             return boardBin;
  519.         }
  520.         /*
  521.         List<int[,]> RecursiveMoves(int[,] board, int currentPlayer)
  522.         {
  523.             List<int[,]> moves = new List<int[,]>();
  524.  
  525.             if (CheckForTicTacToe(true) != 0) return moves;
  526.  
  527.             for (int x = 0; x < 3; x++)
  528.             {
  529.                 for (int y = 0; y < 3; y++)
  530.                 {
  531.                     if (board[x, y] == 0)
  532.                     {
  533.                         int[,] newBoard = CopyBoard(board);
  534.  
  535.                         newBoard[x, y] = currentPlayer + 1;
  536.                         moves.Add(newBoard);
  537.                         List<int[,]> childMoves = RecursiveMoves(newBoard, 3 - currentPlayer);
  538.                         moves.AddRange(childMoves);
  539.                     }
  540.                 }
  541.             }
  542.  
  543.             return moves;
  544.         }
  545.         */
  546.         int Evaluate(int[,] board, int currentPlayer)
  547.         {
  548.             currentPlayer++;
  549.             int opponent = 3 - currentPlayer;
  550.             int WinLoss = 1000;
  551.             int center = 10;
  552.             int cornerFirst = 50;
  553.             int corner = 10;
  554.             int fork = 200;
  555.             int tiar = 100;
  556.             int eval = 0;
  557.  
  558.             // If we've won, the position is obviously good. If we've lost, the position is obviously bad.
  559.             // If it's a draw, it doesn't matter who was winning, the game is over.
  560.             if (CheckForTicTacToe(true) == currentPlayer) return WinLoss;
  561.             if (CheckForTicTacToe(true) == opponent) return -WinLoss;
  562.             if (CheckForTicTacToe(true) == 0 && CountPlayerSpaces(board, 0) == 0) return 0;
  563.  
  564.             // Control of the center is good. Having the enemy control the center, not so much.
  565.             if (board[1, 1] == currentPlayer) eval += center;
  566.             if (board[1, 1] == opponent) eval -= center;
  567.  
  568.             // According to the guide for the perfect Tic-Tac-Toe game, controlling the upper-left corner as X is best if it's our first move.
  569.             // We can determine this due to a neat little quirk where the first move means there's only one occupied space.
  570.             if (board[0, 0] == currentPlayer && CountPlayerSpaces(board, 0) == 8 && board[0, 0] == 1) eval += cornerFirst;
  571.  
  572.             // Corner control is important. We put emphasis on corner control.
  573.             eval += board[0, 0] == currentPlayer ? corner : 0;
  574.             eval += board[0, 2] == currentPlayer ? corner : 0;
  575.             eval += board[2, 0] == currentPlayer ? corner : 0;
  576.             eval += board[2, 2] == currentPlayer ? corner : 0;
  577.             eval -= board[0, 0] == opponent ? corner : 0;
  578.             eval -= board[0, 2] == opponent ? corner : 0;
  579.             eval -= board[2, 0] == opponent ? corner : 0;
  580.             eval -= board[2, 2] == opponent ? corner : 0;
  581.  
  582.             // We don't care too much about edges.
  583.             eval += board[0, 1] == currentPlayer ? 5 : 0;
  584.             eval += board[1, 0] == currentPlayer ? 5 : 0;
  585.             eval += board[1, 2] == currentPlayer ? 5 : 0;
  586.             eval += board[2, 1] == currentPlayer ? 5 : 0;
  587.             eval -= board[0, 1] == opponent ? 5 : 0;
  588.             eval -= board[1, 0] == opponent ? 5 : 0;
  589.             eval -= board[1, 2] == opponent ? 5 : 0;
  590.             eval -= board[2, 1] == opponent ? 5 : 0;
  591.  
  592.             // We should avoid having the opponent have two-in-a-rows (TIARs) while incentivising having us have TIARs
  593.             for (int x = 0; x < 8; x++)
  594.             {
  595.                 eval += IdentifyTIARs(board, currentPlayer - 1, x) != null ? tiar : 0;
  596.             }
  597.             for (int x = 0; x < 8; x++)
  598.             {
  599.                 eval -= IdentifyTIARs(board, opponent - 1, x) != null ? tiar : 0;
  600.             }
  601.             // If we have a fork, the enemy could block it. However, while hard, it's possible to block a fork (I think.) Therefore, we only put
  602.             // 200 points on our forks.
  603.             for (int x = 0; x < 9; x++)
  604.             {
  605.                 eval += IdentifyForks(board, currentPlayer - 1, x) != null ? fork : 0;
  606.             }
  607.             // If the enemy has a fork, and we ignored it, the game might as well be considered lost. Avoid letting the enemy fork us at any cost.
  608.             for (int x = 0; x < 9; x++)
  609.             {
  610.                 eval -= IdentifyForks(board, opponent - 1, x) != null ? WinLoss : 0;
  611.             }
  612.             return eval;
  613.         }
  614.         /*
  615.      * GOALS:
  616.      * Identify if a row/column/diagonal has a two-in-a-row (TIAR) in favor of that player
  617.      * CRD VALUES:
  618.      *      0: Row 1
  619.      *      1: Row 2
  620.      *      2: Row 3
  621.      *      3: Column 1
  622.      *      4: Column 2
  623.      *      5: Column 3
  624.      *      6: Left-Right Diagonal
  625.      *      7: Right-Left Diagonal
  626.      * After that, if a TIAR is found, it outputes:
  627.      *      1: Hole found in first position (Leftmost for rows, Highest for columns and diagonals)
  628.      *      2: Hole found in middle position
  629.      *      3: Hole found in last position
  630.      * If no TIAR was found, the function returns Null.
  631.      */
  632.         int? IdentifyTIARs(int[,] board, int currentPlayer, int crd)
  633.         {
  634.             int? holePos = null;
  635.  
  636.             currentPlayer += 1;
  637.             // Console.WriteLine("CurrentPlayer debug: " + currentPlayer);
  638.             switch (crd)
  639.             {
  640.                 case 0:
  641.                     if (board[0, 0] == 0 && board[0, 1] == currentPlayer && board[0, 2] == currentPlayer) holePos = 1;
  642.                     if (board[0, 0] == currentPlayer && board[0, 1] == 0 && board[0, 2] == currentPlayer) holePos = 2;
  643.                     if (board[0, 0] == currentPlayer && board[0, 1] == currentPlayer && board[0, 2] == 0) holePos = 3;
  644.                     break;
  645.                 case 1:
  646.                     if (board[1, 0] == 0 && board[1, 1] == currentPlayer && board[1, 2] == currentPlayer) holePos = 1;
  647.                     if (board[1, 0] == currentPlayer && board[1, 1] == 0 && board[1, 2] == currentPlayer) holePos = 2;
  648.                     if (board[1, 0] == currentPlayer && board[1, 1] == currentPlayer && board[1, 2] == 0) holePos = 3;
  649.                     break;
  650.                 case 2:
  651.                     if (board[2, 0] == 0 && board[2, 1] == currentPlayer && board[2, 2] == currentPlayer) holePos = 1;
  652.                     if (board[2, 0] == currentPlayer && board[2, 1] == 0 && board[2, 2] == currentPlayer) holePos = 2;
  653.                     if (board[2, 0] == currentPlayer && board[2, 1] == currentPlayer && board[2, 2] == 0) holePos = 3;
  654.                     break;
  655.                 case 3:
  656.                     if (board[0, 0] == 0 && board[1, 0] == currentPlayer && board[2, 0] == currentPlayer) holePos = 1;
  657.                     if (board[0, 0] == currentPlayer && board[1, 0] == 0 && board[2, 0] == currentPlayer) holePos = 2;
  658.                     if (board[0, 0] == currentPlayer && board[1, 0] == currentPlayer && board[2, 0] == 0) holePos = 3;
  659.                     break;
  660.                 case 4:
  661.                     if (board[0, 1] == 0 && board[1, 1] == currentPlayer && board[2, 1] == currentPlayer) holePos = 1;
  662.                     if (board[0, 1] == currentPlayer && board[1, 1] == 0 && board[2, 1] == currentPlayer) holePos = 2;
  663.                     if (board[0, 1] == currentPlayer && board[1, 1] == currentPlayer && board[2, 1] == 0) holePos = 3;
  664.                     break;
  665.                 case 5:
  666.                     if (board[0, 2] == 0 && board[1, 2] == currentPlayer && board[2, 2] == currentPlayer) holePos = 1;
  667.                     if (board[0, 2] == currentPlayer && board[1, 2] == 0 && board[2, 2] == currentPlayer) holePos = 2;
  668.                     if (board[0, 2] == currentPlayer && board[1, 2] == currentPlayer && board[2, 2] == 0) holePos = 3;
  669.                     break;
  670.                 case 6:
  671.                     if (board[0, 0] == 0 && board[1, 1] == currentPlayer && board[2, 2] == currentPlayer) holePos = 1;
  672.                     if (board[0, 0] == currentPlayer && board[1, 1] == 0 && board[2, 2] == currentPlayer) holePos = 2;
  673.                     if (board[0, 0] == currentPlayer && board[1, 1] == currentPlayer && board[2, 2] == 0) holePos = 3;
  674.                     break;
  675.                 case 7:
  676.                     if (board[0, 2] == 0 && board[1, 1] == currentPlayer && board[2, 0] == currentPlayer) holePos = 1;
  677.                     if (board[0, 2] == currentPlayer && board[1, 1] == 0 && board[2, 0] == currentPlayer) holePos = 2;
  678.                     if (board[0, 2] == currentPlayer && board[1, 1] == currentPlayer && board[2, 0] == 0) holePos = 3;
  679.                     break;
  680.             }
  681.  
  682.             return holePos;
  683.         }
  684.         int? IdentifyForks(int[,] board, int currentPlayer, int holePos)
  685.         {
  686.             int? forkID = null;
  687.             int locQuan = 0;
  688.             switch (holePos)
  689.             {
  690.                 case 0:
  691.                     if (IdentifyTIARs(board, currentPlayer, 0) == 2 || IdentifyTIARs(board, currentPlayer, 0) == 3) locQuan++;
  692.                     if (IdentifyTIARs(board, currentPlayer, 3) == 2 || IdentifyTIARs(board, currentPlayer, 3) == 3) locQuan++;
  693.                     if (IdentifyTIARs(board, currentPlayer, 6) == 2 || IdentifyTIARs(board, currentPlayer, 6) == 3) locQuan++;
  694.                     if (locQuan > 1) forkID = 0;
  695.                     break;
  696.                 case 1:
  697.                     if (IdentifyTIARs(board, currentPlayer, 0) == 1 || IdentifyTIARs(board, currentPlayer, 0) == 3) locQuan++;
  698.                     if (IdentifyTIARs(board, currentPlayer, 4) == 2 || IdentifyTIARs(board, currentPlayer, 4) == 3) locQuan++;
  699.                     if (locQuan > 1) forkID = 1;
  700.                     break;
  701.                 case 2:
  702.                     if (IdentifyTIARs(board, currentPlayer, 0) == 1 || IdentifyTIARs(board, currentPlayer, 0) == 2) locQuan++;
  703.                     if (IdentifyTIARs(board, currentPlayer, 5) == 2 || IdentifyTIARs(board, currentPlayer, 5) == 3) locQuan++;
  704.                     if (IdentifyTIARs(board, currentPlayer, 7) == 2 || IdentifyTIARs(board, currentPlayer, 7) == 3) locQuan++;
  705.                     if (locQuan > 1) forkID = 2;
  706.                     break;
  707.                 case 3:
  708.                     if (IdentifyTIARs(board, currentPlayer, 1) == 2 || IdentifyTIARs(board, currentPlayer, 1) == 3) locQuan++;
  709.                     if (IdentifyTIARs(board, currentPlayer, 3) == 1 || IdentifyTIARs(board, currentPlayer, 3) == 3) locQuan++;
  710.                     if (locQuan > 1) forkID = 3;
  711.                     break;
  712.                 case 4:
  713.                     if (IdentifyTIARs(board, currentPlayer, 1) == 1 || IdentifyTIARs(board, currentPlayer, 1) == 3) locQuan++;
  714.                     if (IdentifyTIARs(board, currentPlayer, 4) == 1 || IdentifyTIARs(board, currentPlayer, 4) == 3) locQuan++;
  715.                     if (IdentifyTIARs(board, currentPlayer, 6) == 1 || IdentifyTIARs(board, currentPlayer, 6) == 3) locQuan++;
  716.                     if (IdentifyTIARs(board, currentPlayer, 7) == 1 || IdentifyTIARs(board, currentPlayer, 7) == 3) locQuan++;
  717.                     if (locQuan > 1) forkID = 4;
  718.                     break;
  719.                 case 5:
  720.                     if (IdentifyTIARs(board, currentPlayer, 1) == 1 || IdentifyTIARs(board, currentPlayer, 3) == 2) locQuan++;
  721.                     if (IdentifyTIARs(board, currentPlayer, 5) == 1 || IdentifyTIARs(board, currentPlayer, 5) == 3) locQuan++;
  722.                     if (locQuan > 1) forkID = 5;
  723.                     break;
  724.                 case 6:
  725.                     if (IdentifyTIARs(board, currentPlayer, 2) == 2 || IdentifyTIARs(board, currentPlayer, 2) == 3) locQuan++;
  726.                     if (IdentifyTIARs(board, currentPlayer, 3) == 1 || IdentifyTIARs(board, currentPlayer, 3) == 2) locQuan++;
  727.                     if (IdentifyTIARs(board, currentPlayer, 7) == 1 || IdentifyTIARs(board, currentPlayer, 7) == 2) locQuan++;
  728.                     if (locQuan > 1) forkID = 6;
  729.                     break;
  730.                 case 7:
  731.                     if (IdentifyTIARs(board, currentPlayer, 2) == 1 || IdentifyTIARs(board, currentPlayer, 2) == 3) locQuan++;
  732.                     if (IdentifyTIARs(board, currentPlayer, 4) == 1 || IdentifyTIARs(board, currentPlayer, 4) == 2) locQuan++;
  733.                     if (locQuan > 1) forkID = 7;
  734.                     break;
  735.                 case 8:
  736.                     if (IdentifyTIARs(board, currentPlayer, 2) == 1 || IdentifyTIARs(board, currentPlayer, 2) == 2) locQuan++;
  737.                     if (IdentifyTIARs(board, currentPlayer, 5) == 1 || IdentifyTIARs(board, currentPlayer, 5) == 2) locQuan++;
  738.                     if (IdentifyTIARs(board, currentPlayer, 6) == 1 || IdentifyTIARs(board, currentPlayer, 6) == 2) locQuan++;
  739.                     if (locQuan > 1) forkID = 8;
  740.                     break;
  741.             }
  742.  
  743.             return forkID;
  744.         }
  745.         Move Minimax(int[,] board, int depth, int currentPlayer, bool max = true)
  746.         {
  747.             if (depth == 0 || CountPlayerSpaces(board, 0) == 0)
  748.             {
  749.  
  750.                 return new Move { score = Evaluate(board, currentPlayer) };
  751.             }
  752.  
  753.             int bestScore = 1;
  754.             Move m = new Move();
  755.             if (max)
  756.             {
  757.                 bestScore = int.MinValue;
  758.                 for (int x = 0; x < 3; x++)
  759.                 {
  760.                     for (int y = 0; y < 3; y++)
  761.                     {
  762.                         if (board[x, y] == 0)
  763.                         {
  764.                             int[,] newBoard = CopyBoard(board);
  765.  
  766.                             newBoard[x, y] = currentPlayer + 1;
  767.                             m = Minimax(newBoard, depth - 1, 1 - currentPlayer, false);
  768.                             int score = m.score;
  769.                             m.x = score > bestScore ? x : m.x;
  770.                             m.y = score > bestScore ? y : m.y;
  771.                             bestScore = score > bestScore ? score : bestScore;
  772.                            
  773.                         }
  774.                     }
  775.                 }
  776.             }
  777.             else
  778.             {
  779.                 bestScore = int.MaxValue;
  780.                 for (int x = 0; x < 3; x++)
  781.                 {
  782.                     for (int y = 0; y < 3; y++)
  783.                     {
  784.                         if (board[x, y] == 0)
  785.                         {
  786.                             int[,] newBoard = CopyBoard(board);
  787.                             newBoard[x, y] = currentPlayer + 1;
  788.                             m = Minimax(newBoard, depth - 1, 1 - currentPlayer, true);
  789.                             int score = m.score;
  790.                             m.x = score < bestScore ? x : m.x;
  791.                             m.y = score < bestScore ? y : m.y;
  792.                             bestScore = score < bestScore ? score : bestScore;
  793.                         }
  794.                     }
  795.                 }
  796.             }
  797.             m.score = bestScore;
  798.             return m;
  799.         }
  800.         void FuckassCounter(int[,] board)
  801.         {
  802.  
  803.         }
  804.     }
  805. }
  806.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement