commit c3c9dfcb026e45828d51fd80a7c327d46041eb48 Author: 2258496871@qq.com <2258496871@qq.com> Date: Fri Jan 11 13:37:02 2019 +0800 first commit diff --git a/game.c b/game.c new file mode 100644 index 0000000..bb43771 --- /dev/null +++ b/game.c @@ -0,0 +1,94 @@ +#define _CRT_SECURE_NO_WARNINGS 1 +#include "game.h" +//打印棋盘 +void printboard(char board[ROW][COL],int row,int col) +{ +int i = 0; +for (i = 0;i < row; i++) +{ +printf (" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]); +if (i= 1 && x <= row && y >= 1 && y <= col && board[x-1][y-1] == ' ') +{ +board[x-1][y-1] = 'X'; +break; +} +else +printf("坐标不合法,请重新输入\n"); +} +} +//电脑走 +void computermove(char board[ROW][COL],int row,int col) +{ +int x = 0; +int y = 0; +printf ("电脑走:\n"); +while(1) +{ +x = rand()%3; +y = rand()%3; +if (board[x][y] == ' ') +{ +board[x][y] = '0'; +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 checkwin(char board[ROW][COL],int row,int col) +{ +int i = 0; +//1.玩家赢 返回‘X’2.电脑赢 返回 ‘0’ +for (i = 0; i < row; i++) //三行 +{ +if (board[i][0] == board[i][1] && board[i][1] == board[i][2]) +return board[i][0]; +} +for (i = 0; i < col; i++) //三列 +{ +if (board[0][i] == board[1][i] && board[1][i] == board[2][i]) +return board[0][i]; +} +if (board[0][0]== board[1][1] && board[1][1] == board[2][2]) //对角线 +return board[0][0]; +if (board[0][2]== board[1][1] && board[1][1] == board[2][0]) +return board[0][0]; +//3.平局 返回 ‘Q’//4.继续玩 返回 ' ' +if (isfull(board,ROW,COL)) +return 'Q'; +else +return ' '; +} diff --git a/game.h b/game.h new file mode 100644 index 0000000..4a35ebe --- /dev/null +++ b/game.h @@ -0,0 +1,13 @@ +#include +#include +#include +#include +#define ROW 3 +#define COL 3 +void printboard(char board[ROW][COL],int row,int col); //打印棋盘 +void initboard(char board[ROW][COL],int row,int col); //初始化棋盘 +void playermove(char board[ROW][COL],int row,int col); //玩家下棋 +void computermove(char board[ROW][COL],int row,int col); //电脑下棋 +char checkwin(char board[ROW][COL],int row,int col); //判输赢 +int isfull(char board[ROW][COL],int row,int col); //判断棋盘是否为满 + diff --git a/test.c b/test.c new file mode 100644 index 0000000..15fcba5 --- /dev/null +++ b/test.c @@ -0,0 +1,118 @@ +#define _CRT_SECURE_NO_WARNINGS 1 +#include "game.h" +void game() +{ +char ret = 0; +char board[ROW][COL]={'0'}; +initboard(board,ROW,COL); +printboard(board,ROW,COL); +while(1) +{ +playermove(board,ROW,COL); +printboard(board,ROW,COL); +ret = checkwin(board,ROW,COL); +if(ret == 'X') +{ +printf("恭喜你赢了!\n"); +break; +} +else if (ret == 'Q') +{ +printf ("平局\n"); +break; +} +computermove(board,ROW,COL); +printboard(board,ROW,COL); +ret = checkwin(board,ROW,COL); +if (ret == '0') +{ +printf ("电脑赢!\n"); +break; +} +} +} +void game2() +{ +char ret = 0; +char board[ROW][COL]={'0'}; +initboard(board,ROW,COL); +printboard(board,ROW,COL); +while(1) +{ +computermove(board,ROW,COL); +printboard(board,ROW,COL); +ret = checkwin(board,ROW,COL); +if (ret == '0') +{ +printf ("电脑赢!\n"); +break; +} +else if (ret == 'Q') +{ +printf ("平局\n"); +break; +} +playermove(board,ROW,COL); +printboard(board,ROW,COL); +ret = checkwin(board,ROW,COL); +if(ret == 'X') +{ +printf("恭喜你赢了!\n"); +break; +} +} +} +void menu2() +{ +printf (" *****************************************\n"); +printf (" ** 1.玩家先走 2.电脑先走 **\n"); +printf (" *****************************************\n"); +} +void menu() +{ +printf ("--------------------欢迎来到三子棋游戏--------------------\n"); +printf (" *****************************************\n"); +printf (" ** 1.play 0.exit **\n"); +printf (" *****************************************\n"); +} +void main2() +{ +int input = 0; +printf ("请输入你的选择:"); +scanf ("%d",&input); +switch (input) +{ +case 1: +game(); +break; +case 2: +game2(); +break; +default: +printf ("输入错误,请重新选择!\n"); +} +} +int main () +{ +int input = 0; +srand ((unsigned int)(time (NULL))); +do +{ +menu(); +printf ("请输入你的选择:"); +scanf ("%d",&input); +switch (input) +{ +case 1: +menu2(); +main2(); +break; +case 0: +break; +default: +printf ("输入错误,请重新选择!\n"); +} +}while(input); +system("pause\n"); +return 0; +}