commit 212ef50ecd6e33c7a95e5a431079415ab6b6759c Author: 林青艳 <18373302181@139.com> Date: Tue Jan 8 20:09:36 2019 +0800 first commit diff --git a/yuhj.cpp b/yuhj.cpp new file mode 100644 index 0000000..4a48df7 --- /dev/null +++ b/yuhj.cpp @@ -0,0 +1,286 @@ +//da mu mu de kechengsheji---jngziqi +//game.h Ӧļ +#ifndef __GAME_H__ +#define __GAME_H__ +#define ROW 3 +#define CLO 3 +#include +#include +#include +#include +void menu(); //ӡ˵ +void game(char arr[ROW][CLO], int row, int clo); //ִв˵ +void play(char arr[ROW][CLO], int row, int clo); //Ϸ +void InitBoard(char arr[ROW][CLO], int row, int clo); //ӡ +void Pmove(char arr[ROW][CLO], int row, int clo); // +void Cmove(char arr[ROW][CLO], int row, int clo); // +int Isfull(char arr[ROW][CLO], int row, int clo); // +int Iswin(char arr[ROW][CLO], int row, int clo); //жӮ +#endif +// +//test.c ΪϷ߼ +#include"stdio.h" +int main() +{ + srand((unsigned int)time(NULL)); // + char arr[ROW][CLO]; + game(arr,ROW,CLO); + system("pause"); + return 0; +} +// +void menu() +{ + printf("******************************\n"); + printf("******1.playʼϷ******\n"); + printf("******2.exit˳Ϸ******\n"); + printf("******************************\n"); + printf("***(O:ңX:)***\n"); +} + +//ִв˵ + +void game(char arr[ROW][CLO], int row, int clo) +{ + int input = 0; + do + { + menu(); + scanf("%d",&input); + memset(arr, ' ', ROW*CLO * sizeof(arr[0][0])); + switch (input) + { + case 1: + play(arr, row, clo); break; + case 2: + exit(0); break; + default: + printf("СɰDzǰ?\n"); + } + } while (input); +} + +//Ϸ + +void play(char arr[ROW][CLO], int row, int clo) +{ + int flag = 0; + InitBoard(arr, row, clo); + while (1) + { + printf("\n"); + Pmove(arr, row, clo); + InitBoard(arr, row, clo); + flag = Iswin(arr, row, clo); + if (flag == 1) + { + printf("װңϲӮǸС\n"); + break; + } + flag = Isfull(arr, row, clo); + if (flag == 4) + { + printf("ƽƽ֣ͦӴϷ\n"); + break; + } + printf("\n"); + Cmove(arr, row, clo); + InitBoard(arr, row, clo); + flag = Iswin(arr, row, clo); + if (flag == 1) + { + printf("ȻǵӮˣ~еӴ\n"); + break; + } + } +} + +//̴ӡ + +void InitBoard(char arr[ROW][CLO], int row, int clo) +{ + int i = 0; + int j = 0; + for (j = 0; j < row; j++) + { + for (i = 0; i < clo; i++) + { + printf(" %c ", arr[j][i]); + if (i < (clo - 1)) + { + printf("|"); + } + } + printf("\n"); + if (j < row - 1) + { + for (i = 0; i < clo; i++) + { + printf("---"); + if (i < (clo - 1)) + { + printf("|"); + } + } + } + printf("\n"); + } +} + +// + +void Pmove(char arr[ROW][CLO], int row, int clo) +{ + printf("(ո\n"); + int m = 0, n = 0; + while (1) + { + scanf("%d %d", &m, &n); + if (((m >= 1) && (m <= 3)) && ((n >= 1) && (n <= 3))) + { + if (arr[m - 1][n - 1] == ' ') + { + arr[m - 1][n - 1] = 'O'; + break; + } + else + { + printf("٣СӣѾŶ\n"); + } + } + else + { + printf("룿Ŷ\n"); + } + } +} + +//жӮ + +int Iswin(char arr[ROW][CLO], int row, int clo) +{ + // + int i = 0; + for (i = 0; i < row; i++) + { + if (((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2])) && ((arr[i][0] != ' '))) + { + return 1; + } + } + // + for (i = 0; i < clo; i++) + { + if ((arr[0][i] == arr[1][i]) && (arr[1][i] == arr[2][i]) && ((arr[0][i] != ' ') )) + { + return 1; + } + } + //Ȧ + if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && ((arr[0][0] != ' ') )) + { + return 1; + } + if ((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0]) && ((arr[0][2] != ' '))) + { + return 1; + } + return 0; +} + +//壨ģ + +void Cmove(char arr[ROW][CLO], int row, int clo) +{ + int flag = 0; + int m = 0, n = 0; + for (m = 0; m < row; m++) + { + for (n = 0; n < row; n++) + { + if (arr[m][n] == ' ') + { + arr[m][n] = 'X'; + flag = Iswin(arr, row, clo); + if (flag == 0) + { + arr[m][n] = ' '; + } + if (flag == 1) + { + break; + } + } + } + if (flag == 1) + { + break; + } + } + if (flag == 0) + { + for (m = 0; m < row; m++) + { + for (n = 0; n < row; n++) + { + if (arr[m][n] == ' ') + { + arr[m][n] = 'O'; + flag = Iswin(arr, row, clo); + if (flag == 0) + { + arr[m][n] = ' '; + } + if (flag == 1) + { + arr[m][n] = 'X'; + break; + } + } + } + if (flag == 1) + { + break; + } + } + + if ((m == 3) && (n == 3)) + { + while (1) + { + m = rand() % 3; + n = rand() % 3; + if (arr[m][n] == ' ') + { + arr[m][n] = 'X'; + break; + } + } + } + } +} + +//ж + +int Isfull(char arr[ROW][CLO], int row, int clo) +{ + int i = 0, j = 0; + int count = 0; + for (i = 0; i < row; i++) + { + for (j = 0; j < clo; j++) + { + if (arr[i][j] != ' ') + { + count ++; + } + } + } + if (count == 9) + { + return 4; + } + return 3; +} + + diff --git a/yuhj.exe b/yuhj.exe new file mode 100644 index 0000000..a3a72f8 Binary files /dev/null and b/yuhj.exe differ