*********************************************************** ************ C language five sub-game
* Nanyang Institute of Technology Computer Department 01612 Ban Dapeng course design work
* Operating environment: TC3.0 (VC6.0 does not support draw functions, can't draw board and chess pieces)
* 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, & regs1, & regs1) - '0';
IF (xx == ('q' - '0') || xx == ('q' - '0'))
{Step = 0;
Return;
}
Regs1.h.ah = 0;
YY = INT86 (22, & regs1, & regs1) - '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 a;
}
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
<4) {xx0 ; m ; A ;} m = 0;
While (YY0
<4) {yy0 -; m ; b ;}
IF (a <= b)
{
XX0 = x a;
YY0 = Y A;
}
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 a;
}
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
<4) {xx0 ; m ; A ;} m = 0;
While (YY0
<4) {yy0 -; m ; b ;}
IF (a <= b)
{
XX0 = x a;
YY0 = Y A;
}
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;
}
}