1. 確定的功能:讓玩家通過(guò)按上下左右鍵推箱子,當(dāng)箱子們都推到了目的地后出現(xiàn)過(guò)關(guān)信息,并顯示下一關(guān)。推錯(cuò)了玩家還按空格鍵從新玩過(guò)這關(guān)。直到過(guò)完全部關(guān)卡。
10年積累的成都網(wǎng)站建設(shè)、做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有貢覺(jué)免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
2. 定義的核心數(shù)據(jù)結(jié)構(gòu):我們定義一個(gè)二維數(shù)組ghouse來(lái)記錄屏幕上各點(diǎn)的狀態(tài)。char ghouse[20][20]; 其中:0表示什么都沒(méi)有,'b'表示箱子,'w'表示墻壁,'m'表示目的地,'i'表示箱子在目的地。
3. 對(duì)整個(gè)進(jìn)行功能模塊劃分。
(1)。初始化:在屏幕上輸出歡迎信息,把ghouse數(shù)組的元素初始化為0。并根據(jù)各關(guān)的要求在屏幕上輸出墻、箱子、目的地和人。并用ghouse 數(shù)組記錄各點(diǎn)的狀態(tài)。
(2)。進(jìn)入游戲循環(huán):這個(gè)游戲主循環(huán)是等待按鍵。當(dāng)接受到上下左右鍵時(shí)執(zhí)行相關(guān)操作:接受ESC鍵時(shí)退出游戲;接受空格鍵時(shí)返回本關(guān)開(kāi)頭;接受無(wú)效按鍵時(shí)做忽略處理。重點(diǎn)介紹按上下左右鍵時(shí)如何執(zhí)行相關(guān)操作。
(3)。判斷是否過(guò)關(guān):用一個(gè)鏈表win由每關(guān)的初始化函數(shù)傳給main函數(shù)。Win鏈表主要記錄屏幕上的哪些點(diǎn)是目的地,并記錄目的地的位置。Main函數(shù)每執(zhí)行一次操作后就判斷屏幕上的目的地是不是都有箱子了。
我寫(xiě)的一個(gè)猜數(shù)字游戲,希望對(duì)你有用,代碼如下:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class GuessNumber {
static int trys, A, B;
static String r, t;
public static int[] MakeGuessNumber(){//隨機(jī)生成一個(gè)無(wú)重復(fù)數(shù)字的四位數(shù)
Random r = new Random();
int guess[] = new int[4];
for(int i=0; i4; i++){
guess[i] = r.nextInt(10);
for(int j=i-1; j=0; j--){
if(guess[i]==guess[j]){
i--;
break;}
}
}
return guess;
}
public static String getRundom(){//將此四位數(shù)轉(zhuǎn)化為字符串
int guess[]=MakeGuessNumber();
return ""+guess[0]+guess[1]+guess[2]+guess[3];
}
public static void messageDialog(Object o){
JOptionPane.showMessageDialog(null, o);
}
public static void guessNumber(){//主要算法實(shí)現(xiàn)部分
r=getRundom();
//System.out.println(r);
JFrame jf=new JFrame();
JButton b1=new JButton("新游戲");
JLabel l1=new JLabel("輸入:");
final JTextField jtf=new JTextField(10);
JButton b2=new JButton("提交");
final JTextArea jta=new JTextArea(10,10);
jta.append(" "+"Guess"+" "+"Result"+"\n");
JScrollPane scrollPane=new JScrollPane(jta);
JPanel jp1=new JPanel();
jp1.add(l1);
jp1.add(jtf);
jp1.add(b2);
jf.add(b1,BorderLayout.NORTH);
jf.add(jp1,BorderLayout.CENTER);
jf.add(scrollPane,BorderLayout.SOUTH);
b1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
trys=0;
A=0;
B=0;
jta.setText(" "+"Guess"+" "+"Result"+"\n");
jtf.setText("");
r=getRundom();
//System.out.println(r);
}
});
b2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
t=jtf.getText();
A=0;
B=0;
if(t.length()!=4||t.substring(0, 1).equals(t.substring(1, 2))
||t.substring(0, 1).equals(t.substring(2, 3))
||t.substring(0, 1).equals(t.substring(3, 4))
||t.substring(1, 2).equals(t.substring(2, 3))
||t.substring(1, 2).equals(t.substring(3, 4))
||t.substring(2, 3).equals(t.substring(3, 4))
||!t.matches("[0-9]*"))
messageDialog("Wrong Input!");
else{
jtf.setText("");
trys++;
if(t.substring(0, 1).equals(r.substring(0, 1)))
A++;
if(t.substring(0, 1).equals(r.substring(1, 2)))
B++;
if(t.substring(0, 1).equals(r.substring(2, 3)))
B++;
if(t.substring(0, 1).equals(r.substring(3, 4)))
B++;
if(t.substring(1, 2).equals(r.substring(1, 2)))
A++;
if(t.substring(1, 2).equals(r.substring(0, 1)))
B++;
if(t.substring(1, 2).equals(r.substring(2, 3)))
B++;
if(t.substring(1, 2).equals(r.substring(3, 4)))
B++;
if(t.substring(2, 3).equals(r.substring(2, 3)))
A++;
if(t.substring(2, 3).equals(r.substring(0, 1)))
B++;
if(t.substring(2, 3).equals(r.substring(1, 2)))
B++;
if(t.substring(2, 3).equals(r.substring(3, 4)))
B++;
if(t.substring(3, 4).equals(r.substring(3, 4)))
A++;
if(t.substring(3, 4).equals(r.substring(0, 1)))
B++;
if(t.substring(3, 4).equals(r.substring(1, 2)))
B++;
if(t.substring(3, 4).equals(r.substring(2, 3)))
B++;
jta.append(trys+" "+t+" "+A+"A"+B+"B"+"\n");
if(A==4){
if(trys=4)
messageDialog("You win after "+trys+" trys!");
else if(trys=3)
messageDialog("You win after only "+trys+" trys!");
}
}
}
});
jf.setSize(300, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
guessNumber();
}
}
我沒(méi)有進(jìn)行詳細(xì)注釋?zhuān)@個(gè)程序挺好理解的,你可以自己再看一下
Newload()
{
jf1=new JFrame("猜數(shù)游戲");
jf2=new JFrame("猜數(shù)游戲");
jf3=new JFrame("猜數(shù)游戲");
jf1_title=new JLabel("猜數(shù)游戲-歡迎進(jìn)入");
jf1_title.setFont(new Font("仿宋體",Font.BOLD,40));//設(shè)置字體大小,及文字字體
jf1_title.setHorizontalAlignment(JLabel.CENTER);
JLabel jf2title=new JLabel("猜數(shù)游戲");
jf2title.setFont(new Font("仿宋體",Font.BOLD,40));//設(shè)置字體大小,及文字字體
jf2title.setHorizontalAlignment(JLabel.CENTER);
jf1_username=new JLabel("用戶(hù)名");
jf1_userpass=new JLabel("密碼");
jf2_question=new JLabel("There is question which needs you to guess!");
jf2_question.setFont(new Font("仿宋體",Font.BOLD,20));//設(shè)置字體大小,及文字字體
jf2_question.setHorizontalAlignment(JLabel.CENTER);
jf2_rightface=new JLabel(iron1);
jf2_wrongface=new JLabel(iron2);
jf2_rightface.setVisible(false);
jf2_wrongface.setVisible(false);
jf2_reelresult=new JLabel();
jf3_pinyu=new JLabel("your result is");
jf1_usernameT=new JTextField(6);
jf2_anwser=new JTextField(6);
jf2_anwser.addActionListener(this);
jf1_password=new JPasswordField(6);
jf1_password.addActionListener(this);
jf1_ok=new JButton("確定");
jf1_ok.addActionListener(this);
jf1_quit=new JButton("退出");
jf1_quit.addActionListener(this);
jf2_newgame=new JButton("新游戲(k)");
jf2_newgame.setMnemonic(KeyEvent.VK_K);
jf2_newgame.addActionListener(this);
jf2_ok=new JButton("確定");
jf2_ok.addActionListener(this);
jf1.setLayout(new BorderLayout());
jf2.setLayout(new BorderLayout());
JPanel jf1p1=new JPanel(),jf2p1=new JPanel(),jf2p2=new JPanel(),jf2p3=new JPanel();
jf2p1.setLayout(new BorderLayout());
jf1p1.setLayout(new FlowLayout());
jf2p2.setLayout(new FlowLayout());
jf2p3.setLayout(new FlowLayout());
jf1.add(jf1_title,"Center");
jf1p1.add(jf1_username);jf1p1.add(jf1_usernameT);
jf1p1.add(jf1_userpass);jf1p1.add(jf1_password);
jf1p1.add(jf1_ok);jf1p1.add(jf1_quit);
jf1.add(jf1p1,"South");
jf2p2.add(jf2_rightface);
jf2p2.add(jf2_wrongface);
jf2p2.add(jf2_reelresult);
jf2p1.add(jf2p2,"South");
jf2p1.add(jf2_question);
jf2.add(jf2title,"North");
jf2.add(jf2p1,"Center");
jf2p3.add(jf2_ans);jf2p3.add(jf2_anwser);jf2p3.add(jf2_ok);jf2p3.add(jf2_newgame);
jf2.add(jf2p3,"South");
jf3.add(jf3_pinyu);
jf1.setSize(700,400);
jf2.setSize(700,400);
jf3.setSize(700,400);
jf1.setLocation(300,150);
jf2.setLocation(300,150);
jf3.setLocation(300,150);
jf1.setVisible(true);
jf2.setVisible(false);
jf3.setVisible(false);
jf1.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jf2.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jf1_ok||e.getSource()==jf1_password)
{char[] a=jf1_password.getPassword();String paas="";
for(int i=0;ia.length;i++)//JPasswordField是一種特殊的類(lèi)只能得到char數(shù)組,將其轉(zhuǎn)成String
paas=paas+a[i];
if(jf1_usernameT.getText().equals("user")paas.equals("pass"))
{jf2.setVisible(true);jf1.setVisible(false);
number=returnquestion();
jf2_anwser.requestFocus();
}
else
JOptionPane.showMessageDialog(null,"用戶(hù)名不正確或密碼錯(cuò)誤!");
}
if(e.getSource()==jf1_quit)
{
System.exit(0);
}
if(e.getSource()==jf2_ok||e.getSource()==jf2_anwser)
{
if(times=5){
if(Integer.parseInt(jf2_anwser.getText())==number)
{
jf2_rightface.setVisible(true);
jf2_wrongface.setVisible(false);
jf2_reelresult.setText("you are right! and your have used "+times+" times!"
+((times=3)?"very good!":"pleas do more work for it"));
}
else
if(Integer.parseInt(jf2_anwser.getText())number)
{times++;
jf2_wrongface.setVisible(true);
jf2_rightface.setVisible(false);
jf2_reelresult.setText("your answer is bigger than the one produced by computer!"
+"and your have used "+times+" times!");
}
else
if(Integer.parseInt(jf2_anwser.getText())number)
{times++;
jf2_wrongface.setVisible(true);
jf2_rightface.setVisible(false);
jf2_reelresult.setText("your answer is smaller than the one produced by computer!"
+"and your have used "+times+" times!");
}
}
else
{JOptionPane.showMessageDialog(null,"你已經(jīng)超過(guò)六次了,請(qǐng)重新開(kāi)始吧!");}
jf2_anwser.requestFocus();
jf2_anwser.setText("");
}
if(e.getSource()==jf2_newgame)
{
number=returnquestion();
times=0;
jf2_rightface.setVisible(false);
jf2_wrongface.setVisible(false);
jf2_anwser.setText("");
jf2_reelresult.setText("");
jf2_anwser.requestFocus();
}
}
public static void main(String args[])
{
new Newload();
}
int returnquestion()
{
double db=Math.random()*100;
return (int)db;
}
}
分享題目:簡(jiǎn)單推箱子java源代碼,java課程設(shè)計(jì)推箱子
文章轉(zhuǎn)載:http://sd-ha.com/article38/hooipp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站營(yíng)銷(xiāo)、移動(dòng)網(wǎng)站建設(shè)、ChatGPT、微信公眾號(hào)、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
網(wǎng)頁(yè)設(shè)計(jì)公司知識(shí)