Home > Back-end >  C make renju procedures
C make renju procedures

Time:09-27

#include
#include
#include

# define length 15
# define Wide 700
Double the size;
Int chess [length] [length];
Bool is_red;
Bool is_gameover;
Bool is_init;
Int s=0;
Int h=0;
Int I=0, j=0;
Void Init ()
{
if (! Is_init)
{
Initgraph (Wide, Wide, EW_SHOWCONSOLE);
}
Is_init=true;
Memset (chess, 0, sizeof (chess));
Size=(float) Wide/(length + 1);
Is_red=true;//red chess down
Is_gameover=false;
}
Void draw_background ()
{
int i;
X00aa00 x0000aa setbkcolor (RGB (0, 0, 0 x0000aa));
Setlinecolor (0);
Cleardevice ();

For (I=1; i <=length; I++)
{

Line (size, size * I, Wide - size, size * I);
//horizontal lineLine (size * I, size, size * I, Wide - size);//perpendicular line
TCHAR nums [3].
Wsprintf (nums, _T (" % d "), I);
Outtextxy (size - the size/2, size * I - size/8, nums);
Outtextxy (size * I - size/8, the size - the size/2, nums);
}
Setfillcolor (0 x00);
Int m=3;
Fillcircle (size * m, size * m, size/8);//1 2 is coordinates, radius of 3 is
Fillcircle (size * m, size (length - m + 1), the size/8);
Fillcircle (size * (length - m + 1), the size * m, size/8);
Fillcircle (size * (length - m + 1), the size * (length - m + 1), the size/8);
* (fillcircle (size (length)/2 + 1), the size * ((length)/2 + 1), the size/8);

}
Void draw_chess ()
{
int i, j;
For (I=0; i {
For (j=0; J & lt; length; J++)
{
If (chess [I] [j]==0)
{
continue;
}
If (chess [I] [j]==1)
{

X00aa00 setfillcolor (0);
}
If (chess [I] [j]==2)
{

Xaa0000 setfillcolor (0);

}
Solidcircle (size * (j + 1), the size * (I + 1), the size/4);

}

}


}
Void draw_chess ()
{
int i, j;
For (I=0; i {
For (j=0; J & lt; length; J++)
{
If (chess [I] [j]==0)
{
continue;
}
If (chess [I] [j]==1)
{

X00aa00 setfillcolor (0);
}
If (chess [I] [j]==2)
{

Xaa0000 setfillcolor (0);

}
Solidcircle (size * (j + 1), the size * (I + 1), the size/4);

}

}


}
Void draw_Init ()/draw/
{
BeginBatchDraw ();
Draw_background ();
Draw_chess ();
EndBatchDraw ();

}
Bool is_win (int x, int y)//whether the game end
{
If (chess [y] [x]==0)
{

return false;
}
Int dir [], [2]={{1, 0}, {0, 1}, {1, 1}, {1, 1}};
For (int I=0; i <4. I++)
{

Int sum=1;
Int count=1;
Bool=true f1, f2=true;
For (int j=1; J & lt;=5; J++)
{
Int dx=x + dir [I] [1] * j;
Int dy=y + dir [I] [0] * j;
Int rx=x - dir [I] [1] * j;
Int ry=y - dir [I] [0] * j;
If (chess [y] [x]==1)
{
If (dx & gt;=0 & amp; & Dx & lt; Length & amp; & Dy & gt;=0 & amp; & Dy & lt; Length & amp; & Chess [y] [x]==chess [dy] (dx) & amp; & F1)//if equal to prove they are same color piece
{

Sum++;

}
The else
{

F1=false;


}

If (rx & gt;=0 & amp; & Rx & lt; Length & amp; & Ry & gt;=0 & amp; & Ry & lt; Length & amp; & Chess [y] [x]==chess [ry] (rx) & amp; & F2)//if equal to prove they are same color piece
{
Sum++;
}
The else
{

F2=false;
}
}
Else if (chess [y] [x]==2)
{
If (dx & gt;=0 & amp; & Dx & lt; Length & amp; & Dy & gt;=0 & amp; & Dy & lt; Length & amp; & Chess [y] [x]==chess [dy] (dx) & amp; & F1)//if equal to prove they are same color piece
{

count++;

}
The else
{

F1=false;


}

If (rx & gt;=0 & amp; & Rx & lt; Length & amp; & Ry & gt;=0 & amp; & Ry & lt; Length & amp; & Chess [y] [x]==chess [ry] (rx) & amp; & F2)//if equal to prove they are same color piece
{
count++;
}
The else
{

F2=false;
}
}
}
If (sum & gt;
=5){
S++;
return true;

}
If (count & gt;
=5){

H++;
return true;


}


}

return false;
}
Void play ()//player interaction
{
MOUSEMSG MSG=GetMouseMsg ();
If (MSG. UMsg==WM_LBUTTONDOWN)
{

Int x=(MSG. X - size/2)/size;
Int y=(MSG. Y - size/2)/size;
If (chess [y] [x]==0 & amp; & x>=0 & amp; & X=0 & amp; & Y{

Chess [y] [x]=is_red? 1:2;
If (is_win (x, y))
{
Is_gameover=true;
return;
}
Is_red=! Is_red;
}
}
If (MSG. UMsg==WM_RBUTTONDOWN)
{

UINT nRset=MessageBox (NULL, _T (" sure you want to start again? "), _T (" tip "), MB_YESNO);
If (nRset==IDNO)
{
MessageBox (GetForegroundWindow (), _T (" "), _T (" game to continue "), 0).
}
If (nRset==IDYES)
{
The Init ();
}
}

}
Void start_game ()//start the game
{

The Init ();
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related