The_Programmer_Gamer

Tic_Tac_Toe (finished version)

Mar 30th, 2020 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 24.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace TIC_TAC_TOE
  8. {
  9.     class Program
  10.     {
  11.         //da ich sehr viele complications mit dem Internet und YouToube Videos gelöst habe, verstehe ich noch nich 100%ig den aufbau.
  12.         //jedoch versuchte ich so viel es ging mit meine skills und meinem wissen zu lösen
  13.         //da ich bei der PvP variante es nicht ganz hinbekommen habe, habe ich noch einen leichten KI/AI einprogrammiert
  14.         //
  15.         //Alle Versuche und Lösungen basieren teilweise auch auf Lösungswege aus dem Internet.
  16.         //
  17.         //has anyone an idea for a AI and how to programm it (a good one)?
  18.         //Thx for help
  19.         //
  20.         //(please visit this link if you want to see my faild trys)
  21.         //https://pastebin.com/B1dJyVVd
  22.  
  23.         //boolean = true or fals (1 or 0)
  24.         //try/catch = das Programm vor Fehler schützen --> wenn es fasch ist nicht sofort beenden sonder nochmals wiederholen
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.         static void Main(string[] args)
  32.         {
  33.             Console.WriteLine("Tic_Tac_Toe                                                             made by The_Programmer_Gamer");
  34.             Menu();
  35.  
  36.             Console.Clear();
  37.             Console.WriteLine("Thanks for playing the game!");
  38.             System.Threading.Thread.Sleep(1000);
  39.         }
  40.  
  41.  
  42.         private static void Menu()
  43.         {
  44.             Console.Clear();
  45.  
  46.             Console.WriteLine("Tic_Tac_Toe\n\n\nTic_Tac_Toe PvP (1)             Tic_Tac_Toe PvAI (2)");
  47.             string choice = Console.ReadLine();
  48.  
  49.             if (choice == "1" || choice == "PvP")
  50.             {
  51.                 Console.WriteLine("You choose PvP");
  52.                 Console.WriteLine("There happaned a mistake and i can't fix it (10:15 PM 30.03.2019)");
  53.                 XO();
  54.             }
  55.             else if (choice == "2" || choice == "PvAI")
  56.             {
  57.                 Console.WriteLine("You choose PvAI\n\nIt's not good but an AI so...");
  58.                 AI();
  59.             }
  60.         }
  61.  
  62.         private static void X()
  63.         {
  64.             Console.Clear();
  65.             Console.WriteLine("TicTacToe\n\n\nP1 (X)   starts: (write in 1/2/3/4/5/6/7/8/9)");
  66.             Console.WriteLine();
  67.             Console.WriteLine();
  68.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  69.             Console.WriteLine("                 ||           ||                                           1    ||     2     ||    3     ");
  70.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  71.             Console.WriteLine("       ===================================                            ===================================");
  72.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  73.             Console.WriteLine("                 ||           ||                                           4    ||     5     ||    6     ");
  74.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  75.             Console.WriteLine("       ===================================                            ===================================");
  76.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  77.             Console.WriteLine("                 ||           ||                                           7    ||     8     ||    9     ");
  78.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  79.             Console.WriteLine();
  80.             Console.WriteLine("A short info: I don't know but I made a mistake because at the array pos 1/2/... it says 2 istead of 1/3 instead of 2/...and so on.");
  81.             System.Threading.Thread.Sleep(3500);
  82.             Console.Clear();
  83.             Console.WriteLine("And Start (P1/PX)!! Good Luck!");
  84.             System.Threading.Thread.Sleep(1000);
  85.             Console.Clear();
  86.  
  87.             string[] stringin = new string[10];
  88.             bool p1x = true;
  89.             bool wrong = false;
  90.             int TRY = 1;
  91.             for (int i = 0; i < stringin.Length; i++)
  92.             {
  93.                 stringin[i] = (i + 1).ToString();
  94.             }
  95.  
  96.             Grid(stringin);
  97.  
  98.             int pinput = 1;
  99.             string P1xP2o;
  100.             for (int i = 0; i < 9; i++)
  101.             {
  102.                 int round = TRY++;
  103.                 Console.WriteLine("\n\nRound: " + round);
  104.                 P1xP2o = p1x ? "X" : "O";
  105.                 bool error = false;
  106.                 do
  107.                 {
  108.                     try
  109.                     {
  110.                         pinput = Int32.Parse(Console.ReadLine());
  111.                         error = false;
  112.  
  113.                         if (stringin[pinput] == "X" || stringin[pinput] == "O")
  114.                         {
  115.                             Console.WriteLine("Does not exist or something going wrong!");
  116.                         }
  117.                         else
  118.                         {
  119.                             Console.WriteLine("Next one!");
  120.                         }
  121.                     }
  122.                     catch
  123.                     {
  124.                         Console.WriteLine("Does not exist or something going wrong!");
  125.                         error = true;
  126.                     }
  127.  
  128.                 } while(error || pinput > 9 || pinput < 1 || stringin[pinput] == "X" || stringin[pinput] == "O");
  129.  
  130.                 stringin[pinput] = P1xP2o;
  131.                 Console.Clear();
  132.                 Grid(stringin);
  133.                 p1x = !p1x;
  134.                 wrong = WWON(stringin, P1xP2o);
  135.                 if (wrong) break;
  136.             }
  137.  
  138.             bool P1xW = WWON(stringin, "X");
  139.            
  140.             int retour = 0;
  141.             int x = 2;
  142.             int testy = retour - x;
  143.  
  144.             bool P2oW = WWON(stringin, "O");
  145.  
  146.             if (P1xW)
  147.             {
  148.                 Console.WriteLine("P1 or PX won!");
  149.                 System.Threading.Thread.Sleep(1000);
  150.                 Console.Clear();
  151.  
  152.             }
  153.             else if (P2oW)
  154.             {
  155.                 Console.WriteLine("P2 or PO won!");
  156.             }
  157.             else Console.WriteLine("Default!");
  158.  
  159.             Console.ReadKey();
  160.             Menu();
  161.  
  162.         }
  163.  
  164.  
  165.         private static void O()
  166.         {
  167.             Console.Clear();
  168.             Console.WriteLine("TicTacToe\n\n\nP1 (X)   starts: (write in 1/2/3/4/5/6/7/8/9)");
  169.             Console.WriteLine();
  170.             Console.WriteLine();
  171.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  172.             Console.WriteLine("                 ||           ||                                           1    ||     2     ||    3     ");
  173.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  174.             Console.WriteLine("       ===================================                            ===================================");
  175.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  176.             Console.WriteLine("                 ||           ||                                           4    ||     5     ||    6     ");
  177.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  178.             Console.WriteLine("       ===================================                            ===================================");
  179.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  180.             Console.WriteLine("                 ||           ||                                           7    ||     8     ||    9     ");
  181.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  182.             Console.WriteLine();
  183.             Console.WriteLine("A short info: I don't know but I made a mistake because at the array pos 1/2/... it says 2 istead of 1/3 instead of 2/...and so on.");
  184.             System.Threading.Thread.Sleep(3500);
  185.             Console.Clear();
  186.             Console.WriteLine("And Start (P1)!! Good Luck!");
  187.             System.Threading.Thread.Sleep(1000);
  188.             Console.Clear();
  189.  
  190.             string[] stringin = new string[10];
  191.             bool p1x = true;
  192.             bool wrong = false;
  193.             int TRY = 1;
  194.             for (int i = 0; i < stringin.Length; i++)
  195.             {
  196.                 stringin[i] = (i + 1).ToString();
  197.             }
  198.  
  199.             Grid(stringin);
  200.  
  201.             int pinput = 1;
  202.             string P1xP2o;
  203.             for (int i = 0; i < 9; i++)
  204.             {
  205.                 int round = TRY++;
  206.                 Console.WriteLine("\n\nRound: " + round);
  207.                 P1xP2o = p1x ? "X" : "O";
  208.                 bool error = false;
  209.                 do
  210.                 {
  211.                     try
  212.                     {
  213.                         pinput = Int32.Parse(Console.ReadLine());
  214.                         error = false;
  215.  
  216.                         if (stringin[pinput] == "X" || stringin[pinput] == "O")
  217.                         {
  218.                             Console.WriteLine("Does not exist or something going wrong!");
  219.                         }
  220.                         else
  221.                         {
  222.                             Console.WriteLine("Next one!");
  223.                         }
  224.                     }
  225.                     catch
  226.                     {
  227.                         Console.WriteLine("Does not exist or something going wrong!");
  228.                         error = true;
  229.                     }
  230.  
  231.                 } while (error || pinput > 9 || pinput < 1 || stringin[pinput] == "X" || stringin[pinput] == "O");
  232.  
  233.                 stringin[pinput] = P1xP2o;
  234.                 Console.Clear();
  235.                 Grid(stringin);
  236.                 p1x = !p1x;
  237.                 wrong = WWON(stringin, P1xP2o);
  238.                 if (wrong) break;
  239.             }
  240.  
  241.             bool P1xW = WWON(stringin, "X");
  242.  
  243.             int retour = 0;
  244.             int x = 2;
  245.             int testy = retour - x;
  246.  
  247.             bool P2oW = WWON(stringin, "O");
  248.  
  249.             if (P1xW)
  250.             {
  251.                 Console.WriteLine("P1 or X won!");
  252.             }
  253.             else if (P2oW)
  254.             {
  255.                 Console.WriteLine("P2 or O won!");
  256.             }
  257.             else Console.WriteLine("Default!");
  258.  
  259.             Console.ReadKey();
  260.             Menu();
  261.  
  262.         }
  263.  
  264.         private static void XO()
  265.         {
  266.             Console.Clear();
  267.             Console.WriteLine("TicTacToe\n\nNew game! Do you want to be X or O:\n\n");
  268.             string XO = Console.ReadLine();
  269.  
  270.             switch (XO)
  271.             {
  272.                 case "X":
  273.                     Console.WriteLine("You choose X\n\n You're Player one. (P1)");
  274.                     System.Threading.Thread.Sleep(2000);
  275.                     Console.Clear();
  276.                     X();
  277.                     break;
  278.  
  279.                 case "x":
  280.                     Console.WriteLine("You choose X\n\n You're Player one. (P1)");
  281.                     System.Threading.Thread.Sleep(2000);
  282.                     X();
  283.                     break;
  284.  
  285.                 case "O":
  286.                     Console.WriteLine("You choose O\n\n You're Player two. (P2)");
  287.                     System.Threading.Thread.Sleep(2000);
  288.                     Console.Clear();
  289.                     O();
  290.                     break;
  291.  
  292.                 case "o":
  293.                     Console.WriteLine("You choose O\n\n You're Player two. (P2)");
  294.                     System.Threading.Thread.Sleep(2000);
  295.                     Console.Clear();
  296.                     O();
  297.                     break;
  298.  
  299.                 default:
  300.                     Console.WriteLine("There is a problem! Press m to come back to the menu:");
  301.                     string m = Console.ReadLine();
  302.  
  303.                     if (m == "m")
  304.                     {
  305.                         Menu();
  306.                     }
  307.                     else
  308.                     {
  309.                         Console.WriteLine("Default");
  310.                     }
  311.                     break;
  312.  
  313.  
  314.             }
  315.  
  316.  
  317.         }
  318.  
  319.        
  320.         static bool Ifdiagonal(string[] stringin, string searchforStringin, int begin, int xy)
  321.         {
  322.             bool playwon = true;
  323.  
  324.             for (int i = 0; i < 3; ++i)
  325.             {
  326.                 int field = begin + (i * xy);
  327.                 if (stringin[field] != searchforStringin) playwon = false;
  328.             }
  329.  
  330.             return playwon;
  331.         }
  332.  
  333.  
  334.         static bool WWON(string[] stringin, string searchforStringin)
  335.         {
  336.             bool playwon = false;
  337.  
  338.             //1. for horizontal
  339.             //2. for perpendicular
  340.             //3. for diagonal (1/2)
  341.  
  342.             for (int i = 0; i < 3; ++i)
  343.             {
  344.                 playwon = true;
  345.                 int begin = 0 + 3 * i;
  346.                 for (int x = begin; x < begin + 3; x++)
  347.                 {
  348.                     if (stringin[x] != searchforStringin) playwon = false;
  349.                 }
  350.  
  351.                 if (playwon) return true;
  352.             }
  353.  
  354.             for (int i = 0; i < 3; ++i)
  355.             {
  356.                 playwon = true;
  357.                 for (int x = i; x < i + 7; x = x + 3)
  358.                 {
  359.                     if (stringin[x] != searchforStringin) playwon = false;
  360.                 }
  361.  
  362.                 if (playwon) return true;
  363.             }
  364.  
  365.             if (Ifdiagonal(stringin, searchforStringin, 0, 4)) return true;
  366.             if (Ifdiagonal(stringin, searchforStringin, 2, 2)) return true;
  367.             return playwon;
  368.         }
  369.  
  370.         static void Grid(string[] stringin)
  371.         {
  372.             Console.WriteLine("                 ||           ||            ");
  373.             Console.WriteLine("            {0}    ||     {1}     ||     {2}    ", stringin[1], stringin[2], stringin[3]);
  374.             Console.WriteLine("                 ||           ||            ");
  375.             Console.WriteLine("       ===================================  ");
  376.             Console.WriteLine("                 ||           ||            ");
  377.             Console.WriteLine("            {0}    ||     {1}     ||     {2}    ", stringin[4], stringin[5], stringin[6]);
  378.             Console.WriteLine("                 ||           ||            ");
  379.             Console.WriteLine("       ===================================  ");
  380.             Console.WriteLine("                 ||           ||            ");
  381.             Console.WriteLine("            {0}    ||     {1}     ||     {2}    ", stringin[7], stringin[8], stringin[9]);
  382.             Console.WriteLine("                 ||           ||            ");
  383.  
  384.  
  385.             //koennten SIe mir helfen die 1/2/3/4/5/6/7/8/9 im Raster durch leerzeichen zu erstzen
  386.             //
  387.             //can enyone help me to replace the {0}/{1}/... to an space, so there is a cleared grid? Thx for help
  388.  
  389.         }
  390.  
  391.  
  392.  
  393.  
  394.  
  395.         //AI
  396.         //this is an small randomized AI, that means that the computer plays luckly.
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.         private static void AI()
  405.         {
  406.             Console.Clear();
  407.             Console.WriteLine("TicTacToe\n\n\nYou are P1 (X)   starts: (write in 1/2/3/4/5/6/7/8/9)");
  408.             Console.WriteLine();
  409.             Console.WriteLine();
  410.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  411.             Console.WriteLine("                 ||           ||                                           1    ||     2     ||    3     ");
  412.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  413.             Console.WriteLine("       ===================================                            ===================================");
  414.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  415.             Console.WriteLine("                 ||           ||                                           4    ||     5     ||    6     ");
  416.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  417.             Console.WriteLine("       ===================================                            ===================================");
  418.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  419.             Console.WriteLine("                 ||           ||                                           7    ||     8     ||    9     ");
  420.             Console.WriteLine("                 ||           ||                                                ||           ||          ");
  421.             Console.WriteLine();
  422.             System.Threading.Thread.Sleep(2000);
  423.             Console.Clear();
  424.             Console.WriteLine("And Start (P1/PX)!! Good Luck!");
  425.             System.Threading.Thread.Sleep(1000);
  426.             Console.Clear();
  427.  
  428.             int PorAI = 2;
  429.             int input = 0;
  430.             int checkup = 0;
  431.             int round = 0;
  432.  
  433.             char[,] array =
  434.             {
  435.                 {'1','2','3'},{'4','5','6'},{'7','8','9'}
  436.             };
  437.  
  438.             do
  439.             {
  440.  
  441.  
  442.  
  443.                 if (PorAI == 2)
  444.                     PorAI = 1;
  445.                 else if (PorAI == 1)
  446.                     PorAI = 2;
  447.  
  448.                 switch (PorAI)
  449.                 {
  450.                     case 1:
  451.                         {
  452.                             switch (input)
  453.                             {
  454.                                 case 1: array[0, 0] = 'X'; break;
  455.                                 case 2: array[0, 1] = 'X'; break;
  456.                                 case 3: array[0, 2] = 'X'; break;
  457.                                 case 4: array[1, 0] = 'X'; break;
  458.                                 case 5: array[1, 1] = 'X'; break;
  459.                                 case 6: array[1, 2] = 'X'; break;
  460.                                 case 7: array[2, 0] = 'X'; break;
  461.                                 case 8: array[2, 1] = 'X'; break;
  462.                                 case 9: array[2, 2] = 'X'; break;
  463.                             }
  464.                             break;
  465.                         }
  466.  
  467.                     case 2:
  468.                         {
  469.                             switch (input)
  470.                             {
  471.                                 case 1: array[0, 0] = 'O'; break;
  472.                                 case 2: array[0, 1] = 'O'; break;
  473.                                 case 3: array[0, 2] = 'O'; break;
  474.                                 case 4: array[1, 0] = 'O'; break;
  475.                                 case 5: array[1, 1] = 'O'; break;
  476.                                 case 6: array[1, 2] = 'O'; break;
  477.                                 case 7: array[2, 0] = 'O'; break;
  478.                                 case 8: array[2, 1] = 'O'; break;
  479.                                 case 9: array[2, 2] = 'O'; break;
  480.                             }
  481.                             break;
  482.                         }
  483.                 }
  484.  
  485.                 System.Threading.Thread.Sleep(2000);
  486.                 Console.Clear();
  487.                 Console.WriteLine("");
  488.                 Console.WriteLine("");
  489.                 Console.WriteLine("                 ||           ||            ");
  490.                 Console.WriteLine("            {0}    ||     {1}     ||     {2}    ", array[0, 0], array[0, 1], array[0, 2]);
  491.                 Console.WriteLine("                 ||           ||            ");
  492.                 Console.WriteLine("       ===================================  ");
  493.                 Console.WriteLine("                 ||           ||            ");
  494.                 Console.WriteLine("            {0}    ||     {1}     ||     {2}    ", array[1, 0], array[1, 1], array[1, 2]);
  495.                 Console.WriteLine("                 ||           ||            ");
  496.                 Console.WriteLine("       ===================================  ");
  497.                 Console.WriteLine("                 ||           ||            ");
  498.                 Console.WriteLine("            {0}    ||     {1}     ||     {2}    ", array[2, 0], array[2, 1], array[2, 2]);
  499.                 Console.WriteLine("                 ||           ||            ");
  500.  
  501.  
  502.                 round++;
  503.                 Console.WriteLine("Try: " + round);
  504.  
  505.  
  506.  
  507.                 if
  508.                 (
  509.                     ((array[0, 0] == 'O') && (array[0, 1] == 'O') && (array[0, 2] == 'O')) ||
  510.                     ((array[1, 0] == 'O') && (array[1, 1] == 'O') && (array[1, 2] == 'O')) ||
  511.                     ((array[2, 0] == 'O') && (array[2, 1] == 'O') && (array[2, 2] == 'O')) ||
  512.                     ((array[0, 0] == 'O') && (array[1, 0] == 'O') && (array[2, 0] == 'O')) ||
  513.                     ((array[0, 1] == 'O') && (array[1, 1] == 'O') && (array[2, 1] == 'O')) ||
  514.                     ((array[0, 2] == 'O') && (array[1, 2] == 'O') && (array[2, 2] == 'O')) ||
  515.                     ((array[0, 0] == 'O') && (array[1, 1] == 'O') && (array[2, 2] == 'O')) ||
  516.                     ((array[0, 2] == 'O') && (array[1, 1] == 'O') && (array[2, 0] == 'O'))
  517.                 )
  518.                 {
  519.                     Console.WriteLine("\nAIo won!");
  520.                     Console.ReadLine();
  521.                     break;
  522.                 }
  523.  
  524.                 else if
  525.                 (
  526.                     ((array[0, 0] == 'X') && (array[0, 1] == 'X') && (array[0, 2] == 'X')) ||
  527.                     ((array[1, 0] == 'X') && (array[1, 1] == 'X') && (array[1, 2] == 'X')) ||
  528.                     ((array[2, 0] == 'X') && (array[2, 1] == 'X') && (array[2, 2] == 'X')) ||
  529.                     ((array[0, 0] == 'X') && (array[1, 0] == 'X') && (array[2, 0] == 'X')) ||
  530.                     ((array[0, 1] == 'X') && (array[1, 1] == 'X') && (array[2, 1] == 'X')) ||
  531.                     ((array[0, 2] == 'X') && (array[1, 2] == 'X') && (array[2, 2] == 'X')) ||
  532.                     ((array[0, 0] == 'X') && (array[1, 1] == 'X') && (array[2, 2] == 'X')) ||
  533.                     ((array[0, 2] == 'X') && (array[1, 1] == 'X') && (array[2, 0] == 'X'))
  534.                 )
  535.                 {
  536.                     Console.WriteLine("\nPx won!");
  537.                     Console.ReadLine();
  538.                     break;
  539.                 }
  540.                 else if (round == 10)
  541.                 {
  542.                     Console.WriteLine("\nDefault!");
  543.                     Console.ReadLine();
  544.                     break;
  545.                 }
  546.  
  547.  
  548.  
  549.  
  550.  
  551.                 do
  552.                 {
  553.                     Console.Write("\nPlayer {0}: Choose Array 1/2/3/4/5/6/7/8/9! ", PorAI);
  554.  
  555.                     if (PorAI == 1)
  556.                     {
  557.                         Console.WriteLine("It's AI's turn...");
  558.                         int min = 1;
  559.                         int max = 10;
  560.  
  561.                         Random random = new Random();
  562.                         int number = random.Next(min, max);
  563.  
  564.                         input = Convert.ToInt32(number); //das erste mal wo ich verstehe warum man statt Parse Convert nimmt.
  565.                     }
  566.                     else
  567.                     {
  568.                         input = Int32.Parse(Console.ReadLine());
  569.                     }
  570.  
  571.  
  572.                     if ((input == 1) && (array[0, 0] == '1'))
  573.                         checkup = 0;
  574.                     else if ((input == 2) && (array[0, 1] == '2'))
  575.                         checkup = 0;
  576.                     else if ((input == 3) && (array[0, 2] == '3'))
  577.                         checkup = 0;
  578.                     else if ((input == 4) && (array[1, 0] == '4'))
  579.                         checkup = 0;
  580.                     else if ((input == 5) && (array[1, 1] == '5'))
  581.                         checkup = 0;
  582.                     else if ((input == 6) && (array[1, 2] == '6'))
  583.                         checkup = 0;
  584.                     else if ((input == 7) && (array[2, 0] == '7'))
  585.                         checkup = 0;
  586.                     else if ((input == 8) && (array[2, 1] == '8'))
  587.                         checkup = 0;
  588.                     else if ((input == 9) && (array[2, 2] == '9'))
  589.                         checkup = 0;
  590.                     else
  591.                     {
  592.                         Console.WriteLine("\nDoes not exist or something going wrong! ");
  593.                         checkup = 1;
  594.                     }
  595.                 } while (checkup == 1);
  596.  
  597.  
  598.             } while (true);
  599.         }
  600.     }
  601.  
  602. }
Add Comment
Please, Sign In to add comment