因为假期内被朋友带入坑后起了兴趣,但发现网上似乎没有什么人写过国际跳棋的相关制作过程,于是制作了一个单纯的java的国际跳棋程序,虽然没有AI,但能够实现玩家双方的任务和皮肤(目前只设置了四个国家)选择,同时也增加了相关声效。
对局时,棋子的原始摆法为:20枚黑兵排列在已方后四排的黑格内,白方棋子同黑,黑棋摆在1到20棋位,白棋摆在31到50棋位。经过一段对局,任何一方的兵冲破重重障碍,走至并停留在对方底线,即升变为王棋,如果没有停留在对方底线不能立即成王。王棋可以用两个兵摞起来表示,也可以将兵翻转过来做王棋。
兵与王棋的走法: 对局开始,由执白棋者先行,然后由黑棋行棋;双方轮流走子,直到终局。
白、黑各走一着叫一个回合。对局开始时,出现在棋盘上的都是兵。
兵在走棋时,每步棋只能向前方邻近的空棋位上向左或向右移动一格,并且只能前进,不允许后退。
兵的吃子法是用跳的形式进行的。这和一般跳棋的走法相似,只要自己的一个兵与对方的一枚棋子相遇,并且与这两枚棋子成一斜行的、紧挨着对方棋子的棋位是空着的,那么,轮至走子的一方就要用自己的兵跳过对方的棋子,放在紧挨着对方棋子后面的空棋位上,将对方的那枚棋子吃掉。吃子时,可以象普通跳棋一样一次连跳连吃几枚棋子,但连跳时不允许以自己的棋子为桥梁,也就是说,自己的棋子不能从自己的棋子上越过去再去吃对方的棋子。兵吃子时可以后退。
王棋的走法与兵不同,它可以前进,可以后退;有些规则中,只要在一条斜线上,一次移动几格都可能。有些规则则没有这些规定,一次仍然走一格。王棋的跳吃,也比兵的跳吃自由度要大得多。有些规则中,只要在同一斜线上,不管距离多么远,都可以跳过对方的这枚棋子,停在它后面的任何一个空格里,从而将对方这枚棋子吃掉。有些规则则没有这些规定,跳吃除了可进可退和兵一样。王棋的连跳,与兵的连跳大致相同,只是有些规则不限距离;只要有机会,一次可以跳吃对方的数枚棋子。
吃子还有三条重要规则:第一,能吃子必须吃子,不能不吃;第二,能多吃子,必须多吃,不能少吃;第三,能吃子的时候必须吃到底,不许半路停下不再吃了.以上规则无论是否对自己有利都必须执行.
如果将对方的棋子吃光或者让对方没有可以走的棋了即为获胜。
大概包含7个小类用以实现不同相关功能
public class UI {
JFrame jiemian = new JFrame();
qipan qi = new qipan();
Graphics g;
int l = 1;
JButton lun = new JButton();
JPanel jj = new JPanel();
int[] se = new int[2];// 双方的皮肤
String[] guojia = { "德国", "法国", "美国", "巴西" };
public void show() {
xuanze k = new xuanze();
k.startx = qi.startx;
k.starty = qi.starty;
k.size = qi.size;
k.qi = qi;
k.lun = lun;
k.f = jiemian;
k.qw[0] = guojia[se[0]];
k.qw[1] = guojia[se[1]];// 对战双方的名字
qi.se[0] = se[0];
qi.se[1] = se[1];// 双方皮肤颜色
lun.setText(guojia[se[0]]);
lun.setBackground(new Color(163, 184, 204));
lun.setFont(new Font("华文新魏", Font.PLAIN, 40));
lun.addMouseListener(k);
qi.addMouseListener(k);
qi.yuchuli();// 预处理棋盘;
jj.add(lun);
jiemian.add(jj, BorderLayout.NORTH);
jiemian.add(qi, BorderLayout.CENTER);
g = qi.getGraphics();
k.g = qi.getGraphics();
jiemian.setSize(1000, 1000);
jiemian.setVisible(true);
jiemian.setLocationRelativeTo(null);
}
public static void main(String[] args) {
huangfu h = new huangfu();
h.choose();
}
}
public class huangfu {
JFrame f=new JFrame();
JPanel g=new JPanel();
JPanel k2=new JPanel();
pifu gg=new pifu();
String [] g2= {"德国","法国","美国","巴西"};
public void choose(){
f.setVisible(true);
f.setSize(200, 200);
f.add(g,BorderLayout.CENTER);
f.add(k2,BorderLayout.NORTH);
JButton h=new JButton();
k2.add(h);//添加按钮
h.setText("选择先手P1国家");
gg.h=h;
f.setLocationRelativeTo(null);
gg.f=f;
for(int i=0;i<g2.length;i++)
{
JButton j=new JButton(g2[i]);
g.add(j);
j.addActionListener(gg);
}
}
}
3.皮肤
主要作为huangfu类中界面添加的按钮的监听器,用来通过按钮的选择改变棋盘绘制的相关类的相关值,从而来进行皮肤选择
public class pifu implements ActionListener {
UI u = new UI();
JFrame f = new JFrame();
JButton h = new JButton();
int flag = 0;
public void actionPerformed(ActionEvent e) {
String a = e.getActionCommand();
if (a.equals("德国")) {
u.se[flag] = 0;//此数组代表的是颜色编号
flag++;
}
if (a.equals("法国")) {
u.se[flag] = 1;
flag++;
}
if (a.equals("美国")) {
u.se[flag] = 2;
flag++;
}
if (a.equals("巴西")) {
u.se[flag] = 3;
flag++;
}
if (flag == 2) {
f.setVisible(false);
u.show();// 选择完皮肤后开始进行游戏;
} else {
h.setText("选择后手P2国家");
}
}
}
4.qipan
该类主要进行棋盘相关数据的预处理与通过重写其重绘代码来实现棋盘的实时变化
public class qipan extends JPanel {
int[][] pan = new int[40][40];
int[][] kk = new int[40][40];
int[][] wan = new int[40][40];// 记录王的状态
int startx = 130, starty = 70, size = 70;
int[] se = new int[2];// 双方的皮肤
Image[] putong = new Image[10];
Image[] wangqi = new Image[10];
String[] tu = { "C:\\Users\\lenovo\\Desktop\\素材\\德国.png", "C:\\Users\\lenovo\\Desktop\\素材\\法国.png",
"C:\\Users\\lenovo\\Desktop\\素材\\美国.png", "C:\\Users\\lenovo\\Desktop\\素材\\巴西.png" };
String[] tu2 = { "C:\\Users\\lenovo\\Desktop\\素材\\德.png", "C:\\Users\\lenovo\\Desktop\\素材\\法.png",
"C:\\Users\\lenovo\\Desktop\\素材\\美.png", "C:\\Users\\lenovo\\Desktop\\素材\\巴.png" };
public void yuchuli() {
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++) {
pan[j][i] = 0;
kk[i][j] = 0;
wan[i][j] = 0;
}
for (int i = 1; i <= 4; i++)
for (int j = i % 2 + 1; j <= 10; j += 2)
pan[j][i] = 1;// 黑子
for (int i = 7; i <= 10; i++)
for (int j = i % 2 + 1; j <= 10; j += 2)
pan[j][i] = 2;// 白子
for (int i = 1; i <= 10; i++)
for (int j = i % 2 + 1; j <= 10; j += 2) {
kk[i][j] = 1;
}
for (int i = 0; i < tu.length; i++) {
ImageIcon g = new ImageIcon(tu[i]);
putong[i] = g.getImage();
}
for (int i = 0; i < tu2.length; i++) {
ImageIcon g = new ImageIcon(tu2[i]);
wangqi[i] = g.getImage();
} // 初始化皮肤
}// 预处理棋盘
public void paint(Graphics g) {
super.paint(g);
ImageIcon ol = new ImageIcon("C:\\Users\\lenovo\\Desktop\\素材\\背景.jpg");
Image ii = ol.getImage();
g.drawImage(ii, 0, 0, this.getWidth(), this.getHeight(), null);
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j += 1) {
int placex = startx + (i - 1) * size;
int placey = starty + (j - 1) * size;
Color a = new Color(224, 226, 224);
g.setColor(a);
g.fillRect(placex, placey, size, size);
if (kk[i][j] == 1) {// 褐色格
a = new Color(200, 140, 109);
g.setColor(a);
g.fillRect(placex, placey, size, size);
}
if (kk[i][j] == 3) {// 被选定后
a = new Color(247, 100, 46);
g.setColor(a);
g.fillRect(placex, placey, size, size);
}
if (kk[i][j] == 4) {// 必选的棋子
a = new Color(249, 190, 40);
g.setColor(a);
g.fillRect(placex, placey, size, size);
}
if (kk[i][j] == 5) {// 能选的棋子
a = new Color(249, 190, 40);
g.setColor(a);
g.fillRect(placex, placey, size, size);
}
} // kk值代表各表格不同的状态
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++) {
if (pan[i][j] == 1 && wan[i][j] == 0) {
int placex = startx + (i - 1) * size;
int placey = starty + (j - 1) * size;
g.drawImage(putong[se[1]], placex + 4, placey + 4, size - 8, size - 8, null);
}
if (pan[i][j] == 2 && wan[i][j] == 0) {
int placex = startx + (i - 1) * size;
int placey = starty + (j - 1) * size;
g.drawImage(putong[se[0]], placex + 4, placey + 4, size - 8, size - 8, null);
}
if (wan[i][j] == 1 && pan[i][j] == 1) {
int placex = startx + (i - 1) * size;
int placey = starty + (j - 1) * size;
g.drawImage(wangqi[se[1]], placex + 4, placey + 4, size - 8, size - 8, null);
} // 王棋
if (wan[i][j] == 1 && pan[i][j] == 2) {
int placex = startx + (i - 1) * size;
int placey = starty + (j - 1) * size;
g.drawImage(wangqi[se[0]], placex + 4, placey + 4, size - 8, size - 8, null);
} // 王棋
} // 画棋子
for (int i = 1; i <= 11; i++) {
Color a = new Color(27, 28, 29);
g.setColor(a);
g.drawLine(startx + (i - 1) * size, starty, startx + (i - 1) * size, starty + 10 * size);
g.drawLine(startx, starty + (i - 1) * size, startx + 10 * size, starty + (i - 1) * size);
} // 画线
}
}
5.xuanze
对棋盘鼠标操作的监听器,用于获取点击位置来对棋盘棋子信息进行选择,从而来进行状态变化
public class xuanze implements MouseListener {
int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
caozuo k = new caozuo();
int startx, starty, size;
int cc, qq, ci = 0, flag = 0;// flag判断是不是出现了必吃;
qipan qi = new qipan();
Graphics g;
JButton lun = new JButton();
JFrame f = new JFrame();
String[] qw = new String[2];
public void mouseClicked(MouseEvent e) {
k.qi = this.qi;
k.yupan(ci);
if (x1 == 0) {
x1 = (e.getX()/* 获取的x值 */ - startx) / size + 1;
y1 = (e.getY()/* 获取的y值 */ - starty) / size + 1;// 在棋盘中的行列位置
System.out.println(x1 + " " + y1);
if (flag == 1 && qi.kk[x1][y1] != 4)// 有吃必选能吃子的那个
x1 = y1 = 0;
else if (flag == 0 && qi.kk[x1][y1] != 5 || qi.pan[x1][y1] % 2 != ci % 2 || qi.pan[x1][y1] == 0)// 无效选择;
x1 = y1 = 0;
else {
qq = qi.kk[x1][y1];
qi.kk[x1][y1] = 3;// 标记为选择状态;
qi.repaint();
}
} else {
cc = 0;
x2 = (e.getX() - startx) / size + 1;
y2 = (e.getY() - starty) / size + 1;
if (x2 != x1 && y2 != y1) {
cc = k.tiao(x1, y1, x2, y2);
if (cc == 2)
x1 = y1 = x2 = y2 = 0;
else {// 能跳则清空
qi.kk[x1][y1] = qq;
int lianchi = 0;
if (cc == 3) {
lianchi = k.pand(x2, y2);
ci += lianchi;
}
if (lianchi == 0)
x1 = x2 = y1 = y2 = 0;
else// 确定可以连吃后就不需要再点击同一个棋子了。
{
flag = k.yupan(ci);
x1 = x2;
y1 = y2;
x2 = y2 = 0;
}
ci++;
ci %= 2;
lun.setText(qw[ci]);// 更新轮盘状态
}
flag = k.yupan(ci);
if (flag == -9) {
int result;
if (ci == 0)
result = JOptionPane.showConfirmDialog(qi, qw[1] + "胜利,游戏结束,你想继续吗?", "友情提示",
JOptionPane.YES_NO_OPTION);
else
result = JOptionPane.showConfirmDialog(qi, qw[0] + "胜利,游戏结束,你想继续吗?", "友情提示",
JOptionPane.YES_NO_OPTION);
System.out.println("result=" + result);
if (result == 0) {
qi.yuchuli();
} else {
chengfa cc = new chengfa();
f.setVisible(false);// 关闭界面
cc.cf();
}
}
qi.repaint();// 重新绘制
}
if (x2 == x1 && y2 == y1)// 取消操作
{
qi.kk[x1][y1] = qq;
x1 = x2 = y1 = y2 = 0;
qi.repaint();
}
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
6.caozuo
用于对xuanze类的选择棋子进行可行性的判断与状态的更改,从而实现棋盘的动态变化,来使游戏进行
public class caozuo {
qipan qi = new qipan();
int flag = 0;
int[][] lj = { { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } };// 位置
public int yupan(int x) {// 需满足能吃则吃
if (x == 0)
x = 2;
flag = 0;
for (int i = 1; i <= 10; i++) {
if (qi.pan[i][1] == 2 && qi.wan[i][1] == 0) {
qi.wan[i][1] = 1;
File f = new File("C:\\Users\\lenovo\\Desktop\\素材\\p1变王.wav");
URL sy;
try {
sy = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(sy);// 播放声音文件
aau.play();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (qi.pan[i][10] == 1 && qi.wan[i][10] == 0) {
qi.wan[i][10] = 1;
File f = new File("C:\\Users\\lenovo\\Desktop\\素材\\p2变王.wav");
URL sy;
try {
sy = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(sy);// 播放声音文件
aau.play();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
for (int i = 1; i <= 10; i++)// 王的有子必吃判定
for (int j = 1; j <= 10; j++) {
if (qi.wan[i][j] == 1 && qi.pan[i][j] == x) {
for (int ci = i - 1, ci2 = j - 1; ci > 1 && ci2 > 1; ci--, ci2--) {
if (qi.pan[ci][ci2] == qi.pan[i][j]) {
break;
}
if (qi.pan[ci][ci2] == 3 - qi.pan[i][j]) {
if (qi.pan[ci - 1][ci2 - 1] == 0) {
qi.kk[i][j] = 4;
flag = 1;
}
break;
}
}
for (int ci = i - 1, ci2 = j + 1; ci > 1 && ci2 < 10; ci--, ci2++) {
if (qi.pan[ci][ci2] == qi.pan[i][j]) {
break;
}
if (qi.pan[ci][ci2] == 3 - qi.pan[i][j]) {
if (qi.pan[ci - 1][ci2 + 1] == 0) {
qi.kk[i][j] = 4;
flag = 1;
}
break;
}
}
for (int ci = i + 1, ci2 = j - 1; ci < 10 && ci2 > 1; ci++, ci2--) {
if (qi.pan[ci][ci2] == qi.pan[i][j]) {
break;
}
if (qi.pan[ci][ci2] == 3 - qi.pan[i][j]) {
if (qi.pan[ci + 1][ci2 - 1] == 0) {
qi.kk[i][j] = 4;
flag = 1;
}
break;
}
}
for (int ci = i + 1, ci2 = j + 1; ci < 10 && ci2 < 10; ci++, ci2++) {
if (qi.pan[ci][ci2] == qi.pan[i][j]) {
break;
}
if (qi.pan[ci][ci2] == 3 - qi.pan[i][j]) {
if (qi.pan[ci + 1][ci2 + 1] == 0) {
qi.kk[i][j] = 4;
flag = 1;
}
break;
}
}
}
}
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++) {
int dd = 0;
if (qi.wan[i][j] == 0 || qi.wan[i][j] == 1 && qi.pan[i][j] != x) {// 非本方王棋的其他棋子的能吃判定
if (qi.pan[i][j] == x) {
for (int k = 0; k <= 3; k++)// 判断必吃
{
if (i + lj[k][0] <= 9 && i + lj[k][0] >= 2 && j + lj[k][1] <= 9 && j + lj[k][1] >= 2)// 判断棋子的范围
if (qi.pan[i + lj[k][0]][j + lj[k][1]] != 0
&& qi.pan[i + lj[k][0]][j + lj[k][1]] != qi.pan[i][j]
&& qi.pan[i + lj[k][0] * 2][j + lj[k][1] * 2] == 0) {
qi.kk[i][j] = 4;
flag = 1;
dd = 1;
}
}
}
if (dd == 0) {
qi.kk[i][j] = (i + j) % 2;
}
}
}
if (flag == 0) {
int ll = -9;
for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++) {
if (qi.pan[i][j] == x) {
for (int k = 0; k <= 3; k++)// 判断能动子
{
if (i + lj[k][0] <= 10 && i + lj[k][0] >= 1 && j + lj[k][1] <= 10 && j + lj[k][1] >= 1)// 判断棋子的范围
if (qi.pan[i + lj[k][0]][j + lj[k][1]] == 0) {
qi.kk[i][j] = 5;
ll = 0;
}
}
}
}
flag = ll;
}
qi.repaint();
return flag;
}
public int pand(int x, int y) {
if (qi.wan[x][y] == 0)
for (int i = 0; i <= 3; i++)// 判断连吃
{
if (x + lj[i][0] <= 9 && x + lj[i][0] >= 2 && y + lj[i][1] <= 9 && y + lj[i][1] >= 2)// 判断棋子的范围
if (qi.pan[x + lj[i][0]][y + lj[i][1]] != 0 && qi.pan[x + lj[i][0]][y + lj[i][1]] != qi.pan[x][y]
&& qi.pan[x + lj[i][0] * 2][y + lj[i][1] * 2] == 0) {// 判断有没有能连吃的
return -1;
}
}
else {// 王棋的连吃
for (int ci = x - 1, ci2 = y - 1; ci > 1 && ci2 > 1; ci--, ci2--) {
if (qi.pan[ci][ci2] == qi.pan[x][y]) {
break;
}
if (qi.pan[ci][ci2] == 3 - qi.pan[x][y]) {
if (qi.pan[ci - 1][ci2 - 1] == 0) {
return -1;
}
break;
}
}
for (int ci = x - 1, ci2 = y + 1; ci > 1 && ci2 < 10; ci--, ci2++) {
if (qi.pan[ci][ci2] == qi.pan[x][y]) {
break;
}
if (qi.pan[ci][ci2] == 3 - qi.pan[x][y]) {
if (qi.pan[ci - 1][ci2 + 1] == 0) {
return -1;
}
break;
}
}
for (int ci = x + 1, ci2 = y - 1; ci < 10 && ci2 > 1; ci++, ci2--) {
if (qi.pan[ci][ci2] == qi.pan[x][y]) {
break;
}
if (qi.pan[ci][ci2] == 3 - qi.pan[x][y]) {
if (qi.pan[ci + 1][ci2 - 1] == 0) {
return -1;
}
break;
}
}
for (int ci = x + 1, ci2 = y + 1; ci < 10 && ci2 < 10; ci++, ci2++) {
if (qi.pan[ci][ci2] == qi.pan[x][y]) {
break;
}
if (qi.pan[ci][ci2] == 3 - qi.pan[x][y]) {
if (qi.pan[ci + 1][ci2 + 1] == 0) {
return -1;
}
break;
}
}
}
return 0;
}
/*
* public int tiao(int x,int y,int x2,int y2,int[][] data){
*
* return 0; }
*/
public int tiao(int x, int y, int x2, int y2) {
if (qi.pan[x][y] == 0 || qi.pan[x2][y2] != 0 || Math.abs(x2 - x) != Math.abs(y2 - y)) {
System.out.println("wrong");// 不能选没有子的格为起点,不能选非对角线线上的和有子的格子
return 2;
} else {
if (qi.pan[x][y] == 1 || qi.pan[x][y] == 2) {
if (Math.abs(x2 - x) == 1) {
if (qi.kk[x][y] != 4/* 必须吃子的棋不能直跳一格 */ && (qi.wan[x][y] == 1 || qi.pan[x][y] == 1 && y2 - y == 1
|| qi.pan[x][y] == 2 && y2 - y == -1)) {// 棋子只能前进,不能后退,但王可以
qi.pan[x2][y2] = qi.pan[x][y];
qi.pan[x][y] = 0;
qi.wan[x2][y2] = qi.wan[x][y];
qi.wan[x][y] = 0;
File f = new File("C:\\Users\\lenovo\\Desktop\\素材\\下棋.wav");// 下棋音效
URL sy;
try {
sy = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(sy);// 播放声音文件
aau.play();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("wrong");
return 2;
}
} else if (qi.wan[x][y] != 1 && Math.abs(x2 - x) == 2) {
if (qi.pan[(x + x2) / 2][(y + y2) / 2] == qi.pan[x][y] || qi.pan[(x + x2) / 2][(y + y2) / 2] == 0) {
System.out.println("wrong");
return 2;
} else {
qi.pan[x2][y2] = qi.pan[x][y];
qi.pan[x][y] = 0;
qi.pan[(x + x2) / 2][(y + y2) / 2] = 0;
qi.wan[x2][y2] = qi.wan[x][y];
qi.wan[x][y] = 0;
qi.wan[(x + x2) / 2][(y + y2) / 2] = 0;
File f = new File("C:\\Users\\lenovo\\Desktop\\素材\\吃子.wav");
URL sy;
try {
sy = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(sy);// 播放声音文件
aau.play();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 3;// 吃子状态返回
}
} else if (qi.wan[x][y] == 1) {// 王棋随心所欲
int xz = 1, yz = 1, ci = qi.pan[x][y], ff = 0;
if (x2 < x)
xz = -1;
if (y2 < y)
yz = -1;
for (int i = 1; i < Math.abs(x2 - x); i++) {
if (qi.pan[x + i * xz][y + i * yz] == ci) {
System.out.println("wrong");// 路上不能出现自己的子
return 2;
}
if (qi.pan[x + i * xz][y + i * yz] == 3 - ci) {
ff++;
if (ff >= 2) {
System.out.println("wrong");// 中间不能出现两颗其他的子
return 2;
}
}
}
if (qi.kk[x][y] == 4 && ff != 1)// 必吃却没吃
{
System.out.println("wrong");
return 2;
}
for (int i = 1; i < Math.abs(x2 - x); i++) {
qi.pan[x + i * xz][y + i * yz] = 0;
qi.wan[x + i * xz][y + i * yz] = 0;// 可以则清空路上的子
}
qi.pan[x2][y2] = qi.pan[x][y];
qi.pan[x][y] = 0;
qi.wan[x2][y2] = qi.wan[x][y];
qi.wan[x][y] = 0;
if (ff != 1) {
File f = new File("C:\\Users\\lenovo\\Desktop\\素材\\下棋.wav");
URL sy;
try {
sy = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(sy);// 播放声音文件
aau.play();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ff == 1)// 吃了子要返回吃子状态,以便判断连吃
{
File f = new File("C:\\Users\\lenovo\\Desktop\\素材\\吃子.wav");
URL sy;
try {
sy = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(sy);// 播放声音文件
aau.play();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 3;
}
} else {
System.out.println("wrong");
return 2;
}
}
}
return 0;// 跳棋状态返回
}
}
7.chengfa
游戏嘛…输了当然要有惩罚2333,该类主要就是来进行惩罚内容的决定
public class chengfa {
JFrame cc = new JFrame();
JPanel c1 = new JPanel();
JButton c2 = new JButton();
String [] f={"说说自己的感情经历","俯卧撑20个","向左边第一位同性告白233","深蹲40次"};
public void cf(){
cc.setSize(500,150);
cc.setVisible(true);
cc.setLocationRelativeTo(null);
cc.add(c1,BorderLayout.CENTER);
c1.add(c2);
int gg1=f.length;
int gg2 =(int)(Math.random()*gg1);
c2.setBackground(new Color(163, 184, 204));
c2.setFont(new Font("华文新魏", Font.PLAIN, 40));
c2.setText(f[gg2]);
}
}