C语言实现三子棋小游戏详解
时间:2021-06-03 09:08:57|栏目:C代码|点击: 次
在用C语言实现三子棋小游戏之前,我们应当了解三子棋的游戏规则:在九宫格棋盘上,哪一方三个棋子连成一线(一行或一列或对角线)即判定哪一方胜利。
以下为源代码:
1.首先打印出菜单。
void menu() { printf("**********************************\n"); printf("**********************************\n"); printf("***** 0.play *************\n"); printf("***** 1.exit *************\n"); printf("**********************************\n"); } menu(); do { printf("请输入要选的的选项: "); scanf("%d",&input); switch (input) { case 0: game(); break; case 1: printf("退出游戏"); break; default: printf("请输入要选的选项:\n"); break; } } while (input);
2.创建一个二维数组board用来储存三子棋的元素,并对其进行初始化。
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] = ' '; } } }
3.打印九宫格棋盘。
void Displayboard(char board[ROW][COL], int row, int col) //打印棋盘 { int i; int j; for (i = 0; i < row; i++) { for (j = 0; j < col ; j++) { printf(" %c ", board[i][j]); if (j == col - 1) continue; printf("|"); } printf("\n"); if (i < row - 1) printf("-----------\n"); } }
4.使用Playermove和Computermove函数来对board数组进行更改,模拟玩家和电脑落子情况。
void Playermove(char board[ROW][COL]) //玩家输入坐标 { while (1) { int x = 0; int y = 0; printf("请玩家输入坐标"); scanf_s("%d %d", &x, &y); if (x <= ROW && x >= 1 && y <= COL && y >= 1) //判断输入的坐标是否越界 { 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 x = 0; int y = 0; while (1) { x = rand() % 3; //使用rand函数生成随机数并取余得到电脑落子坐标 y = rand() % 3; if (board[x][y] == ' ') { printf("电脑输入坐标%d %d\n", x+1, y+1); board[x][y] = '#'; break; //打印后跳出循环 } } }
5.玩家和电脑每落一次子,就要进行一次判断是否获胜,根据返回值来进行判断游戏结果。
int Iswin(char board[ROW][COL], int row, int col) //判断谁获胜 { int i; int j; //1为玩家获胜 0为电脑获胜 2为平局 for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '*') //判断同一行是否相等 return 1; else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '#') return 0; } for (j = 0; j < col; j++) { if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '*') //判断同一列是否相等 return 1; else if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '#') return 0; } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '*') //判断左对角线是否相等 return 1; else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '#') return 0; if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '*') //判断右对角线是否相等 return 1; else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '#') return 0; if (Full(board, ROW, COL)) //判断是否平局 return 2; return 3; //返回3为继续进行游戏 }
6.函数声明,宏定义,函数的定义实现,测试。
#include<stdio.h> #include<string.h> #include<time.h> #include<stdlib.h> #include"game.h" # define _CRT_SECURE_NO_WARNINGS #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]); void Computer(char board[ROW][COL]); int Iswin(char board[ROW][COL], int row, int col); int Full(char board[ROW][COL], int row, int col); void menu() { printf("**********************************\n"); printf("**********************************\n"); printf("***** 0.play *************\n"); printf("***** 1.exit *************\n"); printf("**********************************\n"); } void game() { char board[ROW][COL] = { 0 }; int ret; Initboard(board, ROW, COL); //用Initboard函数初始化数组,并用‘ '初始化数组 printf("游戏开始\n"); while (1) { Playermove(board); //玩家输入坐标 Displayboard(board, ROW, COL); ret = Iswin(board, ROW, COL); //判断是否获胜 if (ret == 1) { printf("玩家获胜"); break; } else if (ret == 0) { printf("电脑获胜"); break; } else if (ret == 2) { printf("平局"); break; } printf("\n\n"); Computer(board); //电脑输入坐标 Displayboard(board, ROW, COL); ret = Iswin(board, ROW, COL); //判断是否获胜 if (ret == 1) { printf("玩家获胜"); break; } else if (ret == 0) { printf("电脑获胜"); break; } else if (ret == 2) { printf("平局"); break; } printf("\n\n"); } } int main() { int input = 0; srand((unsigned int)time(NULL)); menu(); do { printf("请输入要选的的选项: "); scanf("%d",&input); switch (input) { case 0: game(); break; case 1: printf("退出游戏"); break; default: printf("请输入要选的选项:\n"); break; } } while (input); } 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; int j; for (i = 0; i < row; i++) { for (j = 0; j < col ; j++) { printf(" %c ", board[i][j]); if (j == col - 1) continue; printf("|"); } printf("\n"); if (i < row - 1) printf("-----------\n"); } } void Playermove(char board[ROW][COL]) //玩家输入坐标 { while (1) { int x = 0; int y = 0; printf("请玩家输入坐标"); scanf_s("%d %d", &x, &y); if (x <= ROW && x >= 1 && y <= COL && y >= 1) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else printf("坐标被占用,请重新输入\n"); } else printf("坐标有误,请重新输入\n"); } } void Computer(char board[ROW][COL]) //电脑输入坐标 { int x = 0; int y = 0; while (1) { x = rand() % 3; y = rand() % 3; if (board[x][y] == ' ') { printf("电脑输入坐标%d %d\n", x+1, y+1); board[x][y] = '#'; break; //打印后跳出循环 } } } int Iswin(char board[ROW][COL], int row, int col) //判断谁获胜 { int i; int j; //1为玩家获胜 0为电脑获胜 2为平局 for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '*') //判断同一行是否相等 return 1; else if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] == '#') return 0; } for (j = 0; j < col; j++) { if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '*') //判断同一列是否相等 return 1; else if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] == '#') return 0; } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '*') //判断左对角线是否相等 return 1; else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] == '#') return 0; if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '*') //判断右对角线是否相等 return 1; else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] == '#') return 0; if (Full(board, ROW, COL)) //判断是否平局 return 2; return 3; //返回3为继续进行游戏 } int Full(char board[ROW][COL], int row, int col) //判断棋盘是否下满 { int i; int j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] == ' ') return 0; //此时棋盘未满 } } return 1; //此时棋盘已满 }
更多有趣的经典小游戏实现专题,分享给大家: