欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现五子棋小游戏

程序员文章站 2024-03-04 08:14:41
java实现五子棋小游戏 package gomoku; import java.awt.toolkit; import javax.swing....

java实现五子棋小游戏

package gomoku;
 
import java.awt.toolkit;
 
import javax.swing.jframe;
 
 
public class gomokuframe extends jframe 
{
  //定义一个操作面板
  operatorpane op=null;
   
  public gomokuframe()
  {
    //设置名称
    this.settitle("五子棋");
    //设置窗口大小
    this.setsize(510,510);
    //设置窗口位置
      //取得电脑屏幕大小
    int computer_width=toolkit.getdefaulttoolkit().getscreensize().width;
    int computer_height=toolkit.getdefaulttoolkit().getscreensize().height;
    system.out.println("电脑屏幕的宽度:\n"+computer_width+"\n电脑屏幕的高度:\n"+computer_height);
        //居中
    this.setlocation((computer_width-510)/2, (computer_height-510)/2);    
     
    //实例化幕布
    op=new operatorpane();
    //导入幕布
    this.add(op);
    //添加鼠标监听
    this.addmouselistener(op);
     
    //设置窗口的显示
    this.setvisible(true);
    //设置窗口的正常关闭
    this.setdefaultcloseoperation(jframe.exit_on_close);
  }
   
   //执行测试
  public static void main(string[] args) 
  {
    new gomokuframe();
  }
     
} 
  
package gomoku;
 
 
import java.awt.color;
import java.awt.font;
import java.awt.graphics;
import java.awt.image;
import java.awt.event.mouseevent;
import java.awt.event.mouselistener;
 
 
 
import java.awt.image.bufferedimage;
import java.awt.image.bufferedimagefilter;
 
import javax.swing.imageicon;
import javax.swing.joptionpane;
import javax.swing.jpanel;
 
public class operatorpane extends jpanel implements mouselistener,runnable
{
  //定义背景图片对象
  image imagebackground = null;
  //定义棋盘的格子的行数
  int boardrows=18;
  //定义棋盘的格子的列数
  int boardcols=18;
  //定义棋盘的格子的大小
  int boardsize=20;
  //保存棋子坐标
  int x=0,y=0;
  //保存之前下过的全部棋子坐标
  // 其中数据内容 0: 表示这个点并没有棋子, 1: 表示这个点是黑子, 2:表示这个点是白子
  int allchess[][]=new int [19][19];
  //标记下一步下黑棋还是白棋
  boolean isblack=true;
  //判断游戏是否能够继续
  boolean canplay=true;
  //保存显示信息
  string message="黑方先行";
  // 保存最多拥有多少时间(秒)
  int maxtime = 0;
  // 做倒计时的线程类
  thread t = new thread(this);
  // 保存黑方与白方的剩余时间
  int blacktime = 0;
  int whitetime = 0;
  // 保存双方剩余时间的显示信息
  string blackmessage = "无限制";
  string whitemessage = "无限制";
   
   
  @suppresswarnings("deprecation")
  public operatorpane()
  {
 
    t.start();
    t.suspend();
     
     imagebackground=new imageicon("image/background.jpg").getimage();
  }
   
