In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to write the code of Gobang Mini Game in C language". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Basic ideas:
1. Create a game selection panel.
two。 Create and initialize the chessboard.
3. The player falls and decides, the computer falls and judges.
4. Judge the result, the game is over!
The code is as follows:
Header file:
# pragma once#include#include#include#include # pragma warning (disable:4996) extern void Game () / / the game code needs to define the function to be called # define ROW 3//FOW, FOL represent the length and width of the boundary of the backgammon # define COL 3 # define INIT''/ / INIT is the space macro definition # define WHITE'X' / X represents Player#define BLACK'O' / O in the three characters represents Computer#define DRAW'D' / / DRAW represents the draw # define NEXT'N' / / NEXT represents the continuation / / MineClearence
Various functions:
Void InitBoard (char board [] [COL], int row, int col) / / Chessboard initialization {for (int I = 0; I)
< row; i++){ for (int j = 0; j < col; j++){ board[i][j] = INIT; } }}static void ShowBoard(char board[][COL], int row, int col)//显示棋盘{ system("cls"); printf(" "); for (int i = 0; i < col; i++){ printf("%4d", i + 1); } printf("\n--------------\n"); for (int i = 0; i < row; i++){ printf("%-2d", i + 1); //2 for (int j = 0; j < col; j++){ printf("| %c ", board[i][j]); } printf("\n--------------\n"); }}static char IsEnd(char board[][COL], int row, int col)//最终结果{ for (int i = 0; i < row; i++){ if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != INIT){ return board[i][0]; } } for (int j = 0; j < COL; j++){ if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[0][j] != INIT){ return board[0][j]; } } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != INIT){ return board[1][1]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != INIT){ return board[1][1]; } for (int i = 0; i < row; i++){ for (int j = 0; j < col; j++){ if (board[i][j] == INIT){ return NEXT; } } } return DRAW;}static void PlayerMove(char board[][COL], int row, int col)//玩家{ int x = 0; int y = 0; while (1){ printf("Please Enter Postion# "); scanf("%d %d", &x, &y); if (x < 1 || y < 1 || x >| y > 3) {printf ("Enter Postion Error!\ n"); continue;} if (board [x-1] [y-1] = = INIT) {board [x-1] [y-1] = WHITE; break;} else {printf ("Postion Is Not Empty!\ n") } static void ComputerMove (char board [] [COL], int row, int col) / / computer {while (1) {int x = rand ()% row; int y = rand ()% col; if (board [x] [y] = = INIT) {board [x] [y] = BLACK; break;}} void Game () {char board [ROW] [COL]; InitBoard (board, ROW, COL) Srand ((unsigned long) time (NULL)); char result = 0; while (1) {ShowBoard (board,ROW, COL); PlayerMove (board,ROW, COL); result = IsEnd (board,ROW, COL); if (result! = NEXT) {break;} ShowBoard (board,ROW, COL); ComputerMove (board,ROW, COL); result = IsEnd (board,ROW, COL); if (result! = NEXT) {break } ShowBoard (board, ROW,COL); switch (result) {case WHITE: printf ("You Win!\ n"); break; case BLACK: printf ("You Lose!\ n"); break; case DRAW: printf ("You = = Computer!\ n"); break; default: printf ("BUG!\ n"); break;}
Game selection panel (main function)
# include "game.h" static void Menu () {printf ("|-start the game-|\ n"); printf ("+-1. Play-+\ n"); printf ("+-0. Exit-+\ n ");} int main () {int select = 0; int quit = 0; while (! quit) {Menu (); printf (" Please Select# "); scanf ("% d ", & select); switch (select) {case 1: Game (); break; case 0: quit = 1; break; default: printf (" Enter Error, Try Again!\ n "); break;} printf (" bye!\ n ") This is the end of system ("pause"); return 0;} "how to write the code for Gobang Mini Game in C language". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.