当前位置:网站首页>Application of C language array in Sanzi chess -- prototype of Queen n problem
Application of C language array in Sanzi chess -- prototype of Queen n problem
2022-07-25 15:23:00 【GracefulBlack】
C A small game that can be used by language Xiaobai , Let's take a look at ~
After that, I will encounter the data structure next time n The Queen's question will be much easier to understand
What is? **
Three chess game
**?
A row or a column, a diagonal line or an anti focal line are all the same type of chess Son , Then the player wins , Easy to do , Let's try it together !

Main knowledge points : Two dimensional array ,for sentence ,switch sentence , A little knowledge of timestamp
First, we need to design the function we want to use
Write the header file "game.h"
#pragma once
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include<string.h>
#define ROW 3
#define COL 3
// Initialize chessboard
void InitBoard(char board[ROW][COL], int row, int col);
// Print chessboard
void DisplayBoard(char board[ROW][COL], int row, int col);
// Players play chess
void Player_Move(char board[ROW][COL], int row, int col);
// The computer plays chess
void Computer_Move(char board[ROW][COL], int row, int col);
// The criteria for judging whether to win or lose
// Game player wins --‘*’
// Computers win -- ‘#’
// It ends in a draw -- 'Q'
// continue -- 'C'
char is_win(char board[ROW][COL], int row, int col);
Secondly, think about how to realize your game , How to show it , Write a test function , This is more scale
#include "game.h"
void menu() {
printf("*********************************\n");
printf("** Welcome to third-chess game **\n");
printf("******* Choose a number *******\n");
printf("******* 1.Play ****\n");
printf("******* 0.Exit ****\n");
printf("*********************************\n");
printf("*********************************\n");
}
void game() {
// Store the data in a two-dimensional array , Players play chess '*' , Computer chess is '#'
char board[ROW][COL] = { 0 };// The contents of the array should be all spaces
InitBoard(board, ROW, COL);// Initialize chessboard
// Print chessboard
DisplayBoard(board,ROW,COL);
// Playing chess
char ret = 0;
while (1) {
Player_Move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
Computer_Move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = is_win(board, ROW, COL);
if (ret != 'C')
break;
}
if (ret == '*') {
printf("You Win!!!\n");
}
else if (ret == '#') {
printf("You Fail!!!\n");
}
else {
printf("Tie!!!\n");
}
}
void test() {
int input = 0;
srand((unsigned int)time(NULL));
do {
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
printf(" Quit the game ");
break;
default:
printf(" Wrong choice , Please re-enter ");
//break; Feel this break Yes, but not
}
} while (input);
}
int main() {
test();
return 0;
}
Last in game.cpp Complete the specific implementation of the function , Pay attention to *** Test function by function *** Oh !!!! Otherwise, it will be changed later bug Change to death
#include "game.h"
// Initialize chessboard
void InitBoard(char board[ROW][COL], int row, int col) {
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
}
// Print chessboard
void DisplayBoard(char board[ROW][COL], int row, int col) {
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)printf("|");
}
puts("");
for (int j = 0; j < col; j++)
{
printf("---");
if (j < col - 1)printf("|");
}
puts("");
}
puts("");
}
// Players play chess
void Player_Move(char board[ROW][COL], int row, int col) {
printf("Player Move:>");
int x = 0;
int y = 0;
while (1) {
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ') {
board[x - 1][y - 1] = '*';
break;
}
else
printf(" The coordinates are occupied , Please enter other coordinates ");
}
else {
printf(" Illegal coordinates , Please re-enter ");
}
}
}
// The computer plays chess
void Computer_Move(char board[ROW][COL], int row, int col) {
int x = 0;
int y = 0;
printf(" The computer plays chess :>\n");
while (1) {
x = rand() % ROW;// The calculated value is 0-2 Positive integer between
y = rand() % COL;// ditto
if (board[x][y] == ' ') {
board[x][y] = '#';
break;
}
}
}
// The criteria for judging whether to win or lose
// Game player wins --‘*’
// Computers win -- ‘#’
// It ends in a draw -- 'Q'
// continue -- 'C'
char is_win(char board[ROW][COL], int row, int col) {
// Judge three lines
for (int i = 0; i < row; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
return board[i][0];
}
// Judge three columns
for (int i = 0; i < col; i++) {
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
return board[0][i];
}
// Diagonal judgment
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') {
return board[1][1];
}
// Anti diagonal judgment
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ' ') {
return board[1][1];
}
// Continue to operate
return 'C';
}
Hello everyone , I am a Oliver, If you like my article , Don't forget to praise me , Your praise is the biggest motivation for my writing
边栏推荐
- Visual Studio 2022 查看类关系图
- ML - 自然语言处理 - 关键技术
- Single or multiple human posture estimation using openpose
- TypeScript学习1——数据类型
- 记一次redis超时
- How to solve the problem of scanf compilation error in Visual Studio
- pkg_ Resources dynamic loading plug-in
- 《三子棋》C语言数组应用 --n皇后问题雏形
- How spark gets columns in dataframe --column, $, column, apply
- 用OpenPose进行单个或多个人体姿态估计
猜你喜欢
随机推荐
请问seata中mysql参数每个客户端连接最大的错误允许数量要怎么理解呢?
Remember that spark foreachpartition once led to oom
Idea护眼色设置
如何解决Visual Stuido2019 30天体验期过后的登陆问题
Solve the timeout of dbeaver SQL client connection Phoenix query
Spark SQL UDF function
Example of password strength verification
Is it safe to open futures online? Which company has the lowest handling charge?
我的创作纪念日
图片裁剪cropper 示例
Spark获取DataFrame中列的方式--col,$,column,apply
Browser workflow (Simplified)
浏览器工作流程(简化)
Introduction to raspberry Pie: initial settings of raspberry pie
Spark提交参数--files的使用
How spark gets columns in dataframe --column, $, column, apply
Install entityframework method
打开虚拟机时出现VMware Workstation 未能启动 VMware Authorization Service
Understanding the execution order of T-SQL query from the execution order of join on and where
瀑布流布局









