当前位置:网站首页>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
边栏推荐
- Spark submission parameters -- use of files
- 瀑布流布局
- LeetCode第 303 场周赛
- 看到很多App出现闪烁的图片,特别是会员页面
- 简易轮播图和打地鼠
- Gbdt source code analysis of boosting
- My creation anniversary
- System. Accessviolationexception: an attempt was made to read or write to protected memory. This usually indicates that other memory is corrupted
- 如何解决Visual Stuido2019 30天体验期过后的登陆问题
- JVM-参数配置详解
猜你喜欢

分布式原理 - 什么是分布式系统

密码强度验证示例

Spark提交参数--files的使用

《三子棋》C语言数组应用 --n皇后问题雏形

Use the command to check the WiFi connection password under win10 system

Idea远程提交spark任务到yarn集群

Visual Studio 2022 查看类关系图

什么是物联网

Remember that spark foreachpartition once led to oom

Overview of JS synchronous, asynchronous, macro task and micro task
随机推荐
JS 同步、异步,宏任务、微任务概述
Example of password strength verification
HBCK fix problem
Spark 判断DF为空
JVM-垃圾收集器详解
期货在线开户是否安全?去哪家公司手续费最低?
VS2010 add WAP mobile form template
SublimeText-win10光标跟随问题
用setTimeout模拟setInterval定时器
MFC 线程AfxBeginThread基本用法,传多个参数
VMware Workstation fails to start VMware authorization service when opening virtual machine
Understanding the execution order of T-SQL query from the execution order of join on and where
Promise object and macro task, micro task
Recommend 10 learning websites that can be called artifact
LeetCode第 303 场周赛
Install entityframework method
C语言函数复习(传值传址【二分查找】,递归【阶乘,汉诺塔等】)
Use the command to check the WiFi connection password under win10 system
记一次redis超时
什么是物联网