【汇智学堂】-JAVA小游戏(井字游戏-菜鸟版)

游戏执行的效果图:
【汇智学堂】-JAVA小游戏(井字游戏-菜鸟版)_第1张图片

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;  

/**
 * 
 * 程序入口
 *
 */
public class ticktacktoe {   
    public static void main(String[] args) {   
        new DrawSee2();
    }   
}   


class DrawSee2 extends JFrame {
	
	private static final long serialVersionUID = 2L;
	
    private Graphics jg;
    private static final int w2=200;//格子的宽度
    private static final int x1=100;//起点X坐标
    private static final int y1=100;//起点Y坐标
    private static final int x2=700;//终点X坐标
    private static final int y2=700;//终点Y坐标
    
    private int[][] position; // 存放游戏数据的二维数组
    
    public Color c=Color.RED; 
    private int isEnd2=0;
    
    /**
     * DrawSee构造方法
     */
    public DrawSee2() {
        
        setBounds(300, 50, 800, 800);//设置画布大小        
        this.setTitle("雷雷的井子游戏");       
        setVisible(true);
        
        //二维数组初始化
        
        position=new int[3][3];
        for(int i=0;i<3;i++){
        	for(int j=0;j<3;j++) {
        		position[i][j]=0;
        	}
        }   
 
        try {                      
            Thread.sleep(500);
        } catch (Exception e) {
            e.printStackTrace();
        }      

        // 获取专门用于在窗口界面上绘图的对象
        jg =  this.getGraphics();
        
        // 绘制游戏区域
        paintComponents(jg);
        
        
        // 添加鼠监听事件,当鼠标点击时触发
        this.addMouseListener(new MouseAdapter() {
            
            // 定义鼠标点击事件响应过程
            @Override
            public void mouseClicked(MouseEvent e) {
                
            	// 如果游戏结束,返回,不执行后面的代码                
                if(isEnd2==1) {                    
                    JOptionPane.showMessageDialog(null,"游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }   

                //获取鼠标点击的那一点的x,y坐标
                int x = e.getX(), y = e.getY();               
                int dx = (x - x1) / w2, dy = (y - y1) / w2;               
                int newx=dx*w2+x1,newy=dy*w2+y1;
                
               paintComponents2(jg,newx,newy,200,200);//将棋子落在准确的位置
               
               if(c==Color.RED) {
            	   position[dx][dy]=1;//记录落下的是红色的子
               }               
               if(c==Color.green) {
            	   position[dx][dy]=2;//记录落下的是红色的子
               }
               
               winOrNot();//判断输赢            
             }
        });        
    }
   
    public void paintComponents(Graphics g) {
        try {            
            // 设置线条颜色为红色
            g.setColor(Color.RED);            
            
            // 绘制棋盘            
            for(int i=0;i<4;i++) {
            	g.drawLine(x1, y1+i*w2, x2, y1+i*w2);//画横线
            	g.drawLine(x1+i*w2, y1, x1+i*w2, y2);//画竖线
            }
         
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void paintComponents2(Graphics g,int x,int y,int width,int height) {
        try {            
            // 设置线条颜色为红色
        	
        	g.setColor(c);        	
        	if(c==Color.RED) {        		
        		c=Color.green;
        	}        	
        	else {        		
        		c=Color.red;
        	}        	
            // 绘制棋盘   
            g.fillOval(x, y, width, height);
         
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void winOrNot() {
    	if(position[0][0]==position[1][0]&&position[1][0]==position[2][0]) {
     	   if(position[0][0]!=0) {
     		   isEnd2=1;
         	   if(c!=Color.red) {
         	       JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
         	   }
         	   if(c!=Color.green) {
         	       JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
         	   }
     	   }
          }
        
        if(position[0][1]==position[1][1]&&position[1][1]==position[2][1]) {
     	   if(position[0][1]!=0) {
     		   isEnd2=1;
         	   if(c!=Color.red) {
         	       JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
         	   }
         	   if(c!=Color.green) {
         	       JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
         	   }
     	   }
        }
     if(position[0][2]==position[1][2]&&position[1][2]==position[2][2]) {
     	   if(position[0][2]!=0) {
     		   isEnd2=1;
         	   if(c!=Color.red) {
         	       JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
         	   }
         	   if(c!=Color.green) {
         	       JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
         	   }
     	   }
         }
     
     
     if(position[0][0]==position[0][1]&&position[0][1]==position[0][2]) {
   	   if(position[0][0]!=0) {
   		   isEnd2=1;
       	   if(c!=Color.red) {
       	       JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
       	   }
       	   if(c!=Color.green) {
       	       JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
       	   }
   	   }
        }
      
      if(position[1][0]==position[1][1]&&position[1][1]==position[1][2]) {
   	   if(position[1][0]!=0) {
   		   isEnd2=1;
       	   if(c!=Color.red) {
       	       JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
       	   }
       	   if(c!=Color.green) {
       	       JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
       	   }
   	   }
      }
   if(position[2][0]==position[2][1]&&position[2][1]==position[2][2]) {
   	   if(position[2][0]!=0) {
   		   isEnd2=1;
       	   if(c!=Color.red) {
       	       JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
       	   }
       	   if(c!=Color.green) {
       	       JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
       	   }
   	   }
     }
      
   if(position[0][0]==position[1][1]&&position[1][1]==position[2][2]) {
   	   if(position[0][0]!=0) {
   		   isEnd2=1;
       	   if(c!=Color.red) {
       	       JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
       	   }
       	   if(c!=Color.green) {
       	       JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
       	   }
   	   }
      }
   if(position[2][0]==position[1][1]&&position[1][1]==position[0][2]) {
   	   if(position[2][0]!=0) {
   		   isEnd2=1;
       	   if(c!=Color.red) {
       	       JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
       	   }
       	   if(c!=Color.green) {
       	       JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);
       	   }
   	   }
      }
   }
}

你可能感兴趣的:(JAVA游戏开发)