Question
Somebody has helped me with this code but I am unable to touch bases with them again...Does anyone know how I can fix this error
Somebody has helped me with this code but I am unable to touch bases with them again...Does anyone know how I can fix this error without changing the entire program? I believe it's the only thing stopping the game from playing correctly. Everything else is working great. I'll put the code below the picture...
public class TicTacToeApplet extends JApplet implements ChangeListener, ActionListener { private JSlider slider; private JButton oButton, xButton; private Board board; private int lineThickness = 4; private Color oColor = Color.BLUE, xColor = Color.RED; static final char BLANK = ' ', O = 'O', X = 'X'; private char position[] = {BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK, BLANK}; private int wins = 0, losses = 0, draws = 0; @Override public void init() { //Build the panels JPanel topPanel = new JPanel(); topPanel.setLayout(new FlowLayout()); topPanel.add(new JLabel("Line Thickness:")); topPanel.add(slider = new JSlider(SwingConstants.HORIZONTAL, 1,20,4)); //Create the slider slider.setMajorTickSpacing(1); slider.setPaintTicks(true); slider.addChangeListener(this); //Add buttons to the panels topPanel.add(oButton = new JButton("O Color")); topPanel.add(xButton = new JButton("X Color")); oButton.addActionListener(this); xButton.addActionListener(this); //Add the panels to the content pane. add(topPanel, BorderLayout.NORTH); add(board = new Board(), BorderLayout.CENTER); setVisible(true); } //Handles the change events that are generated when the slider is moved @Override public void stateChanged(ChangeEvent e) { lineThickness = slider.getValue(); board.repaint(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == oButton) { Color newColor = JColorChooser.showDialog(this,"Choose a new color" + "for O", oColor); if(newColor != null) oColor = newColor; } else if (e.getSource() == xButton) { Color newColor = JColorChooser.showDialog(this,"Choose a new color" + "for X", xColor); if (newColor != null) xColor = newColor; } board.repaint(); } private class Board extends JPanel implements MouseListener { private Random random = new Random(); private int rows[][]= {{0,2}, {3,5}, {6,8}, {0,8}, {2,6}}; public Board() { //Add a mouse listener addMouseListener(this); } /** * paintComponent method * @param g The panels Graphic object. */ @Override public void paintComponent(Graphics g) { //Call the superclass paintComponent method super.paintComponent(g); //Set width and hight of board int w = getWidth(); int h = getHeight(); Graphics2D g2d = (Graphics2D) g; //Create the board g2d.setPaint(Color.WHITE); g2d.fill(new Rectangle2D.Double(0,0,w,h)); g2d.setPaint(Color.BLACK); g2d.setStroke(new BasicStroke(lineThickness)); g2d.draw(new Line2D.Double(0, h/3, w, h/3)); g2d.draw(new Line2D.Double(0, h*2/3, w, h*2/3)); g2d.draw(new Line2D.Double(w/3, 0, w/3, h)); g2d.draw(new Line2D.Double( w*2/3, 0, w*2/3, h)); for (int i = 0; i = 0 && pos = 0) return result; } return -1; } int find1Row(char player, int a, int b) { int c=(a+b)/2; // middle spot if (position[a]==player && position[b]==player && position[c]==BLANK) return c; if (position[a]==player && position[c]==player && position[b]==BLANK) return b; if (position[b]==player && position[c]==player && position[a]==BLANK) return a; return -1; } boolean isDraw() { for (int i=0; i
public void mouseExited (MouseEvent e) 169 170 void putx if (won (O) 172 173 'else' without 'if eWGame (O) 174 Surround with if (is Draw 175 newGame (BLANK) 176 (Alt-Enter shows hints) else 179 next Move 180 if (won (X) 181 182 newGame (X) 183 else if (is Draw 184 185 newGame (BLANK) 186 187 188 189 boolean won (char player) 190 A 191 for (int i O: i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started