| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1261 人关注过本帖, 1 人收藏
标题:我用Delphi写的第一个程序[猜数字],高手请评点一下
只看楼主 加入收藏
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏(1)
 问题点数:0 回复次数:3 
我用Delphi写的第一个程序[猜数字],高手请评点一下

我打包后还203K传 不上来,我把源码贴上

unit Unit1;

interface

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ExtCtrls;

type TForm1 = class(TForm) SpeedButton1: TSpeedButton; SpeedButton2: TSpeedButton; SpeedButton3: TSpeedButton; SpeedButton4: TSpeedButton; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Button6: TButton; Button7: TButton; Button8: TButton; Button9: TButton; Button10: TButton; Button0: TButton; Button12: TButton; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; Panel1: TPanel; ListBox1: TListBox; procedure Edit1KeyPress(Sender: TObject; var Key: Char); procedure Edit3KeyPress(Sender: TObject; var Key: Char); procedure Edit4KeyPress(Sender: TObject; var Key: Char); procedure Edit2KeyPress(Sender: TObject; var Key: Char); procedure SpeedButton2Click(Sender: TObject); procedure SpeedButton1Click(Sender: TObject); procedure SpeedButton3Click(Sender: TObject); procedure SpeedButton4Click(Sender: TObject); function ProcInput(KeyChar: Char; Index: Integer;SelStart: Integer):Char; function GetBlank(Skip: Integer):Integer; function GetText(Index: Integer):Char; procedure ProcFocus(Index: Integer); procedure ProcButtonClick(Index: Integer); procedure CountRightNum(); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure Button6Click(Sender: TObject); procedure Button7Click(Sender: TObject); procedure Button8Click(Sender: TObject); procedure Button9Click(Sender: TObject); procedure Button0Click(Sender: TObject); procedure Button10Click(Sender: TObject); procedure Button12Click(Sender: TObject); procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure Edit2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure Edit3KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure Edit4KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); private { Private declarations } public { Public declarations } end;

var Form1: TForm1; TrueNum : String; implementation

uses Unit2;

{$R *.DFM}

//处理按钮输入 procedure TForm1.ProcButtonClick(Index: Integer); var tmp: String; begin case Index of 0..9: //数字键 begin if GetBlank(0)=5 then Exit; tmp:='Button'+inttostr(Index); if Assigned(FindComponent(tmp)) then (FindComponent(tmp) as TButton).Enabled:=False;

tmp:='Edit'+intTostr(GetBlank(0)); if Assigned(FindComponent(tmp)) then (FindComponent(tmp) as TEdit).Text:=IntToStr(Index); if GetBlank(0)=5 then Button12.Enabled:=True; end;

10: //清除(C) begin Edit1.Text:=''; Edit2.Text:=''; Edit3.Text:=''; Edit4.Text:=''; Button1.Enabled:=True; Button2.Enabled:=True; Button3.Enabled:=True; Button4.Enabled:=True; Button5.Enabled:=True; Button6.Enabled:=True; Button7.Enabled:=True; Button8.Enabled:=True; Button9.Enabled:=True; Button0.Enabled:=True; Button12.Enabled:=False; Edit1.SetFocus; end; 12: //确定(OK) begin CountRightNum; ProcButtonClick(10); end; end; end;

procedure TForm1.CountRightNum; var RightNum: Integer; PosRightNum: Integer; begin RightNum:=0; PosRightNum:=0; if Edit1.Text[1]=TrueNum[1] then RightNum:=RightNum+1; if Edit2.Text[1]=TrueNum[2] then RightNum:=RightNum+1; if Edit3.Text[1]=TrueNum[3] then RightNum:=RightNum+1; if Edit4.Text[1]=TrueNum[4] then RightNum:=RightNum+1; if Pos(Edit1.Text[1],TrueNum)>0 then PosRightNum:=PosRightNum+1; if Pos(Edit2.Text[1],TrueNum)>0 then PosRightNum:=PosRightNum+1; if Pos(Edit3.Text[1],TrueNum)>0 then PosRightNum:=PosRightNum+1; if Pos(Edit4.Text[1],TrueNum)>0 then PosRightNum:=PosRightNum+1; PosRightNum:=PosRightNum-RightNum; ListBox1.Items.Add(Edit1.Text+Edit2.Text+Edit3.Text+Edit4.Text+'→'+inttostr(RightNum)+'A'+inttostr(PosRightNum)+'B'); if RightNum=4 then application.messagebox(PChar('恭喜您,成功了!'),'关于',mb_ok +MB_ICONINFORMATION) end; //获得某文本框内容(Index=1..4) function TForm1.GetText(Index: Integer):Char; var tmp: String; begin Result:=#0; tmp:='Edit'+inttostr(Index); if Assigned(FindComponent(tmp)) then

if (FindComponent(tmp) as TEdit).Text<>'' then Result:=(FindComponent(tmp) as TEdit).Text[1] else Result:=#0; end; //过滤键盘输入(Index=1..4) function TForm1.ProcInput(KeyChar: Char; Index: Integer; SelStart: Integer):Char; var Flags: Boolean; tmp: String; InputNum: Char; begin Result:=#0; Flags:=False; case KeyChar of '0'..'9': InputNum:=KeyChar; #8: begin InputNum:=KeyChar; Flags:=True; end; else Result:=#0; exit; end; //设置按钮是否有效 if (InputNum>='0') and (InputNum<='9') then begin if GetText(Index)<>'' then exit; tmp:='Button'+inttostr(strtoint(InputNum)); if Assigned(FindComponent(tmp)) then if (FindComponent(tmp) as TButton).Enabled=False then begin ProcInput:=#0; Flags:=True; end else begin ProcInput:=InputNum; (FindComponent(tmp) as TButton).Enabled:=False; if GetBlank(Index)=5 then Button12.Enabled:=True else Button12.Enabled:=False; end; end

else if InputNum=#8 then begin if SelStart=0 then Exit; Result:=InputNum; tmp:='Button'+GetText(Index); if Assigned(FindComponent(tmp)) then if (FindComponent(tmp) as TButton).Enabled=False then begin (FindComponent(tmp) as TButton).Enabled:=True; Button12.Enabled:=False; end end; //设置焦点 if Flags=False then ProcFocus(index); end;

//获取第一个未输入数字的文本框序员(Skip=1..4,跳过哪一个文本框) function TForm1.GetBlank(Skip: Integer):Integer; var i:Integer; tmp:String; begin for i:=1 to Skip-1 do begin tmp:='Edit'+inttostr(i); if Assigned(FindComponent(tmp)) then if (FindComponent(tmp) as TEdit).Text='' then begin GetBlank:=i; exit; end; end; for i:=Skip+1 to 4 do begin tmp:='Edit'+inttostr(i); if Assigned(FindComponent(tmp)) then if (FindComponent(tmp) as TEdit).Text='' then break; end; GetBlank:=i; end;

//设置控件焦点(Index=1..4) procedure TForm1.ProcFocus(Index: Integer); var i: Integer; tmp:String; begin i:=GetBlank(Index); if i<>5 then begin tmp:='Edit'+inttostr(i); if Assigned(FindComponent(tmp)) then (FindComponent(tmp) as TEdit).Setfocus; end; end;

//第一个数字框输入 procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin Key:=ProcInput(Key,1,Edit1.SelStart); end; //第二个数字框输入 procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char); begin Key:=ProcInput(Key,2,Edit2.SelStart); end; //第三个数字框输入 procedure TForm1.Edit3KeyPress(Sender: TObject; var Key: Char); begin Key:=ProcInput(Key,3,Edit3.SelStart); end; //第四个数字框输入 procedure TForm1.Edit4KeyPress(Sender: TObject; var Key: Char); begin Key:=ProcInput(Key,4,Edit4.SelStart); end; //开始游戏,产生四位数字 procedure TForm1.SpeedButton1Click(Sender: TObject); var i:Integer; CurrentNum:String; begin i:=0; TrueNum:=''; Randomize; while i<4 do begin CurrentNum:=inttostr(Random(10)); if Pos(CurrentNum,TrueNum)=0 then begin TrueNum:=TrueNum + CurrentNum; i:=i+1; end; end; SpeedButton3.Enabled:=True; Panel1.Enabled:=True; ListBox1.Clear; end; //退出游戏 procedure TForm1.SpeedButton2Click(Sender: TObject); begin close; end; //显示答案 procedure TForm1.SpeedButton3Click(Sender: TObject); var i:Integer; tmp:String; begin for i:=1 to 4 do begin tmp:='Edit'+inttostr(i); if Assigned(FindComponent(tmp)) then (FindComponent(tmp) as TEdit).Text:=Copy(TrueNum,i,1); end; end; //关于 procedure TForm1.SpeedButton4Click(Sender: TObject); begin Form2.Showmodal; end;

procedure TForm1.Button1Click(Sender: TObject); begin ProcButtonClick(1); end;

procedure TForm1.Button2Click(Sender: TObject); begin ProcButtonClick(2); end;

procedure TForm1.Button3Click(Sender: TObject); begin ProcButtonClick(3); end;

procedure TForm1.Button4Click(Sender: TObject); begin ProcButtonClick(4); end;

procedure TForm1.Button5Click(Sender: TObject); begin ProcButtonClick(5); end;

procedure TForm1.Button6Click(Sender: TObject); begin ProcButtonClick(6); end;

procedure TForm1.Button7Click(Sender: TObject); begin ProcButtonClick(7); end;

procedure TForm1.Button8Click(Sender: TObject); begin ProcButtonClick(8); end;

procedure TForm1.Button9Click(Sender: TObject); begin ProcButtonClick(9); end;

procedure TForm1.Button0Click(Sender: TObject); begin ProcButtonClick(0); end;

procedure TForm1.Button10Click(Sender: TObject); begin ProcButtonClick(10); end;

procedure TForm1.Button12Click(Sender: TObject); begin ProcButtonClick(12); end;

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key=46 then Key:=0; end;

procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key=46 then Key:=0; end;

procedure TForm1.Edit3KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key=46 then Key:=0; end;

procedure TForm1.Edit4KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key=46 then Key:=0; end;

end.

[此贴子已经被作者于2004-05-13 22:05:28编辑过]

搜索更多相关主题的帖子: Delphi 数字 评点 
2004-04-22 23:44
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏
得分:0 
源码打包
呵呵。。。不要笑我哟

天津网站建设 http://www./
2004-04-22 23:47
shujucangku
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2004-5-8
收藏
得分:0 

呵呵,可以嘛!


2004-05-08 16:51
我爱随风
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-2-28
收藏
得分:0 
光给各代码,我不知道怎样用

www.
2006-02-28 10:08
快速回复:我用Delphi写的第一个程序[猜数字],高手请评点一下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035708 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved