⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 field.java

📁 一个java的游戏程序
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;import java.util.*;/** * Creates a field on the board, and keeps track of its neighbours. It also * sets up a new field controller. This class also keeps track of the token * that occupys each field, if any. When a field becomes occupied, it also sets * that it is the next players turn. It also sets up the winline, and cheks if * a move makes one of the players win. */class Field     extends GridCell    implements MouseListener{    // basic data    private Game _game;    private Token _occupier;    private Direction _winline;        /**     * Constructor that takes a game as a parameter. It sets up an array to      * keep track of each fields neigbours.     */    public Field(Game game)    {	super();	_game = game;    }        // mouse listener interface    public void mouseClicked(MouseEvent e)    {	occupy();    }    public void mousePressed(MouseEvent e)    {    }    public void mouseReleased(MouseEvent e)    {    }    public void mouseEntered(MouseEvent e)    {    }    public void mouseExited(MouseEvent e)    {    }    // occupier    /**     * Sets the field to be occupied by the token taken as an argument and     * updates the fieldController     */    public void setOccupier(Token t)    {	_occupier = t;	setChanged();	notifyObservers();    }    /**     * returns the token occupying the field     */    public Token getOccupier()    {	return _occupier;    }        /**     * Removes the token occupying a field     */    public void clear()    {	setOccupier(null);	setWinline(null);    }    // winline    public void setWinline(Direction d)    {	_winline = d;	setChanged();	notifyObservers();    }    public Direction getWinLine()    {	return _winline;    }    // OCCUPATION    /**     * Is called when a token is to occupy a field. Also sets game.over     * and calls the winner game of statusbar, if the game is over if     * the game is not over, it set the next players turn     */    public void occupy()    {	//System.out.println("trying to occupy field");	if(!_game.getFinished() && legalMove()) 	    {		setOccupier(_game.getCurrentPlayer().getToken());		winningMove();		if(!_game.getFinished())		    _game.nextPlayer();	    }    }    // determine whether placing a token in this field is legal    public boolean legalMove()    {	return _occupier == null;    }    // WINNINGMOVE        public void winningMove()    {	if(isPartOfSequence())	    _game.setFinished();    }        /**     * goes through all the directions and checks if the sequence of consecutive     * tokens of the same kind is longer than the requirements for winning the     * game. If there is anyone winning the game it finds on wich fields to draw     * the winline. It also returns true if this is the case.      * If there still isn't a winner, it returns false.     */    public boolean isPartOfSequence()    {	for(Direction d = Direction.north; 	    !d.equals(Direction.south); d = d.next())            {                if(sequenceLength(d) >= _game.getWinningSequence())                    {                        setWinLine(d, getOccupier());                        return true;                    }            }        return false;    }    /**     * Compute the lenth of the sequence of tokens through this field     */    public int sequenceLength(Direction d)    {        return sequenceLength(d.opposite(), getOccupier())	    + ((Field)go(d)).sequenceLength(d, getOccupier());    }    /**     * Recursively goes through the fields in the direction given as first     * parameter and cheks if they are occupied by the same kind of tokens. It     * stops as soon as there is a free field or a field occupied by another kin     d     * of token.     */    public int sequenceLength(Direction d, Token token)    {        if(_border || getOccupier() != token)            return 0;        else             return 1 + ((Field)go(d)).sequenceLength(d, token);    }    /**     * Finds which fields the winline is to be drawn over     */    public void setWinLine(Direction d, Token token)    {        if((getOccupier() == token) && (getWinLine() == null))            {                setWinline(d);                ((Field)go(d)).setWinLine(d, token);                ((Field)go(d.opposite())).setWinLine(d, token);            }    }} // class Field

⌨️ 快捷键说明

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