?? gamecricket.java
字號:
package Darts;
import com.nttdocomo.ui.*;
/** The game of Cricket. */
public class GameCricket extends GameBase {
/* 僋儕働僢僩偵娭偡傞掕媊 */
/* 嵟戝儔僂儞僪悢 */
public static final int ROUNDMAX = 20;
/* 僋儘乕僘僇僂儞僩 */
public static final int CLOSECNT = 3;
/* 僋儕働僢僩僫儞僶乕 */
public static final int[] CRIKETNUMBER = { 20, 19, 18, 17, 16, 15, 25 };
/* 儗僀傾僂僩掕媊 */
/* 儅乕僋昞帵埵抲 */
private int markPos[] = new int[7];
/* 僫儞僶乕昞帵埵抲 */
private int numPos[] = new int[7];
/* 僋儕働僢僩僫儞僶乕 */
private int curCrNum;
/* 忬懺曐帩 */
private int[][] hitResult;
private boolean[] numClosed;
public GameCricket(Darts _parent, int _playerCnt) {
super( _parent, _playerCnt );
for( int i = 0; i < 7; i++) {
markPos[i] = ( ( dsH / 9 ) * ( i + 1 ) );
numPos[i] = ( ( dsH / 9 ) * ( i + 2 ) );
}
}
/* 僋儕働僢僩奐巒 */
public void start() {
reset();
Display.setCurrent( this );
repaint();
}
/* 弶婜壔 */
private void reset() {
player = new Player[playerCnt];
score = new int[playerCnt];
hitResult = new int[playerCnt][7];
numClosed = new boolean[7];
for( int i = 0; i < playerCnt; i++ ) {
player[i] = new Player( ROUNDMAX );
score [i] = 0;
for ( int j = 0; j < 7; j++ ) {
hitResult[i][j] = 0;
}
}
curPlayer = 1;
curRound = 1;
curThrowCnt = 1;
curCrNum = 0;
winPlayer = 0;
evenPlayerCnt = 0;
gameStat = GS_PLAYING;
}
/* 1僗儘乕恑傔傞 */
public void progress(int _markCnt) {
if ( gameStat != GS_PLAYING ) {
return;
}
if ( playerCnt >= 2 ) {
int lastCnt = 0;
if ( numClosed[curCrNum] == true ) {
if ( CLOSECNT - hitResult[curPlayer - 1][curCrNum] > 0 ) {
lastCnt = CLOSECNT - hitResult[curPlayer - 1][curCrNum];
}
if ( lastCnt < _markCnt ) {
_markCnt = lastCnt;
}
}
/* 僆乕僶乕僉儖 */
/* 巇條偑椙偔傢偐傜側偄偺偱枹幚憰 */
}
player[curPlayer - 1].hitting( curRound, curThrowCnt,
curCrNum, _markCnt);
hitResult[curPlayer - 1][curCrNum] += _markCnt;
if ( _markCnt > 0 ) {
numClosed[curCrNum] = true;
for ( int i = 0; i < playerCnt; i++ ) {
if ( hitResult[i][curCrNum] < 3 ) {
numClosed[curCrNum] = false;
break;
}
}
}
scoreRecalc( curPlayer );
/* 傾儚乕僪弌偡側傜偙偙 */
// repaint();
/* 傾儚乕僪弌偡側傜偙偙 */
/* 廔椆敾掕 */
/* 慡偰僋儘乕僘偐偮摼揰偑堦斣崅偗傟偽廔椆 */
if ( allClosed( curPlayer - 1 ) == true ) {
int hiScore = 0;
for ( int i = 0; i < playerCnt; i++ ) {
if ( ( ( curPlayer - 1 ) != i ) &&
( score[i] > hiScore ) ) {
hiScore = score[i];
}
}
if ( score[curPlayer - 1] >= hiScore ) {
gameStat = GS_FINISH;
winPlayer = curPlayer;
}
}
/* 慡偰搳偘偒偭偨傜廔椆 */
if ( curRound == ROUNDMAX && curPlayer == playerCnt &&
curThrowCnt == THROWMAX ) {
gameStat = GS_FINISH;
int hiScore = score[0];
winPlayer = 1;
evenPlayerCnt = 0;
for ( int i = 1; i < playerCnt; i++ ) {
if ( score[i] > hiScore ) {
hiScore = score[i];
winPlayer = i + 1;
evenPlayerCnt = 0;
}
else if ( score[i] == hiScore ) {
evenPlayerCnt++;
}
}
}
if ( gameStat == GS_PLAYING ) {
curThrowCnt++;
if ( curThrowCnt > THROWMAX ) {
curThrowCnt = 1;
curPlayer ++;
if ( curPlayer > playerCnt ) {
curPlayer = 1;
curRound++;
}
}
}
repaint();
}
/* 1僗儘乕栠偡 */
public void prev() {
if ( curThrowCnt == 1 && curPlayer == 1 && curRound == 1 ) {
return;
}
if ( gameStat == GS_PLAYING ) {
curThrowCnt--;
if (curThrowCnt == 0) {
curThrowCnt = THROWMAX;
curPlayer --;
if (curPlayer == 0) {
curPlayer = playerCnt;
curRound--;
if ( curRound == 0 ) {
curRound++;
}
}
}
}
gameStat = GS_PLAYING;
int hitNum = player[curPlayer - 1].getHitNum (curRound, curThrowCnt);
int hitArea = player[curPlayer - 1].getHitArea(curRound, curThrowCnt);
hitResult[curPlayer - 1][hitNum] -= hitArea;
player[curPlayer - 1].hitting( curRound, curThrowCnt, 0, 0 );
if ( hitArea != 0 ) {
numClosed[hitNum] = false;
}
scoreRecalc( curPlayer );
repaint();
}
/* 僗僐傾嵞寁嶼 */
public void scoreRecalc(int playerNum) {
/* 僗僐傾弶婜壔 */
score[playerNum - 1] = 0;
/* 僗僐傾嵞寁嶼 */
for ( int i = 0; i < 7; i++ ) {
if ( hitResult[playerNum - 1][i] > 3 ) {
score[playerNum - 1] +=
( hitResult[playerNum - 1][i] - CLOSECNT ) * CRIKETNUMBER[i];
}
}
}
/* 僆乕儖僋儘乕僘僠僃僢僋 */
public boolean allClosed(int playerNum) {
return ( hitResult[playerNum][0] >= 3 &&
hitResult[playerNum][1] >= 3 &&
hitResult[playerNum][2] >= 3 &&
hitResult[playerNum][3] >= 3 &&
hitResult[playerNum][4] >= 3 &&
hitResult[playerNum][5] >= 3 &&
hitResult[playerNum][6] >= 3 );
}
public synchronized void paint(Graphics g) {
drawBackGround();
drawRound();
drawScore();
drawMark();
drawCursor();
}
/* 攚宨昤夋 */
private void drawBackGround() {
g.lock();
g.clearRect( 0, 0, dsW, dsH );
g.setColor( Color.WHITE );
g.drawLine( 0, strMH + 2, dsW, strMH + 2 );
g.drawLine(0, dsH - strLH, dsW, dsH - strLH);
g.drawLine( dsHW - ( ( strLW * 2 ) + 2 ), strMH + 2,
dsHW - ( ( strLW * 2 ) + 2 ), dsH - strLH );
g.drawLine( dsHW + ( ( strLW * 2 ) + 2 ), strMH + 2,
dsHW + ( ( strLW * 2 ) + 2 ), dsH - strLH );
g.drawLine( dsHW, dsH - strLH, dsHW, dsH );
g.setFont( Font.getFont( Font.FACE_SYSTEM |
Font.SIZE_LARGE |
Font.STYLE_BOLD ) );
g.drawString( "20" , dsHW - strLW, numPos[0] );
g.drawString( "19" , dsHW - strLW, numPos[1] );
g.drawString( "18" , dsHW - strLW, numPos[2] );
g.drawString( "17" , dsHW - strLW, numPos[3] );
g.drawString( "16" , dsHW - strLW, numPos[4] );
g.drawString( "15" , dsHW - strLW, numPos[5] );
g.drawString( "BULL", dsHW - ( strLW * 2 ), numPos[6] );
g.unlock(true);
}
/* 儔僂儞僪昤夋 */
private void drawRound() {
g.lock();
g.setFont( Font.getFont( Font.FACE_SYSTEM |
Font.SIZE_MEDIUM |
Font.STYLE_BOLD ) );
g.drawString( "Round " + curRound + " / " + ROUNDMAX ,
roundPosX, roundPosY );
g.unlock(true);
}
/* 儅乕僋昤夋 */
private void drawMark() {
g.lock();
g.setFont( Font.getFont( Font.FACE_SYSTEM |
Font.SIZE_LARGE |
Font.STYLE_BOLD ) );
for ( int i = 0; i < 7; i++ ) {
if ( ( playerCnt > 1 ) && ( numClosed[i] == true ) ) {
g.setColor( Color.SILVER );
g.fillRect( 0, numPos[i] - 12, getWidth(), 3 );
for ( int j = 0; j < playerCnt; j++ ) {
g.drawString( "乛", playerPos[j], numPos[i] );
g.drawString( "乢", playerPos[j], numPos[i] );
g.drawString( "仜", playerPos[j], numPos[i] );
}
}
else {
for ( int j = 0; j < playerCnt; j++ ) {
if ( j == 0 ) {
g.setColor( Color.BLUE );
}
else if ( j == 1 ) {
g.setColor( Color.RED );
}
else if ( j == 2) {
g.setColor( Color.GREEN );
}
else if ( j == 3 ) {
g.setColor( Color.YELLOW );
}
if ( hitResult[j][i] >= 1 ) {
g.drawString( "乛", playerPos[j], numPos[i] );
}
if ( hitResult[j][i] >= 2 ) {
g.drawString( "乢", playerPos[j], numPos[i] );
}
if ( hitResult[j][i] >= 3 ) {
g.drawString( "仜", playerPos[j], numPos[i] );
}
}
}
}
g.unlock(true);
}
/* 僇乕僜儖昤夋 */
private void drawCursor() {
if ( gameStat == GS_FINISH ) {
return;
}
g.lock();
g.setColor( Color.YELLOW );
g.drawRect( playerPos[curPlayer - 1], markPos[curCrNum] ,28, 28);
g.unlock(true);
}
public void processEvent(int _type, int _key) {
if ( Display.KEY_PRESSED_EVENT == _type ) {
if ( _key == Display.KEY_SOFT2 || gameStat == GS_GAMEOVER ) {
gameMenu = new GameMenu( this );
Display.setCurrent( gameMenu );
}
else if ( _key == Display.KEY_SOFT1 ) {
prev();
}
else if ( gameStat == GS_FINISH ) {
drawFinish();
return;
}
else if ( _key == Display.KEY_UP || _key == Display.KEY_LEFT ) {
if ( curCrNum > 0 ) {
curCrNum--;
}
repaint();
}
else if ( _key == Display.KEY_RIGHT || _key == Display.KEY_DOWN ) {
if ( curCrNum < 6 ) {
curCrNum++;
}
repaint();
}
else if ( _key == Display.KEY_3 ) {
if ( curCrNum != 6 ) {
progress( _key );
}
}
else if ( _key == Display.KEY_2 ||
_key == Display.KEY_1 ||
_key == Display.KEY_0 ) {
progress( _key );
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -