C ++-language wuziqi source program (using coordinates to determine the position of the chess piece)

Source: Internet
Author: User

Http://blog.csdn.net/waterboy/archive/2004/08/24/83316.aspx

**************************************** * ******************** C ++-language wuziqi source program
* Dapeng Course Design Works of class 01612, computer department, Nanyang Institute of Technology
* Running environment: tc3.0 (vc6.0 does not support drawing functions and cannot draw boards and pawns)
* Email: ypsky@mail2.nyist.net
**************************************** * ********************* # Include
# Include
# Include
# Include
# Define backcolor cyan
# Define defaultcolor black
# Define linecolor Magenta
# Define player1_color red
# Define player2_color white
# Define error_color red
# Define winner_color red

Const int left = 40;
Const int Top = 390;
Const int d = 30;
Const int line_num = 9;
Const int turn = 0;
Const int r = D/3;
Const Int J = 10;
Int X, Y, k = 1, step = (line_num + 1) * (line_num + 1 );
Union regs regs1, regs2;
Class player1;
Class player2;
Class qipan {
Public:
Qipan ();
~ Qipan (){};
Void init_qipan ();
Friend void fall (player1 & num1, player2 & num2, qipan & num );
Friend void input (player1 & num1, player2 & num2, qipan & num );

PRIVATE:
Int point [line_num + 1] [line_num + 1];

};
Class player1 {
Public:
Player1 ();
~ Player1 (){};
Friend void fall (player1 & num1, player2 & num2, qipan & num );
Friend void input (player1 & num1, player2 & num2 );
Friend int judge_winner (player1 & num1, player2 & num2 );
PRIVATE:
Int point1 [line_num + 1] [line_num + 1];
};
Class player2 {
Public:
Player2 ();
~ Player2 (){};
Friend void fall (player1 & num1, player2 & num2, qipan & num );
Friend void input (player1 & num1, player2 & num2, qipan & num );
Friend int judge_winner (player1 & num1, player2 & num2 );
PRIVATE:
Int point2 [line_num + 1] [line_num + 1];
};
Void input (player1 & num1, player2 & num2 );
Void fall (player1 & num1, player2 & num2, qipan & num );
Int judge_winner (qipan & num, player1 & num1, player2 & num2 );
Void inputerror ();
Void display_winner (INT );

Void main ()
{
Int driver = detect, mode;
Initgraph (& driver, & mode, "E:/tc30/BGI ");

Qipan num;
Player1 num1;
Player2 num2;
While (step --)
{
Input (num1, num2, num );
Fall (num1, num2, num );
If (judge_winner (num1, num2 ))
{
Display_winner (k );
}
}

// Getchar ();
}
Qipan: qipan (void)
{Int J, I;
Char ch [2] = "0 ";
Setbkcolor (backcolor );
Setcolor (linecolor );
For (I = 0; I <= line_num; I ++)
{
Line (left, top-I * D, left + line_num * D, top-I * D );
}
For (I = 0; I <= line_num; I ++)
{
Line (left + I * D, top, left + I * D, top-line_num * D );
}
For (I = 0; I <= line_num; I ++)
{If (* Ch = '9' + 1) * Ch = 'a ';
Settextstyle (default_font, horiz_dir, 1 );
Outtextxy (left + I * D-2, top + R + 3, ch );
(* Ch) = (* Ch) + 1;
}
* Ch = '0 ';
For (I = 0; I <= line_num; I ++)
{If (* Ch = '9' + 1) * Ch = 'a ';
Settextstyle (default_font, horiz_dir, 1 );
Outtextxy (left-r-10, top-D * I-3, CH );
(* Ch) = (* Ch) + 1;
}

Setcolor (defaultcolor );

For (I = 0; I <= line_num; I ++)
{
For (j = 0; j <= line_num; j ++)
Point [I] [J] = 0;
}
}

Void fall (player1 & num1, player2 & num2, qipan & num)
{
Int flag = K % 2;
If (FLAG)
{Setcolor (player2_color );
Num2.point2 [x] [Y] = 1;
Num. Point [x] [Y] = 2;
Circle (left + D * X, top-D * y, R );
Setfillstyle (1, player2_color );
Floodfill (left + D * X, top-D * y, player2_color );
}
Else
{Num1.point1 [x] [Y] = 1;
Num. Point [x] [Y] = 1;
Setcolor (player1_color );
Circle (left + D * X, top-D * y, R );
Setfillstyle (1, player1_color );
Floodfill (left + D * X, top-D * y, player1_color );
}

Setcolor (defaultcolor );
}

Void input (player1 & num1, player2 & num2, qipan & num)
{Char XX, YY;

K ++;
While (1)
{
Regs1.h. Ah = 0;
Xx = int86 (22, S1, S1)-'0 ';
If (xx = ('q'-'0') | xx = ('q'-'0 '))
{Step = 0;
Return;
}
Regs1.h. Ah = 0;
YY = int86 (22, S1, S1)-'0 ';

If (yy = ('q'-'0') | YY = ('q'-'0 '))
{
Step = 0;
Return;
}
If (XX <0 | XX> line_num)
{Inputerror ();
Continue;
}
If (yy <0 | YY> line_num)
{Inputerror ();
Continue;
}

If (Num. Point [XX] [YY] = 0)
{
Break;
}
Else
{
Inputerror ();
Continue;
}
}

X = (INT) xx;
Y = (INT) YY;

Setcolor (backcolor );
Settextstyle (default_font, horiz_dir, 1 );
Outtextxy (left + D * line_num/3, top + D * 2, "input error ");
Setcolor (defaultcolor );
}

