java五子棋游戏

来源:互联网 发布:mac风扇声音大 编辑:程序博客网 时间:2024/04/29 07:40

FiveChessMain.java

package org.myFiveChess;public class FiveChessMain {       public static void main(String args[]){       FiveChessFrame n = new FiveChessFrame();       }}


 

FiveChessFrame.java

package org.myFiveChess;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Toolkit;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JOptionPane;public class FiveChessFrame extends JFrame implements MouseListener,Runnable {BufferedImage imageBack = null;BufferedImage imageStart = null;BufferedImage imageSet = null;BufferedImage imageIntro = null;BufferedImage imageGiveup = null;BufferedImage imageAbout = null;BufferedImage imageEnd = null;Font font = new Font("宋体", Font.CENTER_BASELINE, 25);int x = 0, y = 0;int[][] allChess = new int[19][19];boolean isBlack = true;boolean canPlay = true;String message = "黑方先行";    String blackMessage = "时间无限制";    String whiteMessage = "时间无限制";    //设置黑棋剩余时间int blackTime = 0;//设置白棋剩余时间int whiteTime = 0;//保存最多有多少时间int maxTime = 0;//做倒计时的线程类      Thread  t = new Thread(this);        public FiveChessFrame() {this.setTitle("五子棋"); // 设置窗口标题this.setSize(500, 500); // 这个是根据 图片进行设置的 窗口大小int w = Toolkit.getDefaultToolkit().getScreenSize().width;int h = Toolkit.getDefaultToolkit().getScreenSize().height;String strBack = "C:/Users/guoximing/Pictures/five2.jpg";String strStart = "C:/Users/guoximing/Pictures/startGame.jpg";String strSet = "C:/Users/guoximing/Pictures/setGame.jpg";String strIntro = "C:/Users/guoximing/Pictures/introGame.jpg";String strGiveup = "C:/Users/guoximing/Pictures/giveupGame.jpg";String strAbout = "C:/Users/guoximing/Pictures/aboutGame.jpg";String strEnd = "C:/Users/guoximing/Pictures/end.jpg";try {imageBack = ImageIO.read(new File(strBack));imageStart = ImageIO.read(new File(strStart));imageSet = ImageIO.read(new File(strSet));imageIntro = ImageIO.read(new File(strIntro));imageGiveup = ImageIO.read(new File(strGiveup));imageAbout = ImageIO.read(new File(strAbout));imageEnd = ImageIO.read(new File(strEnd));} catch (IOException e) {System.out.println("未找到文件");e.printStackTrace();}this.setLocation((w - 500) / 2, (h - 500) / 2); // 设置窗口位置this.setResizable(false); // 设置为窗口大小不可变 由于图片大小限制this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 将窗体的关闭方式设置为默认关闭之后程序结束this.addMouseListener(this);this.setVisible(true); // 将窗体显示出来}public void paint(Graphics g) {BufferedImage bi = new BufferedImage(500, 500,BufferedImage.TYPE_INT_ARGB);Graphics g2 = bi.createGraphics();g2.drawImage(imageBack, 0, 17, 500, 481, this);// g.setColor(Color.GRAY);// g.fillRect(0, 0, 500, 500);g2.setColor(Color.GREEN);g2.setFont(font);g2.drawString("游戏信息:" + message, 10, 70);// 难不成指的是左下角的位置g2.setFont(new Font("宋体", Font.CENTER_BASELINE, 25));g2.setColor(Color.GREEN);g2.drawString("黑方时间", 10, 490);g2.drawString("白方时间", 250, 490);g2.setColor(Color.RED);g2.setFont(new Font("宋体", Font.CENTER_BASELINE, 20));g2.drawString(blackMessage,120,490);g2.drawString(whiteMessage, 380, 490);g2.drawImage(imageStart, 380, 90, 110, 40, this);g2.drawImage(imageSet, 380, 140, 110, 40, this);g2.drawImage(imageIntro, 380, 190, 110, 40, this);g2.drawImage(imageGiveup, 380, 310, 110, 40, this);g2.drawImage(imageAbout, 380, 360, 110, 40, this);g2.drawImage(imageEnd, 380, 410, 110, 40, this);g2.setColor(Color.GRAY);g2.fillRect(10, 90, 360, 360);g2.setColor(Color.BLACK);for (int i = 0; i <= 18; i++)g2.drawLine(10, i * 20 + 90, 370, i * 20 + 90);g2.setColor(Color.BLACK);for (int i = 0; i <= 18; i++)g2.drawLine(i * 20 + 10, 90, i * 20 + 10, 450);for (int i = 0; i < 19; i++)for (int j = 0; j < 19; j++) {if (allChess[i][j] == 1) // 黑子{// g.setColor(Color.BLACK);int tempx = i * 20 + 10;int tempy = j * 20 + 90;g2.fillOval(tempx - 7, tempy - 7, 14, 14);} else if (allChess[i][j] == 2) // 白字{g2.setColor(Color.WHITE);int tempx = i * 20 + 10;int tempy = j * 20 + 90;g2.fillOval(tempx - 7, tempy - 7, 14, 14);g2.setColor(Color.BLACK);g2.drawOval(tempx - 7, tempy - 7, 14, 14);}}g2.setColor(Color.GREEN);g2.fillOval(68, 148, 4, 4);g2.fillOval(188, 148, 4, 4);g2.fillOval(308, 148, 4, 4);g2.fillOval(68, 268, 4, 4);g2.fillOval(188, 268, 4, 4);g2.fillOval(308, 268, 4, 4);g2.fillOval(68, 388, 4, 4);g2.fillOval(188, 388, 4, 4);g2.fillOval(308, 388, 4, 4);g2.fillOval(x - 5, y - 5, 10, 10);g.drawImage(bi, 0, 0, this);}@Overridepublic void mouseClicked(MouseEvent e) {// int x = e.getX();// int y = e.getY();}@Overridepublic void mouseEntered(MouseEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent arg0) {// TODO Auto-generated method stub}@Overridepublic void mousePressed(MouseEvent e) {x = e.getX();y = e.getY();if (canPlay == true) {if (x >= 10 && x <= 371 && y >= 90 && y <= 450) {// System.out.println("在棋盘范围内"+ x +" "+ y);// if((x-10)%20==0&&(y-90)%20==0){// x = (x - 10) / 20;// y = (y - 90) / 20;int xx = (x - 10) / 20;int yy = (y - 90) / 20;if ((x - 10) % 20 >= 10)xx++;if ((y - 90) % 20 >= 10)yy++;x = xx;y = yy;if (allChess[x][y] == 0) {if (isBlack == true) {allChess[x][y] = 1;isBlack = false;message = "轮到白方下棋";}else {allChess[x][y] = 2;isBlack = true;message = "轮到黑方下棋";}// 判断游戏是否结束if (checkWin()) {JOptionPane.showMessageDialog(this, "游戏结束,"+ (allChess[x][y] == 1 ? "黑方" : "白方") + "获胜!");canPlay = false;t.stop();}} else {JOptionPane.showMessageDialog(this, "当前位置已经有棋子了!");}// isBlack = !isBlack;this.repaint();}}if (x >= 380 && x <= 490 && y >= 90 && y <= 130)//JOptionPane.showMessageDialog(this, "开始游戏");{    int res = JOptionPane.showConfirmDialog(this,"是否要重新开始游戏?");    if(res == 0){    for(int i=0;i<=18;i++)    for(int j=0;j<=18;j++)    allChess[i][j] = 0;//allChess = new int[19][19];    canPlay = true;    isBlack = true;    message = "黑方先行";    blackTime = maxTime*60;    whiteTime = maxTime*60;    blackMessage = blackTime/3600 + ":" + (blackTime%3600)/60 + ":" + (blackTime%3600)%60;    whiteMessage = whiteTime/3600 + ":" + (whiteTime%3600)/60 + ":" + (whiteTime%3600)%60;    t.start();    this.repaint();    }    }if (x >= 380 && x <= 490 && y >= 140 && y <= 180)//JOptionPane.showMessageDialog(this, "游戏设置");{String input = JOptionPane.showInputDialog(this,"请输入游戏进行的最长时间(分总),如果输入0,表示没有时间限制");try{maxTime = Integer.parseInt(input);if(maxTime<0){JOptionPane.showMessageDialog(this,"不允许输入负的时间");}if(maxTime == 0){int res = JOptionPane.showConfirmDialog(this,"设置完成(时间为无穷大),是否重新开始游戏!");if(res == 0){    for(int i=0;i<=18;i++)    for(int j=0;j<=18;j++)    allChess[i][j] = 0;//allChess = new int[19][19];    isBlack = true;    message = "黑方先行";    canPlay = true;    //blackTime = maxTime*60;    //whiteTime = maxTime*60;    //blackMessage = blackTime/3600 + ":" + (blackTime%3600)/60 + ":" + (blackTime%3600)%60;    //whiteMessage = whiteTime/3600 + ":" + (whiteTime%3600)/60 + ":" + (whiteTime%3600)%60;    //this.repaint();}}if(maxTime >0){int res1 = JOptionPane.showConfirmDialog(this,"设置完成,是否重新开始游戏!");if(res1 == 0){    for(int i=0;i<=18;i++)    for(int j=0;j<=18;j++)    allChess[i][j] = 0;//allChess = new int[19][19];    isBlack = true;    canPlay = true;    message = "黑方先行";    blackTime = maxTime*60;    whiteTime = maxTime*60;    blackMessage = blackTime/3600 + ":" + (blackTime%3600)/60 + ":" + (blackTime%3600)%60;    whiteMessage = whiteTime/3600 + ":" + (whiteTime%3600)/60 + ":" + (whiteTime%3600)%60;    t.start();    this.repaint();   }}}catch(Exception e1){JOptionPane.showMessageDialog(this,"请输入正确的时间");}        }if (x >= 380 && x <= 490 && y >= 190 && y <= 213)// JOptionPane.showMessageDialog(this,"游戏说明");JOptionPane.showMessageDialog(this, "这是一个五子棋游戏,黑白双方轮流下棋,黑方先下");if (x >= 380 && x <= 490 && y >= 310 && y <= 350){//JOptionPane.showMessageDialog(this, "认输");   int  res = JOptionPane.showConfirmDialog(this,"是否确认认输?");   if(res == 0)   if(isBlack){   JOptionPane.showMessageDialog(this,"黑方方已经认输!");   canPlay = false;   t.stop();   }     else   {   JOptionPane.showMessageDialog(this,"白方已经认输!");      canPlay = false;      t.stop();   }   }if (x >= 380 && x <= 490 && y >= 360 && y <= 400)// JOptionPane.showMessageDialog(this,"关于游戏");JOptionPane.showMessageDialog(this, "开发者:wuxinliulei;有任何问题请咨询博主");if (x >= 380 && x <= 490 && y >= 410 && y <= 450)// JOptionPane.showMessageDialog(this,"结束游戏");System.exit(0);}// }@Overridepublic void mouseReleased(MouseEvent arg0) {// TODO Auto-generated method stub}private boolean checkWin() {// 先判断横向的是否有5个棋子相连/* * if(x+1<=18&&c==allChess[x+1][y]){ count++; * if(x+2<=18&&c==allChess[x+2][y]){ count++; } * if(x+3<=18&&c==allChess[x+3][y]){ count++; } *  * }不合理 不简洁 */int c = allChess[x][y];/* * //x方向判断 int i = 1; int count = 1; while (x+i<=18&&c == allChess[x + * i][y]) { count++; i++; } *  * i = 1; while (x-i>=0&&c == allChess[x - i][y]) { count++; i++; } * if(count >= 5) return true; *  * //方向判断 i = 1; count = 1; while (y+i<=18&&c == allChess[x][y+i]) { * count++; i++; } *  * i = 1; while (y-i>=0&&c == allChess[x][y-i]) { count++; i++; } * if(count >= 5) return true; *  * //右上方向 i = 1; count = 1; while (y-i>=0&&x+i<=18&&c == * allChess[x+i][y-i]) { count++; i++; } *  * i = 1; while (y+i>=0&&x-i>=0&&c == allChess[x-i][y+i]) { count++; * i++; } if(count >= 5) return true; *  * //左上 i = 1; count = 1; while (y-i>=0&&x-i>=0&&c == * allChess[x-i][y-i]) { count++; i++; } *  * i = 1; while (y+i>=0&&x+i<=18&&c == allChess[x+i][y+i]) { count++; * i++; } if(count >= 5) return true; */int count;count = this.checkCount(1, 0, c);if (count >= 5)return true;count = this.checkCount(0, 1, c);if (count >= 5)return true;count = this.checkCount(1, -1, c);if (count >= 5)return true;count = this.checkCount(1, 1, c);if (count >= 5)return true;return false;}private int checkCount(int xChange, int yChange, int c) {int count = 1;int tempx = xChange;int tempy = yChange;while (y + yChange >= 0 &&y+yChange<=18&&x+xChange>=0&& x + xChange <= 18&& c == allChess[x + xChange][y + yChange]) {count++;if (xChange != 0)xChange++;if (yChange != 0) {if (yChange < 0)yChange--;elseyChange++;}}xChange = tempx;yChange = tempy;while (y - yChange >= 0 &&y-yChange<=18&&x-xChange>=0&& x - xChange <= 18&& c == allChess[x - xChange][y - yChange]) {count++;if (xChange != 0)xChange++;if (yChange != 0) {if (yChange < 0)yChange--;elseyChange++;}}return count;}@Overridepublic void run() {//判断是否有时间的限制  如果没有时间限制 直接 停止时间控制线程if(maxTime>0){while(true){if(isBlack){blackTime--;}else{whiteTime--;}try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}blackMessage = blackTime/3600 + ":" + (blackTime%3600)/60 + ":" + (blackTime%3600)%60;    whiteMessage = whiteTime/3600 + ":" + (whiteTime%3600)/60 + ":" + (whiteTime%3600)%60;    //t.resume();    this.repaint();//放前面是有原因的    if(blackTime==0){JOptionPane.showMessageDialog(this,"黑方超时,白方获胜!");canPlay = false;t.stop();}    if(whiteTime==0){JOptionPane.showMessageDialog(this,"白方超时,黑方获胜!");canPlay = false;t.stop();}//System.out.println(blackTime+"  "+whiteTime);    //this.repaint();}}}}


 

原创粉丝点击