三子棋 ,也叫井字棋,是一款大家都玩过的经典小游戏,也是一个适合初学c语言的小伙伴练练手的小项目。
1.整个棋盘采用二维数组存储,下棋时只要获取到x,y两个方向的坐标访问二维数组中的元素改变存储的字符串样式。
2.判断输赢或平局,直接穷举就行了,判断每行、每列、和对角线是否有三个棋子相连。可设置一个带返回值的检查函数,根据不一样的返回值进行不同操作。
3.其余的页面菜单,和棋盘打印。只需要合理利用循环和判断来打印相关内容,很容易就能实现了。
效果展示
附上全部源码,大家可以在自己的编译器上跑一下试一试。
#define _CRT_SECURE_NO_WARNINGS #define ROW 3 #define COL 3 #include<stdio.h> #include<time.h> #include<stdlib.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 PrintBoard(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 (j == col - 1) { printf(" %c ", board[i][j]); } else printf(" %c |", board[i][j]); } printf("\n"); //打印分割行 if (i < row - 1) { for (j = 0; j < col; j++) { if (j == col - 1) { printf("---"); } else printf("---|"); } printf("\n"); } } } void PlayerMove(char board[ROW][COL], int row, int col) { int x = 0, y = 0; while (1) { printf("输入要走的坐标:"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= row) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; return; } else { printf("该位置已有棋子,请重新选择\n"); } } else { printf("输入错误!\n"); } } } void ComputerMove(char board[ROW][COL], int row, int col) { int x = 0, y = 0; while (1) { x = rand() % row; y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; return; } } } //1 玩家赢 //2 电脑赢 //3 继续 //0 平局 int CheckBoard(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] != ' ') { if (board[i][0] == '*') { return 1; } else { return 2; } } if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') { if (board[0][i] == '*') { return 1; } else { return 2; } } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') { if (board[0][0] == '*') { return 1; } else { return 2; } } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') { if (board[0][2] == '*') { return 1; } else { return 2; } } for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { if (board[i][j] == ' ') { return 3; } } } return 0; } void game() { //三子棋的过程 char board[ROW][COL]; //初始化棋盘 InitBoard(board, ROW, COL); //打印棋盘 PrintBoard(board, ROW, COL); //下棋过程 int ret = 0; while (1) { //玩家走 PlayerMove(board, ROW, COL); PrintBoard(board, ROW, COL); ret = CheckBoard(board, ROW, COL); if (ret != 3) { break; } //电脑走 ComputerMove(board, ROW, COL); PrintBoard(board, ROW, COL); system("cls"); ret = CheckBoard(board, ROW, COL); if (ret != 3) { break; } } if (ret == 1) { printf("玩家赢\n"); system("pause"); system("cls"); } else if (ret == 2) { printf("电脑赢\n"); system("pause"); system("cls"); } else { printf("平局!\n"); system("pause"); system("cls"); } } void menu() { printf("**************************************\n"); printf("****** 1.begin 0.exit ******\n"); printf("**************************************\n"); } int main() { srand((unsigned)time(NULL)); while (1) { menu(); int select = 0; printf("请输入你的选择:"); scanf("%d", &select); switch (select) { case 1: game(); break; case 0: printf("退出当前游戏!\n"); system("pause"); system("cls"); return 0; default: printf("输入错误!请重新输入\n"); } } return 0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xd6905/article/details/120010288