![]()  | 
		
			
  | 	
	
	
		
		|||||||
| 論壇說明 | 
| 
				歡迎您來到『史萊姆論壇』  ^___^ 您目前正以訪客的身份瀏覽本論壇,訪客所擁有的權限將受到限制,您可以瀏覽本論壇大部份的版區與文章,但您將無法參與任何討論或是使用私人訊息與其他會員交流。若您希望擁有完整的使用權限,請註冊成為我們的一份子,註冊的程序十分簡單、快速,而且最重要的是--註冊是完全免費的! 請點擊這裡:『註冊成為我們的一份子!』  | 
		
     
![]()  | 
    
    
| 
         | 
    主題工具 | 顯示模式 | 
| 
	 | 
| 
			
			 | 
		#1 | 
| 
			
			 長老會員 
			![]() ![]()  | 
	
	
	
		
		
			
			 開這個版面希望以小弟的程式設計經驗和大家互相學習,也請大家不吝賜教 
		
		
		
	[猜數字]遊戲是請猜的人輸入一組四位數的數字,若猜對數字和位置就以"A"表示,若數字對,位置不對則以"B"表(小弟小時候很喜歡玩) 以下是小弟表單的排列: 語法: 
	object Form1: TForm1
  Left = 192
  Top = 107
  BorderStyle = bsDialog
  Caption = '猜數字'
  ClientHeight = 163
  ClientWidth = 233
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object SpeedButton1: TSpeedButton
    Left = 16
    Top = 56
    Width = 73
    Height = 25
    Caption = '我猜'
    Flat = True
    OnClick = SpeedButton1Click
  end
  object SpeedButton2: TSpeedButton
    Left = 16
    Top = 88
    Width = 73
    Height = 25
    Caption = '重來'
    Flat = True
    OnClick = SpeedButton2Click
  end
  object SpeedButton3: TSpeedButton
    Left = 16
    Top = 128
    Width = 73
    Height = 25
    Caption = '離開'
    Flat = True
    OnClick = SpeedButton3Click
  end
  object Edit1: TEdit
    Left = 8
    Top = 8
    Width = 89
    Height = 40
    Font.Charset = ANSI_CHARSET
    Font.Color = clWindowText
    Font.Height = -27
    Font.Name = 'Fixedsys'
    Font.Style = []
    MaxLength = 4
    ParentFont = False
    TabOrder = 0
    OnChange = Edit1Change
  end
  object ListBox1: TListBox
    Left = 104
    Top = 8
    Width = 121
    Height = 145
    ItemHeight = 13
    TabOrder = 1
  end
end
請在上面第6行TForm1 *Form1; 加一段程式碼String num="5431"; 在設計初,若程式有些是屬於亂數控制的值,您可以先將他設成固定值,這樣除錯會比較方便 再來就是當使用者資料輸入完後按下[我猜]按鈕時的程式碼 語法: 
	void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
    int an=0,bn=0;
    for(int i=1;i<=4;i++){
        if(Edit1->Text.SubString(i,1)==num.SubString(i,1)) an++;  //位置與數字均正確的數量
        for(int j=1;j<=4;j++)
            if(Edit1->Text.SubString(i,1)==num.SubString(j,1)) bn++;  //只有數字正確的數量
    }
    ListBox1->Items->Add(Edit1->Text+" => "+IntToStr(an)+"A"+IntToStr(bn)+"B"); //將結果顯示在ListBox1裡
    if(an==4) ListBox1->Items->Add("猜對了...");  /若是數字位置全對的數目有4組,表示猜對了
    Edit1->Text="";
}
主要原因是if(Edit1->Text.SubString(i,1)==num.SubString(j,1)) bn++;多加了一次... 應該改成if(Edit1->Text.SubString(i,1)==num.SubString(j,1)) if(i!=j)bn++;(判對若位置不同才相加) 不過對於輸入我們也不允許使用者輸入重複的數字,所以我們必須追加限制 當然加在TEdit的OnChange事件 語法: 
	void __fastcall TForm1::Edit1Change(TObject *Sender)
{
    for(int i=1;i<Edit1->Text.Length();i++)
        if(Edit1->Text.SubString(Edit1->Text.Length(),1)==Edit1->Text.SubString(i,1)){
            Edit1->Text=Edit1->Text.Delete(Edit1->Text.Length(),1);
            Edit1->SelStart=Edit1->Text.Length();
        }
}
再來剩下[重來]與[關閉]的按鈕 目前是這樣設計的 [重來]按鈕 語法: 
	void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
    ListBox1->Clear();
}
語法: 
	void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
    Close();
}
<未完待續>  | 
| 
		
		
		
				__________________ 地獄變現記 [人身得來不易,願大家能尊重生命--勿傷胎命][好淫者請好自為之吧--割鳥] 南泉禪師道: 「道不屬知,不屬不知。知是妄覺,不知是無記。若真達不疑之道,猶如太虛,廓然蕩豁,豈可強是非邪?」 德山宣鑒禪師: 「如果明白無事,則勿妄求,妄求而得,亦非得也。汝但無事於心,無心於事,則虛而靈,空而妙。若毛端許,言之本末者,皆為自欺。何故?毫氂繫念,三塗業因。瞥爾情生,萬劫羈鎖。聖名凡號,盡是虛聲。殊相劣形,皆是幻色。汝欲求之,得無累乎?」  | 
