Turboc 1024x768 real-color graphics step-by-step demonstration of eight queens (figure)

Source: Internet
Author: User

Http://hi.baidu.com/alphablend/blog/item/7ad31bc78fe5d9129d163d35.html

 

# Include <dos. h>
# Include <fcntl. h>
# Include <Io. h>
# Include <math. h>
# Include <stdio. h>
# Include <conio. h>
# Include <stdlib. h>

# Define maxsize 15
# Define gridsize 49
# Define text_big_cursor 0
# Define text_normal_cursor 7

Typedef struct/* 16 mrgb pixel type */
{Unsigned char B;
Unsigned char g;
Unsigned char R;
} Rgb16m;

Void drawb (int x, int y, int start, rgb16m color1, rgb16m color2 );
Void drawqueen (int x, int y, rgb16m color1, rgb16m color2 );
Void backtrack (int t, int N, int * X );
Void cleargrid (INT left, int top, int X, int y );

Char point [38] [37] = {
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
{2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 1, 1, 0, 0, 0, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2 },
{2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 1, 1, 2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 1, 1, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2 },
{2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 1, 2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 1, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 2 },
{2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 1, 1, 2, 1, 1, 0, 0, 0, 0, 1, 1, 2, 1, 1, 0, 0, 0, 0, 1, 1, 2, 2, 2, 2, 2 },
{2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1,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 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 2, 2 },
{2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2 },
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2 }};

/* 227,197,140; 149,95, 34 */
Rgb16m backcolor = {140,197,227}, backcolor1 = {34, 95,149 };
Rgb16m queencolor = {0, 0, 0}, queencolor1 = {0, 0,255}, queencolor2 = {255,255,255 };
Rgb16m bordercolor = {52,128,203 };
Int queen_num;
Int left, top;
Int toquit = 0;

Unsigned char set_svga_mode (INT vmode)
{Union regs R;
R. X. Ax = 0x4f02;
R. X. bx = vmode;
Int86 (0x10, & R, & R );
Return (R. H. AH );
}

Void hide_text_cursor (void)
{Union regs R;
R. H. Ah = 1;
R. H. Ch = 32;
Int86 (0x10, & R, & R );
}

Void selectpage (register char page)
{Union regs R;
R. X. Ax = 0x4f05;
R. X. bx = 0;
R. X. dx = page;
Int86 (0x10, & R, & R );
}

Void show_text_cursor (char size)
{Union regs R;
R. H. Ah = 1;
R. H. Cl = size;
R. H. Ch = 7;
Int86 (0x10, & R, & R );
}

Unsigned int get_svga_mode ()
{Union regs R;
R. X. Ax = 0x4f03;
Int86 (0x10, & R, & R );
Return (R. X. bx );
}

Unsigned int vmode = 0;
Void init16m ()
{Vmode = get_svga_mode ();
Hide_text_cursor ();
If (set_svga_mode (0x118 ))
{Printf ("/nsorry, your graphics driver does not supplied./N ");
Show_text_cursor (text_normal_cursor );
Exit (0 );
}
}

Void close16m ()
{
Union regs R;
R. h.ah = 0;
R. H. Al = 3;
Int86 (0x10, & R, & R );
R. h.ah = 0x05;
R. H. Al = 0;
Int86 (0x10, & R, & R );
}

Void putpoint16m (int x, int y, rgb16m color)
{
Static int nowpage = 0, lastpage = 50;
Static long temp;
Unsigned char far * P = (unsigned char *) 0xa0000000;
Temp = y;
X <= 2;
Temp <= 12;
Temp + = X;

Nowpage = temp> 16;
Temp & = 0 xFFFF;
If (nowpage! = Lastpage)
{
Static Union regs R;
R. X. Ax = 0x4f05;
R. X. bx = 0;
R. X. dx = nowpage;
Int86 (0x10, & R, & R );
Lastpage = nowpage;
}

* (Rgb16m *) (p + temp) = color;
}

Void line (INT startx, int starty, int endx, int Endy, rgb16m color)
{
Register int T, distance;
Int x = 0, y = 0;
Int delta_x, delta_y;
Int incx, incy;

Delta_x = endx-startx;
Delta_y = Endy-starty;

If (delta_x> 0) incx = 1;
Else if (delta_x = 0) incx = 0;
Else incx =-1;
If (delta_y> 0) incy = 1;
Else if (delta_y = 0) incy = 0;
Else incy =-1;

If (delta_x <0) delta_x =-delta_x;
If (delta_y <0) delta_y =-delta_y;

If (delta_x> delta_y) distance = delta_x;
Else distance = delta_y;

For (t = 0; t <= distance + 1; t ++ ){
Putpoint16m (startx, starty, color );
X + = delta_x;
Y + = delta_y;
If (x> distance ){
X-= distance;
Startx + = incx;
}
If (Y> distance ){
Y-= distance;
Starty + = incy;
}
}
}

Void fillbox (INT startx, int starty, int endx, int Endy, rgb16m color)
{
Int I, J;

If (startx> endx)
{
I = startx;
Startx = endx;
Endx = I;
}
If (starty> Endy)
{
I = starty;
Starty = Endy;
Endy = I;
}
If (startx <0)
Startx = 0;
If (endx & gt; 1023)
Endx = 1023;
If (starty <0)
Starty = 0;
Else 'if (Endy> 767)
Endy = 767;

For (j = starty; j <= Endy; j ++)
For (I = startx; I <= endx; I ++)
Putpoint16m (I, j, color );
}

