test.c——主文件
game.c——游戏文件
game.h——游戏头文件
----------------------
test.c:
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void menu() { printf("***************************************\n"); printf("********* 0.退出三子棋游戏 ***********\n"); printf("********* 1.开始游戏:玩家先手 ********\n"); printf("********* 2.开始游戏:电脑先手 ********\n"); printf("***************************************\n"); } int game(int flag) { //存储数据 - 二维数组 char board[ROW][COL]; //初始化棋盘 - 空格 InitBoard(board,ROW,COL); //打印棋盘 DisplayBoard(board, ROW, COL); //玩家先手或电脑先手 if (flag == 1) playerfirst(board, ROW, COL); else computerfirst(board, ROW, COL); } int main() { int input = 0; int flag = 1;//判断是否继续游戏 srand((unsigned int)time(NULL)); do { menu(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 0: printf("退出游戏\n"); flag = 0; break; case 1: printf("玩三子棋(玩家先手)\n"); game(input); break; case 2: printf("玩三子棋(电脑先手)\n"); game(input); break; default: printf("输入错误,请重新选择\n"); break; } } while (flag == 1); return 0; }
----------------------
game.c
1 #define _CRT_SECURE_NO_WARNINGS 1 2 3 #include "game.h" 4 #include <stdio.h> 5 6 void InitBoard(char board[ROW][COL], int row, int col) 7 { 8 int i = 0; 9 int j = 0; 10 for (i = 0; i < row; i++) 11 { 12 for (j = 0; j < col; j++) 13 { 14 board[i][j] = ' '; 15 } 16 } 17 } 18 19 void DisplayBoard(char board[ROW][COL], int row, int col) 20 { 21 int i = 0; 22 int j = 0; 23 for (i = 0; i < row; i++) 24 { 25 for (j = 0; j < col; j++) 26 { 27 if (j < col - 1) 28 printf(" %c |", board[i][j]); 29 else 30 printf(" %c \n", board[i][j]); 31 32 } 33 if (i < row - 1) 34 { 35 for (j = 0; j < col; j++) 36 { 37 if (j < col - 1) 38 printf("---|"); 39 else 40 printf("---\n"); 41 } 42 43 } 44 45 } 46 } 47 48 void PlayerMove(char board[][COL], int row, int col) 49 { 50 int x = 0; 51 int y = 0; 52 printf("玩家下棋:> *\n"); 53 while (1) 54 { 55 printf("请输入下棋坐标\n"); 56 scanf("%d %d", &x, &y); 57 if (x >= 1 && y >= 1 && x <= row && y <= col) 58 { 59 if (board[x - 1][y - 1] == ' ')//判断坐标是否被占用 60 { 61 board[x - 1][y - 1] = '*'; 62 break; 63 } 64 else 65 { 66 printf("坐标被占用,请重新输入"); 67 } 68 } 69 else 70 { 71 printf("坐标输入错误,请重新输入"); 72 } 73 } 74 } 75 76 void ComputerMove(char board[][COL], int row, int col) 77 { 78 printf("电脑下棋:> #\n"); 79 while (1) 80 { 81 int x = rand() % row; 82 int y = rand() % col; 83 if (board[x][y] == ' ')//判断坐标是否被占用 84 { 85 board[x][y] = '#'; 86 break; 87 } 88 } 89 } 90 91 int IsFull(char board[ROW][COL], int row, int col) 92 { 93 for (int i = 0; i < ROW; i++)//判断平局 94 { 95 for (int j = 0; j < COL; j++) 96 { 97 if (board[i][j] == ' ') 98 return 0;//棋盘未满 99 } 100 } 101 return 1; 102 } 103 104 105 char IsWin(char board[ROW][COL], int row, int col) 106 { 107 int i = 0; 108 int j = 0; 109 for (i = 0; i < ROW; i++)//判断各行连子 110 { 111 for (j = 0; j < COL - 2; j++) 112 if(board[i][j] == board[i][j + 1] && board[i][j + 1] == board[i][j + 2] && board[i][j] != ' ') 113 { 114 return board[i][j]; 115 } 116 } 117 for (j = 0; j < COL; j++)//判断各列连子 118 { 119 for (i = 0; i < ROW - 2; i++) 120 if (board[i][j] == board[i+1][j] && board[i+1][j] == board[i+2][j] && board[i][j] != ' ') 121 { 122 return board[i][j]; 123 } 124 } 125 for (i = 0; i < ROW - 2; i++)//判断对角连子 126 { 127 for (j = 0; j < COL - 2; j++) 128 { 129 if (board[i][j] == board[i + 1][j + 1] && board[i + 1][j + 1] == board[i + 2][j + 2] && board[i][j] != ' ') 130 { 131 return board[i][j]; 132 } 133 } 134 } 135 136 for (i = ROW; i >= 2; i--)//判断副对角连子 137 { 138 for (j = 0; j < COL - 2; j++) 139 { 140 if (board[i][j] == board[i - 1][j + 1] && board[i - 1][j + 1] == board[i - 2][j + 2] && board[i][j] != ' ') 141 { 142 return board[i][j]; 143 } 144 } 145 } 146 147 int ret = IsFull(board, row, col);//判断平局 148 if (ret == 1) 149 { 150 return 'D'; 151 } 152 return 'C'; 153 } 154 155 void playerfirst(char board[ROW][COL], int row, int col) 156 { 157 printf("\n玩家先手\n"); 158 char ret = 0; 159 while (1) 160 { 161 //玩家走 162 PlayerMove(board, ROW, COL); 163 DisplayBoard(board, ROW, COL); 164 //判断玩家是否获胜 165 ret = IsWin(board, ROW, COL); 166 if (ret != 'C') 167 break; 168 //电脑走 169 ComputerMove(board, ROW, COL); 170 DisplayBoard(board, ROW, COL); 171 //判断电脑是否获胜 172 ret = IsWin(board, ROW, COL); 173 if (ret != 'C') 174 break; 175 } 176 if (ret == '*') 177 { 178 printf("\n***** 玩家获胜 *****\n"); 179 } 180 else if (ret == '#') 181 { 182 printf("\n***** 电脑获胜 *****\n"); 183 } 184 else 185 { 186 printf("\n***** 平局 *****\n"); 187 } 188 DisplayBoard(board, ROW, COL); 189 printf("****************\n\n"); 190 } 191 192 void computerfirst(char board[ROW][COL], int row, int col) 193 { 194 printf("\n电脑先手\n"); 195 char ret = 0; 196 while (1) 197 { 198 //电脑走 199 ComputerMove(board, ROW, COL); 200 DisplayBoard(board, ROW, COL); 201 //判断电脑是否获胜 202 ret = IsWin(board, ROW, COL); 203 if (ret != 'C') 204 break; 205 //玩家走 206 PlayerMove(board, ROW, COL); 207 DisplayBoard(board, ROW, COL); 208 //判断玩家是否获胜 209 ret = IsWin(board, ROW, COL); 210 if (ret != 'C') 211 break; 212 } 213 if (ret == '*') 214 { 215 printf("\n***** 玩家获胜 *****\n"); 216 } 217 else if (ret == '#') 218 { 219 printf("\n***** 电脑获胜 *****\n"); 220 } 221 else 222 { 223 printf("\n***** 平局 *****\n"); 224 } 225 DisplayBoard(board, ROW, COL); 226 printf("********************\n\n"); 227 }
---------------------------------------
game.h
1 #pragma once 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 6 #define ROW 3 7 #define COL 3 8 9 //初始化棋盘 10 void InitBoard(char board[ROW][COL], int row, int col); 11 12 //打印棋盘 13 void DisplayBoard(char board[ROW][COL], int row, int col); 14 15 //玩家下棋 16 void PlayerMove(char board[][COL], int row, int col); 17 18 //电脑下棋 19 void ComputerMove(char board[][COL], int row, int col); 20 21 //判断游戏状态 22 char IsWin(char board[][COL], int row, int col); 23 24 //判断棋盘是否满格 25 int IsFull(char board[ROW][COL], int row, int col); 26 27 //玩家先手 28 void playerfirst(char board[ROW][COL], int row, int col); 29 30 //电脑先手 31 void computerfirst(char board[ROW][COL], int row, int col);
标签:2308,int,三子,col,board,printf,COL,ROW From: https://www.cnblogs.com/scut4787749233/p/17660930.html