通過上下左右移動,讓里面的曹操出來就行了!
創(chuàng)新互聯(lián)專注于孝南企業(yè)網(wǎng)站建設,響應式網(wǎng)站設計,商城網(wǎng)站建設。孝南網(wǎng)站建設公司,為孝南等地區(qū)提供建站服務。全流程按需求定制制作,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務
這個游戲非常小,你可以在網(wǎng)絡上下載一個,很不錯的經典!
數(shù)字華容道,是在4x4的格子中,依次從左到右,從上到下放置1-15這15個數(shù)字。經過一定的隨機,必須將這15個數(shù)字復原。每個數(shù)字只能向相鄰的唯一空格移動。難度更高的,格子和數(shù)字會更多,比如5x5。
我在開發(fā)一個類數(shù)字華容道游戲時,發(fā)現(xiàn)自己3x3的格子,居然怎么都解不出來。比如:一排1、2、3,二排4、5、6,三排8,7。經過網(wǎng)上查詢,才知道完全隨機位置的數(shù)值華容道僅有50%的概率是有解的。而我就是用的完全隨機方式去打亂次序。
網(wǎng)上有兩篇文章說的很好,以下是根據(jù)這兩篇文章的總結。
首先,要弄清楚一個概念:逆序數(shù)。逆序數(shù),即一個數(shù)字序列,將其中所有數(shù)字依次兩兩對比,若大數(shù)在前,小數(shù)在后,那么這就是一對逆序數(shù)。這里說到的逆序數(shù),指的是數(shù)字序列中逆序數(shù)的數(shù)量。比如:上文提到的1、2、3、4、5、6、8、7,逆序數(shù)只有1個,即8和7。
另外,還有一點要提出來。一般來講,復原狀態(tài)(初始狀態(tài))的數(shù)字華容道,會有一個空格,一般會設置在最末行的右下角。但也可以根據(jù)實際的需求,設置在其他行。請留意,初始空格所在的行數(shù),是決定是否有解的一個重要因素。
數(shù)字華容道,必然有解,只存在于如下3個細分情形:
實際的推演涉及到我一時難以徹底理解的數(shù)學推算,我只能用淺顯的方式來理解這個問題。
首先,有解的前提在于:當前空格回到初始空格所在行數(shù)時,逆序數(shù)一定得是偶數(shù)!為什么,我不清楚。
要想把空格移動到初始空格所在行,必須進行若干次上下移動和若干次左右移動。
左右移動,不會改變逆序數(shù);上下移動,若格子列數(shù)為奇數(shù),則每次增減偶數(shù)個逆序數(shù),若格子列數(shù)為偶數(shù),則每次增減奇數(shù)個逆序數(shù)。
也就是說:
具體實現(xiàn)應該很簡單,不多說了,就說一點。如果想更改一個數(shù)字序列的逆序數(shù)的奇偶性,只需要調換一對逆序數(shù)的位置即可。
可能是CS106A課程上的一句話,并不是原文:
不理解沒關系,會用就行。
參考文檔:
[toc]
最近看《最強大腦》,看到其中的“數(shù)字華容道”這個小游戲挺有意思,于是萌生了自己寫一個的想法,正好結合之前的文章 《Android開發(fā)藝術探索》第4章 View的工作原理 ,順便復習一下。
GitHub鏈接:
說做就做。
經過一夜的粗制濫造,初版已經完成,現(xiàn)在復盤一下詳細過程。
在4x4的方格棋盤中,擺放了1 15一共十五個棋子。玩家需要在最短時間內,移動棋子將1 15按順序排列好。
本文app結構很簡單,分為三個界面:目錄,游戲,高分榜。分別對應的是MenuAcitivity、GameActivity、HighScoreActivity。其中MenuActivity為主界面。
新建棋盤類 BoardView ,繼承自ViewGroup。在xml文件中直接加入BoardView即可。
新建棋子類 CubeView ,繼承自TextView。
棋子只包含一個數(shù)字,所以簡單的繼承自TextView即可。由于我們還需要比對棋子是否在正確的位置,所以我們還需要給每個棋子加上數(shù)字和位置屬性。
這里,我們定義了一個類Position,用于描述棋子在棋盤中的位置。
我們參考Android系統(tǒng)屏幕坐標系,以棋盤左上角為零點,每向右一格橫坐標加一,每向下一格縱坐標加一。如圖:
接下來,我們開始定義棋盤View:BoardView,這也是這個游戲的重頭戲。
首先,考慮需要添加哪些屬性。由于時間關系,我這里只加入了棋盤尺寸。
在style.xml文件中加入:
其中sizeH為棋盤列數(shù),sizeV為棋盤行數(shù)。(默認4x4大小,以下文中均以4x4為例)
分別對應BoardView的 mSizeX 和 mSizeY 屬性。
首先我們新建一個 cube_view.xml ,作為單顆棋子的布局。在BoardView的構造方法中,我們使用LayoutInflater將總共15顆棋子加載出來,并指定它們的位置,逐一保存在mChildren數(shù)組中。
最后,我們記錄了沒有棋子的空格所在位置 mBlankPos 。這個位置很關鍵,因為我們之后的的操作中都是圍繞這個空格來的。
measure和layout的過程很簡單,這里由于是自己使用,假定寬高都是定值。因為之前所有的CubeView都沒有定義寬高,默認是0,所以在onMeasure中,我們使用BoardView的寬除以列數(shù),高除以行數(shù),得到每顆棋子的寬高并給其賦值。這樣處理雖然很粗放,但是只是試玩的話并沒有什么影響。
我是按照從左往右、從上往下的方式依次排列棋子,并且沒有考慮棋子的margin屬性,所以onLayout很簡單:
至此,棋子在棋盤中就已經排列好了。
一開始的時候,我考慮的是,生成1~15的不重復隨機數(shù),然后依次給CubeView賦值即可。即:
雖然看起來是能行得通的,但是在實際的游戲過程中,遇到了非常嚴重的問題,那就是會出現(xiàn)無解的死局,也就是說無論如何都不可能解出來的棋局。經過網(wǎng)上搜索之后證實了這個bug的存在,而且市面上流傳的該類app很多都是有這個bug的!所以這個辦法就被廢棄掉了,得想一個新的方法。
由于必須是按照順序放置然后打亂的棋局才能保證有解,不能隨機亂放置,所以我就模擬手動打亂,寫了一個新的棋局生成器:
原理很簡單,因為空格的位置是唯一的,那么我們把空格的上下左右四個棋子隨機找出一個,與空格互換位置,也就模擬了一次手動點擊。當點擊的次數(shù)足夠多時(這里循環(huán)了10000次),就可以看做是已經打亂的棋盤了。
最后把生成好的棋盤,保存在一個二維數(shù)組中即可。
(因為有個10000次的循環(huán),我擔心時間過長,于是將其放在線程中執(zhí)行,但是后來我覺得自己多此一舉了。)
然后,在BoardView中定義一個setData方法,來把生成好的棋局裝進來:
這樣,就完成了棋局的生成。
游戲過程基本是極簡的。
在初始化方法中(2.1),我們給每個棋子都定義了點擊事件,模擬真實場景。具體來講,就是當我們點擊一個棋子的時候:如果棋子在空格周圍,則將棋子移動到空格處;反之,則不進行任何操作。(如果設置滑動同理)
這樣我們的Position類就派上用場了。
在2.1的 init() 方法中,我們有這么一句:
即是,當我們點擊了其中一個棋子時,會觸發(fā) moveChildToBlank(view) 方法。這個方法的目的正是上面所說。
在移動棋子之后,我們需要檢查一下是否是正確排列的順序,如果是的話,那么表明游戲完成。
首先創(chuàng)建HighScore類,包含姓名,用時,步數(shù),時間。
高分榜使用SharedPreferences+Gson,將一個ListHighScore轉換為json形式保存在本地。
最佳成績的記錄是在GameActivity中完成的。流程如下:
總的來說,邏輯簡單清晰。
自己開發(fā)的自然是需要作弊功能了!暫且不表。
由于只用了一個晚上完成,所以還很粗糙,很多功能不夠完善,而且也沒做適配和測試,難免會有bug存在。主要是把思路記錄下來,方便以后自己和他人做個參考。
數(shù)字華容道GitHub地址:
package huaroad;
import java.util.*;
import com.siemens.mp.io.*;
import com.siemens.mp.game.*;
import java.io.IOException;
import javax.microedition.lcdui.*;
// 游戲類
public class HRGame extends Canvas implements CommandListener, Backable
{
private static int BLOCKSIZE = 0; // 每塊大小
private static int IMGOFFSET = 0; // 名字圖片對于邊框的偏移量
// 布局定義 (4*5)塊
// 0-空, 1~4-兵, 5-張飛, 6-趙云, 7-馬超, 8-黃忠, 9-關羽, 10-曹操
private static final byte gamelayout[][][] = {
{ // 橫刀立馬
{ 5,10,10, 6 },
{ 5,10,10, 6 },
{ 7, 9, 9, 8 },
{ 7, 2, 3, 8 },
{ 1, 0, 0, 4 },
},
{ // 過五關
{ 1,10,10, 3 },
{ 2,10,10, 4 },
{ 5, 5, 6, 6 },
{ 7, 7, 8, 8 },
{ 0, 9, 9, 0 },
},
{ // 水泄不通
{ 1,10,10, 5 },
{ 2,10,10, 5 },
{ 6, 6, 7, 7 },
{ 8, 8, 9, 9 },
{ 3, 0, 0, 4 },
},
{ // 小燕出巢
{ 5,10,10, 6 },
{ 5,10,10, 6 },
{ 7, 7, 8, 8 },
{ 1, 9, 9, 3 },
{ 2, 0, 0, 4 },
},
{ // 進在咫尺
{ 1, 5, 6, 7 },
{ 2, 5, 6, 7 },
{ 8, 8, 3, 4 },
{ 9, 9,10,10 },
{ 0, 0,10,10 },
},
{ // 左右夾擊
{ 1,10,10, 2 },
{ 5,10,10, 6 },
{ 5, 9, 9, 6 },
{ 7, 3, 4, 8 },
{ 7, 0, 0, 8 },
},
{ // 重兵擋道
{ 5,10,10, 6 },
{ 5,10,10, 6 },
{ 1, 2, 3, 4 },
{ 7, 9, 9, 8 },
{ 7, 0, 0, 8 },
}
};
private static final byte posSxy[][] = { // 各布局光標起始位置(x,y)
{ 1, 4 }, { 0, 4 }, { 1, 4 }, { 1, 4 }, { 0, 4 },
{ 1, 4 }, { 1, 4 }
};
private static final Command COMMANDS[] = { // 定義菜單
new Command("繼續(xù)游戲", 4, 0),
new Command("新游戲", 1, 1),
new Command("退出", 6, 2),
new Command("按鍵設置", 1, 3),
new Command("幫助信息", 1, 4)
};
public byte ctrlkey[] = { // 按鍵定義(初始按鍵)
'4', // left - 4
'6', // right - 6
'2', // up - 2
'8', // down - 8
'5', // select - 5
};
private Command SndCmd, VibCmd;
final private static String openSnd="開啟聲音", closeSnd="關閉聲音", openVib="開啟振動", closeVib="關閉振動";
public boolean Snd, Vib;
private byte grid[][] = new byte[4][5]; // 游戲布局矩陣
private int posx, posy; // 當前光標位置
private int oldx, oldy; // 上次光標位置
private int offx, offy; // 棋子的大小偏移量(相對于當前塊)。
// 比如當前塊是曹操的右上角,那么offx=-1,offy=1
// 表示x負方向還有一塊,y的正方向有一塊
private int steps, tmpstep; // 已走步數(shù)
private Image nameImg[]; // 名字圖片(由于可寫文字太大,所以用圖片)
private SelNew gamelist;
private Display display;
private Quitable winQuit;
private boolean selected, gameOver; // 是否選中/是否游戲結束
private ExtendedImage ExScreenImg; // 擴展圖片作為繪圖緩存(Siemens擴展)
private Graphics Exg; // 緩存的Graphics對象
private MelodyComposer melody; // midi播放(Siemens擴展)
public HRGame()
{
if (getHeight() 64) // 根據(jù)屏幕高度得到圖形尺寸,以便針對不同機型
{
// 6688i使用
BLOCKSIZE = 15; // 每塊大小
IMGOFFSET = 3; // 名字圖片對于邊框的偏移量
}
else
{
// 3118使用
BLOCKSIZE = 12;
IMGOFFSET = 1;
}
nameImg = new Image[26]; // 載入名字圖片
try
{
nameImg[0] = Image.createImage("images/zhang.png"); // 張
nameImg[1] = Image.createImage("images/zhang2.png"); // 張(反色)
nameImg[2] = Image.createImage("images/fei.png"); // 飛
nameImg[3] = Image.createImage("images/fei2.png"); // 飛(反色)
nameImg[4] = Image.createImage("images/zhao.png"); // 趙
nameImg[5] = Image.createImage("images/zhao2.png"); // 趙(反色)
nameImg[6] = Image.createImage("images/yun.png"); // 云
nameImg[7] = Image.createImage("images/yun2.png"); // 云(反色)
nameImg[8] = Image.createImage("images/ma.png"); // 馬
nameImg[9] = Image.createImage("images/ma2.png"); // 馬(反色)
nameImg[10] = Image.createImage("images/chao.png"); // 超
nameImg[11] = Image.createImage("images/chao2.png"); // 超(反色)
nameImg[12] = Image.createImage("images/huang.png"); // 黃
nameImg[13] = Image.createImage("images/huang2.png"); // 黃(反色)
nameImg[14] = Image.createImage("images/zhong.png"); // 忠
nameImg[15] = Image.createImage("images/zhong2.png"); // 忠(反色)
nameImg[16] = Image.createImage("images/guan.png"); // 關
nameImg[17] = Image.createImage("images/guan2.png"); // 關(反色)
nameImg[18] = Image.createImage("images/yu.png"); // 羽
nameImg[19] = Image.createImage("images/yu2.png"); // 羽(反色)
nameImg[20] = Image.createImage("images/cao.png"); // 曹
nameImg[21] = Image.createImage("images/cao2.png"); // 曹(反色)
nameImg[22] = Image.createImage("images/c.png"); // 操
nameImg[23] = Image.createImage("images/c2.png"); // 操(反色)
nameImg[24] = Image.createImage("images/bing.png"); // 兵
nameImg[25] = Image.createImage("images/bing2.png"); // 兵(反色)
}
catch(IOException ioexception) { }
ExScreenImg = new ExtendedImage(Image.createImage(104, getHeight())); // 新建繪圖緩存
Exg = ExScreenImg.getImage().getGraphics(); // 緩存繪圖的Graphics對象
melody = new MelodyComposer(); // 新建midi
Snd = true; // 音效開啟
Vib = true; // 震動開啟
/////////////////////////////////////////////
// 讀取按鍵設置 共7字節(jié),依次表示:左按鍵、右按鍵、上按鍵、下按鍵、選中按鍵、音效、震動
try
{
byte bflag[] = new byte[2];
bflag[0] = bflag[1] = 1;
File keyfile = new File();
int fid = keyfile.open("CtrlKey"); // 打開文件(Siemens擴展)
if(keyfile.length(fid) 0) keyfile.read(fid, ctrlkey, 0, 5); // 讀取按鍵參數(shù)到ctrlkey
if(keyfile.length(fid) 5) keyfile.read(fid, bflag, 0, 2); // 讀取文件
Snd = (bflag[0] == 1); // 音效參數(shù) (1-開啟,其他-關閉)
Vib = (bflag[1] == 1); // 震動參數(shù) (1-開啟,其他-關閉)
keyfile.close(fid);
}
catch(IOException ioexception) { }
catch(NullPointerException npe){ }
//////////////////////////////////////////////
if (Snd) SndCmd = new Command(closeSnd, Command.OK, 5); // 根據(jù)參數(shù)設置菜單
else SndCmd = new Command(openSnd, Command.OK, 5);
if (Vib) VibCmd = new Command(closeVib, Command.OK, 6);
else VibCmd = new Command(openVib, Command.OK, 6);
for(int i = 0; i COMMANDS.length; i++) // 添加菜單
addCommand(COMMANDS[i]);
addCommand(SndCmd);
addCommand(VibCmd);
}
public void activate(Display disp, Quitable quitable) // 激活
{
winQuit = quitable;
display = disp;
show();
}
public void getkeys(byte ckeys[]) // 從自定義按鍵結果設置按鍵,并顯示游戲界面
{
for (byte i=0; i5; i++) ctrlkey[i] = ckeys[i];
show();
}
public void show() // 顯示游戲畫面
{
display.setCurrent(this);
setCommandListener(this);
}
public void newGame(SelNew gmlist) // 新游戲
{
gamelist = gmlist;
int gnum = gamelist.getSelectedIndex(); // 獲取游戲布局編號
int i, j;
for (i=0; i4; i++) // 根據(jù)編號初始化游戲矩陣
for (j=0; j5; j++)
grid[i][j] = gamelayout[gnum][j][i];
posx = posSxy[gnum][0]; // 初始化當前光標位置
posy = posSxy[gnum][1];
offx = offy = 0;
steps = 0; // 已走步數(shù)為0
gameOver = false;
selected = false; // 沒有選中某一塊
ExScreenImg.clear((byte)0); // 清空緩存
Exg.drawRect(4, 0, 2+BLOCKSIZE*4, 2+BLOCKSIZE*5); // 畫邊框
Exg.drawRect(4+BLOCKSIZE, 2+BLOCKSIZE*5, 2+BLOCKSIZE*2, 2);
// 畫布局名稱
String gname,gname2;
switch(gamelist.getSelectedIndex())
{
case 0:
gname = "橫刀"; gname2 = "立馬"; break;
case 1:
gname = "過"; gname2 = "五關"; break;
case 2:
gname = "水泄"; gname2 = "不通"; break;
case 3:
gname = "小燕"; gname2 = "出巢"; break;
case 4:
gname = "進在"; gname2 = "咫尺"; break;
case 5:
gname = "左右"; gname2 = "夾擊"; break;
default:
gname = "重兵"; gname2 = "擋道"; break;
}
Exg.drawString(gname, 70, 0, Graphics.TOP|Graphics.LEFT);
Exg.drawString(gname2, 70, 13, Graphics.TOP|Graphics.LEFT);
// 畫步數(shù)
Exg.drawString("步數(shù)", 70, 34, Graphics.TOP|Graphics.LEFT);
Exg.setColor(0xffffff);
Exg.fillRect(68, 55, 33, 13);
Exg.setColor(0);
Exg.drawString(Integer.toString(steps), 100, 47, Graphics.TOP|Graphics.RIGHT);
// 畫棋子
byte layout[] = {0,0,0,0,0, 0,0,0,0,0}; // 分別表示 10個棋子是否已經處理過,依次為:
// 1~4-兵, 5-張飛, 6-趙云, 7-馬超, 8-黃忠, 9-關羽, 10-曹操
for(i=0; i4; i++)
for(j=0; j5; j++)
{
if (grid[i][j] != 0)
if (layout[grid[i][j]-1] == 0) // 如果這個棋子沒有畫過
{
moveSelbox(i, j, false); // 畫一個棋子
layout[grid[i][j]-1] = 1; // 已經畫過了,標志置1
}
}
drawSelbox(); // 畫選擇框
SoundPlay(0); // 播放游戲開始音效
}
protected void paint(Graphics parag)
{
redraw();
}
// 畫選擇框
private void drawSelbox()
{
if (selected) // 選中狀態(tài)
{
drawBox(posx, posy, offx, offy, 0xffffff, false, 1, 0);
for (int i=0; i4; i++)
for (int j=0; j5; j++)
{
if (grid[i][j] == 0) drawBox(i, j, 0, 0, 0xffffff, true, 1, 0);
}
drawBox(posx, posy, offx, offy, 0, true, 0, 2);
}
else // 候選狀態(tài)
{
if (grid[posx][posy] != 0) // 當前塊不是空的
{
drawBox(posx, posy, offx, offy, 0xffffff, true, 0, 1);
drawBox(posx, posy, offx, offy, 0, false, 0, 0);
}
drawBox(posx, posy, offx, offy, 0, false, 2, 0); //畫外框
}
}
// 移動選擇框
// i,j當前塊的(x,y)坐標, moving:true-移動到該棋子,false-重畫該棋子
private void moveSelbox(int i, int j, boolean moving)
{
int ox = 0;
int oy = 0;
if (grid[i][j] != 0) // 該塊不是空的
{
if (i 0) // 左側
if (grid[i][j] == grid[i-1][j]) ox = -1;// 左側屬于同一個棋子
if (i 3) // 右側
if (grid[i][j] == grid[i+1][j]) ox = 1; // 右側屬于同一個棋子
if (j 0) // 上面
if (grid[i][j] == grid[i][j-1]) oy = -1;// 上面屬于同一個棋子
if (j 4) // 下面
if (grid[i][j] == grid[i][j+1]) oy = 1;// 下面屬于同一個棋子
}
if (moving)
{
offx = ox;
offy = oy;
drawSelbox(); // 移動選擇框
}
else
drawBox(i, j, ox, oy, 0, false, 0, 1); // 畫棋子
}
// 向緩存畫棋子或外框:(px,py)焦點格坐標,(ox,oy)整塊相對焦點的偏移量,
// color顏色, fill是否填充黑色,bigbox是否大塊(分3級),img是否畫人名:0-不畫、1-正常、2-反色
private void drawBox(int px, int py, int ox, int oy, int color, boolean fill, int bigbox, int img)
{
int gx[] = new int[2];
int gy[] = new int[2];
boolean dir = (ox==0);
if (bigbox != 0)
{
gx[0] = gx[1] = 5;
gy[0] = gy[1] = 1;
}
else
{
gx[0] = 6;
gx[1] = 4;
gy[0] = 2;
gy[1] = 0;
}
if (ox 0)
{
gx[0] += (px+ox)*BLOCKSIZE;
gx[1] += (px+1)*BLOCKSIZE;
}
else
{
gx[0] += px*BLOCKSIZE;
gx[1] += (px+ox+1)*BLOCKSIZE;
}
if (oy 0)
{
gy[0] += (py+oy)*BLOCKSIZE;
gy[1] += (py+1)*BLOCKSIZE;
}
else
{
gy[0] += py*BLOCKSIZE;
gy[1] += (py+oy+1)*BLOCKSIZE;
}
Exg.setColor(color);
if (fill)
Exg.fillRect(gx[0], gy[0], gx[1]-gx[0]+1, gy[1]-gy[0]+1);
else
Exg.drawRect(gx[0], gy[0], gx[1]-gx[0], gy[1]-gy[0]);
if (bigbox == 2)
{
Exg.drawLine(gx[0]+1, gy[0]-1, gx[1]-1, gy[0]-1);
Exg.drawLine(gx[0]+1, gy[1]+1, gx[1]-1, gy[1]+1);
Exg.drawLine(gx[0]-1, gy[0]+1, gx[0]-1, gy[1]-1);
Exg.drawLine(gx[1]+1, gy[0]+1, gx[1]+1, gy[1]-1);
}
else if (bigbox == 3)
{
if (ox != 0)
{
if (py 0 grid[px][py-1]!=grid[px+ox][py-1])
Exg.drawLine((gx[0]+gx[1])/2, gy[0]-1, (gx[0]+gx[1])/2, gy[0]-1);
if (py 4 grid[px][py+1]!=grid[px+ox][py+1])
Exg.drawLine((gx[0]+gx[1])/2, gy[1]+1, (gx[0]+gx[1])/2, gy[1]+1);
}
if (oy != 0)
{
if (px 0 grid[px-1][py]!=grid[px-1][py+oy])
Exg.drawLine(gx[0]-1, (gy[0]+gy[1])/2, gx[0]-1, (gy[0]+gy[1])/2);
if (px 3 grid[px+1][py]!=grid[px+1][py+oy])
Exg.drawLine(gx[1]+1, (gy[0]+gy[1])/2, gx[1]+1, (gy[0]+gy[1])/2);
}
}
Exg.setColor(0);
if (img0)
{
if (grid[px][py] == 10)
{
Exg.drawImage(nameImg[20+img-1], gx[0]+BLOCKSIZE/2+IMGOFFSET, gy[0]+1+IMGOFFSET, 20);
Exg.drawImage(nameImg[22+img-1], gx[0]+BLOCKSIZE/2+IMGOFFSET, gy[0]+BLOCKSIZE, 20);
}
else if (grid[px][py] 5)
Exg.drawImage(nameImg[24+img-1], gx[0]+IMGOFFSET, gy[0]+IMGOFFSET, 20);
else if(dir)
{
Exg.drawImage(nameImg[(grid[px][py]-5)*4+img-1], gx[0]+IMGOFFSET, gy[0]+1+IMGOFFSET, 20);
Exg.drawImage(nameImg[(grid[px][py]-5)*4+img-1+2], gx[0]+IMGOFFSET, gy[0]+BLOCKSIZE, 20);
}
else
{
Exg.drawImage(nameImg[(grid[px][py]-5)*4+img-1], gx[0]+1+IMGOFFSET, gy[0]+IMGOFFSET, 20);
Exg.drawImage(nameImg[(grid[px][py]-5)*4+img-1+2], gx[0]+BLOCKSIZE, gy[0]+IMGOFFSET, 20);
}
}
}
protected void keyPressed(int kcode) // 按鍵響應
{
if (gameOver) return; // 游戲結束了,不響應
if (selected) // 處于選中狀態(tài)
{
if (kcode == ctrlkey[4]) // 選擇
{
selected = false; // 不選中
drawSelbox();
}
else
{
int tmpx, tmpy;
tmpx = posx;
tmpy = posy;
if (kcode == ctrlkey[0]) // 左移
{
if (offx 0) tmpx += offx;
tmpx --;
if (tmpx 0) tmpx = posx; // 靠邊,移不動
else if (grid[tmpx][posy] == 0 grid[tmpx][posy+offy] == 0)
tmpx = posx-1;
else tmpx = posx;
}
else if (kcode == ctrlkey[1]) // 右移
{
if (offx 0) tmpx += offx;
tmpx ++;
if (tmpx 3) tmpx = posx; // 靠邊,移不動
else if (grid[tmpx][posy] == 0 grid[tmpx][posy+offy] == 0)
tmpx = posx+1;
else tmpx = posx;
}
else if (kcode == ctrlkey[2]) // move up
{
if (offy 0) tmpy += offy;
tmpy --;
if (tmpy 0) tmpy = posy; // 靠頂,移不動
else if (grid[posx][tmpy] == 0 grid[posx+offx][tmpy] == 0)
tmpy = posy-1;
else tmpy = posy;
}
else if (kcode == ctrlkey[3]) // move down
{
if (offy 0) tmpy += offy;
tmpy ++;
if (tmpy 4) tmpy = posy; // 靠底,移不動
else if (grid[posx][tmpy] == 0 grid[posx+offx][tmpy] == 0)
tmpy = posy+1;
else tmpy = posy;
}
// 重畫焦點塊
if ( tmpx != posx || tmpy != posy)
{
byte oldbnum = grid[posx][posy];
grid[posx][posy] = grid[posx+offx][posy] = 0;
grid[posx][posy+offy] = grid[posx+offx][posy+offy] = 0;
drawBox(posx, posy, offx, offy, 0xffffff, true, 1, 0);
posx = tmpx;
posy = tmpy;
grid[posx][posy] = grid[posx+offx][posy] = oldbnum;
grid[posx][posy+offy] = grid[posx+offx][posy+offy] = oldbnum;
drawSelbox();
}
// 計算、畫 步數(shù)
if (posx == oldx posy == oldy) // 如果等于上一步的位置,表示回退了一步
steps = tmpstep; // 步數(shù)返回上一次的步數(shù)
else if (steps == tmpstep)
steps ++; // 步數(shù)增加
Exg.setColor(0xffffff);
Exg.fillRect(68, 55, 33, 13);
Exg.setColor(0);
Exg.drawString(Integer.toString(steps), 100, 45, Graphics.TOP| Graphics.RIGHT);
// 判斷曹操(10)的位置
if (grid[1][4] == 10 grid[2][4] == 10) // 勝利
{
gameOver = true;
SoundPlay(1);
if (Vib) Vibrator.triggerVibrator(100); // 震動100ms(Siemens擴展)
Exg.setColor(0xffffff);
Exg.fillRect(11, 28, 47, 18);
Exg.setColor(0);
Exg.drawRect(11, 28, 47, 18);
Exg.drawString("勝利了!", 14, 32, 20);
}
}
}
else
{
if (kcode == ctrlkey[4]) // select
{
if (grid[posx][posy] != 0)
{
selected = true; // 選中
tmpstep = steps;
oldx = posx;
oldy = posy;
drawSelbox();
}
}
else
{
int tmpx, tmpy;
tmpx = posx;
tmpy = posy;
if (kcode == ctrlkey[0]) // move left
{
tmpx --;
if (tmpx 0) tmpx = 0;
else if (grid[tmpx][posy]==grid[posx][posy] grid[posx][posy]!=0)
{
tmpx --;
if (tmpx 0) tmpx = posx;
}
}
else if (kcode == ctrlkey[1]) // move right
{
tmpx ++;
if (tmpx 3) tmpx = 3;
else if (grid[tmpx][posy]==grid[posx][posy] grid[posx][posy]!=0)
{
tmpx ++;
if (tmpx 3) tmpx = posx;
}
}
else if (kcode == ctrlkey[2]) // move up
{
tmpy --;
if (tmpy 0) tmpy = 0;
else if (grid[posx][tmpy]==grid[posx][posy] grid[posx][posy]!=0)
{
tmpy --;
if (tmpy 0) tmpy = posy;
}
}
else if (kcode == ctrlkey[3]) // move down
{
tmpy ++;
if (tmpy 4) tmpy = 4;
else if (grid[posx][tmpy]==grid[posx][posy] grid[posx][posy]!=0)
{
tmpy ++;
if (tmpy 4) tmpy = posy;
}
}
if ( tmpx != posx || tmpy != posy)
{
drawBox(posx, posy, offx, offy, 0xffffff, false, 3, 0);
for (int i=0; i4; i++)
for (int j=0; j5; j++)
{
if (grid[i][j] == 0) drawBox(i, j, 0, 0, 0xffffff, true, 1, 0);
}
posx = tmpx;
posy = tmpy;
moveSelbox(posx, posy, true);
}
}
}
redraw();
}
public void commandAction(Command command, Displayable displayable) // 菜單響應
{
boolean savepara = false;
if(command == COMMANDS[1]) // new game
{
gamelist.activate(display, winQuit);
}
else if(command == COMMANDS[2]) // exit
winQuit.quit();
else if(command == COMMANDS[3]) // 按鍵設置
{
SetKeys fskey = new SetKeys(ctrlkey);
fskey.activate(display, this);
}
else if(command == COMMANDS[4]) // about
{
HRAbout hrabout = new HRAbout();
hrabout.activate(display, this);
}
else if (command == SndCmd)
{
Snd = !Snd;
removeCommand(SndCmd);
if (Snd) SndCmd = new Command(closeSnd, Command.OK, 5);
else SndCmd = new Command(openSnd, Command.OK, 5);
addCommand(SndCmd);
savepara = true;
}
else if (command == VibCmd)
{
Vib = !Vib;
removeCommand(VibCmd);
if (Vib) VibCmd = new Command(closeVib,Command.OK, 6);
else VibCmd = new Command(openVib,Command.OK, 6);
addCommand(VibCmd);
savepara = true;
}
if (savepara)
{
/////////////////////////////////////////////
// 寫入?yún)?shù)
try
{
byte bflag[] = new byte[2];
File keyfile = new File();
int fid = keyfile.open("CtrlKey");
keyfile.write(fid, ctrlkey, 0, 5); // 寫入按鍵數(shù)據(jù)(Siemens擴展)
bflag[0] = (byte)(Snd ? 1 : 0);
bflag[1] = (byte)(Vib ? 1 : 0);
keyfile.write(fid, bflag, 0, 2);
keyfile.close(fid);
}
catch(IOException ioexception) { }
catch(NullPointerException npe){}
//////////////////////////////////////////////*/
}
}
private void redraw()
{
ExScreenImg.blitToScreen(0, 0); // 緩存內數(shù)據(jù)直接輸出到屏幕上
}
//音樂
private void SoundPlay(int n) // 播放音效
{
if (!Snd) return; // 音效關閉,則返回
// Siemens擴展
melody.resetMelody();
try
{
if (n == 0) // new game
{
melody.setBPM(100);
melody.appendNote(MelodyComposer.TONE_G3, MelodyComposer.TONELENGTH_1_16);
melody.appendNote(MelodyComposer.TONE_G3, MelodyComposer.TONELENGTH_1_16);
melody.appendNote(MelodyComposer.TONE_H3, MelodyComposer.TONELENGTH_1_16);
melody.appendNote(MelodyComposer.TONE_G3, MelodyComposer.TONELENGTH_1_8);
}
else if (n == 1) // win
{
melody.setBPM(110);
melody.appendNote(MelodyComposer.TONE_C2, MelodyComposer.TONELENGTH_1_16);
melody.appendNote(MelodyComposer.TONE_E2, MelodyComposer.TONELENGTH_1_16);
melody.appendNote(MelodyComposer.TONE_G2, MelodyComposer.TONELENGTH_1_16);
melody.appendNote(MelodyComposer.TONE_E2, MelodyComposer.TONELENGTH_1_16);
melody.appendNote(MelodyComposer.TONE_G2, MelodyComposer.TONELENGTH_1_16);
以上為核心代碼 由于代碼太多無法一次傳上來
java課程設計題目及代碼分別是:
1、題目:計算器。設計內容是設計一個圖形界面(GUI)的計算器應用程序,完成簡單的算術運算。
設計要求是設計的計算器應用程序可以完成家法、減法、乘法、除法和取余運算。且有小數(shù)點、正負號、求倒數(shù)、退格和清零功能。
2、代碼:
數(shù)字按鈕NumberButton類如下:
import java.awt.
import java.awt.event.
import javax.swing.
public class NumberButton extends Button.
{
int number.
public NumberButton(int number).
{
super(""+number).
this.number=number.
setForeground(Color.blue).
}
public int getNumber().
{
return number;
}
}
其它java課程設計題目及代碼是:
題目:華容道。編寫一個按鈕的子類,使用該子類創(chuàng)建的對象代表華容道中的人物。通過焦點事件控制人物顏色,當人物獲得焦點時顏色為藍色,當失去焦點時顏色為灰色。
通過鍵盤事件和鼠標事件來實現(xiàn)曹操、關羽等人物的移動。當人物上發(fā)生鼠標事件或鍵盤事件時,如果鼠標指針的位置是在人物的下方(也就是組件的下半部分)或按下鍵盤的“↓“鍵,該人物向下移動。向左、向右和向上的移動原理類似。
代碼是:
String name[]={"曹操","關羽","張","劉","馬","許","兵","兵","兵","兵"}.
for(int i=0;iname.length;i++).
{
person[i]=new Person(i,name[i]).
person[i].addKeyListener(this).
person[i].addMouseListener(this).
//? ? ?person[i].addFocusListener(new Person).
add(person[i]).
}
person[0].setBounds(104,54,100,100).
person[1].setBounds(104,154,100,50).
person[2].setBounds(54,154,50,100).
person[3].setBounds(204,154,50,100).
person[4].setBounds(54,54,50,100).
person[5].setBounds(204,54,50,100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);
數(shù)字華容道是一款好玩又鍛煉大腦思維的游戲,在《最強大腦》節(jié)目中也曾被列為一個競賽項目。數(shù)字華容道的玩法,只要掌握幾個關鍵點,就能很快達成目標。下面就介紹其基本的解法。
1、第一行解法
這一步不需要什么技巧,但是如果按照預先確定的順序排列,可以節(jié)省不少時間。
這里介紹兩種排列組合:一種是123+4組合,即先將123排列好,再將4移動到右邊四個方格右下格。
2、第二行解法。
因為第一行和第二行的解法一樣,所以通常視為一步。
這一步首先按照567+8組合排列。然后11,12右移一格,5,6,7整體逆時針退一格,接著14左移一格,8上推一格,12右移一格,14退一格,最后5,6,7復位,第二行完成。
本文名稱:java數(shù)字華容道代碼,數(shù)字華容道c語言源代碼
網(wǎng)頁地址:http://sd-ha.com/article34/hdhcpe.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、外貿建站、關鍵詞優(yōu)化、App設計、網(wǎng)站策劃、品牌網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)