📄 blackvswhite.java
字号:
package blackandwhite;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//将BlackVSWhite转化为线程,
//同时实现Runnable、ActionListener、
//MouseListener(鼠标事件监听器)等接口
public class BlackVSWhite extends Applet implements Runnable, ActionListener,
MouseListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private MediaTracker imageTracker;
// 媒体跟踪器,用来检测图像的装载
private Image redImage;
// 显示红色棋子的图像
private Image blueImage;
// 显示蓝色棋子的图像
private Image noneImage;
// 不能走的地方
private Image offScrImage;
// 屏幕缓冲
private Graphics offScrGraphics;
// 缓冲中的图像
private Font f;
// 显示文字的字体
private int fontHeight;
// 显示文字的高度
private int fontAscent;
// 显示文字的间距
private Button button;
// 按钮,用来开始新游戏
private Button nonedim;
// 按钮,用来提示设置障碍
private Checkbox yesorno;
//确定是否要随机生成棋盘
private Choice rannum;
//选择生成棋盘中已经有的棋子的个数
private Button regretbutton;
// 按钮,用来悔棋
private Button overButton;
// 按钮,当双方都没棋子下时用来结束游戏
private Button helpButton;
// 按钮,用来向电脑求助,并在聊天窗口输出。
private Button exitGame;
//按钮,用来退出游戏
private Button passButton;
//按钮,当人没法下时pass
private JScrollPane scrollPane;
private int buttonHeight;
// 按钮的高度
private Choice choice;
// 选择框,用于设置棋盘的格数
private Choice moveFirst;
// 选择框,用来确定先行的一方
private Choice difficulty;
// 选择框,选择电脑的难度
private TextArea text;
// 文本框,用来初始化棋盘
private int difnum;
// 决定选择哪种难度
private boolean redMoveFirst;
// 判断是否红方先行
private boolean illegalMove;
// 判断落子是否非法
private boolean blueNotMoving;
// 判断蓝方是否放弃这一步
private boolean animating;
// 判断格中棋子的显示是否要用动画效果
private int redScore;
// 红方的计分
private int blueScore;
// 蓝方的计分
private Thread aThread;
// 主线程
// 定义所需要的常量
private static final int EMPTY = 0;
// 格子空白
private static final int NONE = 1;
//格子不允许下子
private static final int RED = 2;
// 格中为红色棋子
private static final int BLUE = 3;
// 格中为蓝色棋子
// 格中为临时的蓝色棋子 private static final int BLUE_TEMP = 4;
// 格中为临时的红色棋子 private static final int RED_TEMP = 5;
private static int DIM = 10;
// 单方向上的格数
private int boardSize = 400;
// 棋盘的尺寸
private int[] grid = new int[400];
// 代表格子的数组
//下面是聊天程序模块
private Label sendlabel;
//聊天发送提示
private Label messageInfo;
//消息提示
private Button sendButton;
//聊天发送按钮
private Button clearButton;
//用来清空聊天消息
private TextArea sendText;
//需要发送的消息
private TextArea messageText;
//聊天记录
private String message;
//记录想要发送
private String cmessage;
//记录电脑想说的话
private int[] save = new int[400 * 400];
//二维数组,用来记录棋盘情况悔棋
private int times = 0;
//记录是第几次下棋
// 开始新游戏,并做一些初始化准备
private synchronized void newGame()
{
// 从列表框中获取棋盘的单方向的格数
DIM = (new Integer((String) choice.getSelectedItem())).intValue();
// 判断先行的一方
int whoMoveFirst = moveFirst.getSelectedIndex();
if (whoMoveFirst == 0)
redMoveFirst = true;// 红方先走
else
redMoveFirst = false;// 蓝方先走
//选择游戏的难度
int chooseTheDifficulty = difficulty.getSelectedIndex();
switch(chooseTheDifficulty)
{
case 0: difnum = 0;break;
case 1: difnum = 1;break;
case 2: difnum = 2;break;
case 3: difnum = 3;break;
}
//一般的初始化开始
nonedim.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//初始化两个变量,准备随机设置障碍
// 放置棋子
//读取棋盘的关键步骤
// 初始化棋盘
offScrGraphics.setColor(Color.white);
offScrGraphics.fillRect(0, 0, boardSize, boardSize);
// 初始化计分
redScore = blueScore = 0;
illegalMove = blueNotMoving = animating = false;
boardSize = -1;
repaint();// 重新绘制页面
// 如果是电脑先走
if (!redMoveFirst)
{
switch(difnum)
{
case 0:blueMove0();break;
case 1:blueMove1();break;
case 2:blueMove2();break;
case 3:blueMove3();break;
}
// 选择电脑的难度
checkScore();// 计算双方计分
repaint();// 重新绘制页面
}
}
});
// 游戏开始,先用两个红棋和两个蓝棋占据中间的四个格子
int n1;
int n2;
for (n1 = 0; n1 < DIM; ++n1)
{
for (n2 = 0; n2 < DIM; ++n2)
grid[n1 * DIM + n2] = EMPTY;
}
n2 = DIM / 2;
// 放置棋子
//读取棋盘的关键步骤
grid[(n2 - 1) * DIM + (n2 - 1)] = RED;
grid[(n2 - 1) * DIM + n2] = BLUE;
grid[n2 * DIM + (n2 - 1)] = BLUE;
grid[n2 * DIM + n2] = RED;
//随机设置障碍,开始设置棋子和障碍
if(yesorno.getState() == true)
{
int num = rannum.getSelectedIndex(); // 2 4 8 16 32 64
int toranx = 0;
int torany = 0;
int total = 0;
switch(num)
{
case 0:
{
//如果还没设置满
while(total != 2)
{
toranx = (int)(Math.random() * DIM);
torany = (int)(Math.random() * DIM);
if(grid[toranx * (DIM - 1) + torany] == 0)
{
grid[toranx * (DIM - 1) + torany] = NONE;
total++;
}
}
break;
}
case 1:
{
while(total != 4)
{
toranx = (int)(Math.random() * DIM);
torany = (int)(Math.random() * DIM);
if(grid[toranx * (DIM - 1) + torany] == 0)
{
grid[toranx * (DIM - 1) + torany] = NONE;
total++;
}
}
break;
}
case 2:
{
while(total != 8)
{
toranx = (int)(Math.random() * DIM);
torany = (int)(Math.random() * DIM);
if(grid[toranx * (DIM - 1) + torany] == 0)
{
grid[toranx * (DIM - 1) + torany] = NONE;
total++;
}
}
break;
}
case 3:
{
while(total != 16)
{
toranx = (int)(Math.random() * DIM);
torany = (int)(Math.random() * DIM);
if(grid[toranx * (DIM - 1) + torany] == 0)
{
grid[toranx * (DIM - 1) + torany] = NONE;
total++;
}
}
break;
}
case 4:
{
while(total != 32)
{
toranx = (int)(Math.random() * DIM);
torany = (int)(Math.random() * DIM);
if(grid[toranx * (DIM - 1) + torany] == 0)
{
grid[toranx * (DIM - 1) + torany] = NONE;
total++;
}
}
break;
}
case 5:
{
while(total != 64)
{
toranx = (int)(Math.random() * DIM);
torany = (int)(Math.random() * DIM);
if(grid[toranx * (DIM - 1) + torany] == 0)
{
grid[toranx * (DIM - 1) + torany] = NONE;
total++;
}
}
break;
}
}
}
//因为设置障碍后
//电脑走棋的算法也要改
// 初始化棋盘
offScrGraphics.setColor(Color.white);
offScrGraphics.fillRect(0, 0, boardSize, boardSize);
// 初始化计分
redScore = blueScore = 0;
illegalMove = blueNotMoving = animating = false;
boardSize = -1;
repaint();// 重新绘制页面
// 如果是电脑先走
if (!redMoveFirst)
{
switch(difnum)
{
case 0:blueMove0();break;
case 1:blueMove1();break;
case 2:blueMove2();break;
case 3:blueMove3();break;
}
// 选择电脑的难度
checkScore();// 计算双方计分
repaint();// 重新绘制页面
}
}
// BlackVSWhite 类的初始化
public synchronized void init()
{
// 一些变量和对象的初始化
aThread = null;
imageTracker = new MediaTracker(this);
offScrImage = createImage(400, 400);
offScrGraphics = offScrImage.getGraphics();
f = new Font("TimesRoman", 0, 12);
offScrGraphics.setFont(f);
fontHeight = getFontMetrics(f).getHeight();
fontAscent = getFontMetrics(f).getAscent();
loadImages();// 图像的装载
if (button == null)
{
button = new Button("新游戏");
button.addActionListener(this);// 为按钮设置事件监听器
button.setBounds(10, 15, 50, 20);
setLayout(null);
setSize(600,400);
add(button);
//用来读取自己设置的棋盘
nonedim = new Button("设置障碍");
add(nonedim);
nonedim.setBounds(76, 15, 60, 20);
//确定是否要设置障碍
yesorno = new Checkbox();
yesorno.setBounds(150, 10, 30, 30);
add(yesorno);
rannum = new Choice();
rannum.setBounds(180, 15, 70, 10);
rannum.addItem("2");
rannum.addItem("4");
rannum.addItem("8");
rannum.addItem("16");
rannum.addItem("32");
rannum.addItem("64");
add(rannum);
// 建立用于设置格数的列表框,初始化为6*6格
choice = new Choice();
choice.addItem("6");
choice.addItem("8");
choice.addItem("10");
choice.addItem("12");
choice.addItem("14");
add(choice);
choice.select("6");
choice.setBounds(270, 15, 60, 30);
}
if (moveFirst == null)
{
// 建立用于设置先行方的列表框,初始化为“玩家先走”
moveFirst = new Choice();
moveFirst.addItem("玩家先走");
moveFirst.addItem("电脑先走");
add(moveFirst);
moveFirst.select(0);
redMoveFirst = true;
}
moveFirst.setBounds(340, 15, 80, 30);
if(difficulty == null)
{
//用来选择游戏的难度
difficulty = new Choice();
difficulty.addItem("菜鸟");
difficulty.addItem("初学者");
difficulty.addItem("高手");
difficulty.addItem("专家");
add(difficulty);
difficulty.select(3);
}
difficulty.setBounds(430, 15, 60, 30);
//用来悔棋的按钮
regretbutton = new Button("悔棋");
button.addActionListener(this);
add(regretbutton);
regretbutton.setBounds(500, 15, 50, 20);
//悔棋的行为
regretbutton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//首先记录每一步
//然后每按一次就回一步
//然后拷贝过数组
//重新绘图和刷新
times += 1;
System.arraycopy(save, 0, grid, 0, 400);
checkScore();
paint(getGraphics());
cmessage = "电脑:悔棋的不是好孩子!";
messageText.setText(messageText.getText() + "\n" + cmessage);
//悔棋太多次电脑就不玩了
if(times >= 20)
{
cmessage = "电脑:你悔棋太多次了,我不和你玩了,重来吧!";
messageText.setText(messageText.getText() + "\n" + cmessage);
//让主线程延迟一下
try
{
Thread.sleep(5000L);
}
catch (Exception unused9)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -