Guest User

Untitled

a guest
Apr 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.image.*;
  3. import java.net.*;
  4. import java.applet.*;
  5.  
  6.  
  7. public
  8. class TicTacToe extends Applet {
  9. /**
  10. * White's current position. The computer is white.
  11. */
  12. int white;
  13.  
  14. /**
  15. * Black's current position. The user is black.
  16. */
  17. int black;
  18.  
  19. /**
  20. * The squares in order of importance...
  21. */
  22. final static int moves[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
  23.  
  24. /**
  25. * The winning positions.
  26. */
  27. static boolean won[] = new boolean[1 << 9];
  28. static final int DONE = (1 << 9) - 1;
  29. static final int OK = 0;
  30. static final int WIN = 1;
  31. static final int LOSE = 2;
  32. static final int STALEMATE = 3;
  33.  
  34. /**
  35. * Mark all positions with these bits set as winning.
  36. */
  37. static void isWon(int pos) {
  38. for (int i = 0 ; i < DONE ; i++) {
  39. if ((i & pos) == pos) {
  40. won[i] = true;
  41. }
  42. }
  43. }
  44. /**
  45. * Initialize all winning positions.
  46. */
  47. static {
  48. isWon((1 << 0) | (1 << 1) | (1 << 2));
  49. isWon((1 << 3) | (1 << 4) | (1 << 5));
  50. isWon((1 << 6) | (1 << 7) | (1 << 8));
  51. isWon((1 << 0) | (1 << 3) | (1 << 6));
  52. isWon((1 << 1) | (1 << 4) | (1 << 7));
  53. isWon((1 << 2) | (1 << 5) | (1 << 8));
  54. isWon((1 << 0) | (1 << 4) | (1 << 8));
  55. isWon((1 << 2) | (1 << 4) | (1 << 6));
  56. }
  57.  
  58. /**
  59. * Compute the best move for white.
  60. * @return the square to take
  61. */
  62. int bestMove(int white, int black) {
  63. int bestmove = -1;
  64. loop:
  65. for (int i = 0 ; i < 9 ; i++) {
  66. int mw = moves[i];
  67. if (((white & (1 << mw)) == 0) && ((black & (1 << mw)) == 0)) {
  68. int pw = white | (1 << mw);
  69. if (won[pw]) {
  70. // white wins, take it!
  71. return mw;
  72. }
  73. for (int mb = 0 ; mb < 9 ; mb++) {
  74. if (((pw & (1 << mb)) == 0) && ((black & (1 << mb)) == 0)) {
  75. int pb = black | (1 << mb);
  76. if (won[pb]) {
  77. // black wins, take another
  78. continue loop;
  79. }
  80. }
  81. }
  82. // Neither white nor black can win in one move, this will do.
  83. if (bestmove == -1) {
  84. bestmove = mw;
  85. }
  86. }
  87. }
  88. if (bestmove != -1) {
  89. return bestmove;
  90. }
  91.  
  92. // No move is totally satisfactory, try the first one that is open
  93. for (int i = 0 ; i < 9 ; i++) {
  94. int mw = moves[i];
  95. if (((white & (1 << mw)) == 0) && ((black & (1 << mw)) == 0)) {
  96. return mw;
  97. }
  98. }
  99.  
  100. // No more moves
  101. return -1;
  102. }
  103.  
  104. /**
  105. * User move.
  106. * @return true if legal
  107. */
  108. boolean yourMove(int m) {
  109. if ((m < 0) || (m > 8)) {
  110. return false;
  111. }
  112. if (((black | white) & (1 << m)) != 0) {
  113. return false;
  114. }
  115. black |= 1 << m;
  116. return true;
  117. }
  118. /**
  119. * Computer move.
  120. * @return true if legal
  121. */
  122. boolean myMove() {
  123. if ((black | white) == DONE) {
  124. return false;
  125. }
  126. int best = bestMove(white, black);
  127. white |= 1 << best;
  128. return true;
  129. }
  130.  
  131. /**
  132. * Figure what the status of the game is.
  133. */
  134. int status() {
  135. if (won[white]) {
  136. return WIN;
  137. }
  138. if (won[black]) {
  139. return LOSE;
  140. }
  141. if ((black | white) == DONE) {
  142. return STALEMATE;
  143. }
  144. return OK;
  145. }
  146.  
  147. /**
  148. * Who goes first in the next game?
  149. */
  150. boolean first = true;
  151.  
  152. /**
  153. * The image for white.
  154. */
  155. Image notImage;
  156.  
  157. /**
  158. * The image for black.
  159. */
  160. Image crossImage;
  161.  
  162. /**
  163. * Initialize the applet. Resize and load images.
  164. */
  165. public void init() {
  166. notImage = getImage(getCodeBase(), "images/not.gif");
  167. crossImage = getImage(getCodeBase(), "images/cross.gif");
  168. }
  169.  
  170. /**
  171. * Paint it.
  172. */
  173. public void paint(Graphics g) {
  174. Dimension d = size();
  175. g.setColor(Color.black);
  176. int xoff = d.width / 3;
  177. int yoff = d.height / 3;
  178. g.drawLine(xoff, 0, xoff, d.height);
  179. g.drawLine(2*xoff, 0, 2*xoff, d.height);
  180. g.drawLine(0, yoff, d.width, yoff);
  181. g.drawLine(0, 2*yoff, d.width, 2*yoff);
  182.  
  183. int i = 0;
  184. for (int r = 0 ; r < 3 ; r++) {
  185. for (int c = 0 ; c < 3 ; c++, i++) {
  186. if ((white & (1 << i)) != 0) {
  187. g.drawImage(notImage, c*xoff + 1, r*yoff + 1, this);
  188. } else if ((black & (1 << i)) != 0) {
  189. g.drawImage(crossImage, c*xoff + 1, r*yoff + 1, this);
  190. }
  191. }
  192. }
  193. }
  194.  
  195. /**
  196. * The user has clicked in the applet. Figure out where
  197. * and see if a legal move is possible. If it is a legal
  198. * move, respond with a legal move (if possible).
  199. */
  200. public boolean mouseUp(Event evt, int x, int y) {
  201. switch (status()) {
  202. case WIN:
  203. case LOSE:
  204. case STALEMATE:
  205. play(getCodeBase(), "audio/return.au");
  206. white = black = 0;
  207. if (first) {
  208. white |= 1 << (int)(Math.random() * 9);
  209. }
  210. first = !first;
  211. repaint();
  212. return true;
  213. }
  214.  
  215. // Figure out the row/colum
  216. Dimension d = size();
  217. int c = (x * 3) / d.width;
  218. int r = (y * 3) / d.height;
  219. if (yourMove(c + r * 3)) {
  220. repaint();
  221.  
  222. switch (status()) {
  223. case WIN:
  224. play(getCodeBase(), "audio/joy.au");
  225. break;
  226. case LOSE:
  227. play(getCodeBase(), "audio/joy.au");
  228. break;
  229. case STALEMATE:
  230. break;
  231. default:
  232. if (myMove()) {
  233. repaint();
  234. switch (status()) {
  235. case WIN:
  236. play(getCodeBase(), "audio/joy.au");
  237. break;
  238. case LOSE:
  239. play(getCodeBase(), "audio/joy.au");
  240. break;
  241. case STALEMATE:
  242. break;
  243. default:
  244. play(getCodeBase(), "audio/ding.au");
  245. }
  246. } else {
  247. play(getCodeBase(), "audio/beep.au");
  248. }
  249. }
  250. } else {
  251. play(getCodeBase(), "audio/beep.au");
  252. }
  253. return true;
  254. }
  255.  
  256. public String getAppletInfo() {
  257. return "TicTacToe by Arthur van Hoff";
  258. }
  259. }
Add Comment
Please, Sign In to add comment