tictactoe.java

来自「一个使用JAVA编写的五子棋游戏」· Java 代码 · 共 162 行

JAVA
162
字号
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class TicTacToe extends JApplet
{
	private char whoseTurn = 'X';

	private Cell[][] cells = new Cell[16][16];

	private JLabel jlblStatus = new JLabel("X's turn to play");

	public TicTacToe()
	{
		JPanel p = new JPanel(new GridLayout(15,15,0,0));
		for(int i=0;i<15;i++)
			for(int j=0;j<15;j++)
			    p.add(cells[i][j] = new Cell());

		p.setBorder(new LineBorder(Color.red,1));
		jlblStatus.setBorder(new LineBorder(Color.yellow,1));

		this.getContentPane().add(p,BorderLayout.CENTER);
		this.getContentPane().add(jlblStatus,BorderLayout.SOUTH);
	}

	public boolean ifFull()
	{
		for(int i=0;i<15;i++)
			for(int j=0;j<15;j++)
				if(cells[i][j].getToken() == ' ')
					return false;
		return true;
	}

	public boolean isWon(char token)
	{
		for(int i=0;i<15;i++)
			for(int j=0;j<15;j++)
				if((cells[i][j].getToken() == token)
					&&(cells[i][j+1].getToken() == token)
					&&(cells[i][j+2].getToken() == token)
					&&(cells[i][j+3].getToken() == token)
					&&(cells[i][j+4].getToken() == token))
				{
					return true;
				}
		
		for(int i=0;i<15;i++)
			for(int j=0;j<15;j++)
					if((cells[i][j].getToken() == token)
					&&(cells[i+1][j].getToken() == token)
					&&(cells[i+2][j].getToken() == token)
					&&(cells[i+3][j].getToken() == token)
					&&(cells[i+4][j].getToken() == token))
				{
					return true;
				}
				
		for(int i=0;i<15;i++)
			for(int j=0;j<15;j++)
				if((cells[i][j].getToken() == token)
					&&(cells[i+1][j+1].getToken() == token)
					&&(cells[i+2][j+2].getToken() == token)
					&&(cells[i+3][j+3].getToken() == token)
					&&(cells[i+4][j+4].getToken() == token))
				{
					return true;
				}
				
		for(int i=0;i<15;i++)
			for(int j=0;j<15;j++)
				if((cells[i][j].getToken() == token)
					&&(cells[i+1][j-1].getToken() == token)
					&&(cells[i+2][j-2].getToken() == token)
					&&(cells[i+3][j-3].getToken() == token)
					&&(cells[i+4][j-4].getToken() == token))
				{
					return true;
				}

		return false;
	}

	public class Cell extends JPanel implements MouseListener
	{
		private char token = ' ';

		public Cell()
		{
			setBorder(new LineBorder(Color.black,1));
			addMouseListener(this);
		}

		public char getToken()
		{
			return token;
		}

		public void setToken(char c)
		{
			token = c;
			repaint();
		}

		protected void paintComponent(Graphics g)
		{
			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);
			}
		}

		public void mouseClicked(MouseEvent e)
		{
			if (token== ' '&&whoseTurn!= ' ')
			{
				setToken(whoseTurn);

				if(isWon(whoseTurn))
				{
					jlblStatus.setText(whoseTurn + "won! The game is over!");
					whoseTurn = ' ';
				}
				else if(ifFull())
				{
					jlblStatus.setText("Draw! The game is over!");
					whoseTurn = ' ';
				}
				else
				{
					whoseTurn = (whoseTurn== 'X')? 'O':'X';
					jlblStatus.setText(whoseTurn + "'s turn");
				}
			}
		}

		public void mousePressed(MouseEvent e)
		{
		}

		public void mouseReleased(MouseEvent e)
		{
		}
		public void mouseEntered(MouseEvent e)
		{
		}
		public void mouseExited(MouseEvent e)
		{
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?