In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to achieve tic-tac-toe chess in C language. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can gain something according to this article.
Tic-tac-toe chess requires that on a 3-by-3 board, each row is the same or every column is the same or the diagonal is the same. So we can use a two-dimensional array to represent the chessboard, and we only need to judge whether the array elements are the same or not. Specifically, we can do it in the following steps:
1. Create a dimension array and initialize it. If it is only a two-dimensional array to represent the chessboard, it doesn't look very clear, so we can print out the chessboard frame with symbols to optimize it:
/ / initialize the chessboard void init (char board [max _ row] [max_col]) {for (int row = 0; row)
< max_row; row++) { for (int col = 0; col < max_col; col++) { board[row][col] =' '; } } srand((unsigned int)time(0));}//打印棋盘void print(char board[max_row][max_col]) { system("cls");//每次打印之前清除掉上一次的结果. for (int a = 0; a < max_col; a++) { printf("+---+---+---+\n"); for (int b = 0; b < max_row; b++) { printf("| %c ", board[a][b]); } printf("| \n"); } printf("+---+---+---+\n");} 2.进行玩家落子,同样是使用数组操作,让玩家输入相对应的坐标,在二维数组的该位置打印'x'来表示玩家下的棋子 void play(char board[max_row][max_col]) { printf("玩家落子阶段!\n"); int a; int b; while (1) { printf("请输入想要落子的坐标: "); scanf("%d %d", &a, &b); //判定玩家落子坐标是否在有效范围内 if (a < 0 || b < 0 || a >= max_row | | b > = max_col) {printf ("enter the coordinates incorrectly, please re-enter!\ n"); continue;} / / determine whether there is already a chess piece if (board [b] [a]! ='') {printf ("it has been dropped here, please re-enter it!\ n"); continue } board [b] [a] ='x'; / / use'x'to indicate the player's break;}}
3. After the player ends, let the computer randomly drop the child in the two-dimensional array, and use the rand function to generate a random number from 0 to 2 for assignment operation, so as to realize the computer random drop.
Void computer (char Board [max _ row] [max_col]) {printf ("computer drop!\ n"); while (1) {int a = rand ()% max_row; / / generate a random number from 0 to 2 int b = rand ()% max_col; if (board [a] [b]! =') {/ / determine whether the location already has a child continue;} board [a] [b] = 'oasis; }
4. To judge the outcome, when the player ends, or the computer ends, judge whether there is a certain row or column or diagonal with the same elements. If so, directly determine the result. If not, it is the next drop.
Char check (char Board [max _ row] [max_col]) {/ / check whether all rows are equal for (int cow = 0; cow)
< max_row; cow++) { if (board[cow][0] != ' ' && board[cow][0] == board[cow][1] && board[cow][0]==board[cow][2] ) { return board[cow][0]; } } //检测所有列是否相等 for (int col = 0; col < max_col; col++) { if (board[0][col] != ' ' && board[0][col] == board[1][col] && board[0][col]==board[2][col] ) { return board[0][col]; } } //检测对角线是否相等 if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) { return board[0][0]; } if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0]) { return board[0][2]; } if (pingju(board)) { return 'q'; //棋子已满和棋 } return 'a'; //棋盘未满}//判断棋盘是否棋子已满,如果满了返回1,未满返回0.int pingju(char board[max_row][max_col]) { for (int row = 0; row < max_row; row++) { for (int col = 0; col < max_col; col++) { if (board[row][col] == ' ') { return 0; } } } return 1;} 5.在主函数中调用以上函数,进行操作: int main() { char board[max_row][max_col] = {0};//数组初始化为0 char winner = 'n'; init(board); //将棋盘中元素初始化为空格 while (1) { print(board); //打印棋盘 play(board); //玩家落子 winner = check(board); //判断胜负 if (winner != 'a') { break; } computer(board); //电脑落子 winner = check(board); //判断胜负 if (winner != 'a') { break; } } if (winner == 'x') { print(board); printf("你赢了!"); } else if (winner == 'o') { print(board); printf("你输了!"); } else if (winner == 'q') { print(board); printf("和棋!"); } return 0; 在主函数中利用while循环来实现玩家与电脑的轮流落子,如果当一方落子之后胜负已出,则跳出循环直接打印最终结果. 完整代码如下: #include #include #include #include #define max_row 3#define max_col 3//初始化棋盘void init(char board[max_row][max_col]) { for (int row = 0; row < max_row; row++) { for (int col = 0; col < max_col; col++) { board[row][col] =' '; } } srand((unsigned int)time(0));}//打印棋盘void print(char board[max_row][max_col]) { system("cls");//每次打印之前清除掉上一次的结果. for (int a = 0; a < max_col; a++) { printf("+---+---+---+\n"); for (int b = 0; b < max_row; b++) { printf("| %c ", board[a][b]); } printf("| \n"); } printf("+---+---+---+\n");} //玩家落子void play(char board[max_row][max_col]) { printf("玩家落子阶段!\n"); int a; int b; while (1) { printf("请输入想要落子的坐标: "); scanf("%d %d", &a, &b); //判定玩家落子坐标是否在有效范围内 if (a < 0 || b < 0 || a >= max_row | | b > = max_col) {printf ("enter the coordinates incorrectly, please re-enter!\ n"); continue;} / / determine whether there is already a chess piece if (board [a] [b]! ='') {printf ("it has been dropped here, please re-enter it!\ n"); continue } board [a] [b] = 'xboy; / / use' x' to denote player break;}} / / void computer (char row [max _ row] [max_col]) {printf ("computer drop!\ n"); while (1) {int a = rand ()% max_row; / / generate a random number int b = rand ()% max_col from 0 to 2 If (board [a] [b]! ='') {/ / determine whether the location already has a child continue;} board [a] [b] = 'oclinic; break;} / /} / / char check (char row [max _ row] [max_col]) {/ / check whether all rows are equal for (int cow = 0; cow < max_row) Cow++) {if (board [cow] [0]! =''& & board [cow] [0] = = board [cow] [1] & & board [cow] [0] = = return board [cow] [0]) {return board [cow] [0];}} / / check whether all columns are equal for (int col = 0; col < max_col) Col++) {if (board [0] [col]! =''& & board [0] [col] = = board [1] [col] & & board [0] [col] = = board [2] [col]) {return board [0] [col] }} / / detect whether diagonals are equal if (board [0] [0]! =''& & board [0] [0] = = board [1] [1] & & board [0] [0] = = board [2] [2]) {return board [0] [0] } if (board [0] [2]! =''& & board [0] [2] = = board [1] [1] & & board [0] [2] = = board [2] [0]) {return board [0] [2];} if (pingju (board)) {return'qcow;} return 'a' } / / determine whether the chessboard is full. If it is full, return 1, and return 0.int pingju (char row [max _ row] [max_col]) {for (int row = 0; row < max_row; row++) {for (int col = 0; col < max_col; col++) {if (board [row] [col] = =') {return 0;} return 1 } int main () {char Board [max _ row] [max_col] = {0}; / the array is initialized to 0 char winner ='n'; init (board); / / initialize the elements in the chessboard to spaces while (1) {print (board); / / print chessboard play (board); / / player winner = check (board); / / judge victory or defeat if (winner! ='a') {break } computer (board); / / winner = check (board); / / if (winner! ='a') {break;}} if (winner = ='x') {print (board); printf ("you win!");} else if (winner = ='o') {print (board); printf ("you lose!");} else if (winner = ='q') {print (board) Printf ("draw!");} return 0;} after reading the above, do you have any further understanding of how C language implements tic-tac-toe chess game? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.