Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1.  
  2. public List<List<Tuple<int,int>>> Lines (int size)
  3. {
  4. List<List<Tuple<int, int>>> lines = new List<List<Tuple<int, int>>>();
  5. List<Tuple<int, int>> line = new List<Tuple<int, int>>();
  6.  
  7. //Do the columns
  8. for (int x = 0; x < size; x++)
  9. {
  10. for (int y = 0; y < size; y++)
  11. {
  12. line.Add(new Tuple<int, int>(x,y));
  13. }
  14. lines.Add(line);
  15. line = new List<Tuple<int, int>>();
  16. }
  17. //Do the rows
  18. for (int y = 0; y < size; y++)
  19. {
  20. for (int x = 0; x < size; x++)
  21. {
  22. line.Add(new Tuple<int, int>(x, y));
  23. }
  24. lines.Add(line);
  25. line = new List<Tuple<int, int>>();
  26. }
  27. //Do the left diagonal
  28. for (int x = 0; x < size; x++)
  29. {
  30. line.Add(new Tuple<int, int>(x, x));
  31. }
  32. lines.Add(line);
  33. line = new List<Tuple<int, int>>();
  34.  
  35. //Do the right diagonal
  36. for (int x = 0; x < size; x++)
  37. {
  38. line.Add(new Tuple<int, int>(x, size - 1 - (x % size)));
  39. }
  40. lines.Add(line);
  41. return lines;
  42. }
  43. public List<Player> LineAsPlayers(List<Tuple<int,int>> line, Game game)
  44. {
  45. List<Player> lineObj = new List<Player>();
  46. foreach (Tuple<int,int> coords in line)
  47. {
  48. lineObj.Add(game.GetLocation(coords.Item1, coords.Item2));
  49. }
  50. return lineObj;
  51. }
  52. public TicTacToeOutcome<Player> LineOutcome(List<Tuple<int, int>> line, Game game)
  53. {
  54. List<Player> listObj = LineAsPlayers(line, game);
  55.  
  56. if (listObj.Contains(Nought) && listObj.Contains(Cross))
  57. {
  58. return TicTacToeOutcome<Player>.Draw;
  59. }
  60. if (listObj.TrueForAll((obj) => obj == Nought))
  61. {
  62. return TicTacToeOutcome<Player>.NewWin(Nought, line.AsEnumerable());
  63. }
  64. if (listObj.TrueForAll((obj) => obj == Cross))
  65. {
  66. return TicTacToeOutcome<Player>.NewWin(Cross, line.AsEnumerable());
  67. }
  68. return TicTacToeOutcome<Player>.Undecided;
  69. }
  70. public TicTacToeOutcome<Player> GameOutcome(Game game)
  71. {
  72. List<TicTacToeOutcome<Player>> boardOutcomes = new List<TicTacToeOutcome<Player>>();
  73. foreach (var line in Lines(game.Size))
  74. {
  75. boardOutcomes.Add(LineOutcome(line, game));
  76. }
  77.  
  78. if (boardOutcomes.TrueForAll((obj) => obj == TicTacToeOutcome<Player>.Draw))
  79. {
  80. return TicTacToeOutcome<Player>.Draw;
  81. }
  82.  
  83. if (boardOutcomes.TrueForAll((obj) => obj == TicTacToeOutcome<Player>.Draw || obj == TicTacToeOutcome<Player>.Undecided))
  84. {
  85. return TicTacToeOutcome<Player>.Undecided;
  86. }
  87.  
  88. return boardOutcomes.Find((obj) => obj != TicTacToeOutcome<Player>.Draw && obj != TicTacToeOutcome<Player>.Undecided);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement