c语言实现三子棋
来源:互联网 发布:淘宝碰到专业差评师 编辑:程序博客网 时间:2024/04/26 11:07
三子棋游戏是指棋盘为3*3,玩家与电脑之间的对决,谁先将各自的棋下为一排(横/竖/斜)者获胜,电脑所下位置是随机产生的。
game,h中定义的函数:
#define ROWS 3 #define COLS 3 void init_board(char board[ROWS][COLS], int rows,int cols);//初始化棋盘 void display_board(char board[ROWS][COLS], int rows, int cols);//打印棋盘 void player_move(char board[ROWS][COLS], int rows, int cols);//玩家下棋的函数 void computer_move(char board[ROWS][COLS], int rows, int cols);//电脑下棋的函数 char check_win(char board[ROWS][COLS], int rows, int cols);//判断输赢函数
test.h用于存放主函数:
#include<stdio.h> #include<stdlib.h> #include"game.h" void menu() //打印菜单 { printf("********** 1.PLAY *********\n"); printf("********** *********\n"); printf("********** 0.EXIT *********\n"); } void game() { int ret = 0; char board[ROWS][COLS] = { 0 }; init_board(board, ROWS, COLS); display_board(board, ROWS, COLS); while (1) { printf("player move;\n"); player_move(board, ROWS, COLS); display_board(board, ROWS, COLS); ret = check_win(board, ROWS, COLS); if (ret != ' ') break; printf("computer move:\n"); computer_move(board, ROWS, COLS); display_board(board, ROWS, COLS); check_win(board, ROWS, COLS); if (ret != ' ') break; } if (ret == 'X') { printf("Congratulatons! player win!\n"); } else if (ret == '0') { printf("Undortunately! player lost!\n"); } else { printf("It's tie,play again?\n"); } } enum Option { EXIT, PLAY, }; int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("please chose:>"); scanf("%d", &input); switch(input) { case PLAY: game(); break; case EXIT: break; default: printf("default!please chose again!\n"); break; } } while (input); system("pause\n"); return 0; }game.c中游戏实现:
#include"game.h" #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> void init_board(char board[ROWS][COLS], int rows, int cols) { memset(board, ' ', sizeof(char)*rows*cols); //设置内存的函数(指定内存地址,初始化,内存大小) } void display_board(char board[ROWS][COLS], int rows, int cols) { int i = 0; for (i = 0; i < rows; i++) { printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]); if (i != rows - 1) printf("---|---|---\n"); } } void player_move(char board[ROWS][COLS], int rows, int cols) { int x = 0; int y = 0; /*printf("please enter the coordinates:>"); scanf("%d%d", &x, &y);*/ /*x--; y--;*/ while (1) { printf("please enter the coordinates:>"); scanf("%d%d", &x, &y); x--; y--; if ((x >= 0) && (x <= rows - 1) && (y >= 0) && (y <= cols - 1)) { if (board[x][y] == ' ') { board[x][y] = 'X'; break; } else { printf("please enter again:>\n"); } } } } void computer_move(char board[ROWS][COLS], int rows, int cols) { while (1) { int x = rand() % 3; int y = rand() % 3; if (board[x][y] == ' ') { board[x][y] = '0'; break; } } } static int is_full(char board[ROWS][COLS], int rows, int cols) { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { if (board[i][j] == ' ') return 0; } } return 1; //表示棋盘已满 } char check_win(char board[ROWS][COLS], int rows, int cols) //判断输赢,三行三列和两个交叉行 { int i = 0; for (i = 0; i < rows; 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 < cols; 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[2][2] != ' ')) { return board[1][1]; } if (is_full(board, rows, cols)) { return 'q';//棋盘满了 } return ' '; }
2 0
- C语言实现三子棋
- C语言实现三子棋
- c语言实现三子棋
- C语言实现三子棋
- C语言实现三子棋
- C语言实现三子棋
- C语言实现三子棋
- 三子棋C语言实现
- C语言三子棋实现
- C语言实现三子棋
- C语言实现三子棋游戏
- 三子棋的C语言实现
- C语言实现三子棋游戏
- 三子棋的C语言实现
- 【C语言】简单实现三子棋
- 用C语言实现三子棋
- 用c语言实现三子棋
- C语言实现小游戏---三子棋
- 我的第一篇博客
- 迷宫问题 DFS写法 BFS写法
- solver.prototxt含义及配置
- [图像处理] 高斯模糊的C++实现(Gaussian Blur)
- 异常:前端发送几次请求后,浏览器一直等待localhost响应
- c语言实现三子棋
- java csv 导入 导出 修改
- android中getSystemService详解
- 机器人与深度学习
- 2014年第五届蓝桥杯C/C++程序设计本科B组省赛-切面条(结果填空)
- zuoyeer
- Linux删除目录中小于指定大小的文件
- Matlab 会话(sessions)多开,运行多个程序
- 使用ssm框架实现用户账户邮箱激活功能(一)