c语言编写三子棋
2016-12-27 16:22
309 查看
c语言编写三子棋
text.c#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<time.h> #include<stdlib.h> #include"game.h" void game() { int ret = 0; char board[ROWS][COLS]={0}; init_board(board,ROWS,COLS); display_board(board,ROWS,COLS); srand((unsigned int)time(NULL)); while(1) { player_move(board,ROWS,COLS); if((ret = check_win(board, ROWS, COLS)) != ' ') break; display_board(board,ROWS,COLS); computer_move(board,ROWS,COLS); if((ret = check_win(board, ROWS, COLS)) != ' ') break; display_board(board,ROWS,COLS); } ret = check_win(board, ROWS, COLS); if(ret == 'X') { printf("恭喜你!你赢了!\n"); } else if(ret == 'O') { printf("不好意思!你输了!\n"); } else if(ret == 'q') { printf("平局!\n"); } display_board(board, ROWS, COLS); } void menu() { printf("*******************************\n"); printf("****** 1. play 0. exit ******\n"); printf("*******************************\n"); } int main () { int input = 0; do { menu(); scanf("%d",&input); switch(input) { case 1: game(); break; case 0: break; default: printf("选择错误!!"); break; } }while(input); return 0; }
game.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void init_board(char board[ROWS][COLS],int row,int col) { memset(board,' ',row*col); } void display_board(char board[ROWS][COLS],int row,int col) { int i = 0; for(i=0; i<row; i++) { printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]); if(i != 2) printf(" 4000 ---|---|---\n"); } } void player_move(char board[ROWS][COLS],int row,int col) { int x = 0; int y = 0; while(1) { printf("请输入坐标:"); scanf("%d%d",&x,&y); x--; y--; if(((x>=0)&&(x<=2))&&((y>=0)&&(y<=2))) { if(board[x][y] == ' ') { board[x][y]='X'; break; } else { printf("输入有误 !"); } } else { printf("输入有误 !"); } } } void computer_move(char board[ROWS][COLS],int row,int col) { printf("电脑下:\n"); while(1) { int x = rand()%3; int y = rand()%3; if(board[x][y] == ' ') { board[x][y] = 'O'; break; } } } static int is_full(char board[ROWS][COLS],int row,int col) { int i = 0; int j = 0; for(i=0;i<row;i++) { for(j=0;j<col;j++) { if(board[i][j]==' ') return 0; } } return 1; } char check_win(char board[ROWS][COLS], int row, int col) { int i = 0; for(i=0; i<row; i++) { if((board[i][0]==board[i][1]) &&(board[i][1]==board[i][2]) &&(board[i][1]!=' ')) return board[i][1]; } for(i=0; i<col; i++) { if((board[0][i]==board[1][i]) &&(board[1][i]==board[2][i]) &&(board[1][i]!=' ')) return board[1][i]; } if((board[0][0]==board[1][1]) &&(board[1][1]==board[2][2]) &&(board[1][1]!=' ')) return board[1][1]; if((board[0][2]==board[1][1]) &&(board[1][1]==board[2][0]) &&(board[1][1]!=' ')) return board[1][1]; if(is_full(board, row, col)) { return 'q'; } return ' '; }
game.h
#ifndef __GAME_H__ #define __GAME_H__ #include<stdio.h> #include<stdlib.h> #include<string.h> #define ROWS 3 #define COLS 3 void init_board(char board[ROWS][COLS],int row,int col); void display_board(char board[ROWS][COLS],int row,int col); void player_move(char board[ROWS][COLS],int row,int col); void computer_move(char board[ROWS][COLS],int row,int col); char check_win(char board[ROWS][COLS], int row, int col) ; #endif //__GAME_H__
相关文章推荐
- 【C语言】用C语言实现三子棋游戏的编写
- 用C语言编写三子棋的具体分析
- 用c语言编写三子棋小游戏
- 【三子棋】用c语言编写一个小游戏---三子棋
- C语言及程序设计提高例程-27 编写查找和排序函数
- 用C语言编写Windows服务程序的五个步骤
- 【c语言】编写一个函数计算字符串长度
- 编写函数,其功能是:将一个数字字符串转换为一个整数(不得调用C语言中提供的将字符串转换成整数的函数。)要求在主函数中输入该字符串并输出转换后的整数的值。
- 《对C语言编写的银行系统用C++进行重构——2》
- 用C语言编写函数,实现strlen计算字符串长度的功能
- C语言编写程序计时
- 编写个从1加到100的程序 谁能用c语言中的for语句
- C语言:编写折半查找函数,即二分法
- C语言学习篇-1Hello, World!(编写、编译、链接、运行)
- 用C语言编写一个“+”“-”“*”“/”简单的计算器
- c语言:编写一个程序,可以直接接收键盘字符
- 编写自己的C语言头文件
- 在Linux下用C语言写一个小程序实现一个进度条编写Makefile来进行运行
- 用C语言编写猜数字游戏
- 用C语言在NDS上编写程序