Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2.  
  3. public class Program
  4. {
  5.     class Person
  6.     {
  7.         public int age = 20;
  8.     }
  9.  
  10.     public static char convert(int[] cells, int i)
  11.     {
  12.         if (cells[i] == -1)
  13.             return ' ';
  14.         else if (cells[i] == 0)
  15.             return '0';
  16.         else
  17.             return 'x';
  18.     }
  19.  
  20.     public static void drawboard(int[] cells)
  21.     {
  22.         Console.WriteLine($"       |    |   ");
  23.         Console.WriteLine("  {0}    | {1} | {2}  ", convert(cells, 0), convert(cells, 1), convert(cells, 2));
  24.         Console.WriteLine($" ______|____|______    ");
  25.         Console.WriteLine($"       |    |            ");
  26.         Console.WriteLine("   {0}   | {1} | {2}    ", convert(cells, 3), convert(cells, 4), convert(cells, 5));
  27.         Console.WriteLine($" ______|____|______          ");
  28.         Console.WriteLine($"       |    |             ");
  29.         Console.WriteLine("   {0}   | {1} | {2}              ", convert(cells, 6), convert(cells, 7), convert(cells, 8));
  30.         Console.WriteLine($"       |    |                ");
  31.     }
  32.  
  33.     public static bool isCellEmpty(int index, int[] array)
  34.     {
  35.         return array[index] == -1;
  36.     }
  37.  
  38.     public enum winstate
  39.     {
  40.         xwin,
  41.         owin,
  42.         tie,
  43.         none,
  44.  
  45.     }
  46.  
  47.     public static bool iswin(bool turn, int[] cells, int a, int b, int c)
  48.     {
  49.         int choice = turn ? 0 : 1;
  50.         return cells[a] == choice && cells[b] == choice && cells[c] == choice;
  51.     }
  52.  
  53.  
  54.     public static winstate checkwin(bool turn, int []cells)
  55.     {
  56.         winstate state = winstate.none;
  57.  
  58.         if(turn == true)
  59.         {
  60.             state = winstate.owin;
  61.         }
  62.         else
  63.         {
  64.             state = winstate.xwin;
  65.         }
  66.  
  67.         if (iswin(turn, cells, 0,1,2))
  68.         {
  69.             return state;
  70.         }
  71.         else if(iswin(turn, cells, 3, 4, 5))
  72.         {
  73.             return state;
  74.         }
  75.         else if (iswin(turn, cells , 6, 7 ,8))
  76.         {
  77.             return state;
  78.         }
  79.         else if (iswin(turn,cells,0,3,6))
  80.         {
  81.             return state;
  82.         }
  83.         else if (iswin(turn,cells,1,4,7))
  84.         {
  85.             return state;
  86.         }
  87.         else if (iswin( turn,cells,2,5,8))
  88.         {
  89.             return state;
  90.  
  91.         }
  92.         else if (iswin( turn ,cells, 0,4,8))
  93.         {
  94.             return state;
  95.  
  96.         }
  97.         else if (iswin(turn, cells, 2,4,6))
  98.         {
  99.             return state;
  100.         }
  101.  
  102.         return winstate.none;
  103.     }
  104.  
  105.     public static void Main()
  106.     {
  107.         bool turn = false;
  108.         int[] cells = new int[] { -1, -1, -1, -1, -1, -1, -1, -1, -1 };
  109.         while (true)
  110.         {
  111.             Console.Clear();
  112.             drawboard(cells);
  113.             string a = Console.ReadLine();
  114.             int num = int.Parse(a);
  115.             if (isCellEmpty (num, cells))
  116.             {
  117.                 if (turn)
  118.                 {
  119.                     cells[num] = 0;
  120.                     turn = false;
  121.                 }
  122.                 else
  123.                 {
  124.                     cells[num] = 1;
  125.                     turn = true;
  126.                 }
  127.             }
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement