新聞中心
華容道的游戲規(guī)則是怎樣的?
通過(guò)上下左右移動(dòng),讓里面的曹操出來(lái)就行了!
創(chuàng)新互聯(lián)專注于孝南企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),商城網(wǎng)站建設(shè)。孝南網(wǎng)站建設(shè)公司,為孝南等地區(qū)提供建站服務(wù)。全流程按需求定制制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
這個(gè)游戲非常小,你可以在網(wǎng)絡(luò)上下載一個(gè),很不錯(cuò)的經(jīng)典!
數(shù)字華容道怎樣才能有解
數(shù)字華容道,是在4x4的格子中,依次從左到右,從上到下放置1-15這15個(gè)數(shù)字。經(jīng)過(guò)一定的隨機(jī),必須將這15個(gè)數(shù)字復(fù)原。每個(gè)數(shù)字只能向相鄰的唯一空格移動(dòng)。難度更高的,格子和數(shù)字會(huì)更多,比如5x5。
我在開(kāi)發(fā)一個(gè)類數(shù)字華容道游戲時(shí),發(fā)現(xiàn)自己3x3的格子,居然怎么都解不出來(lái)。比如:一排1、2、3,二排4、5、6,三排8,7。經(jīng)過(guò)網(wǎng)上查詢,才知道完全隨機(jī)位置的數(shù)值華容道僅有50%的概率是有解的。而我就是用的完全隨機(jī)方式去打亂次序。
網(wǎng)上有兩篇文章說(shuō)的很好,以下是根據(jù)這兩篇文章的總結(jié)。
首先,要弄清楚一個(gè)概念:逆序數(shù)。逆序數(shù),即一個(gè)數(shù)字序列,將其中所有數(shù)字依次兩兩對(duì)比,若大數(shù)在前,小數(shù)在后,那么這就是一對(duì)逆序數(shù)。這里說(shuō)到的逆序數(shù),指的是數(shù)字序列中逆序數(shù)的數(shù)量。比如:上文提到的1、2、3、4、5、6、8、7,逆序數(shù)只有1個(gè),即8和7。
另外,還有一點(diǎn)要提出來(lái)。一般來(lái)講,復(fù)原狀態(tài)(初始狀態(tài))的數(shù)字華容道,會(huì)有一個(gè)空格,一般會(huì)設(shè)置在最末行的右下角。但也可以根據(jù)實(shí)際的需求,設(shè)置在其他行。請(qǐng)留意,初始空格所在的行數(shù),是決定是否有解的一個(gè)重要因素。
數(shù)字華容道,必然有解,只存在于如下3個(gè)細(xì)分情形:
實(shí)際的推演涉及到我一時(shí)難以徹底理解的數(shù)學(xué)推算,我只能用淺顯的方式來(lái)理解這個(gè)問(wèn)題。
首先,有解的前提在于:當(dāng)前空格回到初始空格所在行數(shù)時(shí),逆序數(shù)一定得是偶數(shù)!為什么,我不清楚。
要想把空格移動(dòng)到初始空格所在行,必須進(jìn)行若干次上下移動(dòng)和若干次左右移動(dòng)。
左右移動(dòng),不會(huì)改變逆序數(shù);上下移動(dòng),若格子列數(shù)為奇數(shù),則每次增減偶數(shù)個(gè)逆序數(shù),若格子列數(shù)為偶數(shù),則每次增減奇數(shù)個(gè)逆序數(shù)。
也就是說(shuō):
具體實(shí)現(xiàn)應(yīng)該很簡(jiǎn)單,不多說(shuō)了,就說(shuō)一點(diǎn)。如果想更改一個(gè)數(shù)字序列的逆序數(shù)的奇偶性,只需要調(diào)換一對(duì)逆序數(shù)的位置即可。
可能是CS106A課程上的一句話,并不是原文:
不理解沒(méi)關(guān)系,會(huì)用就行。
參考文檔:
Android小游戲 之《數(shù)字華容道》
[toc]
最近看《最強(qiáng)大腦》,看到其中的“數(shù)字華容道”這個(gè)小游戲挺有意思,于是萌生了自己寫一個(gè)的想法,正好結(jié)合之前的文章 《Android開(kāi)發(fā)藝術(shù)探索》第4章 View的工作原理 ,順便復(fù)習(xí)一下。
GitHub鏈接:
說(shuō)做就做。
經(jīng)過(guò)一夜的粗制濫造,初版已經(jīng)完成,現(xiàn)在復(fù)盤一下詳細(xì)過(guò)程。
在4x4的方格棋盤中,擺放了1 15一共十五個(gè)棋子。玩家需要在最短時(shí)間內(nèi),移動(dòng)棋子將1 15按順序排列好。
本文app結(jié)構(gòu)很簡(jiǎn)單,分為三個(gè)界面:目錄,游戲,高分榜。分別對(duì)應(yīng)的是MenuAcitivity、GameActivity、HighScoreActivity。其中MenuActivity為主界面。
新建棋盤類 BoardView ,繼承自ViewGroup。在xml文件中直接加入BoardView即可。
新建棋子類 CubeView ,繼承自TextView。
棋子只包含一個(gè)數(shù)字,所以簡(jiǎn)單的繼承自TextView即可。由于我們還需要比對(duì)棋子是否在正確的位置,所以我們還需要給每個(gè)棋子加上數(shù)字和位置屬性。
這里,我們定義了一個(gè)類Position,用于描述棋子在棋盤中的位置。
我們參考Android系統(tǒng)屏幕坐標(biāo)系,以棋盤左上角為零點(diǎn),每向右一格橫坐標(biāo)加一,每向下一格縱坐標(biāo)加一。如圖:
接下來(lái),我們開(kāi)始定義棋盤View:BoardView,這也是這個(gè)游戲的重頭戲。
首先,考慮需要添加哪些屬性。由于時(shí)間關(guān)系,我這里只加入了棋盤尺寸。
在style.xml文件中加入:
其中sizeH為棋盤列數(shù),sizeV為棋盤行數(shù)。(默認(rèn)4x4大小,以下文中均以4x4為例)
分別對(duì)應(yīng)BoardView的 mSizeX 和 mSizeY 屬性。
首先我們新建一個(gè) cube_view.xml ,作為單顆棋子的布局。在BoardView的構(gòu)造方法中,我們使用LayoutInflater將總共15顆棋子加載出來(lái),并指定它們的位置,逐一保存在mChildren數(shù)組中。
最后,我們記錄了沒(méi)有棋子的空格所在位置 mBlankPos 。這個(gè)位置很關(guān)鍵,因?yàn)槲覀冎蟮牡牟僮髦卸际菄@這個(gè)空格來(lái)的。
measure和layout的過(guò)程很簡(jiǎn)單,這里由于是自己使用,假定寬高都是定值。因?yàn)橹八械腃ubeView都沒(méi)有定義寬高,默認(rèn)是0,所以在onMeasure中,我們使用BoardView的寬除以列數(shù),高除以行數(shù),得到每顆棋子的寬高并給其賦值。這樣處理雖然很粗放,但是只是試玩的話并沒(méi)有什么影響。
我是按照從左往右、從上往下的方式依次排列棋子,并且沒(méi)有考慮棋子的margin屬性,所以onLayout很簡(jiǎn)單:
至此,棋子在棋盤中就已經(jīng)排列好了。
一開(kāi)始的時(shí)候,我考慮的是,生成1~15的不重復(fù)隨機(jī)數(shù),然后依次給CubeView賦值即可。即:
雖然看起來(lái)是能行得通的,但是在實(shí)際的游戲過(guò)程中,遇到了非常嚴(yán)重的問(wèn)題,那就是會(huì)出現(xiàn)無(wú)解的死局,也就是說(shuō)無(wú)論如何都不可能解出來(lái)的棋局。經(jīng)過(guò)網(wǎng)上搜索之后證實(shí)了這個(gè)bug的存在,而且市面上流傳的該類app很多都是有這個(gè)bug的!所以這個(gè)辦法就被廢棄掉了,得想一個(gè)新的方法。
由于必須是按照順序放置然后打亂的棋局才能保證有解,不能隨機(jī)亂放置,所以我就模擬手動(dòng)打亂,寫了一個(gè)新的棋局生成器:
原理很簡(jiǎn)單,因?yàn)榭崭竦奈恢檬俏ㄒ坏?,那么我們把空格的上下左右四個(gè)棋子隨機(jī)找出一個(gè),與空格互換位置,也就模擬了一次手動(dòng)點(diǎn)擊。當(dāng)點(diǎn)擊的次數(shù)足夠多時(shí)(這里循環(huán)了10000次),就可以看做是已經(jīng)打亂的棋盤了。
最后把生成好的棋盤,保存在一個(gè)二維數(shù)組中即可。
(因?yàn)橛袀€(gè)10000次的循環(huán),我擔(dān)心時(shí)間過(guò)長(zhǎng),于是將其放在線程中執(zhí)行,但是后來(lái)我覺(jué)得自己多此一舉了。)
然后,在BoardView中定義一個(gè)setData方法,來(lái)把生成好的棋局裝進(jìn)來(lái):
這樣,就完成了棋局的生成。
游戲過(guò)程基本是極簡(jiǎn)的。
在初始化方法中(2.1),我們給每個(gè)棋子都定義了點(diǎn)擊事件,模擬真實(shí)場(chǎng)景。具體來(lái)講,就是當(dāng)我們點(diǎn)擊一個(gè)棋子的時(shí)候:如果棋子在空格周圍,則將棋子移動(dòng)到空格處;反之,則不進(jìn)行任何操作。(如果設(shè)置滑動(dòng)同理)
這樣我們的Position類就派上用場(chǎng)了。
在2.1的 init() 方法中,我們有這么一句:
即是,當(dāng)我們點(diǎn)擊了其中一個(gè)棋子時(shí),會(huì)觸發(fā) moveChildToBlank(view) 方法。這個(gè)方法的目的正是上面所說(shuō)。
在移動(dòng)棋子之后,我們需要檢查一下是否是正確排列的順序,如果是的話,那么表明游戲完成。
首先創(chuàng)建HighScore類,包含姓名,用時(shí),步數(shù),時(shí)間。
高分榜使用SharedPreferences+Gson,將一個(gè)ListHighScore轉(zhuǎn)換為json形式保存在本地。
最佳成績(jī)的記錄是在GameActivity中完成的。流程如下:
總的來(lái)說(shuō),邏輯簡(jiǎn)單清晰。
自己開(kāi)發(fā)的自然是需要作弊功能了!暫且不表。
由于只用了一個(gè)晚上完成,所以還很粗糙,很多功能不夠完善,而且也沒(méi)做適配和測(cè)試,難免會(huì)有bug存在。主要是把思路記錄下來(lái),方便以后自己和他人做個(gè)參考。
數(shù)字華容道GitHub地址:
用Eclipse怎么寫華容道問(wèn)題
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; // 名字圖片對(duì)于邊框的偏移量
// 布局定義 (4*5)塊
// 0-空, 1~4-兵, 5-張飛, 6-趙云, 7-馬超, 8-黃忠, 9-關(guān)羽, 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 },
},
{ // 過(guò)五關(guān)
{ 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 },
},
{ // 進(jìn)在咫尺
{ 1, 5, 6, 7 },
{ 2, 5, 6, 7 },
{ 8, 8, 3, 4 },
{ 9, 9,10,10 },
{ 0, 0,10,10 },
},
{ // 左右?jiàn)A擊
{ 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[][] = { // 各布局光標(biāo)起始位置(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("按鍵設(shè)置", 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="開(kāi)啟聲音", closeSnd="關(guān)閉聲音", openVib="開(kāi)啟振動(dòng)", closeVib="關(guān)閉振動(dòng)";
public boolean Snd, Vib;
private byte grid[][] = new byte[4][5]; // 游戲布局矩陣
private int posx, posy; // 當(dāng)前光標(biāo)位置
private int oldx, oldy; // 上次光標(biāo)位置
private int offx, offy; // 棋子的大小偏移量(相對(duì)于當(dāng)前塊)。
// 比如當(dāng)前塊是曹操的右上角,那么offx=-1,offy=1
// 表示x負(fù)方向還有一塊,y的正方向有一塊
private int steps, tmpstep; // 已走步數(shù)
private Image nameImg[]; // 名字圖片(由于可寫文字太大,所以用圖片)
private SelNew gamelist;
private Display display;
private Quitable winQuit;
private boolean selected, gameOver; // 是否選中/是否游戲結(jié)束
private ExtendedImage ExScreenImg; // 擴(kuò)展圖片作為繪圖緩存(Siemens擴(kuò)展)
private Graphics Exg; // 緩存的Graphics對(duì)象
private MelodyComposer melody; // midi播放(Siemens擴(kuò)展)
public HRGame()
{
if (getHeight() 64) // 根據(jù)屏幕高度得到圖形尺寸,以便針對(duì)不同機(jī)型
{
// 6688i使用
BLOCKSIZE = 15; // 每塊大小
IMGOFFSET = 3; // 名字圖片對(duì)于邊框的偏移量
}
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"); // 關(guān)
nameImg[17] = Image.createImage("images/guan2.png"); // 關(guān)(反色)
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對(duì)象
melody = new MelodyComposer(); // 新建midi
Snd = true; // 音效開(kāi)啟
Vib = true; // 震動(dòng)開(kāi)啟
/////////////////////////////////////////////
// 讀取按鍵設(shè)置 共7字節(jié),依次表示:左按鍵、右按鍵、上按鍵、下按鍵、選中按鍵、音效、震動(dòng)
try
{
byte bflag[] = new byte[2];
bflag[0] = bflag[1] = 1;
File keyfile = new File();
int fid = keyfile.open("CtrlKey"); // 打開(kāi)文件(Siemens擴(kuò)展)
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-開(kāi)啟,其他-關(guān)閉)
Vib = (bflag[1] == 1); // 震動(dòng)參數(shù) (1-開(kāi)啟,其他-關(guān)閉)
keyfile.close(fid);
}
catch(IOException ioexception) { }
catch(NullPointerException npe){ }
//////////////////////////////////////////////
if (Snd) SndCmd = new Command(closeSnd, Command.OK, 5); // 根據(jù)參數(shù)設(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[]) // 從自定義按鍵結(jié)果設(shè)置按鍵,并顯示游戲界面
{
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(); // 獲取游戲布局編號(hào)
int i, j;
for (i=0; i4; i++) // 根據(jù)編號(hào)初始化游戲矩陣
for (j=0; j5; j++)
grid[i][j] = gamelayout[gnum][j][i];
posx = posSxy[gnum][0]; // 初始化當(dāng)前光標(biāo)位置
posy = posSxy[gnum][1];
offx = offy = 0;
steps = 0; // 已走步數(shù)為0
gameOver = false;
selected = false; // 沒(méi)有選中某一塊
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 = "過(guò)"; gname2 = "五關(guān)"; break;
case 2:
gname = "水泄"; gname2 = "不通"; break;
case 3:
gname = "小燕"; gname2 = "出巢"; break;
case 4:
gname = "進(jìn)在"; 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個(gè)棋子是否已經(jīng)處理過(guò),依次為:
// 1~4-兵, 5-張飛, 6-趙云, 7-馬超, 8-黃忠, 9-關(guān)羽, 10-曹操
for(i=0; i4; i++)
for(j=0; j5; j++)
{
if (grid[i][j] != 0)
if (layout[grid[i][j]-1] == 0) // 如果這個(gè)棋子沒(méi)有畫過(guò)
{
moveSelbox(i, j, false); // 畫一個(gè)棋子
layout[grid[i][j]-1] = 1; // 已經(jīng)畫過(guò)了,標(biāo)志置1
}
}
drawSelbox(); // 畫選擇框
SoundPlay(0); // 播放游戲開(kāi)始音效
}
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) // 當(dāng)前塊不是空的
{
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); //畫外框
}
}
// 移動(dòng)選擇框
// i,j當(dāng)前塊的(x,y)坐標(biāo), moving:true-移動(dòng)到該棋子,false-重畫該棋子
private void moveSelbox(int i, int j, boolean moving)
{
int ox = 0;
int oy = 0;
if (grid[i][j] != 0) // 該塊不是空的
{
if (i 0) // 左側(cè)
if (grid[i][j] == grid[i-1][j]) ox = -1;// 左側(cè)屬于同一個(gè)棋子
if (i 3) // 右側(cè)
if (grid[i][j] == grid[i+1][j]) ox = 1; // 右側(cè)屬于同一個(gè)棋子
if (j 0) // 上面
if (grid[i][j] == grid[i][j-1]) oy = -1;// 上面屬于同一個(gè)棋子
if (j 4) // 下面
if (grid[i][j] == grid[i][j+1]) oy = 1;// 下面屬于同一個(gè)棋子
}
if (moving)
{
offx = ox;
offy = oy;
drawSelbox(); // 移動(dòng)選擇框
}
else
drawBox(i, j, ox, oy, 0, false, 0, 1); // 畫棋子
}
// 向緩存畫棋子或外框:(px,py)焦點(diǎn)格坐標(biāo),(ox,oy)整塊相對(duì)焦點(diǎn)的偏移量,
// color顏色, fill是否填充黑色,bigbox是否大塊(分3級(jí)),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) // 按鍵響應(yīng)
{
if (gameOver) return; // 游戲結(jié)束了,不響應(yīng)
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; // 靠邊,移不動(dòng)
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; // 靠邊,移不動(dòng)
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; // 靠頂,移不動(dòng)
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; // 靠底,移不動(dòng)
else if (grid[posx][tmpy] == 0 grid[posx+offx][tmpy] == 0)
tmpy = posy+1;
else tmpy = posy;
}
// 重畫焦點(diǎn)塊
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();
}
// 計(jì)算、畫 步數(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); // 震動(dòng)100ms(Siemens擴(kuò)展)
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) // 菜單響應(yīng)
{
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]) // 按鍵設(shè)置
{
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擴(kuò)展)
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); // 緩存內(nèi)數(shù)據(jù)直接輸出到屏幕上
}
//音樂(lè)
private void SoundPlay(int n) // 播放音效
{
if (!Snd) return; // 音效關(guān)閉,則返回
// Siemens擴(kuò)展
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);
以上為核心代碼 由于代碼太多無(wú)法一次傳上來(lái)
java課程設(shè)計(jì)題目及代碼是什么?
java課程設(shè)計(jì)題目及代碼分別是:
1、題目:計(jì)算器。設(shè)計(jì)內(nèi)容是設(shè)計(jì)一個(gè)圖形界面(GUI)的計(jì)算器應(yīng)用程序,完成簡(jiǎn)單的算術(shù)運(yùn)算。
設(shè)計(jì)要求是設(shè)計(jì)的計(jì)算器應(yīng)用程序可以完成家法、減法、乘法、除法和取余運(yùn)算。且有小數(shù)點(diǎn)、正負(fù)號(hào)、求倒數(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課程設(shè)計(jì)題目及代碼是:
題目:華容道。編寫一個(gè)按鈕的子類,使用該子類創(chuàng)建的對(duì)象代表華容道中的人物。通過(guò)焦點(diǎn)事件控制人物顏色,當(dāng)人物獲得焦點(diǎn)時(shí)顏色為藍(lán)色,當(dāng)失去焦點(diǎn)時(shí)顏色為灰色。
通過(guò)鍵盤事件和鼠標(biāo)事件來(lái)實(shí)現(xiàn)曹操、關(guān)羽等人物的移動(dòng)。當(dāng)人物上發(fā)生鼠標(biāo)事件或鍵盤事件時(shí),如果鼠標(biāo)指針的位置是在人物的下方(也就是組件的下半部分)或按下鍵盤的“↓“鍵,該人物向下移動(dòng)。向左、向右和向上的移動(dòng)原理類似。
代碼是:
String name[]={"曹操","關(guān)羽","張","劉","馬","許","兵","兵","兵","兵"}.
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ù)字華容道解法
數(shù)字華容道是一款好玩又鍛煉大腦思維的游戲,在《最強(qiáng)大腦》節(jié)目中也曾被列為一個(gè)競(jìng)賽項(xiàng)目。數(shù)字華容道的玩法,只要掌握幾個(gè)關(guān)鍵點(diǎn),就能很快達(dá)成目標(biāo)。下面就介紹其基本的解法。
1、第一行解法
這一步不需要什么技巧,但是如果按照預(yù)先確定的順序排列,可以節(jié)省不少時(shí)間。
這里介紹兩種排列組合:一種是123+4組合,即先將123排列好,再將4移動(dòng)到右邊四個(gè)方格右下格。
2、第二行解法。
因?yàn)榈谝恍泻偷诙械慕夥ㄒ粯樱酝ǔR暈橐徊健?/p>
這一步首先按照567+8組合排列。然后11,12右移一格,5,6,7整體逆時(shí)針退一格,接著14左移一格,8上推一格,12右移一格,14退一格,最后5,6,7復(fù)位,第二行完成。
網(wǎng)頁(yè)題目:java數(shù)字華容道代碼,數(shù)字華容道c語(yǔ)言源代碼
路徑分享:http://www.dlmjj.cn/article/hdhcpe.html