  public void paint(graphics g) 
  {
    //双缓冲技术
    bufferedimage b1=new bufferedimage(495,475,bufferedimage.type_int_argb);
    graphics g2=b1.creategraphics();
     
    // 画出背景图片
    g2.drawimage(imagebackground, 0, 0,495,475,null);
     
     
    //画出棋盘线
    color c=g2.getcolor();
    g2.setcolor(color.black);
    for(int i=0;i<=boardrows;i++)
    {
      g2.drawline(10,50+boardsize*i,10+boardsize*boardrows,50+boardsize*i);
    }
    for(int i=0;i<=boardcols;i++)
    {
      g2.drawline(10+boardsize*i,50,10+boardsize*i,50+boardsize*boardcols);
    }
     
    //画出三三位置
    g2.filloval(67, 107, 6, 6);
    g2.filloval(67, 347, 6, 6);
    g2.filloval(307, 107, 6, 6);
    g2.filloval(307, 347, 6, 6);
    //画出附点
    g2.filloval(67, 227, 6, 6);
    g2.filloval(307, 227, 6, 6);
    g2.filloval(187, 107, 6, 6);
    g2.filloval(187, 347, 6, 6);
     
    //画出天元
    g2.filloval(187, 227, 6, 6);
     
     
     
     
    //画出文字提示
    /*font f=new font("黑体", font.bold, 24);
    g.setfont(f);*/
    g2.setfont(new font("黑体", font.bold, 20));
    g2.setcolor(color.black);
    g2.drawstring("游戏信息:"+message, 130, 40);
    g2.setfont(new font("宋体", font.italic, 15));
    g2.drawstring("黑方时间:"+blackmessage,25, 445);
    g2.drawstring("白方时间:"+whitemessage,245, 445);
    
    //绘制全部棋子
    for(int i=0;i<=boardrows;i++)
    {
      for(int j=0;j<=boardcols;j++)
      {
        //存储黑棋
        if(allchess[i][j]==1)
        {
          int tempx=i*20-10;
          int tempy=j*20+30;
          //画出黑棋
          g2.setcolor(color.black);
          g2.filloval(tempx+12, tempy+13, 15, 15);
        }
         
        //存储白棋
        if(allchess[i][j]==2)
        {
          int tempx=i*20-10;
          int tempy=j*20+30;
          //画出白棋
          g2.setcolor(color.black);
          g2.drawoval(tempx+12, tempy+13, 15, 15);
          g2.setcolor(color.white);
          g2.filloval(tempx+12, tempy+13, 15, 15);
        }
      }
 
    }
   
    g2.setcolor(c);
     
    g.drawimage(b1,0,0,this);
  }
   
   
  private boolean checkwin()
  {
    boolean flag=false;
     
    int color = allchess[x][y];
     
    /*// 保存共有相同颜色多少棋子相连
    int count1=1;
    int count2=1;
    int count3=1;
    int count4=1;
     
    // 判断横向是否有5个棋子相连,特点 纵坐标 是相同, 即allchess[x][y]中y值是相同
    // 通过循环来做棋子相连的判断      
    int i = 1;
    while (color == allchess[x+i][y]) 
    {
      count1++; 
      i++; 
    }
    //将i值复位
    i = 1;
    while (color == allchess[x-i][y])
    { 
      count1++; 
      i++; 
    }
    if(count1 >= 5)
    { 
      flag = true; 
    }
     
     
    //判断纵向 ,即allchess[x][y]中x值是相同
    int j = 1;
    while (color == allchess[x][y+j]) 
    {
      count2++; 
      j++; 
    }
    //将j值复位
    j = 1;
    while (color == allchess[x][y-j])
    { 
      count2++; 
      j++; 
    }
    if(count2>= 5)
    { 
      flag = true; 
    }
     
    //判断斜向"\"
    int m1=1;
    int n1=1;
    while (color == allchess[x+m1][y+n1]) 
    {
      count3++; 
      m1++; 
      n1++;
    }
    m1=1;
    n1=1;
    while (color == allchess[x-m1][y-n1]) 
    {
      count3++; 
      m1++; 
      n1++;
    }
    if(count3>= 5)
    { 
      flag = true; 
    }
     
    //判断斜向"/"
    int m2=1;
    int n2=1;
    while (color == allchess[x+m2][y-n2]) 
    {
      count4++; 
      m2++; 
      n2++;
    }
    m2=1;
    n2=1;
    while (color == allchess[x-m2][y+n2]) 
    {
      count4++; 
      m2++; 
      n2++;
    }
    if(count4>= 5)
    { 
      flag = true; 
    }
     
*/
    int count;
    //横向判断
    count=this.checkcount(1, 0, color);
    if(count>=5)
    {
      flag = true; 
    }
    else
    {
      //纵向判断
      count=this.checkcount(0, 1, color);
      if(count>=5)
      {
        flag = true; 
      }
      else
      {
        //斜向“/”
        count=this.checkcount(1, 1, color);
        if(count>=5)
        {
          flag = true; 
        }
        else
        {
          //斜向“\”
          count=this.checkcount(1, -1, color);
          if(count>=5)
          {
            flag = true; 
          }
        }
      }
    }
     
     
     
    return flag;
  }
   
