votako

not work(TicTacToe)

May 23rd, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.65 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.Applet;
  4.  
  5.  
  6. public class TicTacToe extends Applet implements ActionListener {
  7.    
  8. /**
  9.      *
  10.      */
  11.     private static final long serialVersionUID = 1831218949168974884L;
  12. /**
  13.      *
  14.      */
  15.    
  16. Button squares[];
  17. Button newGameButton;
  18. Label score;
  19. int emptySquaresLeft=9;
  20.  
  21.     public void init(){
  22.         //add manager location applet, font and color
  23.         this.setLayout(new BorderLayout());
  24.         this.setBackground(Color.CYAN);
  25.        
  26.         //change font applet on bold and had size 20
  27.         Font appletFont = new Font("Monospased", Font.BOLD, 20);
  28.         this.setFont(appletFont);
  29.        
  30.         //create button "NewGame" and register it action listener
  31.        
  32.         newGameButton = new Button("new game");
  33.         newGameButton.addActionListener(this);
  34.        
  35.         Panel topPanel = new Panel();
  36.         topPanel.add(newGameButton);
  37.        
  38.             this.add(topPanel, "North");
  39.            
  40.             Panel centerPanel = new Panel();
  41.             centerPanel.setLayout(new GridLayout(3,3));
  42.             this.add(centerPanel, "Center");
  43.            
  44.             score = new Label("You turn");
  45.             this.add(score, "South");
  46.        
  47.         //create massive for store links on 9 buttons
  48.            
  49.             squares = new Button[9];
  50.        
  51.         //create buttons, save links on massive, register them action listner,
  52.         //color it in orange and add on panel
  53.            
  54.             for(int i=0;i<9;i++){
  55.                 squares[i] = new Button();
  56.                 squares[i].addActionListener(this);
  57.                 squares[i].setBackground(Color.ORANGE);
  58.                 centerPanel.add(squares[i]);
  59.             }
  60.        
  61.     }
  62.    
  63.     public void actionPerfomed(ActionEvent e){
  64.         Button theButton = (Button) e.getSource();
  65.        
  66.         //it button NewGame?
  67.         if(theButton == newGameButton){
  68.             for(int i=0;i<9;i++){
  69.                 squares[i].setEnabled(true);
  70.                 squares[i].setLabel("");
  71.                 squares[i].setBackground(Color.ORANGE);
  72.                
  73.             }
  74.            
  75.             emptySquaresLeft=9;
  76.             score.setText("You turn!");
  77.             newGameButton.setEnabled(false);
  78.            
  79.             return; //exit method
  80.         }
  81.        
  82.         String winner = "";
  83.         //it one of cells?
  84.        
  85.         for(int i=0;i<9;i++){
  86.             if (theButton == squares[i]){
  87.                 squares[i].setLabel("X");
  88.                 winner = lookForWinner();
  89.                
  90.                 if(!"".equals(winner)){
  91.                     endTheGame();
  92.                 }else{
  93.                     computerMove();
  94.                     winner = lookForWinner();
  95.                    
  96.                     if(!"".equals(winner)){
  97.                         endTheGame();
  98.                     }
  99.                 }
  100.                 break;
  101.             }
  102.         } //end of for
  103.        
  104.         if(winner.equals("X")){
  105.             score.setText("You won");
  106.         }else if(winner.equals("O")){
  107.             score.setText("lost");
  108.         }else if (winner.equals("T")){
  109.             score.setText("its a tie");
  110.         }
  111.     }//end action perfomed method
  112.    
  113. /*this method call after move, for know who is winner.
  114.  *he checks each rows,  for find same cell.
  115.  * @retErn "X", "O", "T" - tie. "" - no winner
  116. */
  117.     String lookForWinner(){
  118.         String theWinner = "";
  119.         emptySquaresLeft--;
  120.        
  121.         if(emptySquaresLeft==0){
  122.             return "T"; //tie
  123.         }
  124.        
  125.     //cheek row 1 - massive elements 0,1,2
  126.     if(!squares[0].getLabel().equals("") &&
  127.         squares[0].getLabel().equals(squares[1].getLabel()) &&
  128.         squares[0].getLabel().equals(squares[2].getLabel())){
  129.             theWinner = squares[0].getLabel();
  130.             highlightWinnrer(0,1,2);
  131.     //chek row 2 - massive elements 3,4,5
  132.     }else if(!squares[3].getLabel().equals("") &&
  133.         squares[3].getLabel().equals(squares[4].getLabel()) &&
  134.         squares[3].getLabel().equals(squares[5].getLabel())){
  135.             theWinner = squares[3].getLabel();
  136.             highlightWinnrer(3,4,5);
  137.     }else if(!squares[6].getLabel().equals("") &&
  138.             squares[6].getLabel().equals(squares[7].getLabel()) &&
  139.             squares[6].getLabel().equals(squares[8].getLabel() )) {
  140.                 theWinner = squares[6].getLabel();
  141.                 highlightWinnrer(6,7,8);
  142.     }else if(!squares[0].getLabel().equals("") &&
  143.             squares[0].getLabel().equals(squares[3].getLabel()) &&
  144.             squares[0].getLabel().equals(squares[6].getLabel())){
  145.                 theWinner = squares[0].getLabel();
  146.                 highlightWinnrer(0,3,6);
  147.     }else if(!squares[1].getLabel().equals("") &&
  148.             squares[1].getLabel().equals(squares[4].getLabel()) &&
  149.             squares[1].getLabel().equals(squares[7].getLabel())){
  150.                 theWinner = squares[1].getLabel();
  151.                 highlightWinnrer(1,4,7);
  152.     }else if(!squares[2].getLabel().equals("") &&
  153.             squares[2].getLabel().equals(squares[5].getLabel()) &&
  154.             squares[2].getLabel().equals(squares[8].getLabel())){
  155.                 theWinner = squares[2].getLabel();
  156.                 highlightWinnrer(2,5,8);
  157.     }else if(!squares[0].getLabel().equals("") &&
  158.             squares[0].getLabel().equals(squares[4].getLabel()) &&
  159.             squares[0].getLabel().equals(squares[8].getLabel())){
  160.                 theWinner = squares[0].getLabel();
  161.                 highlightWinnrer(0,4,8);
  162.     }else if(!squares[2].getLabel().equals("") &&
  163.             squares[2].getLabel().equals(squares[4].getLabel()) &&
  164.             squares[2].getLabel().equals(squares[6].getLabel())){
  165.                 theWinner = squares[2].getLabel();
  166.                 highlightWinnrer(2,4,6);
  167.     }
  168.     return theWinner;
  169.  
  170. }
  171.    
  172. /**
  173.  * this method applies set of rules, for look best computer move
  174.  * if best move didn't find, selected a random cell.
  175.  */
  176.     void computerMove(){
  177.         int selectedSquare;
  178.        
  179.         //at first computer try find empty cell beside
  180.         //2 zero's, for victory.
  181.         selectedSquare = findEmptySquare("O");
  182.        
  183.         //if he can't find two zero, then at last try not to give opponent
  184.         //make row of 3 crosses, placing zero beside crosses
  185.         if(selectedSquare == -1){
  186.             selectedSquare = findEmptySquare("X");
  187.         }
  188.        
  189.         //if selectedSquare == -1, then try take center cell
  190.         if((selectedSquare == -1) && (squares[4].getLabel().equals("")) ){
  191.             selectedSquare = 4;
  192.         }
  193.         //didn't luck with central cell, take a random
  194.         if(selectedSquare == -1){
  195.             selectedSquare = getRandomSquare();
  196.         }
  197.         squares[selectedSquare].setLabel("O");
  198.     }
  199.  
  200. /**
  201.  * this method cheek all row, column and diagonal for know, whether it two cells
  202.  * with a both inscriptions and empty cell.
  203.  * @param give X - for user and O - for computer
  204.  * @return the number of free cells, or -1 if didn't find two cell with same inscription
  205.  */
  206.     int findEmptySquare(String player){
  207.         int weight[] = new int[9];
  208.        
  209.         for( int i=0; i<9; i++){
  210.             if(squares[i].getLabel().equals("O") )
  211.                 weight[i] = -1;
  212.             else if (squares[i].getLabel().equals("X") )
  213.                 weight[i] = 1;
  214.             else
  215.                 weight[i] = 0;
  216.         }
  217.        
  218.         int twoWeights = player.equals("O") ? -2 : 2;
  219.        
  220.         //cheek if in first row two identical and one empty cell
  221.        
  222.         if(weight[0] + weight[1] + weight[2] == twoWeights){
  223.             if(weight[0] == 0)
  224.                 return 0;
  225.             else if (weight[1] == 0)
  226.                 return 1;
  227.             else
  228.                 return 2;
  229.         }
  230.        
  231.         //cheek if in two row two identical and one empty cell
  232.         if(weight[3] + weight[4] + weight[5] == twoWeights){
  233.             if(weight[3] == 0)
  234.                 return 3;
  235.             else if (weight[4] == 0)
  236.                 return 4;
  237.             else
  238.                 return 5;
  239.         }
  240.         //and row three
  241.         if(weight[6] + weight[7] + weight[8] == twoWeights){
  242.             if(weight[6] == 0)
  243.                 return 6;
  244.             else if (weight[7] == 0)
  245.                 return 7;
  246.             else
  247.                 return 8;
  248.         }
  249.         //now column 1
  250.         if(weight[0] + weight[3] + weight[6] == twoWeights){
  251.             if(weight[0] == 0)
  252.                 return 0;
  253.             else if (weight[3] == 0)
  254.                 return 3;
  255.             else
  256.                 return 6;
  257.         }
  258.         //column 2
  259.         if(weight[1] + weight[4] + weight[7] == twoWeights){
  260.             if(weight[1] == 0)
  261.                 return 1;
  262.             else if (weight[4] == 0)
  263.                 return 4;
  264.             else
  265.                 return 7;
  266.         }
  267.         //and 3
  268.         if(weight[2] + weight[5] + weight[8] == twoWeights){
  269.             if(weight[2] == 0)
  270.                 return 2;
  271.             else if (weight[5] == 0)
  272.                 return 5;
  273.             else
  274.                 return 8;
  275.         }
  276.         //now diagonal
  277.        
  278.         if(weight[0] + weight[4] + weight[8] == twoWeights){
  279.             if(weight[0] == 0)
  280.                 return 0;
  281.             else if (weight[4] == 0)
  282.                 return 4;
  283.             else
  284.                 return 8;
  285.         }
  286.        
  287.         //and two diagonal
  288.         if(weight[2] + weight[4] + weight[6] == twoWeights){
  289.             if(weight[2] == 0)
  290.                 return 2;
  291.             else if (weight[4] == 0)
  292.                 return 4;
  293.             else
  294.                 return 6;
  295.         }
  296.         //didn't find two identical cells
  297.         return -1;
  298.        
  299.     }//end findEmptySquare();
  300.  
  301.     int getRandomSquare(){
  302.  
  303.         boolean gotEmptySquare = false;
  304.        
  305.         int selectedSquare = -1;
  306.        
  307.         do{
  308.             selectedSquare = (int) (Math.random()*9);
  309.            
  310.             if(squares[selectedSquare].getLabel().equals("")){
  311.                 gotEmptySquare = true; //for finish cycle
  312.             }
  313.         } while (!gotEmptySquare);
  314.        
  315.         return selectedSquare;
  316.     }//end getRandom method
  317.  
  318. /**
  319.  * method selected winning line.
  320.  * @param 1,2,3 cells for release
  321.  */
  322.    
  323.     void highlightWinnrer(int win1, int win2, int win3){
  324.         squares[win1].setBackground(Color.CYAN);
  325.         squares[win2].setBackground(Color.CYAN);
  326.         squares[win3].setBackground(Color.CYAN);
  327.     }
  328.     //after game all cells freeze. button "new game" - enabled;
  329.     void endTheGame(){
  330.         newGameButton.setEnabled(true);
  331.        
  332.         for(int i=0;i<9;i++){
  333.             squares[i].setEnabled(false);
  334.         }
  335.     }
  336.  
  337.     @Override
  338.     public void actionPerformed(ActionEvent e) {
  339.         // TODO Auto-generated method stub
  340.        
  341.     }
  342.  
  343. }//end
Advertisement
Add Comment
Please, Sign In to add comment