黑白棋c语言程序(Othello C language program)
黑白棋c语言程序(Othello C language program) # include "graphics.h 图形系统头文件" / * * /
# define left 0x4b00 光标左键值 / * * /
# define right 0x4d00 光标右键值 / * * /
# define down 0x5000 光标下键值 / * * /
# define the ip 0x4800 光标上键值 / * * /
# define esc 0x011b esc键值 / * * /
# define enter 0x1c0d 回车键值 / * * /
int a [8] [8] = {0}, key, score1, score2 具体分数以及按键与存放棋子的变量; / * * /
char playone [3], playtwo [3]; / * 两个人的得分转换成字符串输出 * /
playtoplay void (void); / * 人人对战函数 * /
drawqp void (void); / * 画棋盘函数 * /
void setplaycolor (int x); / * 设置棋子第一次的颜色 * /
void movecolor (int x, int y); / * 恢复原来棋盘状态 * /
int qpchange (int x, int y, int z); / * 判断棋盘的变化 * /
doscore void (void); / * 处理分数 * /
void printscore (int n); / * 输出成绩 * /
playwin void (void); / * 输出胜利者信息 * /
/ * * * * * * 主函数 * * * * * * * * * /
void main (void)
{
int dg = detect, gr;
initgraph (& dg, & gr, "c: \ ^ tc"); / * 初始化图形系统 * /
drawqp (); / * 画棋盘 * /
playtoplay (); / * 人人对战 * /
getch ();
closegraph (); / * 关闭图形系统 * /
}
void drawqp () / * 画棋盘 * /
{
int i, j;
score1 = score2 = 0; / * 棋手一开始得分都为0 * /
setbkcolor (blue);
for (i = 100; in < = 420; + = 40)
{
line (100, in 420, in 画水平线); / * * /
line (in 100, 420); / * 画垂直线 * /
}
setcolor (0); / * 取消圆周围的一圈东西 * /
setfillstyle (solid _ fill, 15); / * 白色实体填充模式 * /
fillellipse (500200,15,15); / * 在显示得分的位置画棋 * /
setfillstyle (solid _ fill, 8); / * 黑色实体填充模式 * /
fillellipse (500300,15,15);
a [3] [3] = a [4] [4] = 1; / * 初始两个黑棋 * /
a [3] [4] = a [4] [3] = 2; / * 初始两个白棋 * /
setfillstyle (solid _ fill, white);
fillellipse (120 + 3 * a + 3 * 40,15,15);
fillellipse (120 + 4 * a + 4 * 40,15,15);
setfillstyle (solid _ fill, 8);
fillellipse (120 + 3 * a + 4 * 40,15,15);
fillellipse (120 + 4 * a + 3 * 40,15,15);
score1 = score2 = 2; / * 有棋后改变分数 * /
doscore (); / * 输出开始分数 * /
}
void playtoplay () / * 人人对战 * /
{
int x, y, t = 1, i, j, cc = 0;
while (1) / * 换棋手走棋 * /
{
x = 120, y = 80; / * 每次棋子一开始出来的坐标, x为行坐标, y
为列坐标 * /
while (1) / * 具体一个棋手走棋的过程 * /
{
printscore (1); / * 输出棋手1的成绩 * /
printscore (2); / * 输出棋手2的成绩 * /
setplaycolor (t); / * t变量是用来判断棋手所执棋子的颜色 * /
fillellipse (x, y, 15.15);
key = bioskey (0); / * 接收按键 * /
if (key = = esc) / * 跳出游戏 * /
break;
else
if (key = = enter 如果按键确定就可以跳出循环) / * * /
{
if (y! = 80 & a [(x - 120) / 40] [(y - 120) / 40]. = 1
& & a [(x - 120) / 40] [(y - 120) / 40]. = 2) / * 如果落子位置没有棋子 * /
{
if (t% 2 = = 1) / * 如果是棋手1移动 * /
a [(x - 120) / 40] [(y - 120) / 40] = 1;
Else/* or 2 * / mobile player
A[(x-120) /40][(y-120) /40]=2;
If (! QpChange (x, y, t)) / * after judgment changes * / chessboard Lazi
{
A[(x-120) /40][(y-120) /40]=0; / * * / restore state space
Cc++; / * * / the number of statistics began to try
If (cc>=64-score1-score2) / * if you try to exceed the number of spaces will stop.
{
MoveColor (x, y);
Fillellipse (x, y, 15,15);
Break;
}
Else
Continue; / * * / if the key is invalid
}
(DoScore); / * * / doscore
Break; / * board changes, then go round each other.
}
Else/* has a piece is key to.
Continue;
}
Else four / * * / determine the direction key
If (key==LEFT&&x>120) / * * / left arrow
{
MoveColor (x, y);
Fillellipse (x, y, 15,15);
SetPlayColor (t);
X-=40;
Fillellipse (x, y, 15,15);
}
Else
If (key==RIGHT&&x<400&&y>80) / * * / right arrow
{
MoveColor (x, y);
Fillellipse (x, y, 15,15);
SetPlayColor (t);
X+=40;
Fillellipse (x, y, 15,15);
}
Else
If (key==UP&&y>120) / * * / on the direction key
{
MoveColor (x, y);
Fillellipse (x, y, 15,15);
SetPlayColor (t);
Y-=40;
Fillellipse (x, y, 15,15);
}
Else
If (key==DOWN&&y<400) / * * / under the direction key
{
MoveColor (x, y);
Fillellipse (x, y, 15,15);
SetPlayColor (t);
Y+=40;
Fillellipse (x, y, 15,15);
}
}
If (key==ESC) / * * / the end of the game
Break;
If ((score1+score2) ==64||score1==0||score2==0) / * lattice has been filled with one or 0 pieces to judge the outcome.
{
(playWin); / * * / output end result
Break;
}
T=t%2+1; / * party left, change the color of each other go / round pieces
Cc=0; / * count recovery for 0*/
} /*endwhile*/
}
Void SetPlayColor (int t) / * * / color set pieces
{
If (t%2==1)
Setfillstyle (SOLID_FILL, 15); / * * / white
Else
Setfillstyle (SOLID_FILL, 8); / * * / gray
}
Void MoveColor (int x, int y) the original state grid recovery / * * / take a step back
{
If (y<100) / * if it is from a starting point on the restoration of the blue.
Setfillstyle (SOLID_FILL, BLUE);
Else/* other circumstances if it is 1 on the restoration of a white pawn, 2 black or blue back piece, recovery board * /
Switch (a[(x-120) /40][(y-120) /40])
{
Case 1:
Setfillstyle (SOLID_FILL, 15); break; / * * / white
Case 2:
Setfillstyle (SOLID_FILL, 8); break; / * * / Black
Default:
Setfillstyle (SOLID_FILL, BLUE); / * * / blue
}
}
Int QpChange (int x, int y, int t) / * * / board of the change of judgment
{
Int, I, J, K, KK, II, JJ, yes;
Yes=0;
I= (x-120) /40; / * * / subscript array elements for calculation
J= (y-120) /40; / * * / column subscript array element calculation
SetPlayColor (T); / * * / color change set pieces
8 / * began to determine the direction of change.
If (j<6) / * * / to the right.
{
For (k=j+1;
j.j. * 120 + 40,15,15);
}
if (ii. = + 1)
yes = 1;
}
}
返回是否改变过棋子颜色的标记 return yes; / * * /
}
void doscore 处理分数 () / * * /
{
int i, j;
score1 = score2 = 0; / * 重新开始计分数 * /
for (i = 0; i < 8; i + +)
for (j = 0, j < 8; j + +)
if (a [i] [j] = = 1) / * 分别统计两个人的分数 * /
score1 + +;
else
if (a [i] [j] = = 2)
score2 + +;
}
void printscore (int playnum 输出成绩) / * * /
{
if (playnum = = 1) / * 清除以前的成绩 * /
{
setfillstyle (solid _ fill, blue);
bar (550100640400);
}
setcolor (red).
settextstyle (0,0,4 设置文本输出样式); / * * /
if (playnum = = 1) / * 判断输出哪个棋手的分, 在不同的位置输出 * /
{
sprintf (playone, "% d", score1);
outtextxy (550200, playone);
}
else
{
sprintf (playtwo, "% d", score2);
outtextxy (550300, playtwo);
}
setcolor (0);
}
void playwin 输出最后的胜利者结果 () / * * /
{
settextstyle (0,0,4);
setcolor (12).
if (score2 > score1 开始判断最后的结果) / * * /
outtextxy (100,50, "black win!") ;
else
if (score2 < score1)
outtextxy (100,50, white win! ") ;
else
outtextxy (60,50, "you all win!") ;
}
五子棋游戏
/ * 五子棋 * /
# includes stdio.h > <
# includes stdlib.h > <
# includes graphics.h > <
# includes bios.h > <
# includes conio.h > <
# define left 0x4b00
# define right 0x4d00
# define down 0x5000
# define up 0x4800
# define esc 0x011b
# define space 0x3920
# define stable 20
# define jz 4
# define js 3
# define n 19
int box [n] [n];
int step step _ _ x, y;
int key;
int flag = 1;
void draw (); _ box
void draw _ cicle (int x, int y, int color);
void change ();
void judgewho (int x, int y)
void judgekey ();
int judgeresult (int x, int y)
void attentoin ();
void attention ()
{
char ch;
window (1,1,80,25);
textbackground (lightblue);
textcolor (yellow).
clrscr ();
gotoxy (15.2).
printf ("游戏操作规则:");
gotoxy (15.4).
printf ("play rules").
gotoxy (15.6).
printf ("1、按左右上下方向键移动棋子");
gotoxy (15.8).
printf ("1. press left, right, up, down key to move the piece ");
gotoxy (15.10).
printf ("2、按空格确定落棋子");
gotoxy (15,12);
printf ("2. press space to place the piece ");
gotoxy (15,14);
printf ("3、禁止在棋盘外按空格");
gotoxy (15,16);
printf ("3. do not press the space outside of the chessboard ");
gotoxy (15.18);
printf ("你是否接受上述的游戏规则 (y / n));
gotoxy (3.20).
printf ("do you accept the above playing rules? [y / n]: ");
while (1)
{
gotoxy(60,20);
CH = getche();
如果(CH = = Y | | CH = = Y)
打破;
如果(CH = = n | | CH = = n)
{
窗口(1,1,80,25);
设置文本的背景颜色(黑色);
文字颜色(浅灰色);
clrscr();
出口(0);
}
gotoxy(51,12);
printf(“”);
}
}
无效draw_box()
{
int X1,X2,Y1,Y2;
setbkcolor(浅蓝色);
设置颜色(黄色);
gotoxy(7,2);
printf(“左,右,上,下键移动,空间放,ESC退出。”);
为(x1 = 1 = 1,Y1,Y2 = 18;X1 < = 18;X1 + +)
线((X1 + JZ)*比莉(Y1 + JS)*比莉(X1 + JZ)*比莉(Y2 + JS)* BILI);
为(x1 = 1,Y1 = 1,X2 = 18;Y1 < = 18;Y1 + +)
线((X1 + JZ)*比莉(Y1 + JS)*比莉(x2 + JZ)*比莉(Y1 + JS)* BILI);
对于(X1 = 1;X1 < = 18;X1 + +)
对于(Y1 = 1;Y1 < = 18;Y1 + +)
盒[ ] [ ] = 0 x1 y1;
}
无效draw_circle(x,y,int的颜色)
{
设置颜色(颜色);
setlinestyle(solid_line,0,1);
X =(x + JZ)*比莉;
Y =(Y + JS)*比莉;
圆(x,y,8);
}
无效judgekey()
{
int i;
Int J;
开关(键)
{
例左右:
如果(step_x-1,0)
打破;
其他的
{
对(我= step_x-1,J = step_y;我> = 1;我--)
如果(框i = = = 0)
{
draw_circle(step_x,step_y,浅蓝色);
打破;
}
如果(i < 1)中断;
step_x =我;
法官(step_x,step_y);
打破;
}
权案:
如果(step_x + 1 > 18)
打破;
其他的
{
对(我= step_x + 1,J = step_y;我< = 18;i++)
如果(框i = = = 0)
{
draw_circle(step_x,step_y,浅蓝色);
打破;
}
如果(i)突破18;
step_x =我;
法官(step_x,step_y);
打破;
}
的情况下:
如果((step_y + 1)> 18)
打破;
其他的
{
对(我= step_x,J = step_y + 1;J < = 18;j++)
如果(框i = = = 0)
{
draw_circle(step_x,step_y,浅蓝色);
打破;
}
如果(j = 18)断;
step_y = J;
法官(step_x,step_y);
打破;
}
案子:
如果((step_y-1),0)
打破;
其他的
{
对(我= step_x,J = step_y-1;J > = 1;J)
如果(框i = = = 0)
{
draw_circle(step_x,step_y,浅蓝色);
打破;
}
如果(j,1)断裂;
step_y = J;
法官(step_x,step_y);
打破;
}
案例ESC:
打破;
案例空间:
如果(step_x > = 1和step_x <= 18和step_y > = 1和step_y < =
18)
{
如果(盒[ step_x ] [ step_y ] = = 0)
{
盒[ step_x ] [ step_y ] =旗;
如果(judgeresult(step_x,step_y)= = 1)
{
声音(1000);
延迟(1000);
nosound();
gotoxy(30400);
如果(标志= 1)
{
setbkcolor(蓝色);
cleardevice();
setviewport(100100540380,1);
定义一个图形窗口/ * * /
设置填充模式(1,2);
绿色以实填充/ * * /
设置颜色(黄色);
矩形(0,0439279);
画线工具(50,50,14);
设置颜色为红色(12);
设置文本格式(1,0,5);
/ *三重笔划字体,水平放,5倍*/
outtextxy(20,20,“白赢~”);
设置颜色为红色(15);
设置文本格式(3,0,5);
/ *无衬笔划字体,水平放大5倍*/
outtextxy(120120,“白赢~”);
设置颜色为红色(14);
设置文本格式(2,0,8);
getch();
closegraph();
出口(0);
}
如果(标志= 2)
{
setbkcolor(蓝色);
cleardevice();
setviewport(100100540380,1);
定义一个图形窗口/ * * /
设置填充模式(1,2);
绿色以实填充/ * * /
设置颜色(黄色);
矩形(0,0439279);
画线工具(50,50,14);
设置颜色为红色(12);
设置文本格式(1,0,8);
/ *三重笔划字体,水平放大8倍*/
outtextxy(20,20,“红赢~”);
设置颜色为红色(15);
设置文本格式(3,0,5);
/ *无衬笔划字体,水平放大5倍*/
outtextxy(120120,“红赢~”);
设置颜色为红色(14);
设置文本格式(2,0,8);
getch();
closegraph();
出口(0);
}
}
change();
打破;
}
}
其他的
打破;
}
}
无效change()
{
如果(标志= 1)
标志,2;
其他的
标志,1;
}
无效的判断(x,y)
{
如果(标志= 1)
draw_circle(x,y,15);
如果(标志= 2)
draw_circle(x,y,4);
}
国际judgeresult(x,y)
{
j,k,N1,n2;
(1)
{
N1,0;
n2,0;
水平向左数/ * * /
对于(j = x,k = y;j = 1;j -)
{
如果(框[„] =标志)
N1 + +;
其他的
打破;
}
水平向右数/ * * /
对于(j = x,k = y;j,18;j + +)
{
如果(框[„] =标志)
n2,;
其他的
打破;
}
如果(N1 + n2-1 > = 5)
{
返回(1);
打破;
}
垂直向上数/ * * /
N1,0;
n2,0;
对于(j,x,k,y;k,1;k—)
{
如果(框[„] =标志)
N1 + +;
其他的
打破;
}
垂直向下数/ * * /
对于(j = x,k = y;k,18;K + +)
{
如果(框[„] =标志)
n2,;
其他的
打破;
}
如果(N1 + n2-1 > = 5)
{
返回(1);
打破;
}
向左上方数/ * * /
N1,0;
n2,0;
对于(= x,k = y;j = 1,k = 1;j,k -)
{
如果(框[„] =标志)
N1 + +;
其他的
打破;
}
向右下方数/ * * /
对于(j = x,k = y;j,18,k,18;j + +,k +)
{
如果(框[„] =标志)
n2,;
其他的
打破;
}
如果(N1 + n2-1 > = 5)
{
返回(1);
打破;
}
向右上方数/ * * /
N1,0;
n2,0;
对于(j = x,k = y;j,18,k = 1;j + +,K -)
{
如果(框[„] =标志)
N1 + +;
其他的
打破;
}
向左下方数/ * * /
对于(= x,k = y;j = 1,k,18;j,k +)
{
如果(框[„] =标志)
n2,;
其他的
打破;
}
如果(N1 + n2-1 > = 5)
{
返回(1);
打破;
}
返回(0);
打破;
}
}
无效main()
{
国际gdriver = VGA,gmode = vgahi;
clrscr();
attention();
这句话的意思是(与gdriver,与gmode,“C:\ TC”);
/ * * / setwritemode(xor_put);
标志,1;
draw_box();
做
{
step_x = 0;
step_y = 0;
/ * draw_circle(step_x,step_y,8);* /
法官(step_x-1,step_y-1);
做
{
而(bioskey(1)= = 0);
(0)bioskey关键=;
judgekey();
}
当(键)~=空格键~= ESC键);
}
当(键)~= ESC键);
closegraph();
}
本文档为【黑白棋c语言程序(Othello C language program)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。