-2

I am coding in Java and I have a final project due friday and I've done the whole thing but I can't fix my brackets at the end so the whole thing is messed up. Also for some reason the tag List won't work in my code. Please see below. I know they are both easy fixes but I can't seem to figure it out! any help is appreciated. thanks! what brackets do i need to add/fix?

edited! everywhere where there is doesn't call back into my class position where i defined them. bestMove gameEnd win move

package ttt;
import ttt.position;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.text.Position;

public class Game {

position position = new position();
// the word button has a red mark next to the semicolon
protected Component button; 


public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        private Game game;
        private int idx;

        public void run() {
            JFrame frame = new JFrame("Java TTT");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new GridLayout(3, 3));
            game = new Game();
            final JButton[] buttons = new JButton[9];
            for (int i = 0; i < 9; i++) {
                idx = i;
                final JButton button = new JButton();
                buttons[i] = button;
                button.setPreferredSize(new Dimension(100, 100));
                button.setBackground(Color.BLACK);
                button.setOpaque(true);
                button.setFont(new Font(null, Font.PLAIN, 100));
                button.addMouseListener(new MouseListener() {
                    public void mouseReleased(MouseEvent e) {}
                    public void mousePressed(MouseEvent e) {}
                    public void mouseExited(MouseEvent e) {}
                    public void mouseEntered(MouseEvent e) {}
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        button.setText("" + game.position.turn);
                        game.move(idx);
                            if (!game.position.gameEnd()) {
                                int best = game.position.bestMove();
                                buttons[best].setText("" + game.position.turn);
                                game.move(best);
                            }
                            if (game.position.gameEnd()) {
                                String message = "";
                                if (game.position.win('x')) {
                                    message = "You won! (Mr. Lebron I made the game let you win, I think that deserves an A [Quit to play again])";
                                } else if (game.position.win('o')) {
                                    message = "Computer won! (I think I still deserve an A [Quit to play again])";
                                } else {
                                    message = "It's a tie! (Good try though [Quit to play again])";
                                }

                                JOptionPane.showMessageDialog(null, message);

                            }
                        }
                });
                frame.add(button);
            }
            // realize components
            frame.pack();
            frame.setVisible(true);
        }
    } );

}

protected void move(int idx) 
{
// the words position
    position = position.move(idx); 
//the following bracket
}

}

position class

import java.util.LinkedList;

public class position {
public char[] board;
public char turn;
public int dim = 3;
private Integer mm;

public position() {
    this.board = "       ".toCharArray();
    this.turn = 'x';

}

public position(char[] board, char turn) {
    this.board = board;
    this.turn = turn;
}

public position(String str) {
    this.board = str.toCharArray();
    this.turn = 'x';
}

public position(String str, char turn) {
    this.board = str.toCharArray();
    this.turn = turn;
}

public String toString() {
    return new String(board);
}

public position move(int idx) {
    char[] newBoard = board.clone();
    newBoard[idx] = turn;
    return new position(newBoard, turn == 'x' ? 'o' : 'x');
}

// list type can't be generic edit

public Integer[] possibleMoves() {
    java.util.List<Integer> list = new LinkedList<Integer>();
    for (int i = 0; i < board.length; i++) {
        if (board[i] == ' ') {
            list.add(i);
        }

    }
    Integer[] array = new Integer[list.size()];
    list.toArray(array);

    return array;
}

public boolean win_line(char turn, int start, int step) {
    for (int i = 0; i < 3; i++) {
        if (board[start + step * 1] != turn) {
            return false;
        }
    }
    return true;
}

// calling win here

public boolean win(char turn) {
    for (int i = 0; i < dim; i++) {
        if (win_line(turn, i * dim, 1) || win_line(turn, i, dim)) {
            return true;
        }
    }
    if (win_line(turn, dim - 1, dim - 1) || win_line(turn, 0, dim + 1)) {
        return true;
    }
    {
        return false;
    }
}

@SuppressWarnings("null")
public int minimax() {
    if (win('x')) {
        return 100;
    }
    if (win('o')) {
        return -100;
    }
    if (possibleMoves().length == 0) {
        return 0;
    }
    mm = null;
    for (Integer idx : possibleMoves()) {
        Integer value = null;
        if (mm == null || turn == 'x' && mm < value || turn == 'o'
                && value < mm) {
            mm = value;
        }
    }
    return mm + (turn == 'x' ? -1 : 1);
}

// fix game end here
public boolean gameEnd() {

    return win('x') || win('o') || possibleMoves().length == 0;

}

public int bestMove() {
    Integer mm = null;
    int best = -1;
    for (Integer idx : possibleMoves()) {
        Integer value = move(idx).minimax();
        if (mm == null || turn == 'x' && mm < value || turn == 'o'
                && value < mm) {
            mm = value;
            best = idx;
        }
    }
    return best;
}

}

