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 tic-tac-toe chess game through C language

2025-04-10 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 through the C language, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Tic-tac-toe chess game: namely Gobang, which is called Tic-Tac-Tic in English, is a kind of Lianzhu game played on 3x-3 squares, which is similar to Gobang. It gets its name because the chessboard generally does not draw a border frame, and the grid lines are arranged into zigzag.

Topic analysis:

To complete the writing of the game, we need to analyze what is needed to complete the whole process of the game.

1. First of all, we need to define a 3-3 chessboard. According to the relevant knowledge, we can represent the chessboard in the form of a two-dimensional array.

two。 After the chessboard is defined, you need to initialize the chessboard and initialize each position of the 3x3 two-dimensional array to (space)

3. With the chessboard, we can start to play chess, first of all to determine whether the player plays first or the computer plays first. In

4. Every time after playing chess, it needs to be tested to determine whether the chess position is legal, to judge whether to win, and so on.

Based on the above analysis, the following function windows can be roughly defined:

Void InitGame (); / / initialize the game (chessboard) void PrintChess (); / / output chessboard void PlayerMove (); / / player chess void ComputerMove (); / / computer chess char CheckGameOver (); / / judge whether the game is over (player win / computer win / draw)

Initialize the chessboard: initialize each position of the 3D array chessboard to''.

Void InitGame () {for (int I = 0; I)

< ROW; i++) { for (int j = 0; j < COL; j++) chess_board[i][j] = ' '; }} 输出棋盘: 输出棋盘时,棋盘的风格可以根据自己的喜好来设计 void PrintfChess()//输出棋盘,棋盘的设计可以根据自己的喜好设计{ for (int i = 0; i < ROW; i++) { printf("| %c | %c | %c |\n", chess_board[i][0], chess_board[i][1], chess_board[i][2]); if (i < ROW - 1) printf("|---|---|---|\n"); }} 玩家下棋: 玩家输入下棋位置后,需要判断该位置是否合法、输入位置是否已被占用 void PlayerMove()//玩家下棋{ printf("玩家落子.\n"); int row, col; while (1) { printf("请输入一组坐标(下棋位置):>

"); scanf (" d d ", & row, & col); / / check the validity of coordinates if (row)

< 0 || row >

ROW | | col

< 0 || col >

COL) {printf ("illegal input, please re-enter..."); continue;} if (chess_ chess_ [row] [col]! =') {printf ("the location entered has been occupied, please re-enter..."); continue } chess_ Board [row] [col] ='x chess break;} on behalf of the player's chess

Computer chess: when the computer plays chess, the position of the game is randomly generated by the srand function.

