Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class Program
- {
- class Person
- {
- public int age = 20;
- }
- public static char convert(int[] cells, int i)
- {
- if (cells[i] == -1)
- return ' ';
- else if (cells[i] == 0)
- return '0';
- else
- return 'x';
- }
- public static void drawboard(int[] cells)
- {
- Console.WriteLine($" | | ");
- Console.WriteLine(" {0} | {1} | {2} ", convert(cells, 0), convert(cells, 1), convert(cells, 2));
- Console.WriteLine($" ______|____|______ ");
- Console.WriteLine($" | | ");
- Console.WriteLine(" {0} | {1} | {2} ", convert(cells, 3), convert(cells, 4), convert(cells, 5));
- Console.WriteLine($" ______|____|______ ");
- Console.WriteLine($" | | ");
- Console.WriteLine(" {0} | {1} | {2} ", convert(cells, 6), convert(cells, 7), convert(cells, 8));
- Console.WriteLine($" | | ");
- }
- public static bool isCellEmpty(int index, int[] array)
- {
- return array[index] == -1;
- }
- public enum winstate
- {
- xwin,
- owin,
- tie,
- none,
- }
- public static bool iswin(bool turn, int[] cells, int a, int b, int c)
- {
- int choice = turn ? 0 : 1;
- return cells[a] == choice && cells[b] == choice && cells[c] == choice;
- }
- public static winstate checkwin(bool turn, int []cells)
- {
- winstate state = winstate.none;
- if(turn == true)
- {
- state = winstate.owin;
- }
- else
- {
- state = winstate.xwin;
- }
- if (iswin(turn, cells, 0,1,2))
- {
- return state;
- }
- else if(iswin(turn, cells, 3, 4, 5))
- {
- return state;
- }
- else if (iswin(turn, cells , 6, 7 ,8))
- {
- return state;
- }
- else if (iswin(turn,cells,0,3,6))
- {
- return state;
- }
- else if (iswin(turn,cells,1,4,7))
- {
- return state;
- }
- else if (iswin( turn,cells,2,5,8))
- {
- return state;
- }
- else if (iswin( turn ,cells, 0,4,8))
- {
- return state;
- }
- else if (iswin(turn, cells, 2,4,6))
- {
- return state;
- }
- return winstate.none;
- }
- public static void Main()
- {
- bool turn = false;
- int[] cells = new int[] { -1, -1, -1, -1, -1, -1, -1, -1, -1 };
- while (true)
- {
- Console.Clear();
- drawboard(cells);
- string a = Console.ReadLine();
- int num = int.Parse(a);
- if (isCellEmpty (num, cells))
- {
- if (turn)
- {
- cells[num] = 0;
- turn = false;
- }
- else
- {
- cells[num] = 1;
- turn = true;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement