Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TIC_TAC_TOE
- {
- class Program
- {
- //da ich sehr viele complications mit dem Internet und YouToube Videos gelöst habe, verstehe ich noch nich 100%ig den aufbau.
- //jedoch versuchte ich so viel es ging mit meine skills und meinem wissen zu lösen
- //da ich bei der PvP variante es nicht ganz hinbekommen habe, habe ich noch einen leichten KI/AI einprogrammiert
- //
- //Alle Versuche und Lösungen basieren teilweise auch auf Lösungswege aus dem Internet.
- //
- //has anyone an idea for a AI and how to programm it (a good one)?
- //Thx for help
- //
- //(please visit this link if you want to see my faild trys)
- //https://pastebin.com/B1dJyVVd
- //boolean = true or fals (1 or 0)
- //try/catch = das Programm vor Fehler schützen --> wenn es fasch ist nicht sofort beenden sonder nochmals wiederholen
- static void Main(string[] args)
- {
- Console.WriteLine("Tic_Tac_Toe made by The_Programmer_Gamer");
- Menu();
- Console.Clear();
- Console.WriteLine("Thanks for playing the game!");
- System.Threading.Thread.Sleep(1000);
- }
- private static void Menu()
- {
- Console.Clear();
- Console.WriteLine("Tic_Tac_Toe\n\n\nTic_Tac_Toe PvP (1) Tic_Tac_Toe PvAI (2)");
- string choice = Console.ReadLine();
- if (choice == "1" || choice == "PvP")
- {
- Console.WriteLine("You choose PvP");
- Console.WriteLine("There happaned a mistake and i can't fix it (10:15 PM 30.03.2019)");
- XO();
- }
- else if (choice == "2" || choice == "PvAI")
- {
- Console.WriteLine("You choose PvAI\n\nIt's not good but an AI so...");
- AI();
- }
- }
- private static void X()
- {
- Console.Clear();
- Console.WriteLine("TicTacToe\n\n\nP1 (X) starts: (write in 1/2/3/4/5/6/7/8/9)");
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" || || 1 || 2 || 3 ");
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" =================================== ===================================");
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" || || 4 || 5 || 6 ");
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" =================================== ===================================");
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" || || 7 || 8 || 9 ");
- Console.WriteLine(" || || || || ");
- Console.WriteLine();
- 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.");
- System.Threading.Thread.Sleep(3500);
- Console.Clear();
- Console.WriteLine("And Start (P1/PX)!! Good Luck!");
- System.Threading.Thread.Sleep(1000);
- Console.Clear();
- string[] stringin = new string[10];
- bool p1x = true;
- bool wrong = false;
- int TRY = 1;
- for (int i = 0; i < stringin.Length; i++)
- {
- stringin[i] = (i + 1).ToString();
- }
- Grid(stringin);
- int pinput = 1;
- string P1xP2o;
- for (int i = 0; i < 9; i++)
- {
- int round = TRY++;
- Console.WriteLine("\n\nRound: " + round);
- P1xP2o = p1x ? "X" : "O";
- bool error = false;
- do
- {
- try
- {
- pinput = Int32.Parse(Console.ReadLine());
- error = false;
- if (stringin[pinput] == "X" || stringin[pinput] == "O")
- {
- Console.WriteLine("Does not exist or something going wrong!");
- }
- else
- {
- Console.WriteLine("Next one!");
- }
- }
- catch
- {
- Console.WriteLine("Does not exist or something going wrong!");
- error = true;
- }
- } while(error || pinput > 9 || pinput < 1 || stringin[pinput] == "X" || stringin[pinput] == "O");
- stringin[pinput] = P1xP2o;
- Console.Clear();
- Grid(stringin);
- p1x = !p1x;
- wrong = WWON(stringin, P1xP2o);
- if (wrong) break;
- }
- bool P1xW = WWON(stringin, "X");
- int retour = 0;
- int x = 2;
- int testy = retour - x;
- bool P2oW = WWON(stringin, "O");
- if (P1xW)
- {
- Console.WriteLine("P1 or PX won!");
- System.Threading.Thread.Sleep(1000);
- Console.Clear();
- }
- else if (P2oW)
- {
- Console.WriteLine("P2 or PO won!");
- }
- else Console.WriteLine("Default!");
- Console.ReadKey();
- Menu();
- }
- private static void O()
- {
- Console.Clear();
- Console.WriteLine("TicTacToe\n\n\nP1 (X) starts: (write in 1/2/3/4/5/6/7/8/9)");
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" || || 1 || 2 || 3 ");
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" =================================== ===================================");
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" || || 4 || 5 || 6 ");
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" =================================== ===================================");
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" || || 7 || 8 || 9 ");
- Console.WriteLine(" || || || || ");
- Console.WriteLine();
- 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.");
- System.Threading.Thread.Sleep(3500);
- Console.Clear();
- Console.WriteLine("And Start (P1)!! Good Luck!");
- System.Threading.Thread.Sleep(1000);
- Console.Clear();
- string[] stringin = new string[10];
- bool p1x = true;
- bool wrong = false;
- int TRY = 1;
- for (int i = 0; i < stringin.Length; i++)
- {
- stringin[i] = (i + 1).ToString();
- }
- Grid(stringin);
- int pinput = 1;
- string P1xP2o;
- for (int i = 0; i < 9; i++)
- {
- int round = TRY++;
- Console.WriteLine("\n\nRound: " + round);
- P1xP2o = p1x ? "X" : "O";
- bool error = false;
- do
- {
- try
- {
- pinput = Int32.Parse(Console.ReadLine());
- error = false;
- if (stringin[pinput] == "X" || stringin[pinput] == "O")
- {
- Console.WriteLine("Does not exist or something going wrong!");
- }
- else
- {
- Console.WriteLine("Next one!");
- }
- }
- catch
- {
- Console.WriteLine("Does not exist or something going wrong!");
- error = true;
- }
- } while (error || pinput > 9 || pinput < 1 || stringin[pinput] == "X" || stringin[pinput] == "O");
- stringin[pinput] = P1xP2o;
- Console.Clear();
- Grid(stringin);
- p1x = !p1x;
- wrong = WWON(stringin, P1xP2o);
- if (wrong) break;
- }
- bool P1xW = WWON(stringin, "X");
- int retour = 0;
- int x = 2;
- int testy = retour - x;
- bool P2oW = WWON(stringin, "O");
- if (P1xW)
- {
- Console.WriteLine("P1 or X won!");
- }
- else if (P2oW)
- {
- Console.WriteLine("P2 or O won!");
- }
- else Console.WriteLine("Default!");
- Console.ReadKey();
- Menu();
- }
- private static void XO()
- {
- Console.Clear();
- Console.WriteLine("TicTacToe\n\nNew game! Do you want to be X or O:\n\n");
- string XO = Console.ReadLine();
- switch (XO)
- {
- case "X":
- Console.WriteLine("You choose X\n\n You're Player one. (P1)");
- System.Threading.Thread.Sleep(2000);
- Console.Clear();
- X();
- break;
- case "x":
- Console.WriteLine("You choose X\n\n You're Player one. (P1)");
- System.Threading.Thread.Sleep(2000);
- X();
- break;
- case "O":
- Console.WriteLine("You choose O\n\n You're Player two. (P2)");
- System.Threading.Thread.Sleep(2000);
- Console.Clear();
- O();
- break;
- case "o":
- Console.WriteLine("You choose O\n\n You're Player two. (P2)");
- System.Threading.Thread.Sleep(2000);
- Console.Clear();
- O();
- break;
- default:
- Console.WriteLine("There is a problem! Press m to come back to the menu:");
- string m = Console.ReadLine();
- if (m == "m")
- {
- Menu();
- }
- else
- {
- Console.WriteLine("Default");
- }
- break;
- }
- }
- static bool Ifdiagonal(string[] stringin, string searchforStringin, int begin, int xy)
- {
- bool playwon = true;
- for (int i = 0; i < 3; ++i)
- {
- int field = begin + (i * xy);
- if (stringin[field] != searchforStringin) playwon = false;
- }
- return playwon;
- }
- static bool WWON(string[] stringin, string searchforStringin)
- {
- bool playwon = false;
- //1. for horizontal
- //2. for perpendicular
- //3. for diagonal (1/2)
- for (int i = 0; i < 3; ++i)
- {
- playwon = true;
- int begin = 0 + 3 * i;
- for (int x = begin; x < begin + 3; x++)
- {
- if (stringin[x] != searchforStringin) playwon = false;
- }
- if (playwon) return true;
- }
- for (int i = 0; i < 3; ++i)
- {
- playwon = true;
- for (int x = i; x < i + 7; x = x + 3)
- {
- if (stringin[x] != searchforStringin) playwon = false;
- }
- if (playwon) return true;
- }
- if (Ifdiagonal(stringin, searchforStringin, 0, 4)) return true;
- if (Ifdiagonal(stringin, searchforStringin, 2, 2)) return true;
- return playwon;
- }
- static void Grid(string[] stringin)
- {
- Console.WriteLine(" || || ");
- Console.WriteLine(" {0} || {1} || {2} ", stringin[1], stringin[2], stringin[3]);
- Console.WriteLine(" || || ");
- Console.WriteLine(" =================================== ");
- Console.WriteLine(" || || ");
- Console.WriteLine(" {0} || {1} || {2} ", stringin[4], stringin[5], stringin[6]);
- Console.WriteLine(" || || ");
- Console.WriteLine(" =================================== ");
- Console.WriteLine(" || || ");
- Console.WriteLine(" {0} || {1} || {2} ", stringin[7], stringin[8], stringin[9]);
- Console.WriteLine(" || || ");
- //koennten SIe mir helfen die 1/2/3/4/5/6/7/8/9 im Raster durch leerzeichen zu erstzen
- //
- //can enyone help me to replace the {0}/{1}/... to an space, so there is a cleared grid? Thx for help
- }
- //AI
- //this is an small randomized AI, that means that the computer plays luckly.
- private static void AI()
- {
- Console.Clear();
- Console.WriteLine("TicTacToe\n\n\nYou are P1 (X) starts: (write in 1/2/3/4/5/6/7/8/9)");
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" || || 1 || 2 || 3 ");
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" =================================== ===================================");
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" || || 4 || 5 || 6 ");
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" =================================== ===================================");
- Console.WriteLine(" || || || || ");
- Console.WriteLine(" || || 7 || 8 || 9 ");
- Console.WriteLine(" || || || || ");
- Console.WriteLine();
- System.Threading.Thread.Sleep(2000);
- Console.Clear();
- Console.WriteLine("And Start (P1/PX)!! Good Luck!");
- System.Threading.Thread.Sleep(1000);
- Console.Clear();
- int PorAI = 2;
- int input = 0;
- int checkup = 0;
- int round = 0;
- char[,] array =
- {
- {'1','2','3'},{'4','5','6'},{'7','8','9'}
- };
- do
- {
- if (PorAI == 2)
- PorAI = 1;
- else if (PorAI == 1)
- PorAI = 2;
- switch (PorAI)
- {
- case 1:
- {
- switch (input)
- {
- case 1: array[0, 0] = 'X'; break;
- case 2: array[0, 1] = 'X'; break;
- case 3: array[0, 2] = 'X'; break;
- case 4: array[1, 0] = 'X'; break;
- case 5: array[1, 1] = 'X'; break;
- case 6: array[1, 2] = 'X'; break;
- case 7: array[2, 0] = 'X'; break;
- case 8: array[2, 1] = 'X'; break;
- case 9: array[2, 2] = 'X'; break;
- }
- break;
- }
- case 2:
- {
- switch (input)
- {
- case 1: array[0, 0] = 'O'; break;
- case 2: array[0, 1] = 'O'; break;
- case 3: array[0, 2] = 'O'; break;
- case 4: array[1, 0] = 'O'; break;
- case 5: array[1, 1] = 'O'; break;
- case 6: array[1, 2] = 'O'; break;
- case 7: array[2, 0] = 'O'; break;
- case 8: array[2, 1] = 'O'; break;
- case 9: array[2, 2] = 'O'; break;
- }
- break;
- }
- }
- System.Threading.Thread.Sleep(2000);
- Console.Clear();
- Console.WriteLine("");
- Console.WriteLine("");
- Console.WriteLine(" || || ");
- Console.WriteLine(" {0} || {1} || {2} ", array[0, 0], array[0, 1], array[0, 2]);
- Console.WriteLine(" || || ");
- Console.WriteLine(" =================================== ");
- Console.WriteLine(" || || ");
- Console.WriteLine(" {0} || {1} || {2} ", array[1, 0], array[1, 1], array[1, 2]);
- Console.WriteLine(" || || ");
- Console.WriteLine(" =================================== ");
- Console.WriteLine(" || || ");
- Console.WriteLine(" {0} || {1} || {2} ", array[2, 0], array[2, 1], array[2, 2]);
- Console.WriteLine(" || || ");
- round++;
- Console.WriteLine("Try: " + round);
- if
- (
- ((array[0, 0] == 'O') && (array[0, 1] == 'O') && (array[0, 2] == 'O')) ||
- ((array[1, 0] == 'O') && (array[1, 1] == 'O') && (array[1, 2] == 'O')) ||
- ((array[2, 0] == 'O') && (array[2, 1] == 'O') && (array[2, 2] == 'O')) ||
- ((array[0, 0] == 'O') && (array[1, 0] == 'O') && (array[2, 0] == 'O')) ||
- ((array[0, 1] == 'O') && (array[1, 1] == 'O') && (array[2, 1] == 'O')) ||
- ((array[0, 2] == 'O') && (array[1, 2] == 'O') && (array[2, 2] == 'O')) ||
- ((array[0, 0] == 'O') && (array[1, 1] == 'O') && (array[2, 2] == 'O')) ||
- ((array[0, 2] == 'O') && (array[1, 1] == 'O') && (array[2, 0] == 'O'))
- )
- {
- Console.WriteLine("\nAIo won!");
- Console.ReadLine();
- break;
- }
- else if
- (
- ((array[0, 0] == 'X') && (array[0, 1] == 'X') && (array[0, 2] == 'X')) ||
- ((array[1, 0] == 'X') && (array[1, 1] == 'X') && (array[1, 2] == 'X')) ||
- ((array[2, 0] == 'X') && (array[2, 1] == 'X') && (array[2, 2] == 'X')) ||
- ((array[0, 0] == 'X') && (array[1, 0] == 'X') && (array[2, 0] == 'X')) ||
- ((array[0, 1] == 'X') && (array[1, 1] == 'X') && (array[2, 1] == 'X')) ||
- ((array[0, 2] == 'X') && (array[1, 2] == 'X') && (array[2, 2] == 'X')) ||
- ((array[0, 0] == 'X') && (array[1, 1] == 'X') && (array[2, 2] == 'X')) ||
- ((array[0, 2] == 'X') && (array[1, 1] == 'X') && (array[2, 0] == 'X'))
- )
- {
- Console.WriteLine("\nPx won!");
- Console.ReadLine();
- break;
- }
- else if (round == 10)
- {
- Console.WriteLine("\nDefault!");
- Console.ReadLine();
- break;
- }
- do
- {
- Console.Write("\nPlayer {0}: Choose Array 1/2/3/4/5/6/7/8/9! ", PorAI);
- if (PorAI == 1)
- {
- Console.WriteLine("It's AI's turn...");
- int min = 1;
- int max = 10;
- Random random = new Random();
- int number = random.Next(min, max);
- input = Convert.ToInt32(number); //das erste mal wo ich verstehe warum man statt Parse Convert nimmt.
- }
- else
- {
- input = Int32.Parse(Console.ReadLine());
- }
- if ((input == 1) && (array[0, 0] == '1'))
- checkup = 0;
- else if ((input == 2) && (array[0, 1] == '2'))
- checkup = 0;
- else if ((input == 3) && (array[0, 2] == '3'))
- checkup = 0;
- else if ((input == 4) && (array[1, 0] == '4'))
- checkup = 0;
- else if ((input == 5) && (array[1, 1] == '5'))
- checkup = 0;
- else if ((input == 6) && (array[1, 2] == '6'))
- checkup = 0;
- else if ((input == 7) && (array[2, 0] == '7'))
- checkup = 0;
- else if ((input == 8) && (array[2, 1] == '8'))
- checkup = 0;
- else if ((input == 9) && (array[2, 2] == '9'))
- checkup = 0;
- else
- {
- Console.WriteLine("\nDoes not exist or something going wrong! ");
- checkup = 1;
- }
- } while (checkup == 1);
- } while (true);
- }
- }
- }
Add Comment
Please, Sign In to add comment