  private int checkcount(int xchange,int ychange,int color)
  {
    int count=1;
    int tempx=xchange;
    int tempy=ychange;
     
    while (color==allchess[x+xchange][y+ychange]) 
    {
      count++;
      if(xchange!=0)
      {
        xchange++; 
      }
      if(ychange!=0)
      {
        if(ychange<0)
        {
          ychange--;
        }  
        else
          {
          ychange++;
          }
         
      }
       
    }
    //复位
    xchange=tempx;
    ychange=tempy;
    while (color==allchess[x-xchange][y-ychange]) 
    {
      count++;
      if(xchange!=0)
      {
        xchange++; 
      }
      if(ychange!=0)
      {
        if(ychange<0)
        {
          ychange--;
        }  
        else
        {
          ychange++;
        }
      }  
    }
    return count;
 
  }
 
   
   
   
 
  public void mouseclicked(mouseevent e) 
  {
     
    system.out.println("x:"+e.getx()+"y:"+e.gety());
    x=e.getx();
    y=e.gety();
    if(x>=10&&x<=(10+boardsize*boardrows+20)&&y>=50&&y<=(50+boardsize*boardcols+40))
    {
      //system.out.println("点在棋盘内。");
      x=(x-10)/20;
      y=(y-50-20)/20;
       
       
      if(canplay==true)
      {
        //判断当前要下什么颜色
        if(allchess[x][y]==0)
        {
          if(isblack==true)
          {
            allchess[x][y]=1;
            isblack=false;
            message="轮到白方";
          }
          else
          {
            allchess[x][y]=2;
            isblack=true;
            message="轮到黑方";
          }
 
          // 判断这个棋子是否和其他的棋子连成5连,即判断游戏是否结束
          boolean winflag=this.checkwin();
          if(winflag==true)
          {
 
            joptionpane.showmessagedialog(this,"游戏结束!"+
                (allchess[x][y]==1?"黑方":"白方")+"获胜。");
            canplay=false;
          }
        }
        else
        {
          joptionpane.showmessagedialog(this,"当前位置已经有棋子,请重新落子!");
        }
      }
      this.repaint();
    }
     
     
    //点击,游戏开始按钮
        //重新开始新的游戏
    if(e.getx()>=400&&e.getx()<=470&&e.gety()>=80&&e.gety()<=110)
    {
      int result=joptionpane.showconfirmdialog(this, "设置完成,是否重新开始游戏?");
      if(result==0)
      {
        //重新开始的操作,allchess[][]数组中的信息全部为0
        //清空棋盘
        for (int i = 0; i < 19; i++) 
        {
          for (int j = 0; j < 19; j++) 
          {
            allchess[i][j] = 0;
          }
        }
        //另一种方式 allchess = new int[19][19];
        blacktime = maxtime;
        whitetime = maxtime;
        if (maxtime > 0) 
        {
          blackmessage = maxtime / 3600 + ":"
              + (maxtime / 60 - maxtime / 3600 * 60) + ":"
              + (maxtime - maxtime / 60 * 60);
          whitemessage = maxtime / 3600 + ":"
              + (maxtime / 60 - maxtime / 3600 * 60) + ":"
              + (maxtime - maxtime / 60 * 60);
          t.resume();
        } 
        else
        {
          blackmessage = "无限制";
          whitemessage = "无限制";
        }
        message = "黑方先行";
        isblack = true;
        this.canplay = true; 
        this.repaint();
      }
    }
     
    //点击 游戏设置按钮
    if(e.getx()>=400&&e.getx()<=470&&e.gety()>=130&&e.gety()<=160)
    {
      string input = joptionpane
          .showinputdialog("请输入游戏的最大时间(单位:分钟),如果输入0,表示没有时间限制:");
      try {
        maxtime = integer.parseint(input) * 60;
        if (maxtime < 0) 
        {
          joptionpane.showmessagedialog(this, "请输入正确信息,不允许输入负数!");
        }
        if (maxtime == 0) 
        {
          int result = joptionpane.showconfirmdialog(this,
              "设置完成,是否重新开始游戏?");
          if (result == 0) 
          {
            for (int i = 0; i < 19; i++) 
            {
              for (int j = 0; j < 19; j++) 
              {
                allchess[i][j] = 0;
              }
            }
            // 另一种方式 allchess = new int[19][19];
            message = "黑方先行";
            isblack = true;
            blacktime = maxtime;
            whitetime = maxtime;
            blackmessage = "无限制";
            whitemessage = "无限制";
            this.canplay = true; 
            this.repaint();
          }
        }
        if (maxtime > 0)
        {
          int result = joptionpane.showconfirmdialog(this,
              "设置完成,是否重新开始游戏?");
          if (result == 0) 
          {
            for (int i = 0; i < 19; i++)
            {
              for (int j = 0; j < 19; j++) 
              {
                allchess[i][j] = 0;
              }
            }
            // 另一种方式 allchess = new int[19][19];
            message = "黑方先行";
            isblack = true;
            blacktime = maxtime;
            whitetime = maxtime;
            blackmessage = maxtime / 3600 + ":"
                + (maxtime / 60 - maxtime / 3600 * 60) + ":"
                + (maxtime - maxtime / 60 * 60);
            whitemessage = maxtime / 3600 + ":"
                + (maxtime / 60 - maxtime / 3600 * 60) + ":"
                + (maxtime - maxtime / 60 * 60);
            t.resume();
            this.canplay = true; 
            this.repaint();
          }
        }
      }
      catch (numberformatexception e1) 
      {
        // todo auto-generated catch block
        joptionpane.showmessagedialog(this, "请正确输入信息!");
      }
    }
     
    //点击 游戏说明按钮
    if(e.getx()>=400&&e.getx()<=470&&e.gety()>=180&&e.gety()<=210)
    {
      joptionpane.showmessagedialog(this,"这个一个五子棋游戏程序,黑白双方轮流下棋,当某一方连到五子时,游戏结束。");
    }
     
    //点击 认输按钮
    if(e.getx()>=400&&e.getx()<=470&&e.gety()>=280&&e.gety()<=310)
    {
      int result=joptionpane.showconfirmdialog(this,"是否确定认输?");
      if (result == 0) 
      {
        if (isblack)
        {
          joptionpane.showmessagedialog(this, "黑方已经认输,游戏结束!");
        } 
        else
        {
          joptionpane.showmessagedialog(this, "白方已经认输,游戏结束!");
        }
        canplay = false;
      }
    }
     
    //点击 关于按钮
    if(e.getx()>=400&&e.getx()<=470&&e.gety()>=330&&e.gety()<=360)
    {
      joptionpane.showmessagedialog(this,"本游戏由南木工作室制作,有相关问题可以访问www.yiyiinformation.com");
    }
     
    //点击 退出按钮
    if(e.getx()>=400&&e.getx()<=470&&e.gety()>=380&&e.gety()<=410)
    {
      joptionpane.showmessagedialog(this, "游戏结束");
      system.exit(0);
    }
     
  }
   
   
   
