Hướng dẫn lập trình game caro trên eclipse

Hôm nay mình tiếp tục chia sẻ tới các bạn Game caro 2 người chơi với giao diện đồ họa đẹp mắt, thân thiện và dễ sử dụng.

Hướng dẫn lập trình game caro trên eclipse

Chương trình cho phép người chơi có thể chọn trò chơi mới, ván mới, đi lại và xin thua. Bên phía trên mỗi hình người có ghi tỷ số trận đấu và trạng thái đến lượt người nào đi.

Khi bắt đầu vào game cho phép chọn người đi trước và nhập tên của 2 người chơi.

Hướng dẫn lập trình game caro trên eclipse

Khi có một người chơi thắng cuộc thì tỷ số được cập nhật, hiển thị ra thông báo thắng hiệp và cho phép lựa chọn các tùy chọn để chơi tiếp hoặc thoát.

Hướng dẫn lập trình game caro trên eclipse

Tại các menu Trò chơi cho phép chọn các hiệp/trận đấu mới hoặc thoát. Menu Hướng dẫn sẽ hiển thị cửa sổ hướng dẫn chơi và giới thiệu về trò chơi.

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package test;import java.awt.*;import java.awt.event.*;import javax.swing.*;import static javax.swing.JFrame.EXIT_ON_CLOSE;//Khởi tạo 1 lưới các ô vuông là các button public class caro extends JPanel implements ActionListener {static int COUNT \= 0;// JPanel right=new JPanel(); //public JFrame frame1 = new JFrame();//tạo mới 1 jframe có tên là frame JPanel panelNorth \= new JPanel();JPanel panelWest \= new JPanel();JPanel panelEast \= new JPanel();JPanel panelSouth \= new JPanel();JPanel panelCenter \= new JPanel();//tạo mới 1 jpanel có tên là pan public JFrame frame \= new JFrame();//tạo mới 1 jframe có tên là frame public int n \= 19, m \= 19, num \= 0, diem \= 0;//khởi tạo các giá trị : kích thước của từng button, điểm, số public JButton btn[][] = new JButton[n][m];//Tạo mới 1 jbutton kiểu mảng 2 chiều có n,m phần tử int pos[][] = new int[m][n]; //Tạo 1 biến kiểu int chứa giá trị tương ứng của phần tử trong mảng 2 chiều //ham add public void add() { //Khởi tạo 1 hàm để add các giá trị vào frame.setLayout(new BorderLayout());panelNorth.setBackground(Color.pink);panelEast.setBackground(Color.yellow);panelSouth.setBackground(Color.red);panelWest.setBackground(Color.yellow);JLabel lbHeader \= new JLabel("GAME CARO");JLabel lbTime \= new JLabel("Time : ");panelNorth.add(lbHeader);panelNorth.add(lbTime);JLabel lbPlayer1 \= new JLabel("Player1");JLabel lbImagePlayer1 \= new JLabel();ImageIcon ImgPlay1 \= new ImageIcon("C://Users//DUONGNGUYEN//Pictures/player1.jpg");lbImagePlayer1.setIcon(ImgPlay1);JLabel lbName1 \= new JLabel("Steps : ");JLabel lbTime1 \= new JLabel("Time Thinhking : ");//panelEast.setLocation(2, 2); panelEast.add(lbPlayer1);panelEast.add(lbImagePlayer1);panelEast.add(lbName1);panelEast.add(lbTime1);//lbName1.setLocation(0,0); JLabel lbPlayer2 \= new JLabel("Player2");JLabel lbImagePlayer2 \= new JLabel();ImageIcon ImgPlay2 \= new ImageIcon("C://Users//DUONGNGUYEN//Pictures/player2.jpg");lbImagePlayer2.setIcon(ImgPlay2);JLabel lbName2 \= new JLabel("Steps : ");JLabel lbTime2 \= new JLabel("Time Thinhking : ");panelWest.add(lbImagePlayer2);panelWest.add(lbPlayer2);panelWest.add(lbName2);panelWest.add(lbTime2);JButton btnStart \= new JButton("Start");JButton btnPause \= new JButton("Pause");JButton btnPlay \= new JButton("Play");JButton btnRule \= new JButton("Rule");btnStart.addActionListener(new ActionListener() {@Override public void actionPerformed(ActionEvent ae) {JOptionPane.showMessageDialog(null, "Trò Chơi Mới", "Thoát", JOptionPane.INFORMATION_MESSAGE);for (int i1 \= 0; i1 < n; i1++) {for (int j1 \= 0; j1 < m; j1++) {btn[i1][j1].setText("");btn[i1][j1].setBackground(Color.white); } } } });btnRule.addActionListener(new ActionListener() {@Override public void actionPerformed(ActionEvent ae) {JOptionPane.showMessageDialog(null, "- Cách chơi: \n" + "Trò chơi được chơi trên bàn cờ có 324 ô, với 18 dòng và 18 cột." + " Người chiến thắng là người tạo được đường thẳng theo chiều dọc" + " hoặc ngang hoặc chéo với chính xác 5 con cờ của mình.", "Thoát", JOptionPane.INFORMATION_MESSAGE); } });panelSouth.add(btnStart);panelSouth.add(btnPause);panelSouth.add(btnPlay);panelSouth.add(btnRule);frame.add(panelNorth, BorderLayout.NORTH);frame.add(panelEast, BorderLayout.EAST);frame.add(panelWest, BorderLayout.WEST);frame.add(panelSouth, BorderLayout.SOUTH);frame.add(panelCenter); //add jpanel vào 1 jframe panelCenter.setLayout(new GridLayout(n, m));//xét giá trị vào jpanel for (int i \= 0; i < n; i++) {//số hàng của jpanel for (int j \= 0; j < m; j++) {//số cột của jpanel pos[i][j] = num;//gán từng giá trị của từng phần tử bằng giá trị số int num++;//tăng giá trị số lên lần lượt từng đơn vị } }for (int i \= 0; i < n; i++) {for (int j \= 0; j < m; j++) {btn[i][j] = new JButton();//khởi tạo bộ nhớ cho từng jbutton btn[i][j].addActionListener(this);//khi con trỏ chuột trỏ vào phần tử tương ứng nào panelCenter.add(btn[i][j]);//add giá trị vào phần tử trỏ chuột tương ứng } }for (int i \= 0; i < n; i++) {for (int j \= 0; j < m; j++) {btn[i][j].setBackground(Color.white);//Xét màu cho Jbutton tương ứng đang xét } }frame.setDefaultCloseOperation(EXIT_ON_CLOSE);frame.setVisible(true);panelCenter.setSize(700, 9000);panelEast.setSize(50, 9000);panelWest.setSize(50, 9000);frame.setSize(1500, 700);frame.setResizable(false);/*frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);*/ }/**/ JLabel lb1 \= new JLabel("Tọa độ: ");JLabel lb2 \= new JLabel("Tọa độ : ");/**/ //tim o trong de set text public void actionPerformed(ActionEvent e) {/**/ if (COUNT \== 0) {panelEast.add(lb1);lb1.setLocation(100, 100);panelWest.add(lb2);lb2.setLocation(100, 100); } else {panelEast.remove(lb1);panelWest.remove(lb2);panelEast.add(lb1);lb1.setLocation(100, 100);panelWest.add(lb2);lb2.setLocation(100, 100); }COUNT \= 1;/**/ for (int i \= 0; i < n; i++) {for (int j \= 0; j < m; j++) {if (e.getSource() == btn[i][j] && btn[i][j].getText() != "X" && btn[i][j].getText() != "O") {if (diem % 2 \== 0) {/**/ lb1.setText("Tọa độ : (" + i + ", " + j + ")");/**/ btn[i][j].setText("X");btn[i][j].setForeground(Color.RED);diem++;if (win(i, j, btn[i][j].getText())) {btn[i][j].setBackground(Color.red);JOptionPane.showMessageDialog(null, "X win!", "Game Over!", JOptionPane.INFORMATION_MESSAGE);for (int i1 \= 0; i1 < n; i1++) {for (int j1 \= 0; j1 < m; j1++) {btn[i1][j1].setText("");btn[i1][j1].setBackground(Color.white); } } } } else {/**/ lb2.setText("Tọa độ : (" + i + ", " + j + ")");/**/ btn[i][j].setText("O");btn[i][j].setForeground(Color.BLACK);diem++;if (win(i, j, btn[i][j].getText())) {btn[i][j].setBackground(Color.green);JOptionPane.showMessageDialog(null, "O win!", "Game Over!", JOptionPane.INFORMATION_MESSAGE);JOptionPane.showMessageDialog(null, "Trò Chơi Mới", "Thoát", JOptionPane.INFORMATION_MESSAGE);for (int i1 \= 0; i1 < n; i1++) {for (int j1 \= 0; j1 < m; j1++) {btn[i1][j1].setText("");btn[i1][j1].setBackground(Color.white); } } } }// JOptionPane.showMessageDialog(this, i + " " + j); } } } }//kiem tra thang public boolean win(int x, int y, String name) {int k, j;int d \= 0;// kt chieu doc for (k \= -4; k <= 4; k++) {if (x + k \>= 0 && x + k < n) {if (btn[x + k][y].getText() == name) {d++; } else if (d < 5) {d \= 0; } } }if (d \>= 5) {return true; } else {d \= 0; }//xet ngang for (k \= -5; k <= 5; k++) {if (y + k \>= 0 && y + k < n) {if (btn[x][y + k].getText() == name) {d++; } else if (d < 5) {d \= 0; } } }if (d \>= 5) {return true; } else {d \= 0; }//cheo for (k \= -4, j \= 4; k <= 4 && j \>= -4; k++, j--) {if (y + k \>= 0 && y + k < n && x + j \>= 0 && x + j < m) {if (btn[x + j][y + k].getText() == name) {d++; } else if (d < 5) {d \= 0; } } }if (d \>= 5) {return true; } else {d \= 0; }for (k \= -4; k <= 4; k++) {if (y + k \>= 0 && y + k < n && x + k \>= 0 && x + k < m) {if (btn[x + k][y + k].getText() == name) {d++; } else if (d < 5) {d \= 0; } } }if (d \>= 5) {return true; }return false; }public static void main(String[] args) {caro c \= new caro();c.add(); } }