Continous Integration in der Praxis Gruppenarbeit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace MiniGames.Shared.Models
  7. {
  8. public class TicTacToeBrett
  9. {
  10. public const int LEER = -1;
  11. int[,] Felder;
  12. public TicTacToeBrett()
  13. {
  14. Felder = new[,]
  15. {
  16. { LEER, LEER, LEER },
  17. { LEER, LEER, LEER },
  18. { LEER, LEER, LEER },
  19. };
  20. }
  21. public TicTacToeBrett(int[,] felder)
  22. {
  23. Felder = felder;
  24. }
  25. public bool Gleich(TicTacToeBrett anderes)
  26. {
  27. for (int i = 0; i < 3; i++)
  28. {
  29. for (int j = 0; j < 3; j++)
  30. {
  31. if (Felder[i, j] != anderes.Felder[i, j])
  32. {
  33. return false;
  34. }
  35. }
  36. }
  37. return true;
  38. }
  39. public bool Set(int pos, int wert)
  40. {
  41. int x = pos / 3;
  42. int y = pos % 3;
  43. try
  44. {
  45. if (Felder[x, y] == LEER)
  46. {
  47. Felder[x, y] = wert;
  48. return true;
  49. }
  50. }
  51. catch (IndexOutOfRangeException e)
  52. {
  53. // absichtlich leer
  54. }
  55. return false;
  56. }
  57. public bool Voll()
  58. {
  59. for (int i = 0; i < 3; i++)
  60. {
  61. for (int j = 0; j < 3; j++)
  62. {
  63. if (Felder[i, j] == LEER)
  64. {
  65. return false;
  66. }
  67. }
  68. }
  69. return true;
  70. }
  71. protected bool dreiGleichGefuellt(int a, int b, int c)
  72. {
  73. return a != LEER && a == b && b == c;
  74. }
  75. public int Gewinner()
  76. {
  77. for (int i = 0; i < 3; i++)
  78. {
  79. if (dreiGleichGefuellt(Felder[i, 0], Felder[i, 1], Felder[i, 2])) return Felder[i, 0];
  80. if (dreiGleichGefuellt(Felder[0, i], Felder[1, i], Felder[2, i])) return Felder[0, i];
  81. }
  82. if (
  83. dreiGleichGefuellt(Felder[0, 0], Felder[1, 1], Felder[2, 2]) ||
  84. dreiGleichGefuellt(Felder[2, 0], Felder[1, 1], Felder[0, 2])
  85. )
  86. {
  87. return Felder[1, 1];
  88. }
  89. return LEER;
  90. }
  91. }
  92. }