效果展示
全部代码
test.c文件:
#include"game.h" //三子棋 void menu() { printf("*****************************\n"); printf("****** 1.play ************\n"); printf("****** 0.exit ************\n"); printf("*****************************\n"); printf("input(1/0)>"); } int main(void) { srand((unsigned)time(NULL)); //游戏框架 int input = 0; do { menu();//菜单 scanf("%d", &input); //选择 switch (input) { case 1:game(); break; case 0:printf("退出游戏\n"); break; default:printf("输入错误,请重新输入\n"); break; } } while (input); return 0; }
game.h头文件:
#define _CRT_SECURE_NO_WARNINGS 1 #pragma once #include<stdio.h> #include<stdlib.h> #include<time.h> #define COL 3 #define ROW 3 void game(); void BoardInit(char board[][ROW], int col, int row); void BoardPrint(char board[][ROW], int col, int row); void PlayMove(char board[][ROW], int col, int row); void ComputerMove(char board[][ROW], int col, int row); int IsWin(char board[][ROW], int col, int row); char GameJudge(char board[][ROW], int col, int row);
game.c文件:
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void BoardInit(char board[][ROW], int col, int row) { int i = 0, j = 0; for (i = 0; i < col; i++) { for (j = 0; j < row; j++) { board[i][j] = ' '; } } } void BoardPrint(char board[][ROW], int col, int row) { int i = 0, j = 0; printf(" "); for (i = 1; i <=row; i++) { printf(" %d ",i); } printf("\n"); for (i = 0; i < col; i++) { printf("%d ", i+1); for (j = 0; j < row; j++) { if (j == row - 1) printf(" %c ", board[i][j]); else printf(" %c |", board[i][j]); } printf("\n "); for (j = 0; j < row; j++) if (i != col - 1) { if (j < row - 1) printf("---|"); else printf("---"); } printf("\n"); } } void PlayMove(char board[][ROW], int col, int row) { printf("玩家走 * \n"); while (1) { printf("请输入你要走的位置:(1--%d)>",col); int x, y; scanf("%d%d", &x, &y); if (x<=col&&y<=row&&board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("输入有误,请重新输入\n"); } } } void ComputerMove(char board[][ROW], int col, int row) { printf("电脑走 # \n"); while (1) { int x = rand() % col; int y = rand() % row; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } int IsWin(char board[][ROW], int col, int row) { char ret = GameJudge(board, col, row); switch (ret) { case '*': printf("恭喜你赢了!\n"); break; case '#': printf("很遗憾,你输了,别气馁,再来一局吧\n"); break; case 'p': printf("平局,别气馁,再来一局\n"); break; case 'c':return 1; } return 0; } char GameJudge(char board[][ROW], int col, int row) { int i = 0, j = 0; for (i = 0; i < col - 2; i++) { for (j = 0; j < row-2; j++) { //第一行,第一列相等 if ((board[i][j] == board[i + 1][j] && board[i + 1][j] == board[i + 2][j] && board[i][j] != ' ') ||(board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2] && board[i][j] != ' ')) { return board[i][j]; } //第二行,第二列相等;正对角线,斜对角线相等; else if ((board[i+1][j+1]==board[i][j+1] && board[i + 1][j + 1] == board[i+2][j + 1] && board[i + 1][j + 1] != ' ') ||(board[i + 1][j + 1] == board[i+1][j] && board[i + 1][j + 1] == board[i + 1][j +2 ] && board[i + 1][j + 1] != ' ') ||(board[i+2][j] == board[i + 1][j + 1] && board[i + 1][j + 1] == board[i][j + 2] && board[i+1][j+1] != ' ') ||(board[i][j] == board[i + 1][j + 1] && board[i + 1][j + 1] == board[i + 2][j + 2] && board[i+1][j+1] != ' ')) { return board[i+1][j+1]; } //第三行,第三列相等 else if ((board[i+2][j+2] == board[i + 2][j+1] && board[i + 2][j+2] == board[i + 2][j] && board[i+2][j+2] != ' ') || (board[i+2][j+2] == board[i][j + 2] && board[i+2][j + 2] == board[i+1][j + 2] && board[i+2][j+2] != ' ')) { return board[i+2][j+2]; } else if(board[i][j] == ' '|| board[i][j+1] == ' '|| board[i][j+2] == ' '|| board[i+1][j] == ' ' || board[i+1][j + 1] == ' ' || board[i+2][j + 2] == ' ' || board[i+2][j] == ' ' || board[i+2][j + 1] == ' ' || board[i+2][j + 2] == ' ') { continue; } else { return 'p'; } } } return 'c'; } //游戏实现 void game() { //初始化棋盘 char board[COL][ROW]; BoardInit(board, COL, ROW); //打印棋盘 BoardPrint(board,COL,ROW); while (1) { //玩家操作 PlayMove(board, COL, ROW); //打印棋盘 BoardPrint(board, COL, ROW); //判断游戏是否继续 if (IsWin(board, COL, ROW)==0) { return; } //电脑走 ComputerMove(board, COL, ROW); //打印棋盘 BoardPrint(board, COL, ROW); //判断游戏是否继续 if (IsWin(board, COL, ROW)==0) { return; } } }
游戏效果
你们可以自己尝试着玩一玩,我就不一一展示效果了!