|
| 
		 | 
	
	
	
		
	
	 
		送花文章: 5469,
		
			
		 
	
 
	
		
	
				 
		
		
		
		
		
		
			
		
		
		
		
		
			
					
						
					
			
		
		
		
		
		
			
				
			
		
		
		
	 | 
| 
			
			 | 
		#4 (permalink) | 
| 
			
			 長老會員 
			![]() ![]()  | 
	
	
	
		
		
			
			 再來輸入判斷既然已經完成 
		
		
		
	當然就是亂數部分啦 首先將第6行的 String num="5431"; 改成 String num; 接著亂數產生時機應該在表單的OnCreat事件 語法: 
	void __fastcall TForm1::FormCreate(TObject *Sender)
{
  int s;
  randomize();
  num=random(10);
  while(1){
    s=random(10);
    for(int i=1;i<=num.Length();i++){
      if(IntToStr(s)==num.SubString(i,1)) {s=0; break;}
    }
    if(s!=0) num=num+IntToStr(s);
    if(num.Length()==4) break;
  }
  ShowMessage(num);
}
while{...}是為了防止出現重複數字 好了,大家玩玩看吧!!! 然而大家或許會發現仍有些問題,譬如當您猜對後依然可以繼續猜 這時我們必須修改[我猜(SpeedButton1)]的OnClick事件,如下 語法: 
	void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
    int an=0,bn=0;
    for(int i=1;i<=4;i++){
        if(Edit1->Text.SubString(i,1)==num.SubString(i,1)) an++;
        for(int j=1;j<=4;j++)
            if(Edit1->Text.SubString(i,1)==num.SubString(j,1)) if(i!=j)bn++;
    }
    ListBox1->Items->Add(Edit1->Text+" => "+IntToStr(an)+"A"+IntToStr(bn)+"B");
    if(an==4) {
        ListBox1->Items->Add("猜對了...");
        SpeedButton1->Enabled=false;
        Edit1->Enabled=false;
    }
    Edit1->Text="";
}
語法: 
	void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
    ListBox1->Clear();
    num="";
    FormCreate(Sender);
    SpeedButton1->Enabled=true;
    Edit1->Enabled=true;
    Edit1->SetFocus();
}
下個禮拜再與大家討論利用Socket物件設計可和好友網路對戰吧 <  | 
| 
		 | 
	
	
	
		
	
	 
		送花文章: 5469,
		
			
		 
	
 
	
		
	
				 
		
		
		
		
		
		
			
		
		
		
		
		
		
		
		
		
			
				
						
				
			
		
		
		
	 | 
| 
			
			 | 
		#5 (permalink) | 
| 
			
			 長老會員 
			![]() ![]()  | 
	
	
	
		
		
			
			 剛剛想到還可以在加入一個功能,就是當使用者按下Enter時等於按下[我猜] 
		
		
		
	這是利用Edit1的OnKeyPress事件,如下: 語法: 
	void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
  if((Key==13)&&(Edit1->Text.Length()==4))SpeedButton1Click(Sender);
}
<  | 
| 
		 | 
	
	
	
		
	
	 
		送花文章: 5469,
		
			
		 
	
 
	
		
	
				 
		
		
		
		
		
		
			
		
		
		
		
		
		
		
		
		
			
				
						
				
			
		
		
		
	 |