Int main (void)
{
Int I, J;
Int queenlist [maxsize + 1];

Rein :;

Clrscr ();
Gotoxy (10, 12 );
Printf ("input the number of Queens (4-15, 0 to quit ):");
Scanf ("% d", & queen_num );

If (queen_num = 0)
Return 0;

If (queen_num <4 | queen_num> 15)
Goto rein;

Init16m ();

Left = (1024-gridsize * queen_num)/2;
Top = (768-gridsize * queen_num)/2;

Drawb (left, top, 1, backcolor, backcolor1 );
Fillbox (left-10, top-12, left-2, top + queen_num * gridsize + 12, bordercolor );
Fillbox (left + gridsize * queen_num + 1, top-12, left + gridsize * queen_num + 10, top + queen_num * gridsize + 12, bordercolor );
Fillbox (left, top-12, left + queen_num * gridsize-1, top-2, bordercolor );
Fillbox (left, top + queen_num * gridsize + 12, left + queen_num * gridsize-1, top + queen_num * gridsize + 1, bordercolor );

Toquit = 0;
Backtrack (1, queen_num, queenlist );

Close16m ();
Goto rein;
}

Void drawb (int x, int y, int start, rgb16m color1, rgb16m color2)
{
Int I, J;

For (j = 0; j <queen_num; j ++)
{
For (I = start-1; I <queen_num; I ++)
{
Fillbox (x + I * gridsize, Y + J * gridsize, x + (I + 1) * gridsize-1, Y + (J + 1) * gridsize-1, (I % 2 = 0 & J % 2 = 0) | (I % 2! = 0 & J % 2! = 0 ))? Color1: color2 );
}
}
}

Void drawqueen (int x, int y, rgb16m color1, rgb16m color2)
{
Int I, J;
Const int xdis = (gridsize-37)/2, ydis = (gridsize-38)/2;

For (I = 0; I <38; I ++)
{
For (j = 0; j <37; j ++)
{
If (point [I] [J] = 0)
{
Putpoint16m (X-1) * gridsize + J + Left + xdis, (Y-1) * gridsize + I + TOP + ydis, color1 );
}
Else if (point [I] [J] = 1)
{
Putpoint16m (X-1) * gridsize + J + Left + xdis, (Y-1) * gridsize + I + TOP + ydis, color2 );
}
}
}
}

Int place (int K, int * X)
{
Int J;
For (j = 1; j <K; j ++)
If (ABS (k-j) = ABS (X [J]-X [k]) | (X [J] = x [k])
Return 0;

Return 1;
}

Void backtrack (int t, int N, int * X)
{
Int I;
Int A, B;
Static int step = 1;
Static int oldp [maxsize] =
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1 };

If (T> N)
{
For (A = 1; A <= queen_num; A ++)
If (X [a]! = Oldp [a] & oldp [a]! =-1)
Cleargrid (left, top, A, oldp [a]);
For (I = 1; I <= N; I ++)
{
Drawqueen (I, X [I], queencolor, queencolor2 );
Oldp [I] = x [I];
}
If (toquit = 1)
Return;

A = getch ();
If (A = 0)
A = getch ();
Else if (a = 27)
{
Toquit = 1;
Return;
}
Else if (a = 32)
{
Step = 1-step;
For (A = 1; A <= queen_num; A ++)
Cleargrid (left, top, A, X [a]);
}
}
Else
For (I = 1; I <= N; I ++)
{
X [T] = I;
If (step = 1)
{
Drawb (left, top, T, backcolor, backcolor1 );
For (A = 1; A <= T; A ++)
{
If (t = N & PLACE (t, x ))
Drawqueen (A, X [a], queencolor, queencolor2 );
Else
Drawqueen (A, X [a], (a = T )? Queencolor1: queencolor, queencolor2 );
}
If (! (T = N & PLACE (t, x )))
{
A = getch ();
If (A = 27)
{
Toquit = 1;
Return;
}
Else if (a = 32)
{
Step = 1-step;
For (A = 1; A <= queen_num; A ++)
Cleargrid (left, top, A, X [a]);
}
}
}
If (Place (t, x ))
{
Backtrack (t + 1, n, x );
If (toquit = 1)
Return;
}
}
}

Void cleargrid (INT left, int top, int X, int y)
{
X --;
Y --;
Fillbox (left + x * gridsize, top + y * gridsize, left + (x + 1) * gridsize-1, top + (Y + 1) * gridsize-1, (X % 2 = 0 & Y % 2 = 0) | (X % 2! = 0 & Y % 2! = 0 ))? Backcolor: backcolor1 );
}

 

 

 

 

 

 

 

 

 

 

The program directly uses a 10 h 1024 2 h interrupt call to achieve 16 m true color graphics display at a resolution of 768 ×, without the need for turboc graphics library <graphics. h> support. After an EXE is generated, it can be run independently.

A maximum of 15 queens can be displayed.

Turboc 2.0, 3.0 run through.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.