每晚两个C词汇小工程项目,提高你的程式设计潜能!
《西洋棋》也叫滑动棋或是哈姆雷特,其格斗游戏操作过程是互相滑动旁人的方格,最终以方格上谁的方格多来推论输赢。尽管准则单纯,但变动繁杂,是众所周知的初学难精,奥妙无穷,没关系您就试一试吧!
格斗作法
轮到另一方打牌时,要把棋下在与旁人方格交界处的空当上,明确要求科折粉的方格和旧有的利皮扬卡方格扣住旁人的最少两个方格(上领斜夹均可),接着把被扣住的子变为我方的色调(也叫王将)。打牌操作过程中,任何方格既不能从方格上偷走,也不能从两个方格移至另两个方格。在轮到另一方打牌时,假如阿提斯鲁夫尔谷无子嗣可吃,就要停一步棋,让旁人竭尽全力打牌,此种情形叫pass;而当另一方在五子可吃时,即便想pass也不容许pass 。
那时他们就用C词汇来同时实现它!
虽然工程项目包涵许多相片,因而这儿得出整座工程项目的LiveCD:
https://codebus.cn/f/a/0/0/1/reversi.zip下列是工程项目的全数源代码:
#include <graphics.h> #include <cstdio> #include <stdio.h> /*******************************定义全局变量*****************************/ char map[8][8]; //方格 IMAGE img[6]; //保存相片 int black, white; //双方的方格数 char today; //当前轮到谁走 /**********************************定义函数*****************************/ void load(void) //加载相片 { loadimage(&img[0], “相片\\黑空.jpg”); loadimage(&img[1], “相片\\白空.jpg”); loadimage(&img[2], “相片\\黑子黑空.jpg”); loadimage(&img[3], “相片\\黑子白空.jpg”); loadimage(&img[4], “相片\\白子黑空.jpg”); loadimage(&img[5], “相片\\白子白空.jpg”); } void print(void) //画方格 { int x, y; black = white = 0; for(x = 0; x < 8; x++) for(y = 0; y <8; y++) switch(map[x][y]) { case 0: if((x + y) % 2) putimage(60 * y, 60 * x, &img[0]); elseputimage(60 * y, 60 * x, &img[1]); break; case B: if((x + y) % 2) putimage(60 * y, 60 * x, &img[2]); else putimage(60 * y, 60 * x, &img[3]); black++; break; case W: if((x + y) % 2) putimage(60 * y, 60 * x, &img[4]); elseputimage(60 * y, 60 * x, &img[5]); white++; break; } } inline void print1(void) //画当前谁走的 { setcolor(WHITE); bar(530, 60, 590, 120); bar(530, 360, 590, 420); if(today == B) putimage(530, 60, &img[3]); else putimage(530, 360, &img[4]); } void draw(int x, int y, char a) //下当前子 { char b = ((a == B) ? W : B);//敌方子 int x1, y1, x2, y2; bool sign = false; //是否越过敌方子 for(x1 = x – 1; x1 >= 0 && map[x1][y]; x1–) //推论上方 { if(map[x1][y] == b) sign = true; else { if(sign) { for(x2 = x – 1; x2 > x1; x2–) //推论下方 map[x2][y] = a; }break; } } sign = false; for(x1 = x + 1; x1 < 8 && map[x1][y]; x1++) //推论右方 { if(map[x1][y] == b) sign =true; else { if(sign) { for(x2 = x + 1; x2 < x1; x2++) map[x2][y] = a; } break; } } sign =false; for(y1 = y – 1; y1 >= 0 && map[x][y1]; y1–) //推论左方 { if(map[x][y1] == b) sign =true; else { if(sign) { for(y2 = y – 1; y2 > y1; y2–) map[x][y2] = a; } break; } } sign =false; for(y1 = y + 1; y1 < 8 && map[x][y1]; y1++) //推论右方 { if(map[x][y1] == b) sign = true; else { if(sign) {for(y2 = y + 1; y2 < y1; y2++) map[x][y2] = a; } break; } } sign = false; for(x1 = x –1, y1 = y – 1; x1 >= 0 && y1 >= 0 && map[x1][y1]; x1–, y1–) //左上方 { if(map[x1][y1] == b) sign =true; else { if(sign) { for(x2 = x – 1, y2 = y – 1; x2 > x1 && y2 > y1; x2–, y2–) map[x2][y2] = a; }break; } } sign = false; for(x1 = x + 1, y1 = y + 1; x1 < 8 && y1 < 8 && map[x1][y1]; x1++, y1++)//右下方 { if(map[x1][y1] == b) sign = true; else { if(sign) { for(x2 = x + 1, y2 = y + 1; x2 < x1 && y2 < y1; x2++, y2++)map[x2][y2] = a; } break; } } sign = false; for(x1 = x + 1, y1 = y –1; x1 < 8 && y1 >= 0 && map[x1][y1]; x1++, y1–) //左下方 { if(map[x1][y1] == b) sign = true; else { if(sign) {for(x2 = x + 1, y2 = y – 1; x2 < x1 && y2 > y1; x2++, y2–) map[x2][y2] = a; } break; } } sign =false; for(x1 = x – 1, y1 = y + 1; x1 >= 0 && y1 < 8 && map[x1][y1]; x1–, y1++) //右上方 { if(map[x1][y1] == b) sign =true; else { if(sign) { for(x2 = x – 1, y2 = y + 1; x2 > x1 && y2 < y1; x2–, y2++)map[x2][y2] = a; } break; } } map[x][y] = a; print(); } bool judge(int x, int y, char a) //推论当前是否可以落下,同draw函数 { char b = ((a == B) ? W : B); int x1, y1; bool sign = false, sign1 = false; //sign1推论是否有效 if(map[x][y]) //假如当前不是空的返回假值 return false; for(x1 = x – 1; x1 >= 0 && map[x1][y]; x1–) { if(map[x1][y] == b) sign =true; else { if(sign) sign1 = true; break; } } sign = false; for(x1 = x + 1; x1 <8 && map[x1][y]; x1++) { if(map[x1][y] == b) sign = true; else { if(sign) sign1 = true; break; } } sign =false; for(y1 = y – 1; y1 >= 0 && map[x][y1]; y1–) { if(map[x][y1] == b) sign =true; else { if(sign) sign1 = true; break; } } sign = false; for(y1 = y + 1; y1 < 8 && map[x][y1]; y1++) { if(map[x][y1] == b) sign = true; else { if(sign) sign1 = true; break; } } sign =false; for(x1 = x – 1, y1 = y – 1; x1 >= 0 && y1 >= 0 && map[x1][y1]; x1–, y1–) { if(map[x1][y1] == b) sign =true; else { if(sign) sign1 = true; break; } } sign = false; for(x1 = x +1, y1 = y + 1; x1 < 8 && y1 < 8 && map[x1][y1]; x1++, y1++) { if(map[x1][y1] == b) sign = true; else { if(sign) sign1 = true; break; } } sign = false; for(x1 = x + 1, y1 = y – 1; x1 < 8&& y1 >=0 && map[x1][y1]; x1++, y1–) { if(map[x1][y1] == b) sign = true; else { if(sign) sign1 = true; break; } } sign = false; for(x1 = x – 1, y1 = y + 1; x1 >= 0 && y1 < 8 && map[x1][y1]; x1–, y1++) {if(map[x1][y1] == b) sign = true; else { if(sign) sign1 = true; break; } } return sign1; } bool win(void) //推论是否有棋可走 { int x, y; for(x = 0; x < 8; x++) for(y = 0; y < 8; y++) if(judge(x, y, today))return true; return false; } bool quit(void) //推论是否有棋 { int x, y; bool b = false, w = false; for(x = 0; x <8; x++) for(y = 0; y < 8; y++) { if(map[x][y] == B) b = true; else if(map[x][y] == W) w = true; }return(b && w); } bool ask(void) //弹出对话框 { HWND wnd = GetHWnd(); int key; char str[30] = “黑:”, s[2]; sprintf(s, “%d”, black); strcat(str, s); strcat(str, ” 白:”); sprintf(s, “%d”, white); strcat(str, s); strcat(str, “\n是否重新开始?”); if(black == white) key = MessageBox(wnd, str, “和局”, MB_YESNO | MB_ICONQUESTION);else if(black > white) key = MessageBox(wnd, str, “黑胜”, MB_YESNO | MB_ICONQUESTION); elsekey = MessageBox(wnd, str,“白胜”, MB_YESNO | MB_ICONQUESTION); if(key == IDYES) return true; else return false; }void play(void) //格斗游戏操作过程 { MOUSEMSG m; int x, y; for(x = 0; x < 8; x++) for(y = 0; y < 8; y++) map[x][y] =0; map[3][4] = map[4][3] = B; map[3][3] = map[4][4] = W; today = B; print(); print1(); do { do { while(true) { m = GetMouseMsg(); if(m.uMsg == WM_LBUTTONDOWN) //假如左键点击 break; } x = m.y /60; y = m.x / 60; if(judge(x, y, today)) //假如当前位置有效 { draw(x, y, today); //下子today = ((today ==B) ? W : B); print1(); //交换 } }while(win() && quit()); //假如当前无棋可走 today = ((today == B) ? W : B); print1(); }while(win() && quit()); //双层推论 } int main(void) //主函数 { initgraph(640, 480); load(); setbkcolor(WHITE); cleardevice();do { play(); }while(ask()); closegraph(); return 0; }大家赶紧去动手试试吧!
此外,我也给大家撷取我收集的其他资源,从最零此基础开始的教程到C词汇C++工程项目案例,帮助大家在学习C词汇的道路上披荆斩棘!
程式设计学习书籍撷取:
程式设计学习视频分享:
整理撷取(多年学习的源代码、工程项目实战视频、工程项目笔记,此基础入门教程)最重要的是你可以在群里面交流提问程式设计问题哦!
对于C/C++感兴趣可以关注小编在后台私信我:【程式设计交流】一起来学习哦!可以领取一些C/