java 一个类似于tic tac toe的小型Java游戏
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 一个类似于tic tac toe的小型Java游戏相关的知识,希望对你有一定的参考价值。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Capture {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Capture window = new Capture();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Capture() {
initialize();
}
public void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
final JPanel A = new JPanel();
final JPanel B = new JPanel();
final JPanel C = new JPanel();
final JPanel D = new JPanel();
final JPanel E = new JPanel();
final JPanel F = new JPanel();
final JPanel G = new JPanel();
final JPanel H = new JPanel();
final JPanel I = new JPanel();
A.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (A.getBackground()==Color.RED) {
A.setBackground(Color.BLUE);
computermove(A, B, C, D, E, F, G, H, I);
} else {
A.setBackground(Color.RED);
computermove(A, B, C, D, E, F, G, H, I);
}
}
});
A.setBackground(Color.RED);
A.setBounds(54, 27, 108, 103);
frame.getContentPane().add(A);
B.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (B.getBackground()==Color.RED) {
B.setBackground(Color.BLUE);
computermove(A, B, C, D, E, F, G, H, I);
} else {
B.setBackground(Color.RED);
computermove(A, B, C, D, E, F, G, H, I);
}
}
});
B.setBackground(Color.BLUE);
B.setBounds(162, 27, 119, 103);
frame.getContentPane().add(B);
C.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (C.getBackground()==Color.RED) {
C.setBackground(Color.BLUE);
computermove(A, B, C, D, E, F, G, H, I);
} else {
C.setBackground(Color.RED);
computermove(A, B, C, D, E, F, G, H, I);
}
}
});
C.setBackground(Color.RED);
C.setBounds(282, 27, 119, 103);
frame.getContentPane().add(C);
D.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (D.getBackground()==Color.RED) {
D.setBackground(Color.BLUE);
computermove(A, B, C, D, E, F, G, H, I);
} else {
D.setBackground(Color.RED);
computermove(A, B, C, D, E, F, G, H, I);
}
}
});
D.setBackground(Color.BLUE);
D.setBounds(54, 131, 108, 94);
frame.getContentPane().add(D);
E.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (E.getBackground()==Color.RED) {
E.setBackground(Color.BLUE);
computermove(A, B, C, D, E, F, G, H, I);
} else {
E.setBackground(Color.RED);
computermove(A, B, C, D, E, F, G, H, I);
}
}
});
E.setBackground(Color.RED);
E.setBounds(162, 131, 119, 94);
frame.getContentPane().add(E);
F.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (F.getBackground()==Color.RED) {
F.setBackground(Color.BLUE);
computermove(A, B, C, D, E, F, G, H, I);
} else {
F.setBackground(Color.RED);
computermove(A, B, C, D, E, F, G, H, I);
}
}
});
F.setBackground(Color.BLUE);
F.setBounds(282, 131, 119, 94);
frame.getContentPane().add(F);
G.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (G.getBackground()==Color.RED) {
G.setBackground(Color.BLUE);
computermove(A, B, C, D, E, F, G, H, I);
} else {
G.setBackground(Color.RED);
computermove(A, B, C, D, E, F, G, H, I);
}
}
});
G.setBackground(Color.RED);
G.setBounds(54, 225, 108, 103);
frame.getContentPane().add(G);
H.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (H.getBackground()==Color.RED) {
H.setBackground(Color.BLUE);
computermove(A, B, C, D, E, F, G, H, I);
} else {
H.setBackground(Color.RED);
computermove(A, B, C, D, E, F, G, H, I);
}
}
});
H.setBackground(Color.BLUE);
H.setBounds(162, 225, 119, 103);
frame.getContentPane().add(H);
I.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (I.getBackground()==Color.RED) {
I.setBackground(Color.BLUE);
computermove(A, B, C, D, E, F, G, H, I);
} else {
I.setBackground(Color.RED);
computermove(A, B, C, D, E, F, G, H, I);
}
}
});
I.setBackground(Color.RED);
I.setBounds(282, 225, 119, 103);
frame.getContentPane().add(I);
JLabel lblTictactoe = new JLabel("TTT-Remix");
lblTictactoe.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
lblTictactoe.setBounds(176, 6, 91, 16);
frame.getContentPane().add(lblTictactoe);
JButton btnNewGame = new JButton("New Game");
btnNewGame.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
A.setBackground(Color.RED);
B.setBackground(Color.BLUE);
C.setBackground(Color.RED);
D.setBackground(Color.BLUE);
E.setBackground(Color.RED);
F.setBackground(Color.BLUE);
G.setBackground(Color.RED);
H.setBackground(Color.BLUE);
I.setBackground(Color.RED);
}
});
btnNewGame.setBounds(162, 340, 117, 29);
frame.getContentPane().add(btnNewGame);
JTextArea textArea = new JTextArea();
textArea.setBounds(30, 382, 234, 79);
frame.getContentPane().add(textArea);
}
//computer strategy here
//called after all player moves, player always move first.
public static void computermove(JPanel one, JPanel two, JPanel three, JPanel four,
JPanel five, JPanel six, JPanel seven, JPanel eight, JPanel nine) {
if (two.getBackground()==Color.RED) {
one.setBackground(Color.BLUE);
}
else if (four.getBackground()==Color.RED) {
five.setBackground(Color.BLUE);
}
else if (four.getBackground()==Color.RED && five.getBackground()==Color.RED) {
four.setBackground(Color.BLUE);
}
else {
nine.setBackground(Color.BLUE);
}
}
}
以上是关于java 一个类似于tic tac toe的小型Java游戏的主要内容,如果未能解决你的问题,请参考以下文章
java 348.设计Tic-Tac-Toe(#1).java
java 348.设计Tic-Tac-Toe(#1).java
java 348.设计Tic-Tac-Toe(#1).java