Ошибка в коде - Java (241047)

Узнай цену своей работы

Формулировка задачи:

Возникла ошибка при разбиение на два классы, помогите пожалуйста решить эту проблему.
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
 
public class Pole extends JFrame implements ActionListener
{
 
    private static final long serialVersionUID = 1L;
    public int ButtonsInGame, vin;  
    final int   NULL = 0, 
                X = 1, 
                O = (-1);
 
    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
                Pole thisClass = new Pole();
                thisClass.setVisible(true);
            }
        });
    }
 
    public Pole() 
    {
        initialize();
    }
    public void initialize() 
    {
        this.setSize(240, 290);
        this.setContentPane(getJContentPane());
        this.setTitle("Tic-tac-toe");
        this.New_Game();
    }
    
    public JPanel jContentPane = null;
    public JLabel StatusGame = null;
    public JPanel getJContentPane() 
    {
        if (jContentPane == null) 
        {
            StatusGame = new JLabel();
            StatusGame.setHorizontalAlignment(SwingConstants.CENTER);
            StatusGame.setBackground(Color.black);
            StatusGame.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.setBackground(new Color(0, 204, 0));
            jContentPane.add(getJPanel(), BorderLayout.NORTH);
            jContentPane.add(getJPanel1(), BorderLayout.CENTER);
            jContentPane.add(StatusGame, BorderLayout.SOUTH);
        }
        return jContentPane;
    }   
    
    public JPanel jPanel = null;
    public JPanel getJPanel() 
    {
        if (jPanel == null) 
        {
            jPanel = new JPanel();
            jPanel.setLayout(new FlowLayout());
            jPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
            jPanel.setBackground(new Color(32, 32, 32));
            jPanel.add(getNew(), null);
        }
        return jPanel;
    }
    public JButton New = null;
    public JButton getNew() 
    {
        if (New == null) 
        {
            New = new JButton("Start new game");
            New.setBackground(new Color(0, 204, 0));
        }
        return New;
    }
    public JButton[] field = null;  
    public JPanel jPanel1 = null;
    public JPanel getJPanel1() 
    {
        if (jPanel1 == null) 
        {
            jPanel1 = new JPanel(new GridLayout(3,3,1,1));
            jPanel1.setBackground(new Color(0, 0, 0));  
            field = new JButton[9];
            for(int i=0;i<field.length;i++)
            {
                field[i] = new JButton();
                setState(i);
                field[i].setForeground(new Color(0,204,0));
                jPanel1.add(field[i]);              
                field[i].setBackground(new Color(0,102,0));
            }
        }
        return jPanel1;
    }
    public void setState(int index, int state)
    {
        field[index].setText(toStrTipe(state));
        field[index].setEnabled(!(state==1||state==-1));
        ButtonsInGame--;
    }

    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource().equals(New))New_Game();        
        for(int i=0 ; i<9 ; i++)
        {
            if(e.getSource().equals(field[i]))
            {                   
                setState(i, O);
                set_mass();             
                vin = lock_winner();                
                if(vin!=0)StatusGame.setText(who_win(vin));
                else
                {
                    setState(tactica(), X);
                    set_mass();
                    int ret = lock_winner() ;   
                    if(ret>0)StatusGame.setText(who_win(ret));
                }
            }
        }
    }
}
import java.util.Random;
import java.util.Vector;
 
public class Game extends Pole
{
 
    private static final long serialVersionUID = 1L;
    
    public int k[]= new int [9];
    public int tactica()
    {
        if(ButtonsInGame==9)return 4;
        if(ButtonsInGame==8)            
            while(true)
            {
                switch(getRand(3))
                {
                case 0:
                    if(k[0]==0)return 0;
                case 1:
                    if(k[2]==0)return 2;
                case 2:
                    if(k[6]==0)return 6;
                case 3:
                    if(k[8]==0)return 8;
                    continue;
                }
            }
        if(ButtonsInGame<8)
        {
            int res = empty_field(X);
            if(res!=-1)return res;
                res = empty_field(O);
            if(res!=-1)return res;      
        }               
        return rand_field();
    }
 
    public void New_Game() 
    {
        for(int i=0;i<9;i++)
        {
            setState(i, NULL);
            field[i].setContentAreaFilled(false);
        }
        ButtonsInGame = 9;
        StatusGame.setText(who_win(3));             
        int in = tactica();
        if(in!=-1)setState(in, X);
    }
 
