4.电脑输入
电脑输入这里我们将引用前面猜数字游戏所讲的一个知识点,随机数的产生,接下来我们来看看代码的实现
void ComputerMove(char board[ROW][COL], int row, int col) { printf("电脑输入\n"); while (1)//循环输入,直到输入成功为止 { int x = rand() % row;//与前面所讲的srand()一起使用,可产生1-row的随机数 int y = rand() % col;//1-col的随机数 if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '#';//电脑下棋落子记为'#' break;//输入成功跳出循环 } } }
结果展示可以参考上一张图
5.判断输赢
数据输入了进来,这时候我们就要进行判断游戏是否继续,如果不继续结果是怎么样的?
代码如下:
char IsWin(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][0] != ' ') { return board[i][0];//返回赢得棋子是*还是# } } //列 for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') { return board[0][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 (IsFull(board, row, col) == 1) { return 'Q'; } //继续 return 'C'; }
这个函数里还嵌套了一个IsFull()得函数,用来判断是否所有空格都被填满,代码如下
int IsFull(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { if (board[i][j] == ' ') return 0; } } return 1; }
到此为止所有需要得功能都已经写完,接下来我们要将他们组装起来,组装如下
void game() { char board[ROW][COL] = { 0 }; InitBoard( board, ROW, COL);//数组初始化 DisplayBoard(board, ROW, COL);//打印最初得棋盘 while (1) { PlayerMove(board, ROW, COL);//玩家输入 DisplayBoard(board, ROW, COL);//打印已经改变的棋盘 if (IsWin(board, ROW, COL) != 'C')//判断是否继续游戏 break;//游戏结束跳出循环 ComputerMove(board, ROW, COL);//电脑输入 DisplayBoard(board, ROW, COL);//打印改变得棋盘 if (IsWin(board, ROW, COL) != 'C')//判断是否继续游戏 break; } if (IsWin(board, ROW, COL) == '*') printf("恭喜你,你赢了!!\n");//接受返回值做出判断 if (IsWin(board, ROW, COL) == '#') printf("很遗憾,你输了!!\n");//接受返回值做出判断 if (IsWin(board, ROW, COL) == 'Q') printf("平局!!\n");//接受返回值做出判断 }
至此呢,所有代码都已完成,我们来试玩一下
成功实现,但是我们发现,前面堆积太多了,下一次游戏进行十分不美观,这时候博主前面所讲得一个就起作用了 ,有了这一段代码后,只需要你再按一下回车,就可以清除前面所打印的。
以上就是博主的对三子棋游戏的实现,制作不易,各位看官给个三连呗!!!
以上就是博主的对三子棋游戏的实现,制作不易,各位看官给个三连呗!!!
game.h
#pragma once #include<stdio.h> #include<time.h> #include<windows.h> #define ROW 3 #define COL 3 //数组的初始化 void InitBoard(char board[ROW][COL], int row, int col); //打印棋盘 void DisplayBoard(char board[ROW][COL], int row, int col); //选手输入 void PlayerMove(char board[ROW][COL], int row, int col); //电脑输入 void ComputerMove(char board[ROW][COL], int row, int col); //判断输赢 char IsWin(char board[ROW][COL], int row, int col);
game.c
#include "game.h" void InitBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { board[i][j] = ' '; } } } void DisplayBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { printf(" %c ", board[i][j]); if (j < col - 1) printf("|"); } printf("\n"); for (j = 0; j < row; j++) { if (i < row - 1) { printf("---"); if (j < col - 1) printf("|"); } } printf("\n"); } } void PlayerMove(char board[ROW][COL], int row, int col) { printf("玩家输入\n"); printf("请输入一个坐标,中间用空格隔开>:"); int x = 0; int y = 0; while (1) { scanf("%d%d", &x, &y); if ((x<1 && x>row) || (x<1 && x>col)) printf("非法输入,请重新输入\n"); if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else printf("此处已被占用,请重新输入\n"); } } void ComputerMove(char board[ROW][COL], int row, int col) { printf("电脑输入\n"); while (1) { int x = rand() % row; int y = rand() % col; if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '#'; break; } } } int IsFull(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { if (board[i][j] == ' ') return 0; } } return 1; } char IsWin(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][0] != ' ') { return board[i][0]; } } //列 for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') { return board[0][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 (IsFull(board, row, col) == 1) { return 'Q'; } //继续 return 'C'; } void game() { char board[ROW][COL] = { 0 }; InitBoard( board, ROW, COL); DisplayBoard(board, ROW, COL); while (1) { PlayerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); if (IsWin(board, ROW, COL) != 'C') break; ComputerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); if (IsWin(board, ROW, COL) != 'C') break; } if (IsWin(board, ROW, COL) == '*') printf("恭喜你,你赢了!!\n"); if (IsWin(board, ROW, COL) == '#') printf("很遗憾,你输了!!\n"); if (IsWin(board, ROW, COL) == 'Q') printf("平局!!\n"); }
test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void menu() { printf("******** 三子棋游戏 ********\n"); printf("******** 1 . 玩游戏 *******\n"); printf("******** 2 . 退出游戏 *******\n"); printf("**规则:一次一子,三子相连为赢\n"); printf("******************************\n"); } int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,重新选择!\n"); break; } getchar(); getchar(); system("cls"); } while (input); return 0; }