主要思路
这里一共用三个文件,一个头文件,两个源文件
1.标头.h是存放声明函数的地方
#pragma once #include <stdio.h> #include <stdlib.h> #include <time.h> #define COL 3 #define ROW 3 //初始化 void InitBoard(char arr[ROW][COL], int row, int col); //界面 void DisplayBoard(char arr[ROW][COL], int row, int col); //我方 void playmove(char arr[ROW][COL], int row, int col); //电脑下棋 void playmovediannao(char arr[ROW][COL], int row, int col); //判断谁赢 char IsWin(char arr[ROW][COL], int row, int col);
2.game.c就是实现函数功能的地方
#define _CRT_SECURE_NO_WARNINGS 1 #include "标头.h" void InitBoard(char arr[ROW][COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { arr[i][j] = ' '; } } } //void DisplayBoard(char arr[ROW][COL], int row, int col) { // for (int i = 0; i < row; i++) { // printf(" %c | %c | %c \n", arr[i][0], arr[i][1], arr[i][2]); // if (i < row - 1) { // printf("---|---|---\n"); // } // } //} //画棋盘 void DisplayBoard(char arr[ROW][COL], int row, int col) { //打印数据 for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf(" %c ", arr[i][j]); if (j < col - 1) { printf("|"); } } printf("\n"); //打印下划线 for (int j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("\n"); } } void playmove(char arr[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("玩家下棋>:\n"); printf("请输入下棋的坐标>:"); while (1) { int n = scanf("%d", &x); int q = scanf("%d", &y); if ((x <= row && x >= 1) && (y <= col && y >= 1)) { if (arr[x - 1][y - 1] == ' ') { arr[x - 1][y - 1] = '*'; break; } else { printf("坐标已被占用\n"); } } else { printf("坐标不合法\n"); } } } void playmovediannao(char arr[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("电脑下棋>:\n"); while (1) { x = rand() % row; y = rand() % col; if (arr[x][y] == ' ') { arr[x][y] = '#'; break; } } } int isfull(char arr[ROW][COL], int row, int col) { int flag = 1; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (arr[i][j] == ' ') { flag = 0; } } } return flag; } char IsWin(char arr[ROW][COL], int row, int col) { //判断行相等 for (int i = 0; i < row; i++) { if (arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2] && arr[i][2]!= ' ') { return arr[i][0]; } } //判断列相等 for (int j = 0; j < col; j++) { if (arr[0][j] == arr[1][j] && arr[1][j] == arr[2][j] && arr[2][j] != ' ') { return arr[0][j]; } } //判断对角线相等 if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2] && arr[2][2] != ' ') { return arr[0][0]; } if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0] && arr[2][0] != ' ') { return arr[0][2]; } //判断是否平局 if (isfull(arr,row,col)) { return 'Q'; } //判断是否继续 return 'C'; }
3.text.c是主函数(也就是将各功能函数拼接在一起的地方)
#include "标头.h" menu() { printf("*************************\n"); printf("********1.开始游戏*******\n"); printf("********0.停止游戏*******\n"); printf("*************************\n"); printf("*************************\n"); } game() { char arr[3][3] = { 0 }; //初始化数组 InitBoard(arr, ROW, COL); //画棋盘 DisplayBoard(arr, ROW, COL); //玩家下棋 char ret = 0; while (1) { playmove(arr, ROW, COL); DisplayBoard(arr, ROW, COL); ret = IsWin(arr, ROW, COL); if (ret != 'C') break; //电脑下棋 playmovediannao(arr, ROW, COL); DisplayBoard(arr, ROW, COL); ret = IsWin(arr, ROW, COL); if (ret != 'C') break; } if (ret == '*') printf("玩家赢\n"); else if (ret == '#') printf("电脑赢\n"); else printf("平局\n"); } int main() { menu(); int temp = 0; srand((unsigned int)time(NULL)); do { printf("请选择\n"); int o = scanf("%d", &temp); switch (temp) { case 1: game(); break; case 0: printf("游戏结束"); break; } } while (temp); }