A. 急求c語言編譯的小游戲(如掃雷),附帶源代碼和注釋。
小游戲2048:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<windows.h>
int jsk( ); //計算空格數
void rsgm( ); //重置游戲
void inkey( ); //按鍵輸入
void left( ); //向左移動
void right( ); //向右移動
void up( ); //向上移動
void down( ); //向下移動
void show( ); //輸出界面
void adnum( ); //添加隨機數
void yes( ); //游戲是否結束(1是0否)
void gtxy(int x, int y); //控制游標位置的函數
int a[4][4]; //存儲16個格子中的數字
int score = 0; //每局得分
int best = 0; //最高得分
int ifnum; //是否需要添加數字(1是0否)
int over; //游戲結束標志(1是0否)
int i,j,k;
int main( )
{ rsgm( ); //重置游戲
inkey( ); //按鍵輸入
return 0;
}
void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)
{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);
} //用於控制字元顏色的函數
void rsgm( ) //重置游戲
{ score = 0; ifnum = 1; over = 0; srand((unsigned)time(0)); //啟動隨機數發生器
int n = rand( ) % 16; //隨機函數產生0-15的數字
for (i = 0; i < 4; i++)
{for (j = 0; j < 4; j++)
{ if (n == 0) { int k = rand( ) % 3; if (k == 0 || k == 1) { a[i][j] = 2; }
else { a[i][j] = 4; } n--; }
else { a[i][j] = 0; n--; }
}
}
adnum( );
system("cls");
CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下兩行是隱藏游標的設置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
setColor(14, 0); //設置字體淡黃色,背景為黑色
printf(" 2048小游戲"); setColor(7, 0); //恢復白字黑底
printf(" ┌──────┬──────┬──────┬──────┐");
printf(" │ │ │ │ │");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │ │ │ │ │");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │ │ │ │ │");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │ │ │ │ │");
printf(" └──────┴──────┴──────┴──────┘");
show( );
}
void show( ) //輸出界面
{ for(i=0;i<4;i++)
for(j=0;j<4;j++)
{ gtxy(7*j+9,2*i+4); //gtxy(7*j+9, 2*i+4)是游標到指定位置輸出數字
if(a[i][j]==0){printf(" "); setColor(7, 0); printf("│");}
else if(a[i][j]<10){ if (a[i][j] == 2) {setColor(14, 0); }
else if (a[i][j] == 4) {setColor(13, 0); }
else if (a[i][j] == 8) {setColor(12, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
else if (a[i][j] < 100){if (a[i][j] == 16) {setColor(12, 0); }
else if (a[i][j] == 32) {setColor(10, 0); }
else if (a[i][j] == 64) {setColor(2, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
else if (a[i][j] < 1000) {if (a[i][j] == 128) {setColor(9, 0); }
else if (a[i][j] == 256) {setColor(1, 0); }
else if (a[i][j] == 512) {setColor(13, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
else if (a[i][j] < 10000) {if (a[i][j] == 1024) {setColor(5, 0); }
else {setColor(15, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
}
if (jsk( ) == 0)
{ yes( ); if (over) { gtxy(9,12); setColor(10, 0);
printf(" 游戲結束!是否繼續? [ Y/N ]:"); }
}
}
void inkey( ) //按鍵輸入
{ int key;
while (1)
{ key = getch( );
if (over) { if (key == 89|| key == 121) {rsgm( ); continue; }
else if (key == 78|| key == 110) { return; }
else continue; }
ifnum = 0;
if(key==224)key=getch( );
switch (key)
{ case 75: left( ); break;
case 77: right( ); break;
case 72: up( ); break;
case 80: down( );break;
}
if (score > best) { best = score; }
if (ifnum) { adnum( ); show( ); }
}
}
int jsk( ) //計算空格數
{ int n = 0;
for (i = 0; i < 4; i++)
{ for (j = 0; j < 4; j++) { if ( a[i][j] == 0) {n++;} } }
return n;
}
void left( ) //向左移動
{ for (i = 0; i < 4; i++)
{for (j = 1, k = 0; j < 4; j++)
{ if (a[i][j] > 0)
{ if ( a[i][k] == a[i][j])
{ a[i][k] *= 2; k++;
score = score + 2 * a[i][j];
a[i][j] = 0; ifnum = 1; }
else if ( a[i][k] == 0) { a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }
else { a[i][k + 1] = a[i][j]; if ((k + 1) != j) { a[i][j] = 0; ifnum = 1; }
k++; }
}
}
}
}
void right( ) //向右移動
{for (i = 0; i < 4; i++)
{for (j = 2, k = 3; j >= 0; j--)
{if (a[i][j] > 0)
{ if (a[i][k] == a[i][j])
{a[i][k] *= 2; k--; score = score + 2 * a[i][j]; a[i][j] = 0; ifnum = 1; }
else if ( a[i][k] == 0) {a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }
else { a[i][k - 1] = a[i][j]; if ((k - 1) != j) { a[i][j] = 0; ifnum = 1; } k--; }
}
}
}
}
void up( ) //向上移動
{for (i = 0; i < 4; i++)
{for (j = 1, k = 0; j < 4; j++)
{if (a[j][i] > 0)
{if ( a[k][i] == a[j][i]) { a[k][i] *= 2; k++;score = score + 2 * a[j][i];
a[j][i] = 0; ifnum = 1; }
else if ( a[k][i] == 0) { a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }
else { a[k + 1][i] = a[j][i]; if ((k + 1) != j) { a[j][i] = 0; ifnum = 1; }
k++; }
}
}
}
}
void down( ) //向下移動
{ for (i = 0; i < 4; i++)
{for (j = 2, k = 3; j >= 0; j--)
{if (a[j][i] > 0)
{if (a[k][i] == a[j][i])
{a[k][i] *= 2; k--;score = score + 2 * a[j][i]; a[j][i] = 0; ifnum = 1; }
else if (a[k][i] == 0) {a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }
else {a[k - 1][i] = a[j][i];
if ((k - 1) != j) {a[j][i] = 0; ifnum = 1; } k--; }
}
}
}
}
void adnum( ) //添加隨機數
{ srand(time(0)); int n = rand( ) % jsk( );
for (int i = 0; i < 4; i++)
{for (int j = 0; j < 4; j++)
{ if (a[i][j] == 0) {if (n != 0) { n--; }
else {int k = rand() % 3;
if (k == 0 || k == 1) {a[i][j] = 2; return; }
else {a[i][j] = 4; return; } }
}
}
}
}
void yes( ) //游戲是否結束
{ for (int i = 0; i < 4; i++)
{for (int j = 0; j < 3; j++)
{if (a[i][j] == a[i][j + 1] || a[j][i] == a[j + 1][i]) {over = 0; return; }}
}
over = 1;
}
void gtxy(int x, int y) //控制游標位置的函數
{ COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
B. JAVA,編一個游戲小游戲,比如掃雷,這個程序大概的代碼是什麼,誰能教教我我比較笨,但是我認學。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main
{
public static void main(String[] argus)
{
Landmine Zhang = new Landmine();
}
}
//
//櫻銀 Landmine類 主界面
class Landmine extends JFrame
{
static Resources resources = new Resources();
Playing listener = new Playing(this); //主要監聽者,監聽地雷面板的動作
Help helpListener = new Help(this); //輔助監聽者,監聽「幫助」、「關於」
JPanel landminePanel = new JPanel(); //創建地雷面板
JPanel topPanel = new JPanel(); //創建頂部面板
JPanel lowerPanel = new JPanel(); //創建底部面板
public static MyButton [][] lei; //主區按圓頌閉鈕組
public static int numberOfUnflaged ; //剩餘的雷數,顯示在topPanel上,用於提示用戶
public static int numberOfClicked; //已經翻開的格子數,當數字數字到"總格子數—雷數"時,即勝利
public static int usedTime; //已用時間
public static JLabel numberOfUnflagedLabel = new JLabel(); //創建剩雷數標簽
public static JLabel timeLabel = new JLabel();//創建時間標簽
public static Timer timer; //創建計時
Keylistener keyListener = new Keylistener(this);
public Landmine()
{
super("掃雷__1.2版__小老頭"); //標題
setBounds(300,90,800,800); //設置窗口位置和大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//最大化、最小化、關閉按鈕
BorderLayout ff = new BorderLayout(); //創建布局管理器
setLayout(ff); //關聯布局管理器
setResizable(false); //禁止改變窗口大小
/*初始化一些數據*/
numberOfClicked = 0;
numberOfUnflaged = 40;
usedTime = 0;
/*設置頂部面板*/
numberOfUnflagedLabel.setText("剩餘雷數:"+numberOfUnflaged);//顯示剩餘雷數
numberOfUnflagedLabel.setFont(resources.fontOne);//設置剩雷數標簽字體
numberOfUnflagedLabel.setIcon(resources.bombIconForLabel);//剩雷數標簽圖標(地雷形)
topPanel.add(numberOfUnflagedLabel); //剩雷數標簽加入topPanel
timeLabel.setText("用時:" + usedTime); //顯示剩餘時間
timeLabel.setFont(resources.fontOne); //設置時間標簽字體
timeLabel.setIcon(resources.clockIcon); /橘裂/設置時間標簽圖標
topPanel.add(timeLabel); //時間標簽加入topPanel
add(topPanel,BorderLayout.NORTH); //加入主面板上部
timer = new Timer(1000,new TimerListener());//計算器注冊監聽者
/*設置底部面板*/
JButton aboutJB = new JButton("關於"); //創建「關於」按鈕
JButton helpJB = new JButton("求救"); //創建「求救」按鈕
helpJB.addActionListener(helpListener); //"求救"按鈕加入監聽者
aboutJB.addActionListener(helpListener);//"關於"按鈕加入監聽者
helpJB.addKeyListener(keyListener);
aboutJB.addKeyListener(keyListener); //注冊按鍵監聽
lowerPanel.add(aboutJB); //「關於」按鈕加入lowerPanel
lowerPanel.add(helpJB); //「幫助」按鈕加入lowerPanel
add(lowerPanel,BorderLayout.SOUTH);
/*設置地雷面板*/
GridLayout dd = new GridLayout(16,16);
landminePanel.setLayout(dd); //布局管理
lei = new MyButton[18][18];
for(int i=0; i<18; ++i)
{//創建下標0—17的按鈕,18*18矩陣
for(int j=0; j<18; ++j)
{
lei[i][j] = new MyButton(i,j);
}
}
for(int i=1; i<17; ++i)
{//將下標1-16的按鈕,加入面板、設置圖標、翻開標記為假、加入監聽者
for(int j=1; j<17; ++j)
{
landminePanel.add(lei[i][j]); //按鈕加入地雷面板
lei[i][j].setIcon(resources.smallIcon); //設置按鈕圖標
lei[i][j].isClicked = false; //翻開標記設置為 假lei[i][j].setIcon(dead);
lei[i][j].addActionListener(listener); //加入監聽者
lei[i][j].addMouseListener(listener); //加入滑鼠事件監聽者
lei[i][j].addKeyListener(keyListener); //按鈕注冊按鍵監聽,當焦點在按鈕上是能監聽按鍵
}
}
add(landminePanel,BorderLayout.CENTER); //landminePanel加入主框架中央
addLandmine(); //布雷
timer.start(); //啟動計時器
setVisible(true);//顯示之
}
/*布雷*/
public static void addLandmine()
{//隨機將40的按鈕的是否為雷的標記isBomb設為真
for(int count = 0; count<40; /*blank*/)
{
int i = (int)(Math.random()*100 % 16 +1 ) ;
int j = (int)(Math.random()*100 % 16 +1 ) ;
if(lei[i][j].isBomb == false)
{
lei[i][j].isBomb = true;
count++;
}
}
}
class TimerListener implements ActionListener
{//內部類,時間監聽
public void actionPerformed(ActionEvent e)
{
usedTime++;
timeLabel.setText("用時:" + usedTime);
}
}
}
//
// Playing類 執行主要游戲操作
class Playing implements ActionListener,MouseListener
{
static Resources resources = new Resources();
public static Landmine gui;
public Playing(Landmine in )
{
gui = in;
}
public void actionPerformed(ActionEvent event)
{
MyButton receive = (MyButton)event.getSource();
if(receive.isBomb)
{//如果翻到了雷。。
for(int i=1; i<17; ++i)
{//將所有的雷圖標設為 「地雷」
for(int j=1; j<17; ++j)
{
if(gui.lei[i][j].isBomb)
gui.lei[i][j].setIcon(resources.bombIcon);
}
}
receive.setIcon(resources.deadIcon);//將踩到的地雷圖標設為 「衰」
gui.timer.stop(); //停止計時器
JOptionPane.showMessageDialog(null,"小朋友,你掛了…","失敗!",
JOptionPane.INFORMATION_MESSAGE,
resources.deadIcon);//提示失敗
int yourChose = JOptionPane.showConfirmDialog(null,"你可能是一不小心點錯了,再來一局?" );
if(yourChose == JOptionPane.OK_OPTION)
{//點擊「是」時
replay();
}
else
{//點擊 「否」 或 「取消」 時退出程序
System.exit(0);
}
}
else if(receive.isClicked ==false)
{//未翻到雷
showBombNumber(receive);
}
}
public static void showBombNumber(MyButton in)
{//翻開點擊的按鈕
int numberOfLandmine = 0;//記錄雷的個數
in.isClicked = true; //翻開標記設為真
/*檢測周圍8個方塊是否為雷*/
if(gui.lei[in.num_x-1][in.num_y-1].isBomb == true) numberOfLandmine++;//左上
if(gui.lei[in.num_x][in.num_y-1].isBomb == true) numberOfLandmine++; //上
if(gui.lei[in.num_x+1][in.num_y-1].isBomb == true) numberOfLandmine++;//右上
if(gui.lei[in.num_x+1][in.num_y].isBomb == true) numberOfLandmine++; //右
if(gui.lei[in.num_x+1][in.num_y+1].isBomb == true) numberOfLandmine++;//右下
if(gui.lei[in.num_x][in.num_y+1].isBomb == true) numberOfLandmine++; //下
if(gui.lei[in.num_x-1][in.num_y+1].isBomb == true) numberOfLandmine++;//左下
if(gui.lei[in.num_x-1][in.num_y].isBomb == true) numberOfLandmine++; //左
in.setIcon(new ImageIcon("images/"+numberOfLandmine+".png"));//根據周圍的雷數顯示數字圖標
gui.numberOfClicked++;//翻開格子數+1
if(gui.numberOfClicked==216)
{//翻開216個格子時游戲成功,用戶選擇是否再來一局
int yourChoice = JOptionPane.showConfirmDialog(null,"恭喜你成功了!再來一盤嗎?");
if(yourChoice == JOptionPane.OK_OPTION)
replay();
else
System.exit(0);
}
if(numberOfLandmine==0)
{//如果周圍無雷,則將周圍未翻開格子的全部翻開
if(gui.lei[in.num_x-1][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y-1]);
if(gui.lei[in.num_x][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x][in.num_y-1]);
if(gui.lei[in.num_x+1][in.num_y-1].isClicked == false)
showBombNumber(gui.lei[in.num_x+1][in.num_y-1]);
if(gui.lei[in.num_x+1][in.num_y].isClicked == false)
showBombNumber(gui.lei[in.num_x+1][in.num_y]);
if(gui.lei[in.num_x+1][in.num_y+1].isClicked == false)
showBombNumber(gui.lei[in.num_x+1][in.num_y+1]);
if(gui.lei[in.num_x][in.num_y+1].isClicked == false)
showBombNumber(gui.lei[in.num_x][in.num_y+1]);
if(gui.lei[in.num_x-1][in.num_y+1].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y+1]);
if(gui.lei[in.num_x-1][in.num_y].isClicked == false)
showBombNumber(gui.lei[in.num_x-1][in.num_y]);
}
}
public static void replay()
{//重新開始
gui.dispose(); //釋放框架資源
gui.timer.stop(); //終止計時器
Landmine ff = new Landmine();//重新創建一個主類的實例
//這幾條語句實現了重新開始————關閉上一個窗口,重新開啟一個
//但是這種方法會造成內存的浪費,一個改進的方法是不關閉當年窗口,而是將當前窗口重新初始化
}
public void mousePressed(MouseEvent e)
{//當滑鼠右鍵點擊時自動調用此函數
int mods = e.getModifiers();
MyButton receive = (MyButton)e.getSource();
if((mods & InputEvent.BUTTON3_MASK) != 0)
{//滑鼠右鍵
if(receive.isClicked == false)
{
receive.isRight = receive.isRight ? false : true;//改變receive.isRight的值
if(receive.isRight)
{//如果添加標記,則剩餘雷數-1,設置標簽為「旗幟」
gui.numberOfUnflaged--;
receive.setIcon(resources.flagIcon);
}
else
{//如果清除標記,則剩餘雷數+1,設置標簽為「未翻開」
gui.numberOfUnflaged++;
receive.setIcon(resources.smallIcon);
}
gui.numberOfUnflagedLabel.setText("剩餘雷數:"+gui.numberOfUnflaged);
//更新剩餘雷數標簽
}
}
}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
}
//
// Help類,響應「關於」、「求救」
class Help implements ActionListener
{
static Resources resources = new Resources();
public static Landmine gui;
public Help(Landmine in)
{
gui = in ;
}
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand()=="關於")
JOptionPane.showMessageDialog(null,"掃雷1.2版。。小老頭出品");
if(event.getActionCommand()=="求救")
help();
}
public static void help()
{//求救
int stopNumber = (int)(Math.random() * gui.numberOfUnflaged + 1 );
int count = 0;
for(int i=1; i<17;++i )
{
for(int j=1; j<17; ++j)
{
if( gui.lei[i][j].isBomb && !gui.lei[i][j].isClicked && !gui.lei[i][j].isRight )
{
count++;
}
if(count == stopNumber)
{
gui.lei[i][j].setIcon(resources.badIcon);
return;
}
}
}
}
}
//
// Keylistener類,響應鍵盤事件
class Keylistener implements KeyListener
{
static Resources resources = new Resources();
Landmine gui;
public Keylistener(Landmine in)
{
gui = in;
}
public void keyPressed(KeyEvent e)
{//有鍵按下時自動執行該方法
if(e.getKeyCode() == KeyEvent.VK_UP)
{//按鍵為 向上 時,將所有未標記的地雷顯示出
for(int i=1; i<17; ++i)
{
for(int j=1; j<17; ++j)
{
if(gui.lei[i][j].isBomb && !gui.lei[i][j].isRight)
gui.lei[i][j].setIcon(resources.badIcon);
}
}
}
if(e.getKeyCode() == KeyEvent.VK_DOWN)
{//按鍵為 向下 時,將所有未標記的地雷恢復為未點擊的圖標
for(int i=1; i<17; ++i)
{
for(int j=1; j<17; ++j)
{
if(gui.lei[i][j].isBomb && !gui.lei[i][j].isRight)
gui.lei[i][j].setIcon(resources.smallIcon);
}
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
//
// 按鈕類 MyBtton
class MyButton extends JButton
{
public int num_x,num_y; //第幾號方塊
public boolean isBomb; //是否為雷
public boolean isClicked; //是否被點擊
public int BombFlag; //探雷標記
public boolean isRight; //是否點擊右鍵
public MyButton(int x, int y)
{
BombFlag = 0;
num_x = x;
num_y = y;
isBomb = false;
isClicked = true;
isRight = false;
}
}
//
// 資源類 其他類中用到的圖標,字體等
class Resources
{
public static ImageIcon deadIcon;
public static ImageIcon smallIcon;
public static ImageIcon clockIcon;
public static ImageIcon bombIcon;
public static ImageIcon flagIcon;
public static ImageIcon badIcon;
public static ImageIcon bombIconForLabel;
public static Font fontOne;
public Resources()
{
deadIcon = new ImageIcon("images/dead.gif");
smallIcon = new ImageIcon("images/smallIcon.png");
clockIcon = new ImageIcon("images/clock2.png");
bombIcon = new ImageIcon("images/bomb.png");
flagIcon = new ImageIcon("images/flag_2.png");
badIcon = new ImageIcon("images/bad.gif");
bombIconForLabel = new ImageIcon("images/bombForLabel.gif");
fontOne = new Font("null",Font.BOLD,20);
}
}
C. 掃雷的作弊方法
1.打開掃雷游戲;2.開始游戲,點開一塊做盯御;3.在沒有點中雷而結束游戲之前的任何一個時刻,按下 開始鍵+D(開始鍵就是左邊ctrl和alt中間的那個鍵);4.這個時候你會發現游戲窗口變成了最小化在屏幕最下面的則鄭任務欄里;5.在任務欄里選中掃雷,讓它成為原先純岩大小的當前工作窗口;
D. 我是個1年級學生,請告訴我掃雷游戲的秘籍,
一個數字表示圍繞這個數字的八個格子中共含有幾個地雷
例:當翻了一個格子後,顯示的數字是「5」。那麼說慧態明周圍有五個地雷。如果「5」這個格子周圍已經翻開了三個格子的話散碧升,那麼剩餘的五個格子就都有地雷
把已經知道有或以為有地雷的格子上,按右鍵時。會在那上面留上一隻三角紅旗符號。不過,紅旗標注的地方未必真的有地雷,而沖老「地雷」的贏法就是快准地標注所有有地雷的格子。
E. 那位高手能給一下用C#編寫的掃雷小游戲代碼
我有個java的
///////////////
package jy;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ExtendButton extends JButton
{
private int Button_Pos;
private boolean Button_Status;
private boolean Button_Visited;
public int SetPostion(int pos)
{
this.Button_Pos = (pos >= 0 && pos <= 81) ? pos : 0;
return this.Button_Pos ;
}
public int GetPostion()
{
return this.Button_Pos;
}
public boolean SetStatus(boolean sta)
{
this.Button_Status = sta;
return this.Button_Status;
}
public boolean GetStatus()
{
return this.Button_Status;
}
public boolean Visited()
{
return this.Button_Visited;
}
public boolean SetVisited(boolean vis)
{
this.Button_Visited = vis;
return this.Button_Visited;
}
}
/////////////////
package jy;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game extends JFrame implements ActionListener{
private Container myContainer;
private GridLayout myLayout;
ExtendButton[] myButton = new ExtendButton[81];
private Game()
{
this.setTitle("Game");
this.setSize( 500,500 );
this.InitButton();
}
private void InitButton()
{
myContainer = getContentPane();
myLayout = new GridLayout( 9, 9, 1, 1 );
myContainer.setLayout( myLayout );
for(int i=0; i < 81; i++)
{
myButton[i] = new ExtendButton();
myButton[i].SetPostion(i);
myContainer.add(myButton[i]);
myButton[i].addActionListener( this );
}
System.gc();
this.SetBomb(13);
show();
}
private void SetBomb(int count)
{
int counter = 0;
int tempint;
while(counter != count)
{
tempint = ( int )(Math.random() * 81);
if(!myButton[tempint].GetStatus())
{
myButton[tempint].SetStatus(true);
counter++;
}
}
}
private void ShowBomb()
{
for(int i = 0; i < 81; i++)
{
if(myButton[i].GetStatus())
{
myButton[i].setBackground( new Color(0,0,0) );
}
}
}
private void CheckButton(ExtendButton TempButton)
{
if(TempButton.GetStatus())
{
JOptionPane.showMessageDialog( null, "You Failed!", "Game",JOptionPane.INFORMATION_MESSAGE );
this.ShowBomb();
return;
}
int[] CircleNum = new int[8];
int temp = 0;
if(!TempButton.Visited())
{
CircleNum[0] = TempButton.GetPostion() - 9;
CircleNum[0] = (CircleNum[0] < 0) ? -1 : CircleNum[0];
CircleNum[1] = TempButton.GetPostion() - 8;
CircleNum[1] = (CircleNum[1] < 0 || CircleNum[1] % 9 == 0) ? -1 : CircleNum[1];
CircleNum[2] = TempButton.GetPostion() + 1;
CircleNum[2] = (CircleNum[2] % 9 == 0) ? -1 : CircleNum[2];
CircleNum[3] = TempButton.GetPostion() + 10;
CircleNum[3] = (CircleNum[3] > 80 || CircleNum[3] % 9 == 0) ? -1 : CircleNum[3];
CircleNum[4] = TempButton.GetPostion() + 9;
CircleNum[4] = (CircleNum[4] > 80) ? -1 : CircleNum[4];
CircleNum[5] = TempButton.GetPostion() + 8;
CircleNum[5] = (CircleNum[5] > 80 || CircleNum[5] % 8 == 0) ? -1 : CircleNum[5];
CircleNum[6] = TempButton.GetPostion() - 1;
CircleNum[6] = (CircleNum[6] % 8 == 0) ? -1 : CircleNum[6];
CircleNum[7] = TempButton.GetPostion() - 10;
CircleNum[7] = (CircleNum[7] < 0 || CircleNum[7] % 8 == 0) ? -1 : CircleNum[7];
for(int i = 0; i < 8; i++)
{
if(CircleNum[i] != -1)
{
if(myButton[CircleNum[i]].GetStatus() && !myButton[CircleNum[i]].Visited()) temp++;
}
}
if(temp > 0)
{
TempButton.SetVisited( true );
TempButton.setText( String.valueOf(temp) );
temp = 0;
}
else if(temp == 0)
{
TempButton.SetVisited( true );
TempButton.setBackground(new Color( 125,152,0 ));
for(int i = 0; i < 8; i++)
{
if(CircleNum[i] != -1 && !myButton[CircleNum[i]].Visited())
{
CheckButton(myButton[CircleNum[i]]);
}
}
}
}
}
public void actionPerformed(ActionEvent e)
{
CheckButton((ExtendButton)e.getSource());
}
public static void main(String[] args)
{
Game newGame = new Game();
newGame.addWindowListener(
new WindowAdapter(){
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
}
);
}
}
//當然我還有java的俄羅斯方塊,如果你需要我可以給你不過..輸入有限
F. 僅用c語言能編出哪些小游戲
可以編寫狼追兔子游戲,擲骰子游戲,24點游戲,井字棋游戲,農夫過河游戲,掃雷小游戲,人機猜數游戲,三色球游戲, 推箱子游戲,坦克大戰游戲,貪吃蛇游戲等。