Player1: player1 ()
{
Int I, J;
For (I = 0; I <= line_num; I ++)
{
For (j = 0; j <= line_num; j ++)
Point1 [I] [J] = 0;
}
}

Player2: player2 ()
{Int I, J;
For (I = 0; I <= line_num; I ++)
{
For (j = 0; j <= line_num; j ++)
Point2 [I] [J] = 0;
}
}
Void inputerror (void)
{Setcolor (error_color );
Settextstyle (default_font, horiz_dir, 1 );
Outtextxy (left + D * line_num/3, top + D * 2, "input error ");
Setcolor (defaultcolor );
}

Int judge_winner (player1 & num1, player2 & num2)
{
Int COUNT = 0, m = 0, n = 0, a = 0, B = 0, xx0, yy0;
Int flag = K % 2;
Xx0 = x; yy0 = y;
If (! Flag)
{// Left <-------> right
While (xx0> = 1 & M <4) {xx0 --; m ++ ;}
While (n <9 & xx0 <= line_num)
{
If (num1.point1 [xx0] [Y] = 1)
{
Count ++;
If (COUNT = 5) return 1;
}
Else
{
Count = 0;
}
N ++;
Xx0 ++;
}
// Up <------> down
Count = 0; xx0 = x; M = 0; n = 0;
While (yy0> = 1 & M <4) {yy0 --; m ++ ;}
While (n <9 & yy0 <= line_num)
{
If (num1.point1 [x] [yy0] = 1)
{
Count ++;
If (COUNT = 5)
Return 1;
}
Else
{
Count = 0;
}
N ++;
Yy0 ++;
}
// Left up ----- right down
Xx0 = X;
Yy0 = y;
M = 0;
N = 0;
Count = 0;
While (xx0> = 1 & M <4) {xx0 --; A ++; m ++;} m = 0;
While (yy0 <= line_num & M <4) {yy0 ++; B ++; m ++ ;}
If (A <= B)
{
Xx0 = x-A; yy0 = Y +;
}
Else
{
Xx0 = x-B; yy0 = Y + B;
}
While (xx0 <= line_num & yy0> = 0 & n <9)
{
If (num1.point1 [xx0] [yy0] = 1)
{
Count ++;
If (COUNT = 5)
Return 1;
}
Else
{
Count = 0;
}
Xx0 ++;
Yy0 --;
N ++;
}
// Right up <-----> left down
Count = 0;
A = 0;
B = 0;
N = 0;
M = 0;
Xx0 = X;
Yy0 = y;
While (xx0 While (yy0 If (A <= B)
{
Xx0 = x +;
Yy0 = Y +;
}
Else
{
Xx0 = x + B;
Yy0 = Y + B;
}
While (xx0> = 0 & yy0> = 0 & n <9)
{
If (num1.point1 [xx0] [yy0] = 1)
{
Count ++;
If (COUNT = 5)
Return 1;
}
Else
Count = 0;
Xx0 --;
Yy0 --;
N ++;
}
// No winer
Return 0;
}
Else
{
// Left <-------> right
While (xx0> = 1 & M <4) {xx0 --; m ++ ;}
While (n <9 & xx0 <= line_num)
{
If (num1.point1 [xx0] [Y] = 1)
{
Count ++;
If (COUNT = 5) return 1;
}
Else
{
Count = 0;
}
N ++;
Xx0 ++;
}
// Up <------> down
Count = 0; xx0 = x; M = 0; n = 0;
While (yy0> = 1 & M <4) {yy0 --; m ++ ;}
While (n <9 & yy0 <= line_num)
{
If (num2.point2 [x] [yy0] = 1)
{
Count ++;
If (COUNT = 5)
Return 1;
}
Else
{
Count = 0;
}
N ++;
Yy0 ++;
}
// Left up ----- right down
Xx0 = X;
Yy0 = y;
M = 0;
N = 0;
Count = 0;
While (xx0> = 1 & M <4) {xx0 --; A ++; m ++;} m = 0;
While (yy0 <= line_num & M <4) {yy0 ++; B ++; m ++ ;}
If (A <= B)
{
Xx0 = x-A; yy0 = Y +;
}
Else
{
Xx0 = x-B; yy0 = Y + B;
}
While (xx0 <= line_num & yy0> = 0 & n <9)
{
If (num2.point2 [xx0] [yy0] = 1)
{
Count ++;
If (COUNT = 5)
Return 1;
}
Else
{
Count = 0;
}
Xx0 ++;
Yy0 --;
N ++;
}
// Right up <-----> left down
Count = 0;
A = 0;
B = 0;
N = 0;
M = 0;
Xx0 = X;
Yy0 = y;
While (xx0 While (yy0 If (A <= B)
{
Xx0 = x +;
Yy0 = Y +;
}
Else
{
Xx0 = x + B;
Yy0 = Y + B;
}
While (xx0> = 0 & yy0> = 0 & n <9)
{
If (num2.point2 [xx0] [yy0] = 1)
{
Count ++;
If (COUNT = 5)
Return 1;
}
Else
Count = 0;
Xx0 --;
Yy0 --;
N ++;
}
// No winer
Return 0;

}
}

Void display_winner (int K)
{
Int flag = K % 2;
If (! Flag)
{Setcolor (winner_color );
Settextstyle (default_font, horiz_dir, 2 );
Outtextxy (left + D * 2, top + 40, "Red is winner ");
Setcolor (defaultcolor );
Step = 0;
Getchar ();
}
Else
{Setcolor (winner_color );
Settextstyle (default_font, horiz_dir, 2 );
Outtextxy (left + 2 * D, top + 40, "White is winner ");
Setcolor (defaultcolor );
Step = 0;

}
}

Related Article

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.