    public int lock_winner()
    {           
        if(k[0]+k[1]+k[2]==3||k[0]+k[1]+k[2]==-3)
        {
            line(0,1,2);
            return k[0];
        }
        if(k[0]+k[3]+k[6]==3||k[0]+k[3]+k[6]==-3)
        {
            line(0,3,6);
            return k[0];
        }
        if(k[6]+k[7]+k[8]==3||k[6]+k[7]+k[8]==-3)
        {
            line(6,7,8);
            return k[8];
        }
        if(k[2]+k[5]+k[8]==3||k[2]+k[5]+k[8]==-3)
        {
            line(2,5,8);
            return k[8];
        }
        if(k[0]+k[4]+k[8]==3||k[0]+k[4]+k[8]==-3)
        {
            line(0,4,8);
            return k[4];
        }
        if(k[2]+k[4]+k[6]==3||k[2]+k[4]+k[6]==-3)
        {
            line(2,4,6);
            return k[4];
        }
        if(k[1]+k[4]+k[7]==3||k[1]+k[4]+k[7]==-3)
        {
            line(1,4,7);
            return k[4];
        }
        if(k[3]+k[4]+k[5]==3||k[3]+k[4]+k[5]==-3)
        {
            line(3,4,5);
            return k[4];
        }       
        if(ButtonsInGame==0)return 2;
        return NULL;
    }
    
    public String who_win(int stat)
    {
        if(stat==X)return "Win computer" ;
        if(stat==O)return "You win";
        if(stat==2)return "Tandoff";
        
        return null;
    }
    
    public String toStrTipe(int state)
    {
        if(state==1)return "X";
        if(state==-1)return "O";
        return "";
    }
    
    public void line(int a, int b, int c)
    {
        field[a].setContentAreaFilled(true);
        field[b].setContentAreaFilled(true);
        field[c].setContentAreaFilled(true);
    }
 
    public int empty_field(int player)
    {
        int n = (player==O)?-2:2; 
        if(k[0]+k[1]+k[2]==n)
        {
            if(k[0]==0) return 0;
            if(k[1]==0) return 1;
            if(k[2]==0) return 2;           
        }
        else if(k[3]+k[4]+k[5]==n)
        {
            if(k[3]==0) return 3;
            if(k[4]==0) return 4;
            if(k[5]==0) return 5;           
        }
        else if(k[6]+k[7]+k[8]==n)
        {
            if(k[6]==0) return 6;
            if(k[7]==0) return 7;
            if(k[8]==0) return 8;           
        }
        else if(k[0]+k[3]+k[6]==n)
        {
            if(k[0]==0) return 0;
            if(k[3]==0) return 3;
            if(k[6]==0) return 6;           
        }
        else if(k[1]+k[4]+k[7]==n)
        {
            if(k[1]==0) return 1;
            if(k[4]==0) return 4;
            if(k[7]==0) return 7;           
        }
        else if(k[2]+k[5]+k[8]==n)
        {
            if(k[2]==0) return 2;
            if(k[5]==0) return 5;
            if(k[8]==0) return 8;           
        }
        else if(k[0]+k[4]+k[8]==n)
        {
            if(k[0]==0) return 0;
            if(k[4]==0) return 4;
            if(k[8]==0) return 8;           
        }
        else if(k[6]+k[4]+k[2]==n)
        {
            if(k[6]==0) return 6;
            if(k[4]==0) return 4;
            if(k[2]==0) return 2;           
        }
        return -1;      
    }
 
    public void set_mass()
    {
        for(int ii=0;ii<9;ii++)
        {
            if(field[ii].getText().equals("X")) k[ii]=X;
            else 
                if(field[ii].getText().equals("O")) k[ii]=O;
            else k[ii]=NULL;
        }   
    }
    
    public Random rand = new Random();
    public int getRand(int lim){
        if(lim<1)return -1;
        return Math.abs(rand.nextInt(lim));
    }
 
    public int rand_field(){
        Vector <Integer> v = new Vector<Integer>();
        for(int i=0;i<9;i++)
            if(k[i]==0)v.add(i);
        return v.get(getRand(v.size()));
    }
}

Решение задачи: «Ошибка в коде»

textual
Листинг программы
 setState(i);

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

5   голосов , оценка 4.2 из 5