The code below is a runnable Java program for the Tic-tac-Toe game(井字遊戲).You can copy the code and two images and establish a Java project to test on your own.
o.png and x.png:
package com.tictactoe; import java.awt.EventQueue; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JTable; import javax.swing.JButton; import javax.swing.border.EtchedBorder; import java.awt.Color; import java.awt.Font; import javax.swing.table.DefaultTableModel; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class TicTacToe extends JFrame { private static final long serialVersionUID = 1L; private JPanel contentPane; private static JTable table; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { TicTacToe frame = new TicTacToe(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } // When the value of count is even, it's player O's turn;otherwise, it's player X's turn; private int count = 0; private int[][] situation; // = new int[3][3]; //null empty; true:O // false: X private void setTable() { final Object[][] tableItems = new Object[][] { { null, null, null }, { null, null, null }, { null, null, null }, }; situation = new int[3][3]; table = new JTable(); table.setGridColor(new Color(255, 0, 0)); MouseAdapter mouseListener = new MouseAdapter() { /** * check if the current player wins * * @return */ private boolean checkWhetherTheCurrentPlayerWins() { System.out.println("Count = " + count); if (count <= 3) return false; int player = (count % 2 == 0)? 1 : 2; // check the two oblique lines if ( situation[1][1] == player) { if (situation[0][0] == player && situation[2][2] == player) return true; if (situation[0][2] == player && situation[2][0] == player) return true; } //check rows for (int row = 0; row < 3; row++) { if (situation[row][0] == player && situation[row][1] == player && situation[row][2] == player) return true; } // check columns for (int column = 0 ; column < 3; column ++){ if (situation[0][column] == player && situation[1][column] == player && situation[2][column] == player) return true; } return false; } @Override public void mousePressed(MouseEvent e) { JTable table = (JTable) e.getComponent(); int column = table.columnAtPoint(e.getPoint()); int row = table.rowAtPoint(e.getPoint()); if (table.getValueAt(row, column) != null) return; boolean OorNot = count % 2 == 0; situation[row][column] = OorNot ? 1 : 2; String imagePath = "images/" + (OorNot ? "o.png" : "x.png"); ImageIcon OorXIcon = new ImageIcon(imagePath); Image img = OorXIcon.getImage(); Image newImg = img.getScaledInstance(90, 90, java.awt.Image.SCALE_SMOOTH); DefaultTableModel tableModel = (DefaultTableModel) table .getModel(); tableModel.setValueAt(new ImageIcon(newImg), row, column); if (checkWhetherTheCurrentPlayerWins()){ JOptionPane.showMessageDialog(null, "Congratuations! Player " + (OorNot ? "O":"X") + " Wins."); restart(); return; } count++; } @Override public void mouseReleased(MouseEvent e) { ((JTable) e.getComponent()).putClientProperty("pressedCell", null); } }; table.addMouseListener(mouseListener); table.setModel(new DefaultTableModel(tableItems, new String[] { "0", "1", "2" }) { private static final long serialVersionUID = 1L; @Override public boolean isCellEditable(int row, int column) { return false; } }); table.setBackground(new Color(153, 255, 255)); table.setBorder(new EtchedBorder(EtchedBorder.RAISED, new Color(107, 142, 35), null)); table.setBounds(89, 70, 270, 270); for (int i = 0; i < 3; i++) { table.setRowHeight(i, 90); table.getColumnModel().getColumn(i) .setCellRenderer(new ImageRenderer()); } } // end of reset table private void restart(){ count = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) table.setValueAt(null, i, j); } situation = new int[3][3]; } /** * Create the frame. */ public TicTacToe() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 470); setTitle("Tic-Tac-Toe"); setResizable(false); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(null); setContentPane(contentPane); setTable(); contentPane.add(table); JButton btnNewButton = new JButton("Restart"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { restart(); } }); btnNewButton.setFont(new Font("SansSerif", Font.BOLD, 16)); btnNewButton.setBounds(180, 380, 90, 25); contentPane.add(btnNewButton); } }
No comments:
Post a Comment