//函数声明 voidInitBoard(char board[ROW][COL], int row, int col);//初始化棋盘功能函数的声明 voidDisplayBoard(char board[ROW][COL], int row, int col); //棋盘打印功能函数的声明 voidPlayerMove(char board[ROW][COL], int row, int col); //玩家输入功能函数的声明 voidComputerMove(char board[ROW][COL], int row, int col);//电脑输入函数的声明 charWin(char board[ROW][COL], int row, int col);//胜负判断功能函数的声明 charFull(char board[ROW][COL], int row, int col);//棋盘已满,判断平局功能函数的声明
//玩家输入 voidPlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; while (1) { printf("系统提示:请输入您要下的坐标:>>>"); scanf("%d%d", &x, &y);//获取玩家坐标 printf("\n"); //判断xy是否合法,一个是是否溢出,一个是是否被占用 if (x > 0 && x < row + 1 && y>0 && y < col + 1) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*';//如果是空格就替换成* break; } else { printf("电脑:这我下过了,你个憨憨\n");//占用判断 printf("\n"); } } else { printf("电脑:FBI warning 非法输入,非法输入!!!请您自觉点\n\n");//非法输入 printf("电脑:乖,咱重新输入!!!\n\n"); } } }
电脑输入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//电脑输入 voidComputerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑:我走过啦!!!到你啦!!!\n\n"); while (1) { x = rand() % row;//库函数中的伪随机数,需要引用头文件 y = rand() % col;//同上 if (board[x][y] == ' ') { board[x][y] = '#';//如果是空格就替换成# break; } } }
//判断胜负 charWin(char board[ROW][COL], 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[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') { return board[1][1]; } //判断是否平局 if (1 == Full(board, ROW, COL))//平局判断,函数在下面,已在头文件中声明,位置就无所谓了 { return'Q'; } return'C'; }
平局算法
1 2 3 4 5 6 7 8 9 10 11 12 13
//平局算法函数 charFull(char board[ROW][COL], int row, int col)//棋盘满了还没达到上面的条件,就算平局啦! { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (board[i][j] == ' ') return0; } } return1; }
完整程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//宏定义 #define _CRT_SECURE_NO_WARNINGS 1 #define ROW 3 #define COL 3
//引用库函数 #include<stdio.h> #include<stdlib.h>
//函数声明 voidInitBoard(char board[ROW][COL], int row, int col); voidDisplayBoard(char board[ROW][COL], int row, int col); voidPlayerMove(char board[ROW][COL], int row, int col); voidComputerMove(char board[ROW][COL], int row, int col); charWin(char board[ROW][COL], int row, int col); charFull(char board[ROW][COL], int row, int col);
//电脑输入 voidComputerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑:我走过啦!!!到你啦!!!\n\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
//判断胜负 charWin(char board[ROW][COL], 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[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') { return board[1][1]; } //判断是否平局 if (1 == Full(board, ROW, COL)) { return'Q'; } return'C'; } //平局算法函数 charFull(char board[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (board[i][j] == ' ') return0; } } return1; }