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

📄 grids.java

📁 J2ME小游戏程序 实现的是简单的五子棋的功能
💻 JAVA
字号:
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.TiledLayer;


public class Grids {
	//格子图像种类,数值对应Tile图像中的编号
	public static final int GRID_NONE		= 1;	//没有任何棋子
	public static final int GRID_BLACK	= 2;	//黑色棋子(游戏者棋子)
	public static final int GRID_WHITE	= 3;	//白色棋子(电脑棋子)
	private TiledLayer m_GridTL;					//棋盘Tile
	private int m_nCurCol;							//最近一次黑方下棋位置的列号
	private int m_nCurRow;							//最近一次黑方下棋位置的行号
	private Computer m_Computer;					//电脑对象
	public Grids( int scrWidth, int scrHeight ){
		try{
			Image img = Image.createImage("/demo/grid.png");
			//根据屏幕大小设定棋盘格子的行列数
			int cols = scrWidth / 15 - 1;
			if( cols > 15 )
				cols = 15;
			int rows = scrHeight / 15 - 1;
			if( rows > 15 )
				rows = 15;
			m_GridTL = new TiledLayer( cols,rows, img, 15, 15 );
			
			//设置棋盘的位置
			int x = ( scrWidth - m_GridTL.getWidth() ) / 2;
			int y = ( scrHeight - m_GridTL.getHeight() ) / 2;
			m_GridTL.setPosition(x, y);
		}
		catch(Exception exception){}
	}
	//重新开局
	public void Reset(){
		if( m_Computer == null )
			m_Computer = new Computer();
		//将棋盘的所有格子都设置为空
		for( int col = 0; col < m_GridTL.getColumns(); col ++ ){
			for( int row = 0; row < m_GridTL.getRows(); row ++ ){
				m_GridTL.setCell(col, row, GRID_NONE);
			}
		}
		m_nCurCol = m_GridTL.getColumns() / 2;
		m_nCurRow = m_GridTL.getRows() / 2;
	}
	//处理按键操作,参数keyStates为按键的状态
	//返回1表示游戏者(黑方)获胜,返回-1表示电脑(白方)获胜
	public int Input(int keyStates){
		//移动当前的选择位置
		if( ( keyStates & GameCanvas.LEFT_PRESSED ) != 0 )
			m_nCurCol --;
		if( ( keyStates & GameCanvas.RIGHT_PRESSED ) != 0 )
			m_nCurCol ++;
		if( ( keyStates & GameCanvas.UP_PRESSED ) != 0 )
			m_nCurRow --;
		if( ( keyStates & GameCanvas.DOWN_PRESSED ) != 0 )
			m_nCurRow ++;
		
		//限定选择位置的范围
		if( m_nCurCol < 0 )
			m_nCurCol = 0;
		else if( m_nCurCol >= m_GridTL.getColumns() )
			m_nCurCol = m_GridTL.getColumns() - 1;
		if( m_nCurRow < 0 )
			m_nCurRow = 0;
		else if( m_nCurRow >= m_GridTL.getRows() )
			m_nCurRow = m_GridTL.getRows() - 1;
		
		if( ( keyStates & GameCanvas.FIRE_PRESSED ) != 0 )
		{
			int index = m_GridTL.getCell(m_nCurCol, m_nCurRow);
			if( index == Grids.GRID_NONE )
			{//如果当前位置没有棋子,则可以下棋
				m_GridTL.setCell( m_nCurCol, m_nCurRow, GRID_BLACK);
				
				//判断游戏者是否获胜
				if( hasFive( m_nCurCol, m_nCurRow ) )
					return 1;
				
				//电脑下棋
				if( m_Computer.Input(m_GridTL) == false )
					return 1;
				//判断电脑是否获胜
				if( hasFive( m_Computer.m_nCurCol, m_Computer.m_nCurRow ) )
					return -1;
			}
		}
		return 0;
	}
	//显示图像
	public void Paint(Graphics g){
		//显示棋盘及棋子
		m_GridTL.paint(g);
		//显示游戏者当前的选择位置
		int x = m_GridTL.getX() + m_nCurCol * m_GridTL.getCellWidth();
		int y = m_GridTL.getY() + m_nCurRow * m_GridTL.getCellHeight();
		g.drawRect(x, y, m_GridTL.getCellWidth(), m_GridTL.getCellHeight());
	}
	//判断某位置上的棋子与周围棋子是否构成5个相连
	//返回true表示有5个同类棋子相连,false则表示没有
	private boolean hasFive( int col, int row ){
		int type = m_GridTL.getCell(col, row);		//当前位置的棋子种类
		if( type == GRID_NONE )
			return false;
	
		//计算当前棋子的左右方向相连的棋子数目
		int nNum = 0;			//相连的棋子数目
		for( int x = col - 4; x < col + 5; x ++ )
		{
			if( x < 0 )
				continue;
			else if( x >= m_GridTL.getColumns() )
				break;
			//如果棋子类型不同,则相连数又重新变为0
			if( m_GridTL.getCell(x, row) != type )
				nNum = 0;
			else{
				nNum ++;
				if( nNum >= 5 )		//如果有5个以上棋子相连
					return true;
			}
		}
		
		//计算当前棋子的上下方向相连的棋子数目
		nNum = 0;
		for( int y = row - 4; y < row + 5; y ++ )
		{
			if( y < 0 )
				continue;
			else if( y >= m_GridTL.getRows() )
				break;
			//如果棋子类型不同,则相连数又重新变为0
			if( m_GridTL.getCell(col, y) != type )
				nNum = 0;
			else{
				nNum ++;
				if( nNum >= 5 )		//如果有5个以上棋子相连
					return true;
			}
		}
		
		
		//计算当前棋子的斜45度方向相连的棋子数目
		nNum = 0;
		for( int n = -4; n < 5; n ++ )
		{
			int x = col - n;
			int y = row + n;
			if( x >= m_GridTL.getColumns() )
				continue;
			else if( x < 0 )
				break;
			
			if( y < 0 )
				continue;
			else if( y >= m_GridTL.getRows() )
				break;
			//如果棋子类型不同,则相连数又重新变为0
			if( m_GridTL.getCell(x, y) != type )
				nNum = 0;
			else{
				nNum ++;
				if( nNum >= 5 )		//如果有5个以上棋子相连
					return true;
			}
		}		
		
		//计算当前棋子的斜135度方向相连的棋子数目
		nNum = 0;
		for( int n = -4; n < 5; n ++ )
		{
			int x = col + n;
			int y = row + n;
			if( x < 0 )
				continue;
			else if( x >= m_GridTL.getColumns() )
				break;
			
			if( y < 0 )
				continue;
			else if( y >= m_GridTL.getRows() )
				break;
			//如果棋子类型不同,则相连数又重新变为0
			if( m_GridTL.getCell(x, y) != type )
				nNum = 0;
			else{
				nNum ++;
				if( nNum >= 5 )		//如果有5个以上棋子相连
					return true;
			}
		}
		
		return false;		//没有5个以上的棋子相连
	}
}

⌨️ 快捷键说明

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