Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the simple version of Gobang in C language

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you how to realize the simple version of Gobang in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Preface

Test logic for test.c to play games-- call game.c game.h

Game module:

The implementation Logic of game.c play Game

Game.h play the declaration of the game implementation function

Step1. Initialize the internal value of the chessboard to a space

Train of thought

1. Initialize the values in the chessboard

two。 The contents of the array in the chessboard should be full of spaces

3. In order to easily change the size of the chessboard, use a macro definition to easily modify the size of the chessboard; for example, you only need to change the number after the macro definition to change the chessboard from 3 to 5.

5: 5 chessboard:

# define ROW 5 / / Line # define COL 5 / / column # include void InitBoard (char board [ROW] [COL], int row, int col)

3x 3 chessboard:

# define ROW 3 / / Row # define COL 3 / / column # include void InitBoard (char board [ROW] [COL], int row, int col); void InitBoard (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] = ' ';//初始化为一个空格 } } }step2.打印棋盘

Train of thought

1. Observe the chessboard

two。 Print% c and | for each line first

Printf ("% c |% c |% c\ n", board [I] [0], board [I] [1], board [I] [2])

3. Then print-and |

Printf ("- |-- | -\ n")

4. Get the normal version:

Void DisplayBoard (char board [ROW] [COL], int row, int col) {int I = 0; int j = 0; for (I = 0; I

< row; ++i) { //先打印数据 printf(" %c | %c | %c\n", board[i][0], board[i][1], board[i][2]); //打印分割行 if(i该坐标被占用,请重新输入(需要使用循环语句) 3.输入坐标超过了棋盘大小-->

Illegal coordinates! Please re-enter (need to use a loop statement)

Void player_move (char board [ROW] [COL], int row, int col) {printf ("players play Chess: >"); int x = 0; int y = 0; while (1) {scanf ("% d% d", & x, & y); if (x > = 1 & x = 1 & y if occupied, recalculate random values (cycles)

Note: in order to enrich the variety of loops, the goto statement is used here!

Void computer_move (char board [ROW] [COL], int row, int col) {int x = 0; int y = 0; printf ("computer chess\ n"); again: X = rand ()% ROW;//0-2 y = rand ()% ROW / / 0-2 / / determine whether the coordinates are occupied if (board [x] [y] = ='') {board [x] [y] ='#'; / / the number generated is already in the range of 0-2, and there is no need to subtract 1} else {goto again;}}

Step5. Judge whether the chessboard is full.

Train of thought

1. Traverse a two-dimensional array (loop)

two。 Determine whether the value in a two-dimensional array element is''. If there is a space, the chessboard is not full-- > return 0

3. If there is no space'--> return1 after traversing

Int is_full (char board [ROW] [COL], int row, int col) / / determines whether the function is full {int I = 0; int j = 0; for (I = 0; I < row; + + I) {for (j = 0; j < col) + + j) {if (board [I] [j] = =') {return 0;} return 1;} step6. Judge whether to win or lose

Ordinary version

Train of thought

1. Four endings

One line with the same element wins.

Win with the same element in a row

The same diagonal element wins

Draw-call functions in step5

two。 The return value setting of the function

Players win-return'*'

Computer wins-return'#'

Draw-return'Q'

The game continues-- return'C'

Char is_win (char board [ROW] [COL], int row, int col) {int I = 0; for (I = 0; I < row) + + I) / / 3 lines {if (board [I] [0] = = board [I] [1] & & board [I] [1] = = board [I] [2] & & board [I] [1]! =') {return board [I] [0];}} for (I = 0; I < col) Judgment of + + I) / / 3 columns {if (board [0] [I] = = board [1] [I] & board [1] [I] = = board [2] [I] & & board [1] [I]! =') {return board [1] [I] }} / / judgment of a diagonal if (board [0] [0] = = board [1] [1] & & board [1] [1] = = board [2] [2] & & board [1] [1]! =') {return board [1] [1] } if (board [0] [2] = = board [1] [1] & board [1] [1] = = board [2] [0] & & board [1] [1]! =') {return board [1] [1] } / / judge the tie if (is_full (board, row, col)) / / arguments passed inside the function {return'QQ;} / / the game continues to return'QQ;} step7. Finally, three situations are tested.

Game.h#define _ CRT_SECURE_NO_WARNINGS 1#define ROW 3 / / Row # define COL 3 / / column # include#include#include void InitBoard (char board [ROW] [COL], int row, int col); / / initialize chessboard void DisplayBoard (char board [ROW] [COL], int row, int col); / / print chessboard void player_move (char board [ROW] [COL], int row, int col) / / the player plays chess void computer_move (char board [ROW] [COL], int row, int col); / / computer chess / / the code to judge whether to win or lose / / the player wins -'*'/ / computer wins -'#'/ / draw-'Qgames / continue -' C'char is_win (char board [ROW] [COL], int row, int col) / / still want to test the chessboard, pass in the chessboard value game.c#define _ CRT_SECURE_NO_WARNINGS 1#include "game.h" void InitBoard (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] ='; / initialize to a space} void DisplayBoard (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) / / print data {printf ("% c", board [I] [j]); if (j = 1 & & x = 1 & & y

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report