No older revisions available
No older revisions available
흐흐흐.
~cpp import java.awt.Graphics; import javax.swing.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class Serious extends JFrame { final int markX = 1, markO = 2; int x, y; boolean turn = true; boolean win = false; int block[][] = new int[][] { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} }; int blockDecider(int x, int y) { int retTable[][] = new int[][] { {0, 1, 2}, {3, 4, 5}, {6, 7, 8} }; int xline, yline; y -= 30; for (xline = 0; x > 0; xline++) x -= 100; for (yline = 0; y > 0; yline++) y -= 100; return retTable[--yline][--xline]; } int getX(int blockNum) { int ret; if (blockNum < 3) ret = 0; else if (blockNum < 6) ret = 1; else ret = 2; return ret; } int getY(int blockNum) { int ret; if (blockNum == 0 || blockNum == 3 || blockNum == 6) ret = 0; else if (blockNum == 1 || blockNum == 4 || blockNum == 7) ret = 1; else ret = 2; return ret; } void markX(int blockNum) { int x, y; x = getX(blockNum); y = getY(blockNum); block[x][y] = markX; } void markO(int blockNum) { int x, y; x = getX(blockNum); y = getY(blockNum); block[x][y] = markO; } boolean checkBlockIsVoid(int blockNum) { int x, y; x = getX(blockNum); y = getY(blockNum); if (block[x][y] == 0) return true; return false; } boolean checkWinner() { for (int i = 0; i < 3; i++) if ((block[i][0] == markO && block[i][1] == markO && block[i][2] == markO) || (block[i][0] == markX && block[i][1] == markX && block[i][2] == markX) || (block[0][i] == markO && block[1][i] == markO && block[2][i] == markO) || (block[0][i] == markX && block[1][i] == markX && block[2][i] == markX)) return true; if ((block[0][0] == markO && block[1][1] == markO && block[2][2] == markO) || (block[0][0] == markX && block[1][1] == markX && block[2][2] == markX) || (block[0][2] == markO && block[1][1] == markO && block[2][0] == markO) || (block[0][2] == markX && block[1][1] == markX && block[2][0] == markX)) return true; return false; } public Serious() { addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (win) return; x = e.getX(); y = e.getY(); int blockNum = blockDecider(x, y); if ( checkBlockIsVoid(blockNum) != true ) return; if (turn == true) { markX(blockNum); turn = false; } else { markO(blockNum); turn = true; } if ( checkWinner() ) { if (turn) System.out.println("winner = O"); else System.out.println("winner = X"); win = true; } repaint(); } }); } public static void main(String args[]) { Serious window = new Serious(); window.setBounds(100, 100, 300, 330); window.show(); } void drawX(int x, int y, Graphics g) { g.drawLine(y * 100, (x * 100) + 30, (y * 100) + 100, (x * 100) + 130); g.drawLine((y * 100) + 100, (x * 100) + 30, y * 100, (x * 100) + 130); } void drawO(int x, int y, Graphics g) { g.drawOval(y * 100, (x * 100) + 30, 100, 100); } public void paint(Graphics g) { g.drawLine(100, 30, 100, 330); g.drawLine(200, 30, 200, 330); g.drawLine(0, 130, 300, 130); g.drawLine(0, 230, 300, 230); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { if (block[i][j] == markX) drawX(i, j, g); if (block[i][j] == markO) drawO(i, j, g); } } }