C语言写五子棋练习
2016-07-14 00:19
309 查看
::::::::::::::
chess/readme
::::::::::::::
This is a wuziqi game could be run under Linux OS terminal;
please use "make" command in the directory of Makefile first of all, then the function named "run" will be automatically generated ;
use " ./run" to start the game;
::::::::::::::
chess/Makefile
::::::::::::::
::::::::::::::
chess/data.h
::::::::::::::
::::::::::::::
chess/data.c
::::::::::::::
::::::::::::::
chess/user.c
::::::::::::::
chess/readme
::::::::::::::
This is a wuziqi game could be run under Linux OS terminal;
please use "make" command in the directory of Makefile first of all, then the function named "run" will be automatically generated ;
use " ./run" to start the game;
::::::::::::::
chess/Makefile
::::::::::::::
CC=gcc OBJS=run CFLAGS=-o SRC=data.c user.c OBJS: $(SRC) <span style="white-space:pre"> </span>$(CC) $(SRC) $(CFLAGS) $(OBJS) clean: <span style="white-space:pre"> </span>-rm -rv $(OBJS)
::::::::::::::
chess/data.h
::::::::::::::
/* * 棋子数据结构 * data.h * Longbin Li <beangr@163.com> * 2014-10-05 * */ ifndef __DATA_H__ include <stdio.h> include <stdlib.h> define HEI -1 /* */ define BAI +1 /* O */ typedef struct node { int x; int y; int id; struct node *p_next; } node, *ll_head; ll_head ll_create(void); /* create a linked list */ node *ll_input(node *); void ll_insert(ll_head, node *); /* insert a node to the linked list */ void ll_delete(ll_head, node *); /* delete a node from the linked list */ node *ll_locate(ll_head, int, int); /* return if the node exists */ void ll_traversal(ll_head); /* traversal the linked list */ int ll_win(ll_head, node *); void start_game(); endif /* __DATA_H__*/
::::::::::::::
chess/data.c
::::::::::::::
/* * 五子棋操作实现函数 * data.c * Longbin Li <beangr@163.com> * 2014-10-05 * */ include "data.h" /* create a linked list */ ll_head ll_create(void){ node *new = malloc(sizeof(struct node)); if (new == NULL) { perror("malloc"); return NULL; } new->p_next = NULL; //printf("create linked list successfully !\n"); return new; } /* linked list input function */ node *ll_input(node *tmp){ printf("please input (x,y): "); scanf("%d%d", &tmp->x, &tmp->y); return tmp; } /* insert a node to the linked list */ void ll_insert(ll_head head, node *tmp){ if (ll_locate(head, tmp->x, tmp->y)) { printf("the point (%2d,%2d) has exited\n", tmp->x, tmp->y); return ; } node *new = malloc(sizeof(struct node)); if (new == NULL) { perror("malloc"); return ; } new->x = tmp->x; new->y = tmp->y; new->id = tmp->id; new->p_next = head->p_next; head->p_next = new; } /* delete a node from the linked list */ void ll_delete(ll_head head, node *tmp){ node *this = head; node *fro = this; int flag = 0; for (; this;) { fro = this; this = this->p_next; if (this->x == tmp->x && this->y == tmp->y) { fro->p_next = this->p_next; free(this); this = fro; flag = 1; printf("delete (%2d,%2d)\n", tmp->x, tmp->y); // break; } } if (this == NULL && flag == 0) { printf("the node not exists\n"); } } /* free the memory of the linkedlist */ void ll_free(ll_head head){ node *fro = head; node *this = head; for (; this->p_next;) { fro = this; this = this->p_next; fro->p_next = this->p_next; //printf("free(%02d,%02d) ", this->x, this->y); free(this); this = fro; } //printf("\n"); } /* return if the node exists */ node *ll_locate(ll_head head, int x, int y){ node *this = head; while (this = this->p_next) { if (this->x == x && this->y == y) { return this; } } return NULL; } /* traversal the linked list */ void ll_traversal(ll_head head){ node *this = head; while (this = this->p_next) { printf("(%d,%d),%d\n", this->x, this->y, this->id); } } /* display the chessboard */ void print_chess(ll_head head){ int y_max = 0, y_min = 0; int x_max = 0, x_min = 0; int bd = 2; /* the boarder width of the chessboard */ node *this = head; /* get the value scope of x and y */ while (this = this->p_next) { if (y_max < this->y) { y_max = this->y; } if (y_min > this->y) { y_min = this->y; } if (x_max < this->x) { x_max = this->x; } if (x_min > this->x) { x_min = this->x; } } /* print the chessboard */ int i, j; /* print the chessboard from y_max to y_min */ system("clear"); for (j = y_max + bd; j >= y_min - bd; j--) { printf("%5d| ", j); /* print the chessboard from x_min to x_max */ for (i = x_min - bd; i <= x_max + bd; i++) { if (this = ll_locate(head, i, j)) { if (this->id == HEI) { printf(" "); } else { printf(" O "); } } else { printf(" . "); } } printf("\n"); } printf(" "); for (i = x_min - bd; i <= x_max + bd; i++) { printf(" %2d", i); } printf("\n"); } /* judgement the winner */ int ll_win(ll_head head, node *tmp) { int y_max = 0, y_min = 0; int x_max = 0, x_min = 0; int chess = 5; int x = tmp->x; int y = tmp->y; int id = tmp->id; int cnt = 1; node *this = head; /* get the value scope of x and y */ while (this = this->p_next) { if (y_max < this->y) { y_max = this->y; } if (y_min > this->y) { y_min = this->y; } if (x_max < this->x) { x_max = this->x; } if (x_min > this->x) { x_min = this->x; } } /* judge the winner */ int i = x, j = y; /* horizontal direction */ i = x, j = y; for (i = x - 1; i >= x_min; i--) { if ((this = ll_locate(head, i, j)) && (this->id == id)) { cnt++; } else { break; } } for (i = x + 1; i <= x_max; i++) { if ((this = ll_locate(head, i, j)) && (this->id == id)) { cnt++; } else { break; } } //printf("horizontal direction has %d elements\n", cnt); if (cnt >= chess) { printf("%s win !\n", id == BAI ? "O" : ""); return 1; } else { cnt = 1; } /* vertical direction */ i = x, j = y; for (j = y + 1; j <= y_max; j++) { if ((this = ll_locate(head, i ,j)) && (this->id == id)) { cnt++; } else { break; } } for (j = y - 1; j >= y_min; j--) { if ((this = ll_locate(head, i, j)) && (this->id == id)) { cnt++; } else { break; } } //printf("vertical direction has %d elements\n", cnt); if (cnt >= chess) { printf("%s win !\n", id == BAI ? "O" : ""); return 1; } else { cnt = 1; } /* left-falling stroke */ i = x, j = y; for (i = x - 1, j = y - 1; i >= x_min && j >= y_min; i--, j--) { if ((this = ll_locate(head, i, j)) && (this->id == id)) { cnt++; } else { break; } } for (i = x + 1, j = y + 1; i <= x_max && j <= y_max; i++, j++) { if ((this = ll_locate(head, i, j)) && (this->id == id)) { cnt++; } else { break; } } //printf("left-falling direction has %d elements\n", cnt); if (cnt >= chess) { printf("%s win !\n", id == BAI ? "O" : ""); return 1; } else { cnt = 1; } /* right-falling stroke */ i = x, j = y; for (i = x - 1, j = y + 1; i >= x_min && j <= y_max; i--,j++) { if ((this = ll_locate(head, i, j)) && (this->id == id)) { cnt++; } else { break; } } for (i = x + 1, j = y - 1; i <= x_max && j >= y_min; i++, j--) { if ((this = ll_locate(head, i, j)) && (this->id == id)) { cnt++; } else { break; } } //printf("right-falling stroke direction has %d elements\n", cnt); if (cnt >= chess) { printf("%s win !\n", id == BAI ? "O" : "#"); return 1; } else { cnt = 1; } return 0; } /* start wuziqi game and judge the winner */ void start_game(){ ll_head p_head = ll_create(); print_chess(p_head); node tmp = {}; int flag = HEI; int win = 0; while (1) { flag = 0 - flag; tmp.id = flag; do { printf("%s input x,y : ", flag == BAI ? "BAI" : "HEI"); scanf("%d%d", &tmp.x, &tmp.y); scanf("%*[^\n]"); scanf("%*c"); //printf("%d,%d", tmp.x, tmp.y); } while ((ll_locate(p_head, tmp.x, tmp.y) != NULL) && printf("invalid data, try again\n")); ll_insert(p_head, &tmp); print_chess(p_head); win = ll_win(p_head, &tmp); if (win) { ll_free(p_head); break; } } }
::::::::::::::
chess/user.c
::::::::::::::
/* * 五子棋游戏用户端 * user.c * Longbin Li <beangr@163.com> * 2014-10-05 * */ #include "data.h" int main(){ start_game(); return 0; }
相关文章推荐
- C语言数组
- C++对象模型学习——执行期语意学
- google-breakpad在C++ 11下编译错误修改
- Effective C++: extern
- C语言字符串相关函数大全
- 另类下载者轻松突破瑞星2010主动防御及ESET高启发
- C程序存储空间分布 c知识回顾第三天
- c++ 数据类型
- 自动查找函数地址的程序
- 任意分布的随机数的产生方法
- 内存加载SkinH.dll,不释放文件
- 致命错误 RC1121: RC : fatal error RC1121 : I/O error reading file
- MoveFileEx
- 一个sample学会使用c++比较函数对象和hash函数对象
- c++中while(cin>>str)和ctrl z的相关问题探讨
- POJ 2480 BZOJ 2705 Longge的问题
- C++ 11 : call a function on every item of a vector
- 深入理解C++类的构造函数与析构函数
- 李洪强漫谈iOS开发[C语言-010] - C语言简要复习
- C++实现线程池