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" scrolling="no" marginheight="0" allowtransparency="65535" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-3051157228350391&amp;amp;dt=1120830418796&amp;amp;format=728x90_as&amp;amp;output=html&amp;amp;channel=0045736275&amp;amp;ad_type=text_image&amp;amp;cc=551&amp;amp;u_h=768&amp;amp;u_w=1024&amp;amp;u_ah=740&amp;amp;u_aw=1024&amp;amp;u_cd=32&amp;amp;u_tz=480&amp;amp;u_his=19&amp;amp;u_java=true" marginwidth="0" frameborder="0" height="90" width="728"></iframe>

你可能感兴趣的:(JavaScript,游戏,编程,Google,Nokia)