三子棋小程序 easy
1 //game.h 2 3 4 5 #ifndef __GAME_H__ 6 #define __GAME_H__ 7 8 #include<stdio.h> 9 #include<time.h> 10 #include<stdio.h> 11 #include<stdlib.h> 12 13 #define ROW 3 14 #define COL 3 15 16 void InitBoard(char board[ROW][COL],int row,int col); 17 void DisplayBoard(char board[ROW][COL],int row,int col); 18 void PlayerMove(char board[ROW][COL],int row,int col); 19 void ComputerMove(char board[ROW][COL],int row,int col); 20 int IsFull(char board[ROW][COL],int row,int col); 21 char IsWin(char board[ROW][COL],int row,int col); 22 23 24 // X 电脑赢 25 // * 玩家赢 26 //Q 平局 棋盘无空 27 // " " 棋盘尚有空 游戏继续 28 #endif 29 30 31 32 //game.c 33 34 #include "game.h" 35 36 //初始化棋盘 37 void InitBoard(char board[ROW][COL], int row, int col){ 38 int i = 0; 39 int j = 0; 40 for (i = 0; i < row;i++){ 41 for (j = 0; j < col; j++){ 42 board[i][j] = ' '; 43 } 44 } 45 } 46 47 //打印棋盘 48 void DisplayBoard(char board[ROW][COL], int row, int col){ 49 int i = 0; 50 for (i = 0; i < row; i++){ 51 printf(" %c | %c | %c ", board[i][0], board[i][1], board[i][2]); 52 printf("\n"); 53 if (i < row - 1){ 54 printf("___|___|___"); 55 printf("\n"); 56 } 57 if (i==2){ 58 printf(" | | "); 59 } 60 } 61 printf("\n"); 62 } 63 64 65 //电脑走 66 void ComputerMove(char board[ROW][COL], int row, int col){ 67 int x = 0; 68 int y = 0; 69 while (1){ 70 x = rand() % ROW; 71 y = rand() % COL; 72 if (board[x][y] == ' '){ 73 board[x][y] = 'X'; 74 break; 75 } 76 } 77 } 78 79 80 //玩家走 81 void PlayerMove(char board[ROW][COL], int row, int col){ 82 int x = 0; 83 int y = 0; 84 while (1){ 85 printf("请输入坐标x y\n"); 86 scanf("%d %d", &x, &y); 87 if ((x >= 1) && (x <= row) && (y >= 1) && (y <= col)){ 88 if (board[x-1][y-1] == ' '){ 89 board[x-1][y-1] = '*'; 90 break; 91 } 92 } 93 else 94 printf("坐标有误,请重新输入\n"); 95 } 96 } 97 98 //判断棋盘是否已满 99 int IsFull(char board[ROW][COL], int row, int col){ 100 int i = 0; 101 int j = 0; 102 int r = 0; 103 for (i = 0; i < row; i++){ 104 for (j = 0; j < col; j++){ 105 if (board[i][j] ==' '){ 106 return 0; 107 } 108 } 109 } 110 return 1; 111 } 112 113 114 //判断输赢 115 char IsWin(char board[ROW][COL], int row, int col){ 116 int i = 0; 117 int j = 0; 118 for (i = 0; i < row; i++){ 119 if ((board[i][0] == board[i][1] )&&( board[i][1] == board[i][2])) //横排一样 120 return board[i][0]; 121 } 122 for (j = 0; j < col; j++){ 123 if ((board[0][j] == board[1][j] )&& (board[1][j] == board[2][j])) //竖排一样 124 return board[0][j]; 125 } 126 if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])){ //对角线一样 127 return board[1][1]; 128 } 129 else if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])){ //对角线一样 130 return board[1][1]; 131 } 132 else if (IsFull(board,ROW,COL)==1){ //平局 133 return 'q'; 134 } 135 else//棋盘未满游戏继续 136 return ' '; 137 138 } 139 //test.c 140 #include "game.h" 141 142 void menu(){ 143 printf("ーーーーーーーーーーーーーーーーー\n"); 144 printf("ー 三子棋小游戏 ー\n"); 145 printf("ー 1. play ー\n"); 146 printf("ー 2. exit ー\n"); 147 printf("ーーーーーーーーーーーーーーーーー\n"); 148 } 149 150 void game(){ 151 char board[ROW][COL] = { 0 }; 152 InitBoard(board,ROW,COL); 153 DisplayBoard(board, ROW, COL); 154 while(1){ 155 printf("电脑走->\n"); 156 ComputerMove(board, ROW, COL); //电脑每走一步判断一次是否要继续游戏 157 if (IsWin(board, ROW, COL) != ' '){ 158 break; 159 } 160 DisplayBoard(board, ROW, COL); 161 162 printf("玩家走->\n"); 163 PlayerMove(board, ROW, COL);//玩家每走一步判断一次是否要继续游戏 164 if (IsWin(board, ROW, COL) != ' '){ 165 break; 166 } 167 DisplayBoard(board, ROW, COL); 168 169 170 } 171 if (IsWin(board, ROW, COL) == 'X') 172 printf("电脑赢\n"); 173 else if (IsWin(board, ROW, COL) == '*') 174 printf("玩家赢\n"); 175 else 176 printf("平局\n"); 177 } 178 179 180 181 182 183 int main(){ 184 int input = 0; 185 srand((unsigned int)time(NULL)); 186 do{ 187 menu(); 188 printf("请选择->\n"); 189 scanf("%d", &input); 190 switch (input){ 191 case 1: 192 game(); 193 break; 194 case 0: 195 printf("退出游戏\n"); 196 break; 197 default: 198 printf("请输入1或0\n"); 199 } 200 } while (input); 201 system("pause"); 202 return 0; 203 204 }