|
|
 |
 |
|
Join the Discussion
|
"Post your views, comments, questions and doubts to this article."
Discuss!
|
|
 |
 |
|
|
 |
 |
 |
|
|
 |
 |
 |
|
|
Guess A Number
Let's create a simple console game. The computer will randomly pick an integer number from 0 to 50. Your task is to guess the number. Each time you pick a number a program will tell that your number is larger or smaller than the one you are looking for. When, finally, you find what the number was it'll tell you how many times it took you to find it.
I'll give you the code, but first let's see what are two most seen commands in a console application:
Write and Read RTL procedures are typically used for writing and reading from a file. There are two standard text-file variables, Input and Output. In a Console application, Delphi automatically associates the Input and Output files with the application's console window. The standard file variable Input is a read-only file associated with the operating system's standard input (typically the keyboard). The standard file variable Output is a write-only file associated with the operating system's standard output (typically the display).
Thus, Writeln is used to display a message; ReadLn is normally used to read in variables. In the code below, you will notice it ends with a ReadLn. Readln without any parameters simply waits for the [Enter] key to be pressed. One of the peculiarities of a console application is that when it has stopped running, the console window is automatically closed. The ReadLn statement is necessary so that the user can see any text produced by Writeln statements before it disappears off the screen when the program finishes.
Here goes the code. I hope you understand it.
program GuessANumber;
{$APPTYPE CONSOLE}
uses
SysUtils;
var
rn, un, cnt: Integer;
guess: Boolean;
begin
Randomize;
rn := Trunc(Random(50) + 1);
Write('Computer has picked an integer number,');
Write('from 1 to 50, guess the number!');
WriteLn('Your guess is: ');
cnt := 1; un := 0;
Guess := False;
while Guess = False do begin
ReadLn(un);
if un > rn then
Write('Wrong, gimme a smaller number: ')
else if un < rn then
Write('Wrong, gimme a larger number: ')
else //un=rn
begin
Guess:=True;
Writeln;
Write('Correct! It took you ' +
IntToStr(cnt) +
' times to guess!')
end;
cnt := cnt + 1;
end; //while
ReadLn; //don't close the window, wait for [Enter]
end.
|
Simply run the project and play....
Console applications: Tips and Tricks
Since Console applications are not standard Delphi projects, you should be familiar with several situations that evolve when working with console windows.
Here's how to Capture the Output From a Console mode application and how to Determine the output of a console application.
Console applications should be written to handle all exceptions - to prevent windows from displaying a dialog during its execution. "Simply" add all the code inside a try/except/finally blocks.
To return an exit code to the calling application or batch file from a Windows Console mode application written in Delphi call the Halt procedure, passing the exit code you wish to return.
There is a IsConsole variable defined in the Sytsem unit that indicates whether the module was compiled as a console application.
If you start a Console application form the command prompt or a batch file you have to use the start command to create a new console window for the application. Otherwise it takes over the DOS prompt window from which it was started.
Console applications also provide sophisticated low-level API support that gives direct access to a console's screen buffer and that enables applications to receive extended input information (such as mouse input). For example, the SetConsoleTextAttribute function sets the foreground (text) and background color attributes of characters written to the screen. Note that you need to add the Windows unit to the uses clause in order to use the API calls. The next peace of code could be used to display red text on a blue background:
SetConsoleTextAttribute(GetStdHandle(
STD_OUTPUT_HANDLE),
FOREGROUND_RED OR
BACKGROUND_BLUE);
Writeln('Red text on a blue background !');
|
Many of the nice things that conventional Pascal uses to manage the text screen, like clrscr, gotoxy and so on are not available in a console-based application. Again, Win API has all we need. Here are some examples. As you will see, you can even make a "message loop".
If you want to have console input/output for GUI applications you should use the AllocConsole and FreeConsole functions. The AllocConsole function allocates a new console for the calling process. The FreeConsole function detaches the calling process from its console. Here's the example:
var s: string;
begin
AllocConsole;
try
Write('Type something and press [ENTER]');
Readln(s);
ShowMessage('You typed ' + s);
finally
FreeConsole;
end;
end;
|
If you have ever tried to run a console application and capture the output, you may have tried to make sense of the topic in the Win32 help files called 'Creating a child process with redirected input and output'. The tip called "Capture the Output From a DOS Window" solves the problem. Here is another example of redirecting a console application output.
Screen scraping is a technique for "grabbing" text from the screen to another application, or simple converting character images on the screen back to text. Screen scraping is often used to convert old terminal applications to the web by running the terminal software in the background and letting a web front-end do all the manipulation of the older application. In cases you only need simple screen scraping techniques, you can easily use the built-in Delphi VCL components to convert character images on a canvas to text, see how.
Se sure to check the Delphi WDosX Project, a freeware 32-bit DOS extender, which enables 32-bit programs, to be executed independently from Windows with plain DOS. For this purpose WDosX makes available a DPMI interface, an extended DOS Int 21 API interface, as well as the Windows core DLLs Kernel32 and User32 to the 32-bit program. Additionally, WDosX includes some useful libraries, sample programs and a full screen debugger.
That's it
I hope you'll find a situation where a console mode application is more appropriate than a GUI one ... for example, CGI apps are console apps. Anyway if you need any help, please post to the Delphi Programming Forum.
First page > Setting up a console mode application with Delphi > Page 1, 2
|