18.6
1 import java.awt.*; 2 import java.awt.event.ActionEvent; 3 import java.awt.event.ActionListener; 4 import java.awt.event.MouseAdapter; 5 import java.awt.event.MouseEvent; 6 7 import javax.swing.*; 8 import javax.swing.border.LineBorder; 9 10 public class Test_18_6 extends JApplet{ 11 static char whoseTurn = 'X'; 12 private static Cell[][] cells = new Cell[3][3]; 13 static JLabel jlblStatus = new JLabel("X's turn to play"); 14 JButton newGame = new JButton("NEW GAME"); 15 16 public Test_18_6(){ 17 JPanel p = new JPanel(new GridLayout(3,3,0,0)); 18 for(int i = 0; i < 3; i++) 19 for(int j = 0; j < 3; j++) 20 p.add(cells[i][j] = new Cell()); 21 22 p.setBorder(new LineBorder(Color.red,1)); 23 jlblStatus.setBorder(new LineBorder(Color.yellow,1)); 24 25 add(newGame,BorderLayout.NORTH); 26 add(p,BorderLayout.CENTER); 27 add(jlblStatus,BorderLayout.SOUTH); 28 newGame.addActionListener(new ActionListener(){ 29 30 @Override 31 public void actionPerformed(ActionEvent arg0) { 32 // TODO Auto-generated method stub 33 for(int i = 0; i < 3; i++) 34 for(int j = 0; j < 3; j++) 35 cells[i][j].setToken(' '); 36 whoseTurn = 'X'; 37 jlblStatus = new JLabel("X's turn to play"); 38 } 39 40 }); 41 } 42 43 public static boolean isFull(){ 44 for(int i = 0; i < 3; i++) 45 for(int j = 0; j < 3; j++) 46 if(cells[i][j].getToken() == ' ') 47 return false; 48 49 return true; 50 } 51 52 public static boolean isWon(char token){ 53 for(int i = 0; i < 3; i++) 54 if((cells[i][0].getToken() == token) && (cells[i][1].getToken() == token) && (cells[i][2].getToken() == token)){ 55 return true; 56 } 57 58 for(int j = 0; j < 3; j++) 59 if((cells[0][j].getToken() == token) && (cells[1][j].getToken() == token) && (cells[2][j].getToken() == token)){ 60 return true; 61 } 62 63 if((cells[0][0].getToken() == token) && (cells[1][1].getToken() == token) && (cells[2][2].getToken() == token)){ 64 return true; 65 } 66 67 if((cells[0][2].getToken() == token) && (cells[1][1].getToken() == token) && (cells[2][0].getToken() == token)){ 68 return true; 69 } 70 return false; 71 } 72 } 73 74 class Cell extends JPanel{ 75 private char token = ' '; 76 77 public Cell(){ 78 setBorder(new LineBorder(Color.black,1)); 79 addMouseListener(new MyMouseListener()); 80 } 81 82 public char getToken(){ 83 return token; 84 } 85 86 public void setToken(char c){ 87 token = c; 88 repaint(); 89 } 90 91 protected void paintComponent(Graphics g){ 92 super.paintComponent(g); 93 94 if(token == 'X'){ 95 g.drawLine(10, 10, getWidth() - 10, getHeight() - 10); 96 g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10); 97 } 98 else if(token == 'O'){ 99 g.drawOval(10, 10, getWidth() - 20, getHeight() - 20); 100 } 101 } 102 private class MyMouseListener extends MouseAdapter{ 103 public void mouseClicked(MouseEvent e){ 104 if(token == ' ' && Test_18_6.whoseTurn != ' '){ 105 setToken(Test_18_6.whoseTurn); 106 107 if(Test_18_6.isWon(Test_18_6.whoseTurn)){ 108 Test_18_6.jlblStatus.setText(Test_18_6.whoseTurn + "won! the game is over"); 109 Test_18_6.whoseTurn = ' '; 110 } 111 else if(Test_18_6.isFull()){ 112 Test_18_6.jlblStatus.setText("Draw! The game is over"); 113 Test_18_6.whoseTurn = ' '; 114 } 115 else { 116 Test_18_6.whoseTurn = (Test_18_6.whoseTurn == 'X')?'O':'X'; 117 Test_18_6.jlblStatus.setText(Test_18_6.whoseTurn + "'s turn"); 118 } 119 } 120 } 121 } 122 }