3 Answers3

1

You missed quite a few matching/closing brackets in your code. Not sure where you want to place the method possibleMoves(), but the original code posted should work as follows:

public class Game {

    position position = new position();
    // the word button has a red mark next to the semicolon
    protected Component button; 


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            private Game game;
            private int idx;

            public void run() {
                JFrame frame = new JFrame("Java TTT");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridLayout(3, 3));
                game = new Game();
                final JButton[] buttons = new JButton[9];
                for (int i = 0; i < 9; i++) {
                    idx = i;
                    final JButton button = new JButton();
                    buttons[i] = button;
                    button.setPreferredSize(new Dimension(100, 100));
                    button.setBackground(Color.BLACK);
                    button.setOpaque(true);
                    button.setFont(new Font(null, Font.PLAIN, 100));
                    button.addMouseListener(new MouseListener() {
                        public void mouseReleased(MouseEvent e) {}
                        public void mousePressed(MouseEvent e) {}
                        public void mouseExited(MouseEvent e) {}
                        public void mouseEntered(MouseEvent e) {}
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            button.setText("" + game.position.turn);
                            game.move(idx);
                                if (!game.position.gameEnd()) {
                                    int best = game.position.bestMove();
                                    buttons[best].setText("" + game.position.turn);
                                    game.move(best);
                                }
                                if (game.position.gameEnd()) {
                                    String message = "";
                                    if (game.position.win('x')) {
                                        message = "You won! (Mr. Lebron I made the game let you win, I think that deserves an A [Quit to play again])";
                                    } else if (game.position.win('o')) {
                                        message = "Computer won! (I think I still deserve an A [Quit to play again])";
                                    } else {
                                        message = "It's a tie! (Good try though [Quit to play again])";
                                    }

                                    JOptionPane.showMessageDialog(null, message);

                                }
                            }
                    });
                    frame.add(button);
                }
                // realize components
                frame.pack();
                frame.setVisible(true);
            }
        }
    }

    protected void move(int idx) 
    {
    // the words position
        position = position.move(idx); 
    //the following bracket
    }
}
display name
  • 4,165
  • 2
  • 27
  • 52
0

Download an editor like sublime and open the file in that editor.

It will show you each corresponding bracket for each one when you move the cursor to the position of a bracket.

You will find out, if you correct the indentions in your file.

Also you could give jsFormat a go: https://stackoverflow.com/a/12867043/1069083

Community
  • 1
  • 1
rubo77
  • 19,527
  • 31
  • 134
  • 226
0

For the list error you have to import java.util.*

import java.util.*

Importing LinkedList does not include using list.

CyanogenCX
  • 414
  • 6
  • 17
  • Multiple markers at this line - The type List is not generic; it cannot be parameterized with arguments - The type List is not generic; it cannot be parameterized with arguments – Andrew Rothberg Dec 17 '14 at 01:15
  • @AndrewRothberg try instantiating it as java.util.List list = new LinkedList(); – CyanogenCX Dec 17 '14 at 01:34