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

📄 gamescreen.java

📁 经典的tictactoe游戏源代码
💻 JAVA
字号:
package example.tictactoe;

//import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;

public class GameScreen extends Canvas implements CommandListener {

  private static final int  BLACK   = 0x00000000;
  private static final int  WHITE   = 0x00FFFFFF;
  private static final int  RED     = 0x00FF0000;
  private static final int  BLUE    = 0x000000FF;
  private static final int  NO_MOVE = -1;

  private final TicTacToeMIDlet midlet;
  private final Game game;
  private final Command exitCommand;
  private final Command newGameCommand;
  private final Random random = new Random();

  private int screenWidth, screenHeight;
  private int boardCellSize, boardSize, boardTop, boardLeft;

  private boolean playerIsCircle;
  private boolean computerIsCircle;
  private int preCursorPosition, cursorPosition;
  private int computerMove = NO_MOVE;
  private int playerMove = NO_MOVE;
  private int computerGamesWonTally = 0;
  private int playerGamesWonTally = 0;
  private boolean isRestart;

  /** Constructor */
  public GameScreen(TicTacToeMIDlet midlet, boolean playerIsCircle){
    this.midlet = midlet;
    this.playerIsCircle = playerIsCircle;
    computerIsCircle = !playerIsCircle;

    game = new Game(random);
    initializeBoard();

    //Cofigure Screen Commands
    exitCommand = new Command("Exit", Command.EXIT, 1);
    newGameCommand = new Command("New", Command.SCREEN, 2);
    addCommand(exitCommand);
    addCommand(newGameCommand);
    setCommandListener(this);

    //Begin the game play
    initialize();
  }

  private void initialize(){
  	game.initialize();
  	
  	preCursorPosition = cursorPosition = 0;
  	playerMove = NO_MOVE;
  	
  	//随机判断人与电脑哪个先行
  	boolean computerFirst = ((random.nextInt() & 1) == 0);
  	if(computerFirst){
  		computerMove = game.makeComputerMove();
  	}else{
  		computerMove = NO_MOVE;
  	}
  	isRestart = true;
  	repaint();
  }

  private void initializeBoard(){
    //初始化操作面板
    screenWidth = getWidth();
    screenHeight = getHeight();
    if (screenWidth > screenHeight){
      boardCellSize = (screenHeight - 2)/3;
      boardLeft = (screenWidth - (boardCellSize * 3))/2;
      boardTop = 1;
    }else{
      boardCellSize = (screenWidth - 2)/3;
      boardLeft = 1;
      boardTop = (screenHeight - boardCellSize * 3)/2;
    }
  }
  
  protected void paint(Graphics g) {
  	if(game.isGameOver()){
  		paintGameOver(g);
  	}else{
  		paintGame(g);
  	}
  }
  
  private void paintGame(Graphics g){
  	if(isRestart){
  		//Clean the canvas
  		//刷新底色
  		g.setColor(WHITE);
  		g.fillRect(0, 0, screenWidth, screenHeight);
  		
  		drawBoard(g);
  		isRestart = false;
  	}
  	drawCursor(g);
  	
  	if(playerMove != NO_MOVE){
  		drawPiece(g, playerIsCircle, playerMove);
  	}
  	
  	if(computerMove != NO_MOVE){
  		drawPiece(g, computerIsCircle, computerMove);
  	}
  }
  

/**Visually indicates a player selected square on the board image
 * @param g
 */
private void drawCursor(Graphics g) {
	// TODO Auto-generated method stub
	g.setColor(WHITE);
	g.drawRect(((preCursorPosition % 3) * boardCellSize) + 2 +
			boardLeft, ((preCursorPosition / 3) * boardCellSize) + 2 + boardTop, 
			boardCellSize - 3, 
			boardCellSize - 3);
	
	g.setColor(BLACK);
	g.drawRect(((cursorPosition % 3) * boardCellSize) + 2 + boardLeft, 
			((cursorPosition / 3) * boardCellSize) + 2 + boardTop, 
			boardCellSize - 3, 
			boardCellSize - 3);	
}

/**
 * @param g
 */
private void drawBoard(Graphics g) {
	// TODO Auto-generated method stub
	//Clean the board
	g.setColor(WHITE);
	g.fillRect(0, 0, screenWidth, screenHeight);
	
	//Draw the board
	g.setColor(BLACK);
	for(int i = 0; i < 4; i ++){
		g.fillRect(boardLeft, boardCellSize * i + boardTop, 
				(boardCellSize * 3) + 2, 2);
		g.fillRect(boardCellSize * i + boardLeft, boardTop, 
				2, boardCellSize * 3);
	}
}

private void paintGameOver(Graphics g){
  	//控制游戏结束
	String statusMsg = null;
	if(game.isComputerWinner()){
		statusMsg = "I win !";
		computerGamesWonTally++;
	}else if(game.isPlayerWinner()){
		statusMsg = "You win !";
		playerGamesWonTally++;
	}else{
		statusMsg = "Stalemate!";
	}
	String tallyMsg = "You: " + playerGamesWonTally +
	                  "Me: " + computerGamesWonTally;
	
	Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
	int strHeight = font.getHeight();
	
	//返回指定字符串宽度
	int statusMsgWidth = font.stringWidth(statusMsg);
	int tallyMsgWidth  = font.stringWidth(tallyMsg);
	int strWidth       = tallyMsgWidth;
	if(statusMsgWidth > tallyMsgWidth){
		strWidth = statusMsgWidth;
	}
	
	//获取{x, y}位置来描绘字符串
	int x = (screenWidth - strWidth) / 2;
	x = x < 0 ? 0 : x;
	int y = (screenHeight - 2 * strHeight) / 2;
	y = y < 0 ? 0 : y;
	
	g.setColor(WHITE);
	g.fillRect(0, 0, screenWidth, screenHeight);
	
	//Paint the String's text
	g.setColor(BLACK);
	g.drawString(statusMsg, x, y, (Graphics.TOP)|(Graphics.LEFT));
	g.drawString(tallyMsg, x, (y + 1 + strHeight), (Graphics.TOP)|(Graphics.LEFT));
  }
  
  
  /** Main method */
  public void startApp() {
  }

