Advertisement
Guest User

TittyToe

a guest
Jun 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1. using System.Threading;
  2. using System.Linq;
  3. using static System.Console;
  4.  
  5. class Program {
  6.     public static void Main() {
  7.         new Board("Niggy1", "Niggy2").Start();
  8.     }
  9. }
  10.  
  11. class Board {
  12.    
  13.     private class Block {
  14.         public string Mark;
  15.         public int Number { get; }
  16.         public bool IsOccupied => Mark != Number.ToString();
  17.         public Block(int number) {
  18.             Number = number;
  19.             Mark = number.ToString();
  20.         }
  21.         public override string ToString()
  22.             => Mark;
  23.     }
  24.     private class Player {
  25.         public string Mark { get; }
  26.         public string Name;
  27.         public Player(char mark)
  28.             => Mark = mark.ToString();
  29.     }
  30.  
  31.     const string Seperator = "+---+---+---+";
  32.     const string RowFormat = "| {0} | {1} | {2} |";
  33.    
  34.     Player Player1 { get; } = new Player('X');
  35.     Player Player2 { get; } = new Player('O');
  36.  
  37.     Block[] blocks = Enumerable.Range(1, 10).Select(i => new Block(i)).ToArray();
  38.     (int Index, int Step)[] winSeqs = { (0, 1), (3, 1), (6, 1), (0, 3), (1, 3), (2, 3), (0, 4), (6, -2) };
  39.     (int Index, int Step) winningSequence = default;
  40.  
  41.     public Board(string p1Name = "Player1", string p2Name = "Player2") {
  42.         Player1.Name = p1Name;
  43.         Player2.Name = p2Name;
  44.     }
  45.     public void Start() {
  46.         Player[] players = { Player1, Player2 };
  47.         int turn = 0;
  48.         while(true) {
  49.             if(turn == 9) {
  50.                 Clear();
  51.                 WriteLine("\x1B[31mNIGGERS!\x1B[0m");
  52.                 break;
  53.             }
  54.             Clear();
  55.             var p = players[turn++ % 2];
  56.             Display();
  57.             Write($"{ p.Name }'s turn: ");
  58.             if(! int.TryParse(ReadLine(), out int c) || ! Update(c - 1, p.Mark)) {
  59.                 WriteLine("Try again, nigger.");
  60.                 turn--;
  61.                 Thread.Sleep(1000);
  62.                 continue;
  63.             }
  64.             if(CheckWin()) {
  65.                 DisplayWinner(p);
  66.                 break;
  67.             }
  68.         }
  69.     }
  70.     public bool Update(int index, string mark) {
  71.         if(index < 0 || index > 8 || blocks[index].IsOccupied)
  72.             return false;
  73.         blocks[index].Mark = mark;
  74.         return true;
  75.     }
  76.     public void Display() {
  77.         DisplayRow(prevLine: Seperator, rowIndex: 0, nextLine: Seperator);
  78.         DisplayRow(1, nextLine: Seperator);
  79.         DisplayRow(2, nextLine: Seperator);
  80.     }
  81.     public bool CheckWin() {
  82.         winningSequence = winSeqs.SingleOrDefault(s => CheckSequence(s.Index, s.Step));
  83.         return winningSequence.Step != 0;
  84.     }
  85.  
  86.     bool CheckSequence(int index, int step) {
  87.         bool check(int i) => blocks[i].Mark == blocks[index].Mark;
  88.         return new[] { index, index + step, index + step + step}.All(check);
  89.     }
  90.     void DisplayRow(int rowIndex, string prevLine = "", string nextLine = "") {
  91.         int i = rowIndex * 3;
  92.         if(prevLine != "") WriteLine(prevLine);
  93.         WriteLine(string.Format(RowFormat, blocks[i++], blocks[i++], blocks[i]));
  94.         WriteLine(nextLine);
  95.     }
  96.     void DisplayWinner(Player p) {
  97.         Clear();
  98.         int i = winningSequence.Index, s = winningSequence.Step;
  99.         blocks[i].Mark = blocks[i+s].Mark = blocks[i+s+s].Mark = $"\x1B[32m{p.Mark}\x1B[0m";
  100.         Display();
  101.         WriteLine($"\x1B[32mA WINRAR IS {p.Name}!\x1B[0m");
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement