Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

版权申明
查看: 5281|回复: 6

猜数字?

[复制链接]

该用户从未签到

发表于 2013-3-14 22:56:13 | 显示全部楼层 |阅读模式
请问有人会写Lazarus猜数字的程式吗?
就是我们设题目 然后让电脑猜
拜托了~~~
回复

使用道具 举报

该用户从未签到

发表于 2013-3-15 08:23:34 | 显示全部楼层
不懂什么是猜数字
回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2013-3-15 12:19:45 | 显示全部楼层
猜数字是一种小游戏
假设题目是1234 然后让别人去猜
假如别人猜8546 那就是0A1B
A表示数字对位置也对B表示数字对但位置不对
猜的人要一直猜 猜到4A 游戏结束了
另外题目跟猜题的四位数中只能用1~9 不能重覆唷
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2013-3-15 13:15:18 | 显示全部楼层
这个游戏在N年前的快译通时代已经有了。
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2013-3-15 13:18:27 | 显示全部楼层
Andrea 发表于 2013-3-15 12:19
猜数字是一种小游戏
假设题目是1234 然后让别人去猜
假如别人猜8546 那就是0A1B

我觉得这个程序和用什么开发没有关系,这是个逻辑问题,和平台没关系
回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2013-3-15 14:14:15 | 显示全部楼层
我也是这样认为
但因为那是老师的要求
所以只能使用拉撒路
我对电脑的概念很弱
所以我真的需要大家的帮助
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2014-5-5 15:34:11 | 显示全部楼层
  1. unit Unit1;

  2. {$mode objfpc}{$H+}

  3. interface

  4. uses
  5.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

  6. type

  7.   { TForm1 }

  8.   TForm1 = class(TForm)
  9.     Button1: TButton;
  10.     Edit1: TEdit;
  11.     Label1: TLabel;
  12.     Label2: TLabel;
  13.     tishi: TLabel;
  14.     cishu: TLabel;
  15.     procedure Button1Click(Sender: TObject);
  16.     procedure Edit1KeyPress(Sender: TObject; var Key: char);
  17.     procedure FormCreate(Sender: TObject);
  18.   private
  19.     { private declarations }
  20.   public
  21.     { public declarations }
  22.   end;

  23. var
  24.   Form1: TForm1;
  25.   number,i,j,guess,ga,gb,times:longint;
  26.   a,g:array[1..4] of byte;
  27.   flag:boolean;
  28.   ss,sok:string;
  29. implementation

  30. {$R *.lfm}

  31. { TForm1 }

  32. procedure TForm1.FormCreate(Sender: TObject);
  33. begin
  34.    //初始化
  35.    edit1.caption:='';
  36.    tishi.caption:='提示:';
  37.    cishu.Caption:='次数:';
  38.    //给出四位不重复随机数
  39.    randomize;
  40.    flag:=false;
  41.    repeat
  42.      for i:=1 to 4 do a[i]:=random(9)+1;
  43.      if (a[1]<>a[2])and(a[1]<>a[3])and(a[1]<>a[4])and
  44.      (a[2]<>a[3])and(a[2]<>a[4])and(a[3]<>a[4]) then flag:=true;
  45.    until flag;
  46. end;

  47. procedure TForm1.Button1Click(Sender: TObject);
  48. begin
  49.   ga:=0;gb:=0;
  50.   ss:=edit1.caption;
  51.   //出错处理
  52.   for i:=1 to length(ss) do
  53.   if not(ss[i] in ['1'..'9']) then
  54.   begin
  55.     Showmessage('输入有误,请输入一个四位数。');
  56.     exit;
  57.   end;
  58.   guess:=strToint(ss);
  59.   if (guess<1000) or (guess>9999) then Showmessage('输入有误,请输入一个四位数。');
  60.   //取数字
  61.   g[1]:=guess div 1000;g[2]:=guess div 100 mod 10;
  62.   g[3]:=guess div 10 mod 10;g[4]:=guess mod 10;
  63.   //数字比较
  64.   for i:=1 to 4 do
  65.   for j:=1 to 4 do
  66.   if (g[i]=a[j]) then
  67.   begin
  68.     if i=j then inc(ga) else inc(gb);
  69.   end;
  70.   inc(times);  //增加猜测次数
  71.   sok:='恭喜您,猜对了!用了'+intTostr(times)+'次';
  72.   if ga=4 then begin Showmessage(sok);exit;end; //猜对退出
  73.   //猜错给出提示及猜测次数
  74.   Form1.tishi.Caption:='提示:'+intTostr(ga)+'A'+intTostr(gb)+'B';
  75.   form1.cishu.Caption:='次数:'+intTostr(times);
  76. end;

  77. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
  78. begin
  79.   if key=chr(13) then  Button1.Click;
  80. end;

  81. end.
复制代码
lazarus实现

回复 支持 反对

使用道具 举报

*滑块验证:

本版积分规则

QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

GMT+8, 2025-2-13 20:24 , Processed in 0.035931 second(s), 10 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表