在C学习的过程中,我们写了比如hellow world、斐波那契数列、九九乘法表等等很多的程序,今天为大家带来三子棋,也就是井字棋用C实现的教程
首先我们来写出这个游戏的菜单如下:
void menu() { printf("*************************************\n"); printf("***** 1.paly ************ 0.exit ****\n"); printf("*************************************\n"); }
让玩家知道选择1玩游戏,选择0是退出游戏。
在主函数中如下:
void test() { int input = 0;//定义一个让玩家输入选择的变量 do { menu(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 1 ://输入1时开始游戏 game(); break; case 0 ://输入0时退出游戏 printf("退出游戏\n"); break; default ://输入其他数时则选择错误 printf("选择错误,请重新选择!\n"); break; } } while (input);//若为0则判断条件为假退出循环 } int main() { test(); }
#include<stdio.h> #include<stdlib.h> #include<time.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);//判断双方输赢 int IsFull(char board[ROW][COL], int row, int col);//判断棋盘是否满
为了方便以后修改将行列数定义成宏变量
在没有开始下棋时所有的格子都需要是空的
代码如下:
void InitBoard(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { 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"); if (i < row - 1) { for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("\n"); } } }
首次打印出3X3的棋盘
void PlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("玩家走:>\n"); while (1) { printf("请输入要下的坐标:>"); scanf("%d%d", &x, &y); //判断坐标合法性 if (x >= 1 && x <= row && y >= 1 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("该坐标被占用!\n"); } } else { printf("坐标非法,请重新输入!\n"); } } }
每次下棋时输入要下位置的坐标,如果此处的为空则落子,否则让玩家重新输入坐标,直到输入正确位置
玩家落完子后,电脑从剩下的空位置中随机选择一个落子(也可以实现一个比较智能的)
代码如下:
void ComputerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑走:>\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }
为正确生成随机坐标
在tect函数中
加入
srand((unsigned int)time(NULL));
随机数种子
赢棋有几种方式:
1、三横
2、三竖
3、两个对角线
当其中有一个成立时,则返回成立时的三个元素之一,若为电脑子则为电脑赢,若为玩家子则为玩家赢。
若在棋盘满时没有3点中任何一个成立,则为平局
代码如下:
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[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') { return board[1][1]; } if (1 == IsFull(board, ROW, COL)) { return 'Q'; } return 'C'; }
在判断输赢的函数中调用,遍历棋盘若其中没有空则为满,满则返回1,若不满则返回0
代码如下:
int IsFull(char board[ROW][COL], 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; }
开始时创建一个数组作为棋盘,然后初始化棋盘,玩家先下,后判断输赢再由电脑落子,再判断输赢,放入循环中直到棋盘满,或分出输赢
代码如下:
void game() { //数组-存放走出的棋盘信息 char board[ROW][COL] = { 0 }; char ret = 0; //初始化棋盘 InitBoard(board, ROW, COL); //打印棋盘 DisplayBoard(board, ROW, COL); //下棋 while (1) { //玩家下棋 PlayerMove(board, ROW, COL); //打印棋盘 DisplayBoard(board, ROW, COL); //判断玩家是否赢 ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } //电脑下棋 ComputerMove(board, ROW, COL); //打印棋盘 DisplayBoard(board, ROW, COL); //判断电脑是否赢 ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("玩家赢!\n"); } else if (ret == '#') { printf("电脑赢!\n"); } else { printf("平局!\n"); } }
#include<stdio.h> #include<stdlib.h> #include<time.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); int IsFull(char board[ROW][COL], int row, int col);
#include "game.h" void InitBoard(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { 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"); if (i < row - 1) { for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("\n"); } } } void PlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("玩家走:>\n"); while (1) { printf("请输入要下的坐标:>"); scanf("%d%d", &x, &y); //判断坐标合法性 if (x >= 1 && x <= row && y >= 1 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("该坐标被占用!\n"); } } else { printf("坐标非法,请重新输入!\n"); } } } void ComputerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑走:>\n"); while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } 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[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') { return board[1][1]; } if (1 == IsFull(board, ROW, COL)) { return 'Q'; } return 'C'; } int IsFull(char board[ROW][COL], 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; }
#include"game.h" void menu() { printf("*************************************\n"); printf("***** 1.paly ************ 0.exit ****\n"); printf("*************************************\n"); } void game() { //数组-存放走出的棋盘信息 char board[ROW][COL] = { 0 }; char ret = 0; //初始化棋盘 InitBoard(board, ROW, COL); //打印棋盘 DisplayBoard(board, ROW, COL); //下棋 while (1) { //玩家下棋 PlayerMove(board, ROW, COL); //打印棋盘 DisplayBoard(board, ROW, COL); //判断玩家是否赢 ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } //电脑下棋 ComputerMove(board, ROW, COL); //打印棋盘 DisplayBoard(board, ROW, COL); //判断电脑是否赢 ret = IsWin(board, ROW, COL); if (ret != 'C') { break; } } if (ret == '*') { printf("玩家赢!\n"); } else if (ret == '#') { printf("电脑赢!\n"); } else { printf("平局!\n"); } } void test() { 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; } } while (input); } int main() { test(); }
实现完成,下次为大家带来扫雷!