  //************************//
  @override
  public void mouseentered(mouseevent arg0)
  {
    // todo auto-generated method stub
     
  }
 
  @override
  public void mouseexited(mouseevent arg0) 
  {
    // todo auto-generated method stub
     
  }
 
  @override
  public void mousepressed(mouseevent arg0) 
  {
    // todo auto-generated method stub
     
  }
 
  @override
  public void mousereleased(mouseevent arg0) {
    // todo auto-generated method stub
     
  }
 
  @override
  public void run() 
  {
    if (maxtime > 0)
    {
      while (true) 
      {
        if (isblack) 
        {
          blacktime--;
          if (blacktime == 0)
          {
            joptionpane.showmessagedialog(this, "黑方超时,游戏结束!");
          }
        } 
        else
        {
          whitetime--;
          if (whitetime == 0) 
          {
            joptionpane.showmessagedialog(this, "白方超时,游戏结束!");
          }
        }
        blackmessage = blacktime / 3600 + ":"
            + (blacktime / 60 - blacktime / 3600 * 60) + ":"
            + (blacktime - blacktime / 60 * 60);
        whitemessage = whitetime / 3600 + ":"
            + (whitetime / 60 - whitetime / 3600 * 60) + ":"
            + (whitetime - whitetime / 60 * 60);
        this.repaint();
        try
        {
          thread.sleep(1000);
        } 
        catch (interruptedexception e)
        {
          // todo auto-generated catch block
          e.printstacktrace();
        }
        system.out.println(blacktime + " -- " + whitetime);
      }
    }
  }
     
}
   
 
   
  

演示图:

java实现五子棋小游戏

以上所述就是本文的全部内容了,希望能够对大家熟练掌握java有所帮助。