test.c #define _CRT_SECURE_NO_WARNINGS 1 #include"game.h"//自己定义的用"game.h" 头文件的包含 void game() { //存储数据-二维数组 char board[ROW][COL];//创建*和# ROW是行 COL是列 //初始化棋盘,(用空格) InitBoard(board,ROW,COL);//如果没有初始化,则棋盘里面为随机??? //打印棋盘--本质打印数组内容 DisplayBoard(board,ROW,COL); char ret = 0;//结束游戏状态 while (1) { //电脑先走 ComputerMove(board, ROW, COL); DisplayBoard(board, ROW, COL); //判断是否赢得游戏(1玩家赢 2电脑赢 3平局 4游戏继续) ret=Iswin(board, ROW, COL);//判断状态 if (ret != 'C') break; //玩家后走 PlayerMove(board, ROW, COL);//传递ROW和COL目的是判断玩家选择的是否有效(在0.0-3.3之间) DisplayBoard(board, ROW, COL); //判断是否赢得游戏(1玩家赢* 2电脑赢# 3平局Q 4游戏继续C)--char型 ret = Iswin(board, ROW, COL);//判断状态 if (ret != 'C') break; } if (ret == '*') { printf("玩家赢\n"); } else if (ret == '#') { printf("电脑赢\n"); } else { printf("平局\n"); } DisplayBoard(board, ROW, COL); } void menu() { printf("************************************\n"); printf("*********** 1. play ************\n"); printf("*********** 0. exit ************\n"); printf("************************************\n"); } int main()//只执行一次 { int input = 0; srand((unsigned int) time(NULL)); do { menu(); printf("请选择\n"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新输入\n"); break; } } while (input); return 0; }
game.c #define _CRT_SECURE_NO_WARNINGS 1 #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++)//有几列col,就打印几个j { printf(" %c ", board[i][j]); if(j<col-1) printf("|"); } printf("\n"); if (i < row - 1)//为了最后一行没有------ { int j = 0; for (j = 0; j < col; j++) { printf("---"); if(j<col-1) printf("|"); } printf("\n"); } } } void PlayerMove(char board[][COL], int row, int col) { int x = 0; int y = 0; printf("玩家走\n"); while (1)//需要循环下棋 { printf("请输入下棋坐标\n");//玩家认为的1.1是0.0 scanf("%d %d", &x, &y); //判断坐标合法性 if (x >= 1 && x <= row && y >= 1 && y <= col) { //下棋,判断坐标是否被占用 if (board[x-1][y-1]==' ')//玩家的1.1需要减1 { board[x - 1][y - 1] ='*'; break; } else { printf("坐标被占用,请重新输入\n"); } } else { printf("坐标非法,请重新输入\n"); } } } void ComputerMove(char board[ROW][COL], int row, int col) { printf("电脑走\n"); while (1) { int x = rand() % row;//合法 int y = rand() % col; //判断占用 if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } 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;//棋盘满了 } 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][1] != ' ') { return board[i][1];// } } //判断三列 for (i = 0; i < col; i++) { if (board[0][i] ==board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ') { return board[1][i];// } } //判断对角线 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') { return board[1][1]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') { return board[1][1]; } //判断平局, //如果棋盘满了,返回1,不满,返回0 int ret=IsFull(board,row,col); if (ret == 1) { return 'Q'; } //继续 return 'C'; } game.h #pragma once #include<stdio.h> #include<time.h> #include<stdlib.h> //符号定义 #define ROW 3 #define COL 3//#define可以定义符号 //函数声明 //初始化棋盘 void InitBoard(char board[ROW][COL],int row, int col); //打印棋盘函数 void DisplayBoard(char board[ROW][COL],int row,int col); //玩家下棋 void PlayerMove(char board[][COL],int row,int col); //电脑下棋 void ComputerMove(char board[ROW][COL], int row, int col); //判断1游戏是否有输赢(1玩家赢 * 2电脑赢# 3平局Q 4游戏继续C) char Iswin(char board[ROW][COL], int row, int col);
//想变为玩家先,请私聊我