C语言简易三子棋

   这是本人依据现学知识写的简易三子棋,也不是那么简洁明了,望大佬指点

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #define ROW 3
  5 #define COL 3
  6 char chess_board[ROW][COL];
  7 int PlayGame(int choice){
  8     printf("************************\n");
  9     printf("*  欢迎来到三子棋游戏  *\n");
 10     printf("*       请您选择       *\n");
 11     printf("*      1.开始游戏      *\n");
 12     printf("*      2.离开游戏      *\n");
 13     printf("************************\n");
 14     while (1){
 15         scanf("%d", &choice);
 16         if (choice == 1)
 17         {
 18             return 1;
 19             break;
 20         }
 21         else if (choice == 2)
 22         {
 23             return 0;
 24             break;
 25         }
 26         else
 27         {
 28             printf("输入非法,请重新输入!");
 29             continue;
 30         }
 31     }
 32 }
 33 int Init(char a[ROW][COL]){
 34     for (int row = 0; row <ROW; row++){
 35         for (int col = 0; col < COL; col++)
 36         {
 37             a[row][col] = '  ';
 38         }
 39     }
 40     return 0;
 41 }
 42 void Print(char a[ROW][COL]){
 43     printf("           \n");
 44     for (int i = 0; i < ROW; i++){
 45         printf("  %c | %c  | %c \n", a[i][0], a[i][1], a[i][2]);
 46         if (i != 3){
 47             printf("----|----|----\n");
 48         }
 49     }
 50 }
 51 void PlayerMove(char a[ROW][COL]){
 52     int row = -1;
 53     int col = -1;
 54     while (1){
 55         printf("请您选择落子点坐标:");
 56         scanf("%d %d", &row, &col);
 57         if (row<0 || row>2 || col<0 || col>2){
 58             printf("输入非法,请重新输入!");
 59             continue;
 60         }
 61         else if (a[row][col] == 'x' || a[row][col] == 'o')
 62         {
 63             printf("已有棋子,请重新选择坐标!");
 64             continue;
 65         }
 66         else
 67         {
 68             a[row][col] = 'o';
 69             break;
 70         }
 71 
 72     }
 73 }
 74 int IsFull(char a[ROW][COL]){
 75     int sum = 0;
 76     for (int row = 0; row < ROW; row++)
 77     {
 78         for (int col = 0; col < COL; col++)
 79         {
 80             if (a[row][col] != ' ')
 81             {
 82 
 83                 sum++;
 84             }
 85         }
 86     }
 87     if (sum == 9){
 88         return 1;
 89     }
 90 }
 91 char Checkwinner(char a[ROW][COL]){
 92     //1.判断三子 横 竖 斜
 93     for (int row = 0; row < ROW; row++){
 94         if (a[row][0] == a[row][1] && a[row][0] == a[row][2] && a[row][0] != ' ')
 95         {
 96             return 'p';
 97         }
 98     }
 99     for (int col = 0; col < COL; col++){
100         if (a[0][col] == a[1][col] && a[0][col] == a[2][col] && a[0][col] != ' ')
101         {
102             return 'p';
103         }
104     }
105     if (a[0][0] == a[1][1] && a[0][0] == a[2][2] && a[0][0] != ' ')
106     {
107         return 'p';
108     }
109     if (a[0][2] == a[1][1] && a[0][2] == a[2][0] && a[0][2] != ' '){
110         return 'p';
111     }
112 }
113 
114 
115 
116 
117 void ComputeMove(char a[ROW][COL]){
118     printf("请电脑落子!\n");
119     while (1){
120         int col = rand() % 3;
121         int row = rand() % 3;
122         if (a[row][col] == ' ')
123         {
124             a[row][col] = 'x';
125             break;
126         }
127         else continue;
128     }
129 }
130 void Game(){
131     Init(chess_board);//2.棋盘初始化
132     Print(chess_board);//3.打印棋盘
133     while (1){
134 
135         PlayerMove(chess_board);//4.用户落子
136         Print(chess_board);
137         if (Checkwinner(chess_board) == 'p')
138         {
139             printf("玩家获胜!\n");
140             break;
141         }
142         if (IsFull(chess_board) == 1)//2.判断是否和棋
143         {
144             printf("平局!");
145             break;
146         }
147         ComputeMove(chess_board);//6.电脑随机落子
148         Print(chess_board);//3.打印棋盘
149         if (Checkwinner(chess_board) == 'p')
150         {
151             printf("电脑获胜!\n");
152             break;
153         }
154 
155         if (IsFull(chess_board) == 1)//2.判断是否和棋
156         {
157             printf("平局!\n");
158             break;
159         }
160     }
161 }
162 
163 
164 int main(){
165     int choice = 0;
166     if (PlayGame(choice) == 1) {   //1.选择是否开始游戏  a)开始  b)离开
167          Game();
168 
169     }
170     system("pause");
171 }

 

posted @ 2018-10-18 17:39  Duikerdd  阅读(570)  评论(0编辑  收藏  举报