井字棋(applet实例)

package applet;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.border.LineBorder;


public class TicTacToe extends JApplet{
	
	private char whoseTurn ='X';                              //x第一个开始
	private Cell[][] cells=new Cell[3][3];                            //3*3棋盘
	private JLabel jlblStatus=new JLabel(" X's trun to play");             //label显示字
	
	public TicTacToe() {                                          //构造方法初始化布局
		JPanel p=new JPanel (new GridLayout(3,3,0,0));
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++)
				p.add(cells[i][j]=new Cell());
		
		p.setBorder(new LineBorder(Color.red,1));
		jlblStatus.setBorder(new LineBorder(Color.yellow,1));
		
		add(p,BorderLayout.CENTER);
		add(jlblStatus,BorderLayout.SOUTH);
		
	}
	
	public boolean isFull() {                  //判断整个棋盘是否填满(用于判断平局)
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++)
				if(cells[i][j].getToken()==' ')
					return false;
		return true;
	}
	
	public boolean isWon (char token) {                 //赢的情况判断,同行、同列、同对角线、同反对角线
		
		for(int i=0;i<3;i++)
			if((cells[i][0].getToken()==token)
					&&(cells[i][1].getToken()==token)
					&&(cells[i][2].getToken()==token)){
				return true;
				
			}
		
		for(int j=0;j<3;j++)
			if((cells[0][j].getToken()==token)
					&&(cells[1][j].getToken()==token)
					&&(cells[2][j].getToken()==token)){
				return true;
			}
		
		if(   (cells[0][0].getToken()==token)
				&&(cells[1][1].getToken()==token)
				&&(cells[2][2].getToken()==token)){
			return true;
		}
		
		
		if(   (cells[0][2].getToken()==token)
				&&(cells[1][1].getToken()==token)
				&&(cells[2][0].getToken()==token)){
			return true;
		}
		return false;                    //remember add this sentence
	}
	
	public class Cell extends JPanel{           //单元格的属性token(),监听鼠标行为,在单元格内绘制棋子
		private char token=' ';               //token用于获取单元格的状态,空,x,o三种状态
		
		public Cell() {
			setBorder(new LineBorder(Color.black,1));
			addMouseListener(new MyMouseListener());
		}
		public char getToken() {
			return token;
		}
		public void setToken(char c) {        //获取绘制哪种形状,x,o
			token=c;
			repaint();
		}
		protected void paintComponent(Graphics g) {            //这个paint是基于单元格来画的
			super.paintComponent(g);
			
			if(token=='X')
			{
				g.drawLine(10,10,getWidth()-10,getHeight()-10);
				g.drawLine(getWidth()-10,10,10,getHeight()-10);
			}
			else if(token=='O')
				g.drawOval(10,10,getWidth()-20,getHeight()-20);
		}	
	
	
	private class MyMouseListener extends MouseAdapter {         //游戏进行的控制吗,并判断最终结果
		public void mouseClicked(MouseEvent e) {
			if(token==' '&& whoseTurn!=' ') {
				setToken(whoseTurn);
				
				if(isWon(whoseTurn)) {
					jlblStatus.setText(whoseTurn +" won! the game is over");
					whoseTurn =' ';
				}
				else if (isFull()) {
					jlblStatus.setText("Draw ! the game is over");
					whoseTurn=' ';
				}
				else {
					whoseTurn = (whoseTurn=='X')?'O':'X';   //find who is the next player
					jlblStatus.setText(whoseTurn+"'s turn" );
				}			
			}
		}	
	}
	}
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/alike_meng/article/details/84784568