fork(1) download
  1. import java.lang.*;
  2.  
  3. class Reversi
  4. {
  5. static class Mass
  6. {
  7. private final String str;
  8. private Mass(String str) { this.str = str; }
  9. @Override
  10. public String toString() { return str; }
  11.  
  12. public static final Mass NONE = new Mass("-");
  13. public static final Mass BLACK = new Mass("X");
  14. public static final Mass WHITE = new Mass("O");
  15. public static Mass reverse(Mass m)
  16. {
  17. if (m == BLACK) return WHITE;
  18. if (m == WHITE) return BLACK;
  19. return m;
  20. }
  21. }
  22.  
  23. static final int BOARD_WIDTH = 8;
  24. static final int BOARD_HEIGHT = 8;
  25.  
  26. static Mass turn;
  27. static final Mass[][] board = new Mass[BOARD_HEIGHT + 2][BOARD_WIDTH + 2];
  28. static void initBoard()
  29. {
  30. for (Mass[] line : board)
  31. {
  32. for (int i = 0; i < line.length; ++i)
  33. {
  34. line[i] = Mass.NONE;
  35. }
  36. }
  37. board[4][4] = board[5][5] = Mass.BLACK;
  38. board[4][5] = board[5][4] = Mass.WHITE;
  39. turn = Mass.BLACK;
  40. }
  41.  
  42. static void printBoard(boolean drawputable)
  43. {
  44. System.out.print(' ');
  45. for (int i = 0; i < BOARD_WIDTH; ++i)
  46. {
  47. System.out.print(' ');
  48. System.out.print((char)(i + 'A'));
  49. }
  50. System.out.println();
  51. for (int h = 1; h <= BOARD_HEIGHT; ++h)
  52. {
  53. System.out.print(h);
  54. for (int w = 1; w <= BOARD_WIDTH; ++w)
  55. {
  56. if (drawputable && (putable(h, w, turn)))
  57. {
  58. System.out.print(" *");
  59. }
  60. else
  61. {
  62. System.out.print(" " + board[h][w]);
  63. }
  64. }
  65. System.out.println();
  66. }
  67. }
  68.  
  69. // require: called by putable() or put()
  70. static boolean reverse
  71. (int x, int y, int dx, int dy, Mass m, boolean checkonly)
  72. {
  73. int tx = x + dx;
  74. int ty = y + dy;
  75. Mass rv = Mass.reverse(m);
  76. while (board[ty][tx] == rv)
  77. {
  78. tx += dx;
  79. ty += dy;
  80. }
  81. if (board[ty][tx] != m) return false;
  82. if ((tx == (x + dx)) && (ty == (y + dy))) return false;
  83. if (checkonly) return true;
  84. tx -= dx;
  85. ty -= dy;
  86. while (board[ty][tx] == rv)
  87. {
  88. board[ty][tx] = m;
  89. tx -= dx;
  90. ty -= dy;
  91. }
  92. return true;
  93. }
  94.  
  95. static final boolean CHECK_ONLY = true;
  96. static boolean putable(int x, int y, Mass m)
  97. {
  98. if ((x < 1) || (y < 1) || (x > BOARD_WIDTH) || (y > BOARD_HEIGHT))
  99. {
  100. return false;
  101. }
  102. if ((m != Mass.BLACK) && (m != Mass.WHITE)) return false;
  103. if (board[y][x] != Mass.NONE) return false;
  104. for (int dy = -1; dy <= 1; ++dy)
  105. {
  106. for (int dx = -1; dx <= 1; ++dx)
  107. {
  108. if (reverse(x, y, dx, dy, m, CHECK_ONLY))
  109. {
  110. return true;
  111. }
  112. }
  113. }
  114. return false;
  115. }
  116.  
  117. static final boolean REVERSE = false;
  118. static boolean put(int x, int y, Mass m)
  119. {
  120. if ((x < 1) || (y < 1) || (x > BOARD_WIDTH) || (y > BOARD_HEIGHT))
  121. {
  122. return false;
  123. }
  124. if ((m != Mass.BLACK) && (m != Mass.WHITE)) return false;
  125. if (board[y][x] != Mass.NONE) return false;
  126. board[y][x] = m;
  127. for (int dy = -1; dy <= 1; ++dy)
  128. {
  129. for (int dx = -1; dx <= 1; ++dx)
  130. {
  131. reverse(x, y, dx, dy, m, REVERSE);
  132. }
  133. }
  134. return true;
  135. }
  136.  
  137. static void countup()
  138. {
  139. int b = 0;
  140. int w = 0;
  141. for (Mass[] line : board)
  142. {
  143. for (Mass mass : line)
  144. {
  145. if (mass == Mass.WHITE) ++w;
  146. else if (mass == Mass.BLACK) ++b;
  147. }
  148. }
  149. System.out.printf("B%2d - W%2d ", b, w);
  150. if (b > w) System.out.println("Black won");
  151. else if (b < w) System.out.println("White won");
  152. else System.out.println("Drew");
  153. }
  154.  
  155. public static void main (String[] args) throws java.lang.Exception
  156. {
  157. initBoard();
  158. // printBoard(true);
  159. // System.out.println("-----------------");
  160.  
  161. Mass pass = null;
  162. boolean f = true;
  163. int n = 0;
  164. while (f && (n < 65))
  165. {
  166. if (pass == turn)
  167. {
  168. pass = null;
  169. }
  170. f = false;
  171. ++n;
  172. for (int y = 1; (!f) && (y <= BOARD_HEIGHT); ++y)
  173. {
  174. for (int x = 1; (!f) && (x <= BOARD_WIDTH); ++x)
  175. {
  176. if (putable(x, y, turn))
  177. {
  178. f = put(x, y, turn);
  179. turn = Mass.reverse(turn);
  180. }
  181. }
  182. }
  183. if (!f)
  184. {
  185. if (pass != null)
  186. {
  187. break;
  188. }
  189. else
  190. {
  191. pass = turn;
  192. turn = Mass.reverse(turn);
  193. }
  194. }
  195. }
  196. printBoard(false);
  197. System.out.println("-----------------");
  198. countup();
  199. }
  200. }
Success #stdin #stdout 0.07s 381248KB
stdin
Standard input is empty
stdout
  A B C D E F G H
1 X X X X X X X O
2 X X X X X X X O
3 X X X X X X X O
4 X O X O X X X O
5 X O X X O X X O
6 X X O X X O X O
7 X X X O O X O O
8 O O O O O O O O
-----------------
B40 - W24 Black won