Tic Tac Toe / Java

时间:2017-04-24 22:05:07

标签: java

我已经完成了我的第一个应用 - Tic Tac Toe。我想添加一个新功能 - 显示赢家(玩家X或O)。我该怎么做?

public class Test extends JFrame {
int counter = 0;
public Test(){
    setSize(800,800);
    setTitle("Kółko i krzyżyk");
    setVisible(true);
    setLayout(new GridLayout(3,3));
    for (int i = 1; i <= 9; i++){
    JButton button = new JButton ("");
    add (button);

button.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton) e.getSource();
            if (counter % 2 ==0){
                button.setText("X");
            System.out.println("X");
            }   else{
                button.setText("O");
                System.out.println("O");
            }
            button.setEnabled(false);
            counter++;
        }
    });
    }

}

1 个答案:

答案 0 :(得分:0)

1.Player  Class :
import java.util.Scanner;

public class Player {
    private String name;
    private MarkType mark;
    private int row, col;
    Scanner sc = new Scanner(System.in);

    public Player(String name, MarkType mark) {
        this.mark = mark;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public MarkType getMark() {
        return mark;
    }

    public void nextMove() {

        System.out.println(this.getName() + " enter position for " + this.getMark());
        row = sc.nextInt();
        col = sc.nextInt();

    }

    public int getRow() {
        return row - 1;
    }

    public int getCol() {
        return col - 1;
    }
}

2.Board  Class :

public class Board {
    private MarkType board[][];
    private int bsize;

    public Board(int bsize) {
        this.bsize = bsize;
        board = new MarkType[bsize][bsize];
    }

    public int getBoardSize() {
        return bsize;
    }

    public void updateBoard(int row, int col, MarkType mark) {
        if (row > bsize || col > bsize) {
            throw new ArrayIndexOutOfBoundsException();
        } else if (board[row][col] == MarkType.$) {
            board[row][col] = mark;
        } else {
            throw new InvalidPosition("Enter valid position");
        }
    }

    public void initialize() {
        for (int row = 0; row < bsize; row++)
            for (int col = 0; col < bsize; col++)
                board[row][col] = MarkType.$;
    }

    public boolean isWinner(int passedRow, int passedCol, MarkType mark) {
        for (int col = 0; col < bsize; col++) {
            if (board[passedRow][col] != mark)
                break;
            if (col == bsize - 1) {
                return true;
            }
        }

        for (int row = 0; row < bsize; row++) {
            if (board[row][passedCol] != mark)
                break;
            if (row == bsize - 1) {
                return true;
            }
        }

        if (passedRow == passedCol) {
            for (int row = 0; row < bsize; row++) {
                if (board[row][row] != mark)
                    break;
                if (row == bsize - 1) {
                    return true;
                }
            }
        }

        if (passedRow + passedCol == bsize - 1) {
            for (int index = 0; index < bsize; index++) {
                if (board[index][(bsize - 1) - index] != mark)
                    break;
                if (index == bsize - 1) {
                    return true;
                }
            }
        }
        return false;
    }

    public void display() {
        for (int row = 0; row < bsize; row++) {
            for (int col = 0; col < bsize; col++) {
                System.out.print(" " + board[row][col] + " ");
                if (col != bsize - 1) {
                    System.out.print("|");
                }
            }
            System.out.println();
            if (row != bsize - 1) {
                for (int col = 0; col < bsize; col++)
                    System.out.print("--- ");
            }
            System.out.println();
        }
    }

}


for complete answer follow the link below:

http://programmersthing.blogspot.in/2017/06/simple-game-in-java.html
相关问题