您的位置:首页 > 编程语言 > Go语言

MIDP图形设计6

2005-07-08 21:48 281 查看
  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&dt=1120830418796&format=728x90_as&output=html&channel=0045736275&ad_type=text_image&cc=551&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=19&u_java=true" frameborder="0" width="728" scrolling="no" height="90" allowtransparency="65535"></iframe>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息