Advertisement
Guest User

Untitled

a guest
May 30th, 2017
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class TTTSolver
  6. {
  7.    public static int[] TurnMethod(int[][] board, int player)
  8.    {
  9.       var lines = new List<Line> {
  10.          new Line(board, player, 0, 0, 1, 0, 2, 0),
  11.          new Line(board, player, 0, 1, 1, 1, 2, 1),
  12.          new Line(board, player, 0, 2, 1, 2, 2, 2),
  13.          new Line(board, player, 0, 0, 0, 1, 0, 2),
  14.          new Line(board, player, 1, 0, 1, 1, 1, 2),
  15.          new Line(board, player, 2, 0, 2, 1, 2, 2),
  16.          new Line(board, player, 0, 0, 1, 1, 2, 2),
  17.          new Line(board, player, 2, 0, 1, 1, 0, 2)
  18.       }.OrderBy(x => x.GetScore()).ToList();
  19.  
  20.       return lines.Last().GetValidPos().ToArray();
  21.    }
  22.  
  23.    private class Line
  24.    {
  25.       private Dictionary<Pos, int> Cells;
  26.       private readonly int PlayerId;
  27.       private readonly int EnemyId;
  28.       private readonly int EmptyId;
  29.  
  30.       public Line(int[][] board, int playerId, Pos pos1, Pos pos2, Pos pos3)
  31.       {
  32.          PlayerId = playerId;
  33.          EnemyId = playerId == 1 ? 2 : 1;
  34.          EmptyId = 0;
  35.  
  36.          Cells = new Dictionary<Pos, int> {
  37.             { pos1, board[pos1.X][pos1.Y] },
  38.             { pos2, board[pos2.X][pos2.Y] },
  39.             { pos3, board[pos3.X][pos3.Y] }
  40.          };
  41.       }
  42.  
  43.       public Line(int[][] board, int playerId, int x1, int y1, int x2, int y2, int x3, int y3)
  44.          : this(board, playerId, new Pos(x1, y1), new Pos(x2, y2), new Pos(x3, y3))
  45.       {
  46.       }
  47.  
  48.       public int GetScore()
  49.       {
  50.          int emptyCells = Cells.Count(x => x.Value == EmptyId);
  51.          int playerCells = Cells.Count(x => x.Value == PlayerId);
  52.          int enemyCells = Cells.Count(x => x.Value == EnemyId);
  53.  
  54.          switch(emptyCells)
  55.          {
  56.             case 0:
  57.                return 0;
  58.             case 1:
  59.                if(playerCells == 2)
  60.                   return 7;
  61.                else if(enemyCells == 2)
  62.                   return 6;
  63.                else if(enemyCells == 1 && playerCells == 1)
  64.                   return 1;
  65.                else
  66.                   break;
  67.             case 2:
  68.                if(playerCells == 1)
  69.                   return 5;
  70.                else if(enemyCells == 1)
  71.                   return 2;
  72.                else
  73.                   break;
  74.             case 3:
  75.                if(Cells.ContainsKey(new Pos(1, 1)))
  76.                   return 4;
  77.                else
  78.                   return 3;
  79.          }
  80.  
  81.          throw new Exception("Invalid line");
  82.       }
  83.  
  84.       public Pos GetValidPos()
  85.       {
  86.          var pairs = Cells.ToList();
  87.          if(pairs[0].Value == PlayerId && pairs[1].Value == EmptyId && pairs[2].Value == EmptyId)
  88.             return pairs[2].Key;
  89.          if(pairs[2].Value == PlayerId && pairs[1].Value == EmptyId && pairs[0].Value == EmptyId)
  90.             return pairs[0].Key;
  91.  
  92.          if(Cells.ContainsKey(new Pos(1, 1)) && Cells[new Pos(1, 1)] == EmptyId)
  93.             return new Pos(1, 1);
  94.  
  95.          foreach(var pair in Cells)
  96.             if(pair.Value == EmptyId)
  97.                return pair.Key;
  98.          
  99.          throw new Exception("Valid position not found");
  100.       }
  101.    }
  102.  
  103.    private class Pos
  104.    {
  105.       public int X, Y;
  106.  
  107.       public Pos(int x, int y)
  108.       {
  109.          X = x;
  110.          Y = y;
  111.       }
  112.  
  113.       public int[] ToArray()
  114.       {
  115.          return new int[] { X, Y };
  116.       }
  117.  
  118.       public override int GetHashCode()
  119.       {
  120.          return X ^ Y;
  121.       }
  122.  
  123.       public override bool Equals(object other)
  124.       {
  125.          return other is Pos ? Equals((Pos)other) : false;
  126.       }
  127.  
  128.       public bool Equals(Pos other)
  129.       {
  130.          return X == other.X && Y == other.Y;
  131.       }
  132.    }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement