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 Xiaobian to share with you how to use C language to achieve a simple three-game related knowledge points, detailed content, clear logic, I believe most people are still too familiar with this knowledge, so share this article for your reference, I hope you have some harvest after reading this article, let's learn about it together.
1. Header #define _CRT_SECURE_NO_WARNINGS#include #include //identifier definition #define ROW 3#define COL 3//function definition//board initialization void init_board (char board[ROW][COL], int row, int col);(char board[ROW][COL], int row, int col);(char board[ROW][COL], int row, int col);//Computer chess void comeputer_game (char board[ROW][COL], int row, int col);(char board[ROW][COL], int row, int col);2. Game function #include "game.h"//board initialization void init_board(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i
< ROW; i++) { for (j = 0; j < COL; j++) { board[i][j] = ' '; } }}//打印棋盘void display_board(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; int k = 0; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { printf(" %c ",board[i][j]); if (j < COL - 1) { printf("|"); } } printf("\n"); if (i < ROW - 1) { for (k = 0; k < COL; k++) { printf("---"); if (k < COL - 1) { printf("|"); } } } printf("\n"); }}//玩家下棋void player_game(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; while (1) { printf("请输入你要落子的坐标>:"); scanf("%d %d", &x, &y); if (x = 1) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*'; break; } else { printf("The coordinates you entered are occupied!\ n"); } } else { printf("The coordinates you entered are incorrect, please re-enter!\ n"); } } void computer_game(char board[ROW][COL], int row, int col) { while (1) { int x = rand() % row; int y = rand() % col; if (board[x][y] == ' ') { board[x][y] = '#'; break; } } }//Decide whether to win or lose//1. Player wins returns *//2. Computer wins returns #//3. Neither side wins, there is no position on the board, draw returns p//4. Neither side wins, there is still position on the board, continue playing chess returns cchar is_win(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; //judgment line for (x = 0; x
< row; x++) { if (board[x][0] == board[x][1] && board[x][1] == board[x][2] && board[x][1] != ' ') { return board[x][1]; } } //判断列 for (y = 0; y < col; y++) { if (board[0][y] == board[1][y] && board[1][y] == board[2][y] && board[1][y] != ' ') { return board[1][y]; } } //判断对角线 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') { return board[1][1]; } if (board[2][0] == board[1][1] && board[1][1] == board[1][2] && board[1][1] != ' ') { return board[1][1]; } //判断棋盘是否有空位 for (x = 0; x < row; x++) { for (y = 0; y < col; y++) { if (board[x][y] == ' ') { return 'c'; } } } return 'p';}3、游戏实现#include "game.h"//打印菜单void menu() { printf("***************************\n"); printf("********* 三 子 棋 ********\n"); printf("********* 1. play ********\n"); printf("********* 0. exit ********\n"); printf("***************************\n");}void game() { char win = 0; //定义棋盘存储 char board[ROW][COL]; //棋盘初始化 init_board(board, ROW, COL); //打印棋盘 display_board(board, ROW, COL); while (1) { //玩家下棋 player_game(board, ROW, COL); display_board(board, ROW, COL); //判断输赢 win = is_win(board, ROW, COL); if (win != 'c') { break; } //电脑下棋 comeputer_game(board, ROW, COL); display_board(board, ROW, COL); //判断输赢 win = is_win(board, ROW, COL); if (win != 'c') { break; } } if (win == '*') { printf("恭喜玩家胜利!\n"); display_board(board, ROW, COL); system("pause"); } else if (win == '#') { printf("电脑胜利,继续加油哟!\n"); display_board(board, ROW, COL); system("pause"); } else { printf("平局\n"); display_board(board, ROW, COL); system("pause"); }} int main(void) { int num = 0; srand((unsigned)time(NULL)); do { //打印菜单 menu(); //选择菜单 printf("请选择菜单>:"); scanf("%d", &num); switch (num) { case 0: break; case 1: game(); break; default: printf("Your choice is wrong, please select again!\ n"); system("pause"); system("cls"); break; } } while (num); system("pause"); return 0;}
a photographic representation
The above is "how to use C language to achieve a simple three-game chess" all the content of this article, thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.