当前位置:网站首页>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
边栏推荐
- Local cache --ehcache
- Sublimetext-win10 cursor following problem
- npm的nexus私服 E401 E500错误处理记录
- CGO is realy Cool!
- Args parameter parsing
- System. Accessviolationexception: an attempt was made to read or write to protected memory. This usually indicates that other memory is corrupted
- Meanshift clustering-01 principle analysis
- Spark 判断DF为空
- 如何解决Visual Stuido2019 30天体验期过后的登陆问题
- Debounce and throttle
猜你喜欢

ice 100G 网卡分片报文 hash 问题

outline和box-shadow实现外轮廓圆角高光效果

密码强度验证示例

Docker上运行redis以配置文件方式启动,连接客户端报错Error: Server closed the connection

什么是物联网

Idea remotely submits spark tasks to the yarn cluster

Visual Studio 2022 查看类关系图

Spark partition operators partitionby, coalesce, repartition

反射-笔记

打开虚拟机时出现VMware Workstation 未能启动 VMware Authorization Service
随机推荐
Sublimetext-win10 cursor following problem
Handle Oracle deadlock
MySQL之事务与MVCC
本地缓存--Ehcache
Recommend 10 learning websites that can be called artifact
Tasks, micro tasks, queues and scheduling (animation shows each step of the call)
Solve the error caused by too large file when uploading file by asp.net
Iframe nested other website page full screen settings
Application of object detection based on OpenCV and yolov3
Spark提交参数--files的使用
Maxcompute SQL 的查询结果条数受限1W
Redis elimination strategy list
My creation anniversary
npm的nexus私服 E401 E500错误处理记录
任务、微任务、队列和调度(动画展示每一步调用)
图片裁剪cropper 示例
How much memory can a program use at most?
了解一下new的过程发生了什么
Introduction to raspberry Pie: initial settings of raspberry pie
C language function review (pass value and address [binary search], recursion [factorial, Hanoi Tower, etc.))