MIDP图形设计6

  6、Game.java

  这个类封装了九宫格游戏的主要的游戏程序逻辑。前面我们也说过,游戏程序逻辑本身并不在本例程重点讨论的范围之内,本文主要是介绍MIDP图形编程的基础知识。游戏程序逻辑的WINS数组部分来自http://java.sun.com/applets/jdk/1.0/demo/TicTacToe/TicTacToe.java 这个经典例程。

  注意游戏程序逻辑是独立于游戏用户界面的(参见类GameScreen),并且可以使用其它实现方法替代。

package example.tictactoe;
import java.util.Random;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
// The game logic for TicTacToe
class Game {
private static final int[] WINS = {
// horizontals
bit(0) | bit(1) | bit(2),
bit(3) | bit(4) | bit(5),
bit(6) | bit(7) | bit(8),
// verticals
bit(0) | bit(3) | bit(6),
bit(1) | bit(4) | bit(7),
bit(2) | bit(5) | bit(8),
// diagonals
bit(0) | bit(4) | bit(8),
bit(2) | bit(4) | bit(6) }
;
private static final int DRAWN_GAME = bit(0) | bit(1) | bit(2) | bit(3) | bit(4) | bit(5) | bit(6) | bit(7) | bit(8);
private int playerState;
private int computerState;
private Random random;
Game(Random random) {
this.random = random;
initialize();
}
void initialize() {
playerState = 0;
computerState = 0;
}
boolean isFree(int position) {
int bit = bit(position);
return (((playerState & bit) == 0) && ((computerState & bit) == 0));
}
// The 'Contract' is that caller will always make valid moves.
// We don't check that it's the player's turn.
void makePlayerMove(int position) {
playerState |= bit(position);
}
// The 'Contract' is that we will be called only when there is still
// at least one free square.
int makeComputerMove() {
int move = getWinningComputerMove();
if (move == -1) {
// can't win
move = getRequiredBlockingComputerMove();
if (move == -1) {
// don't need to block
move = getRandomComputerMove();
}
}
computerState |= bit(move);
return move;
}


boolean isGameOver() {
return isPlayerWinner() | isComputerWinner() | isGameDrawn();
}
boolean isPlayerWinner() {
return isWin(playerState);
}
boolean isComputerWinner() {
return isWin(computerState);
}
boolean isGameDrawn() {
return (playerState | computerState) == DRAWN_GAME;
}
// Return a winning move if there is at least one, otherwise return -1
private int getWinningComputerMove() {
int move = -1;
for (int i = 0;
i < 9;
++i) {
if (isFree(i) && isWin(computerState | bit(i))) {
move = i;
break;
}
}
return move;
}
// Return a required blocking move if there is at least one (more
// than one and we've inevitably lost), otherwise return -1
private int getRequiredBlockingComputerMove() {
int move = -1;
for (int i = 0;
i < 9;
++i) {


if (isFree(i) && isWin(playerState | bit(i))) {
move = i;
break;
}
}
return move;
}
// Return a random move in a free square, // or return -1 if none are available private int getRandomComputerMove() {
int move = -1;
// determine how many possible moves there are int numFreeSquares = 0;
for (int i = 0;
i < 9;
++i) {
if (isFree(i)) {
numFreeSquares++;
}
}
// if there is at least one possible move, pick randomly
if (numFreeSquares > 0) {
// shift twice to get rid of sign bit, then modulo numFreeSquares
int pick = ((random.nextInt()<<1)>>>1) % numFreeSquares;
// now find the chosen free square by counting pick down to zero
for (int i = 0;
i < 9;
++i) {
if (isFree(i)) {
if (pick == 0) {
move = i;
break;
}
pick--;
}
}
}

return move;
}
private static boolean isWin(int state) {
boolean isWinner = false;
for (int i = 0;
i < WINS.length;
++i) {
if ((state & WINS[i]) == WINS[i]) {
isWinner = true;
break;
}
}
return isWinner;
}
private static int bit(int i) {
return 1 << i;
}
}

  7、TicTacToe.jad

  下面是九宫格MIDlet的应用程序描述文件。

MIDlet-Name: TicTacToe
MIDlet-Vendor: Forum Nokia MIDlet-Version: 1.1.1
MIDlet-Jar-Size: 11409
MIDlet-Jar-URL: TicTacToe.jar
MIDlet-1: TicTacToe, /tictactoe.png, example.tictactoe.TicTacToeMIDlet
<script type="text/javascript"> <!-- google_ad_client = "pub-3051157228350391"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; google_ad_channel ="0045736275"; //--> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script><iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-3051157228350391&amp;dt=1120830418796&amp;format=728x90_as&amp;output=html&amp;channel=0045736275&amp;ad_type=text_image&amp;cc=551&amp;u_h=768&amp;u_w=1024&amp;u_ah=740&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=19&amp;u_java=true" frameborder="0" width="728" scrolling="no" height="90" allowtransparency="65535"></iframe>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值