Void ComputerMove () / / computer chess {srand (time (0)); while (1) {int row = rand ()% ROW; int col = rand ()% COL; if (chess_ Board [row] [col]! =') {continue;} chess_ Board [row] [col] = 'OBO' represents the chess break played by the computer }}

Check the chessboard: when testing the chessboard, judge the rows, columns, and diagonals respectively. Here I stipulate:

The'x 'represents the player to win the' o 'for the computer to win, the' h' for the draw with the chess'c'to continue.

Char CheckGameOver () / / check whether the game is over {/ / check line for (int I = 0; I

< ROW; i++) { if (chess_board[i][0] != ' ' && chess_board[i][0] == chess_board[i][1] && chess_board[i][0] == chess_board[i][2] ) return chess_board[i][0]; } //检查列 for (int j = 0; j < COL; j++) { if (chess_board[0][j] != ' ' && chess_board[0][j] == chess_board[1][j] && chess_board[0][j] == chess_board[2][j] ) return chess_board[0][j]; } //检查对角线 if (chess_board[0][0] != ' ' && chess_board[0][0] == chess_board[1][1] && chess_board[0][0] == chess_board[2][2] ) return chess_board[0][0]; if (chess_board[0][2] != ' ' && chess_board[0][2] == chess_board[1][1] && chess_board[0][2] == chess_board[2][0] ) return chess_board[0][2]; //判断是否和棋 if (ChessFull()) return 'h'; return 'c';} 至此,主要的功能函数均已编写完毕,整个程序的流程如下所示: 1.初始化棋盘; 2.输出棋盘; 3.玩家下棋; 4.检测棋盘; 5.电脑下棋; 6.检测棋盘 代码如下: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #define START 1#define QUIT 0#define ROW 3#define COL 3 static char chess_board[ROW][COL];//定义棋盘 void StartGame();void InitGame();void PrintfChess();void PlayerMove();void ComputerMove();char CheckGameOver();bool ChessFull(); int main(int argc, char* argv[]){ int select = 1; while (select) { printf("*********************\n"); printf("* [1] Start Game *\n"); printf("* [2] Over Game *\n"); printf("*********************\n"); printf("请选择:>

"); scanf_s ("% d ", & select); if (select = = QUIT) break; if (select! = START) {printf (" input error, please re-enter.\ n "); continue;} StartGame ();} printf (" GoodBye. "); return 0 } void StartGame () {char winner; / / 1 initialize the game (chessboard) InitGame (); / / 2 enter the game while (1) {/ / 3 output chessboard PrintfChess (); / / 4 players play chess PlayerMove (); / / 5 check the result winner = CheckGameOver (); if (winner! ='c') break / / 6 computer chess ComputerMove (); / / 7 check results CheckGameOver (); winner = CheckGameOver (); if (winner! ='c') break;} if (winner = ='x') printf ("player wins.\ n"); if (winner = ='o') printf ("computer wins.\ n") If (winner = ='h') printf ("draw.\ n");} void InitGame () {for (int I = 0; I)

< ROW; i++) { for (int j = 0; j < COL; j++) chess_board[i][j] = ' '; }} void PrintfChess()//输出棋盘,棋盘的设计可以根据自己的喜好设计{ for (int i = 0; i < ROW; i++) { printf("| %c | %c | %c |\n", chess_board[i][0], chess_board[i][1], chess_board[i][2]); if (i < ROW - 1) printf("|---|---|---|\n"); }} void PlayerMove()//玩家下棋{ printf("玩家落子.\n"); int row, col; while (1) { printf("请输入一组坐标(下棋位置):>

"); scanf (" d d ", & row, & col); / / check the validity of coordinates if (row)

< 0 || row >

ROW | | col

< 0 || col >

COL) {printf ("illegal input, please re-enter..."); continue;} if (chess_ chess_ [row] [col]! =') {printf ("the location entered has been occupied, please re-enter..."); continue } chess_ Board [row] [col] ='x chess on behalf of the player break;}} void ComputerMove () / / computer chess {srand (time (0)); while (1) {int row = rand ()% ROW; int col = rand ()% COL If (chess_ Board [row] [col]! =') {continue;} chess_ Board [row] [col] = 'break' represents the chess played by the computer. }} / * *'x' means players win *'o' means computer wins *'h' representatives and chess *'c' representatives continue * / char CheckGameOver () / / check whether the game ends {/ / check line for (int I = 0; I)

< ROW; i++) { if (chess_board[i][0] != ' ' && chess_board[i][0] == chess_board[i][1] && chess_board[i][0] == chess_board[i][2] ) return chess_board[i][0]; } //检查列 for (int j = 0; j < COL; j++) { if (chess_board[0][j] != ' ' && chess_board[0][j] == chess_board[1][j] && chess_board[0][j] == chess_board[2][j] ) return chess_board[0][j]; } //检查对角线 if (chess_board[0][0] != ' ' && chess_board[0][0] == chess_board[1][1] && chess_board[0][0] == chess_board[2][2] ) return chess_board[0][0]; if (chess_board[0][2] != ' ' && chess_board[0][2] == chess_board[1][1] && chess_board[0][2] == chess_board[2][0] ) return chess_board[0][2]; //判断是否和棋 if (ChessFull()) return 'h'; return 'c';}bool ChessFull(){ for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (chess_board[i][j] == ' ') return false; } } return true;} 运行测试图:

The running interface of the program can also be optimized by system ("cls"), which can make the interface more beautiful.

After reading the above, do you have any further understanding of how to implement tic-tac-toe chess game in C language? 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.

Share To

Development

Wechat

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

12
Report