  /** Handle pausing the MIDlet */
  public void pauseApp() {
  }

  /** Handle destroying the MIDlet */
  public void destroyApp(boolean unconditional) {
  }

  /** Quit the MIDlet */
  public static void quitApp() {
  }

  public void commandAction(Command c, Displayable displayable) {
  	if(c == exitCommand){
  		midlet.quit();
  	}else if(c == newGameCommand){
  		initialize();
  	}
  }
  
  protected void keyPressed(int keyCode){
  	if (game.isGameOver()){
  		return;
  	}
  	
  	int gameAction = getGameAction(keyCode);
  	
  	switch(gameAction){
  	case FIRE:
  		doPlayerMove();
  		break;
  	case RIGHT:
  		doMoveCursor(1, 0);
  		break;
  	case DOWN:
  		doMoveCursor(0, 1);
  		break;
  	case LEFT:
  		doMoveCursor(-1, 0);
  		break;
  	case UP:
  		doMoveCursor(0, -1);
  		break;
  	default:
  		break;
  		
  	}
  }
  
  private void doPlayerMove(){
  	if(game.isFree(cursorPosition)){
  		//Player Move
  		game.makePlayerMove(cursorPosition);
  		playerMove = cursorPosition;
  		
  		//紧接着, 由电脑自动行走下一步
  		if(!game.isGameOver()){
  			computerMove = game.makeComputerMove();
  		}
  		repaint();
  	}
  }
  
  private void doMoveCursor(int dx, int dy){
  	int newCursorPosition = cursorPosition + dx + 3 * dy;
  	
  	if((newCursorPosition >= 0 ) && (newCursorPosition < 9)){
  		preCursorPosition = cursorPosition;
  		cursorPosition = newCursorPosition;
  		repaint();
  	}
  }
  
  /**Draw a Circle or Cross piece on the board
 * @param g
 * @param isCircle
 * @param pos
 */
  private void drawPiece(Graphics g, boolean isCircle, int pos){
  	int x = ((pos % 3) * boardCellSize) + 3;
  	int y = ((pos / 3) * boardCellSize) + 3;
  	
  	if(isCircle){
  		drawCircle(g, x, y);
  	}else{
  		drawCross(g, x, y);
  	}
  }
  
  /**Draw blue Circle onto the board image
 * @param g
 * @param x
 * @param y
 */
  private void drawCircle(Graphics g, int x, int y){
  	g.setColor(BLUE);
  	g.fillArc(x + boardLeft, y + boardTop, boardCellSize - 4, 
  			  boardCellSize - 4, 0, 360);
  	
  	g.setColor(WHITE);
  	g.fillArc(x + 4 + boardLeft, y + 4 + boardTop, 
  			  boardCellSize - 4 - 8, boardCellSize - 4 - 8, 0, 360);                
  }
  
  /**Draw red Cross onto the board image
 * @param g
 * @param x
 * @param y
 */
  private void drawCross(Graphics g, int x, int y){
  	g.setColor(RED);
  	for(int i = 0; i < 4; i ++){
  		g.drawLine(x + 1 + i + boardLeft, y + boardTop,
  				x + boardCellSize - 4 - 4 + i + boardLeft,
				y + boardCellSize - 5 + boardTop);
  		g.drawLine(x + 1 + i + boardLeft, y + boardCellSize - 5 + boardTop,
  				x + boardCellSize - 4 - 4 + i + boardLeft,
				y + boardTop);
  	}
  }
 

}


⌨️ 快捷键说明

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