Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. public class Board {
  2. private final Player[][] fields;
  3. ...
  4. public Player getWinner() {
  5. Player winner = null;
  6. boolean isWon = false;
  7.  
  8. // check columns (same x)
  9. for (int x = 0; x < fields.length; x++) {
  10. Player value = fields[x][0];
  11.  
  12. if (value == null) {
  13. continue;
  14. }
  15. for (int y = 1; y < fields[x].length; y++) {
  16. Player current = fields[x][y];
  17. if (current == null || !current.equals(value)) {
  18. break;
  19. }
  20. if (y == fields[x].length -1) {
  21. isWon = true;
  22. winner = value;
  23. }
  24. }
  25. if(isWon) {
  26. break;
  27. }
  28. }
  29.  
  30. if (! isWon) {
  31. // check rows (same y)
  32.  
  33. for (int y = 0; y < fields[0].length; y++) {
  34. Player value = fields[0][y];
  35. if (value == null) {
  36. continue;
  37. }
  38. for (int x = 1; x < fields.length; x++) {
  39. Player current = fields[x][y];
  40. if (current == null || !current.equals(value)) {
  41. break;
  42. }
  43. if (x == fields.length -1) {
  44. isWon = true;
  45. winner = value;
  46. }
  47. }
  48. if(isWon) {
  49. break;
  50. }
  51. }
  52.  
  53. }
  54. if (! isWon) {
  55. // check diagonal (bottom left to top right
  56.  
  57. Player value = fields[0][0];
  58. if (value != null) {
  59. for (int i = 1; i < fields.length; i++) {
  60. if (fields[i][i] != value) {
  61. break;
  62. }
  63. if (i == fields.length -1) {
  64. isWon = true;
  65. winner = value;
  66. }
  67. }
  68. }
  69. }
  70.  
  71. if (! isWon) {
  72. // check anti-diagonal (top left to bottom right)
  73. int length = fields.length;
  74. Player value = fields[0][length-1];
  75. if (value != null) {
  76. for (int i = 1; i < length; i++) {
  77. if (fields[i][length-i-1] != value) {
  78. break;
  79. }
  80. if (i == length -1) {
  81. isWon = true;
  82. winner = value;
  83. }
  84. }
  85. }
  86. }
  87. return winner;
  88. }
  89. ...
  90. }
  91.  
  92. public enum Player {
  93. X("X"),
  94. O("O");
  95.  
  96. private String str;
  97. private Player(String str) {
  98. this.str = str;
  99. }
  100.  
  101. @Override public String toString() {
  102. return str;
  103. }
  104. }
  105.  
  106. public enum Player {
  107. X, O;
  108. }
  109.  
  110. List<Player> players = new ArrayList<>();
  111. for (int x = 0; x < fields.length; x++) {
  112. for (int y = 0; y < fields[x].length; y++) {
  113. players.add(fields[x][y]);
  114. }
  115. Player winner = findWinner(players);
  116. if (winner != null)
  117. return winner;
  118. }
  119.  
  120. Player findWinner(List<Player> players) {
  121. Player win = players.get(0);
  122. for (Player pl : players) {
  123. // it's fine that we loop through index 0 again,
  124. // even though we've already processed it.
  125. // It's a fast operation and it won't change the result
  126. if (pl != win)
  127. return null;
  128. }
  129. return pl;
  130. }
  131.  
  132. public Player getWinner() {
  133.  
  134. // check columns (same x)
  135. for (int x = 0; x < fields.length; x++) {
  136. Player value = fields[x][0];
  137.  
  138. if (value == null) {
  139. continue;
  140. }
  141. for (int y = 1; y < fields[x].length; y++) {
  142. Player current = fields[x][y];
  143. if (current == null || !current.equals(value)) {
  144. break;
  145. }
  146. if (y == fields[x].length -1) {
  147. // Early Return.... We have a WINNER!
  148. return value;
  149. }
  150. }
  151. }
  152. // there was no winner in this check
  153. // no need for isWon check or variable... we can't get here unless there is
  154. // no winner yet....
  155. for (int y = 0; y < fields[0].length; y++) {
  156. Player value = fields[0][y];
  157. if (value == null) {
  158. continue;
  159. }
  160. for (int x = 1; x < fields.length; x++) {
  161. Player current = fields[x][y];
  162. if (current == null || !current.equals(value)) {
  163. break;
  164. }
  165. if (x == fields.length -1) {
  166. // We have a winner!
  167. return value;
  168. }
  169. }
  170. }
  171.  
  172. }
  173. // and so on....
  174. ....
  175.  
  176. // there is no winner.
  177. return null;
  178. }
  179.  
  180. void Main()
  181. {
  182. int[][] verticalWin = { new int[] {0,1}, new int[] {0,2} };
  183. int[][] horizontalWin = { new int[] {1,0}, new int[] {2,0} };
  184.  
  185. int[][] playfield = { new int[] { 20,10,10 },
  186. new int[] { 20,10,20 },
  187. new int[] { 10,10,10}};
  188.  
  189. var winner = GetWinner(playfield, verticalWin);
  190. Console.WriteLine(winner);
  191.  
  192. winner = GetWinner(playfield, horizontalWin);
  193. Console.WriteLine(winner);
  194. }
  195.  
  196. // Define other methods and classes here
  197.  
  198. int GetWinner(int[][] playfield, int[][] kernel){
  199. for (int x=0; x<playfield[0].Length; x++){
  200. for (int y=0; y<playfield[0].Length; y++){
  201. var player = playfield[x][y];
  202. int k=0;
  203. while (k<kernel[0].Length
  204. && playfield.Length > (kernel[k][0] + x)
  205. && playfield.Length > (kernel[k][1] + y)
  206. && player == playfield[x+kernel[k][0]][y +kernel[k][1]]
  207. ) {
  208. k++;
  209. }
  210. if (k==kernel.Length){
  211. return player;
  212. }
  213. }
  214. }
  215. return 0;
  216. }
  217.  
  218. int winner(int player, int pos) {
  219. int v = 0, h = 0, s0 = 0, s1 = 0;
  220. int col = pos % 3;
  221. int row = pos / 3;
  222. for (int i = 0; i < 3; i++) {
  223. if (frame[i][col] == player) {
  224. v++;
  225. }
  226. if (frame[row][i] == player) {
  227. h++;
  228. }
  229. }
  230. if (pos % 4 == 0) {
  231. for (int i = 0; i < 3; i++) {
  232. if (frame[i][i] == player) {
  233. s0++;
  234. }
  235. }
  236. }
  237. if ((pos + 2) % 4 == 0) {
  238. for (int i = 0; i < 3; i++) {
  239. if (frame[2 - i][i] == player) {
  240. s1++;
  241. }
  242. }
  243. }
  244.  
  245. if (v == 3 || h == 3 || s0 == 3 || s1 == 3) {
  246. return player;
  247. }
  248. return -1;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement