Advertisement
UnlimitedSupply

TicTacToe but in C#

Jun 19th, 2022
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. using System;
  2. class HelloWorld {
  3.   static void Main() {
  4.     string [,] lines = {
  5.         {"1", "2", "3"},
  6.         {"4", "5", "6"},
  7.         {"7", "8", "9"}
  8.     };
  9.     bool won = false, xWinner = false, oWinner = false, turn = false;
  10.     string action;
  11.     do {
  12.         //Prints out the Board
  13.         int f = 0;
  14.         int t = 0;
  15.         Console.Clear();
  16.         foreach (string x in lines) {
  17.             f++;
  18.             if (f == lines.GetLength(1)) {
  19.                 t++;
  20.                 Console.WriteLine(x);
  21.                 if (t != lines.GetLength(0)) {
  22.                     for (int y = 0; y < 3 * lines.GetLength(1); y++) {
  23.                         Console.Write("-");
  24.                     }
  25.                     Console.Write("\n");
  26.                 }
  27.                 f = 0;
  28.             } else {
  29.                 Console.Write(x + " | ");
  30.             }
  31.         };
  32.        
  33.         //Input
  34.         action = Console.ReadLine();
  35.         if (!(action == "X" || action == "x" || action == "O" || action == "o")) {
  36.             int pop = 0;
  37.             int bop = 0;
  38.             foreach (string xx in lines) {
  39.                 pop++;
  40.                 if (pop > lines.GetLength(1)) {
  41.                     pop = 0;
  42.                     bop++;
  43.                 }
  44.                 if (lines[bop, pop] == action) {
  45.                     if (turn) {
  46.                         lines[bop, pop] = "X";
  47.                     } else {
  48.                         lines[bop, pop] = "O";
  49.                     }
  50.                 }
  51.             }
  52.         };
  53.        
  54.         //Checks for win
  55.         for (int xxx = 0; xxx < lines.GetLength(0); xxx++) {
  56.             for (int yyy = 0; yyy < lines.GetLength(1); yyy++) {
  57.                 //Checks horizontally
  58.                 if (lines.GetLength(1) - yyy < 3) {
  59.                     if ((lines[xxx, yyy] == lines[xxx, yyy + 1] && lines[xxx, yyy + 1] == lines[xxx, yyy + 2]) || (lines[xxx, yyy + 3] == lines[xxx - 1, yyy + 2] && lines[xxx - 1, yyy + 2] == lines[xxx, yyy + 3])) {
  60.                         if (lines[xxx, yyy] == "X") {
  61.                             xWinner = true;
  62.                         } else {
  63.                             oWinner = true;
  64.                         }
  65.                         won = true;
  66.                     }
  67.                 }
  68.                
  69.                 //Checks Vertically
  70.                 if (lines.GetLength(0) - xxx < 3) {
  71.                     if (lines[xxx, yyy] == lines[xxx + 1, yyy] && lines[xxx + 1, yyy] == lines[xxx + 2, yyy]) {
  72.                         if (lines[xxx, yyy] == "X") {
  73.                             xWinner = true;
  74.                         } else {
  75.                             oWinner = true;
  76.                         }
  77.                         won = true;
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.     } while (!won);
  83.    
  84.     //Checks for no winner / draw / tie
  85.     if (xWinner || oWinner) {
  86.         if (xWinner) {
  87.             Console.Write("X has won!");
  88.         } else {
  89.             Console.Write("O has won!");
  90.         }
  91.     } else {
  92.         Console.Write("Nobody won!");
  93.     }
  94.     Console.ReadLine();
  95.   }
  96. }
  97.  
  98.  
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement