Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class TTTSolver
- {
- public static int[] TurnMethod(int[][] board, int player)
- {
- var lines = new List<Line> {
- new Line(board, player, 0, 0, 1, 0, 2, 0),
- new Line(board, player, 0, 1, 1, 1, 2, 1),
- new Line(board, player, 0, 2, 1, 2, 2, 2),
- new Line(board, player, 0, 0, 0, 1, 0, 2),
- new Line(board, player, 1, 0, 1, 1, 1, 2),
- new Line(board, player, 2, 0, 2, 1, 2, 2),
- new Line(board, player, 0, 0, 1, 1, 2, 2),
- new Line(board, player, 2, 0, 1, 1, 0, 2)
- }.OrderBy(x => x.GetScore()).ToList();
- return lines.Last().GetValidPos().ToArray();
- }
- private class Line
- {
- private Dictionary<Pos, int> Cells;
- private readonly int PlayerId;
- private readonly int EnemyId;
- private readonly int EmptyId;
- public Line(int[][] board, int playerId, Pos pos1, Pos pos2, Pos pos3)
- {
- PlayerId = playerId;
- EnemyId = playerId == 1 ? 2 : 1;
- EmptyId = 0;
- Cells = new Dictionary<Pos, int> {
- { pos1, board[pos1.X][pos1.Y] },
- { pos2, board[pos2.X][pos2.Y] },
- { pos3, board[pos3.X][pos3.Y] }
- };
- }
- public Line(int[][] board, int playerId, int x1, int y1, int x2, int y2, int x3, int y3)
- : this(board, playerId, new Pos(x1, y1), new Pos(x2, y2), new Pos(x3, y3))
- {
- }
- public int GetScore()
- {
- int emptyCells = Cells.Count(x => x.Value == EmptyId);
- int playerCells = Cells.Count(x => x.Value == PlayerId);
- int enemyCells = Cells.Count(x => x.Value == EnemyId);
- switch(emptyCells)
- {
- case 0:
- return 0;
- case 1:
- if(playerCells == 2)
- return 7;
- else if(enemyCells == 2)
- return 6;
- else if(enemyCells == 1 && playerCells == 1)
- return 1;
- else
- break;
- case 2:
- if(playerCells == 1)
- return 5;
- else if(enemyCells == 1)
- return 2;
- else
- break;
- case 3:
- if(Cells.ContainsKey(new Pos(1, 1)))
- return 4;
- else
- return 3;
- }
- throw new Exception("Invalid line");
- }
- public Pos GetValidPos()
- {
- var pairs = Cells.ToList();
- if(pairs[0].Value == PlayerId && pairs[1].Value == EmptyId && pairs[2].Value == EmptyId)
- return pairs[2].Key;
- if(pairs[2].Value == PlayerId && pairs[1].Value == EmptyId && pairs[0].Value == EmptyId)
- return pairs[0].Key;
- if(Cells.ContainsKey(new Pos(1, 1)) && Cells[new Pos(1, 1)] == EmptyId)
- return new Pos(1, 1);
- foreach(var pair in Cells)
- if(pair.Value == EmptyId)
- return pair.Key;
- throw new Exception("Valid position not found");
- }
- }
- private class Pos
- {
- public int X, Y;
- public Pos(int x, int y)
- {
- X = x;
- Y = y;
- }
- public int[] ToArray()
- {
- return new int[] { X, Y };
- }
- public override int GetHashCode()
- {
- return X ^ Y;
- }
- public override bool Equals(object other)
- {
- return other is Pos ? Equals((Pos)other) : false;
- }
- public bool Equals(Pos other)
- {
- return X == other.X && Y == other.Y;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement