当前位置:网站首页>2021-08-14 Sanzi chess
2021-08-14 Sanzi chess
2022-07-26 10:45:00 【ZhuMou】
Catalog
#1 Code
The main function main.cpp
#include <stdio.h>
#include "supportGaming.h"
int main() {
/*chessBoard oneBoard;
piece onePiece; onePiece.player = 1; onePiece.x = 1; onePiece.y = 1;
play(&oneBoard, &onePiece);
printBoard(&oneBoard);
printf("%d\n",isDetermined(&oneBoard));*/
printf(" The end result is :%d\n",game());
return 0;
}The header file supporGaming.h
// The board
struct chessBoard {
int num = 0;// The number of pieces on the chessboard
int arr[3][3] = { 0 };// The board ,0 It means no children ,1 It stands for the chess pieces that are played first ,2 Represents the chess pieces of the back hand
};
// Once
struct piece {
int player = 0;// The chess pieces of the first player are 1, The backhand player's chess pieces are 2
int x = 0;// The second of the chessboard x That's ok
int y = 0;// The second of the chessboard y Column
// Note that for chess players ,x and y The range of is [1,3], All subscripts converted to arrays should be subtracted 1
};
int isDetermined(chessBoard* oneBoard);// Read into a chessboard , Return results .
void play(chessBoard* board, piece* onePiece);// A chess player makes a specified modification to the specified chessboard .
void printBoard(chessBoard* board);// Print chessboard
int game();
int examine(chessBoard* board, piece* onePiece); Check whether each drop is legal : Don't fall where you've already fallen Source file supportGaming.cpp
#include <stdio.h>
#include "supportGaming.h"
// The board
// Play a chess game , Return the chess result . First hand wins , return 1; The backhand wins , return 2; Ruo Heqi , Then return to 0.
int game() {
// Generate chessboard and pieces
int result = 0;
chessBoard qipan;
piece player1;
piece player2;
player1.player = 1;
player2.player = 2;
// When there is no winner (isDetermined The return value is 0) Continue a round && The chessboard is not full
printBoard(&qipan);
while (!(result = isDetermined(&qipan))) {
// There is no winner and the chessboard is full , Directly return to the draw . You can't go to the next round
if (qipan.num == 9) {
result = 0;
break;
}
do {
printf(" Please play chess with number one player \n");
scanf("%d %d", &player1.x, &player1.y);
} while (examine(&qipan, &player1) == 0);
play(&qipan, &player1);
printBoard(&qipan);
if (qipan.num == 9) {
result = 0;
break;
}
if ((result = isDetermined(&qipan)) != 0) {
break;
}
do {
printf(" Please play chess with No. 2 player \n");
scanf("%d %d", &player2.x, &player2.y);
} while (examine(&qipan, &player2) == 0);
play(&qipan, &player2);
printBoard(&qipan);
if ((result = isDetermined(&qipan)) != 0) {
break;
}
}
result = isDetermined(&qipan);
return result;
}
// Judge the result of a chessboard , First hand wins , return 1; The backhand wins , return 2; Ruo Heqi , Then return to 0.
int isDetermined(chessBoard* oneBoard) {
//arr For the chessboard , A two-dimensional array ( The first address )
int* row1 = (int*)(oneBoard->arr);// The first address on the first line
int* row2 = row1 + 3;// The first address on the second line
int* row3 = row2 + 3;// The first address on the third line
// These conditions cannot happen at the same time , So it doesn't matter if there is a sequence of judgment .
// The first line and are 3/6 Time will tell the difference
if (*row1 * *(row1 + 1) * *(row1 + 2) == 1)
return 1;
if (*row1 * *(row1 + 1) * *(row1 + 2) == 8)
return 2;
// The second line and are 3/6 Time will tell the difference
if (*row2 * *(row2 + 1) * *(row2 + 2) == 1)
return 1;
if (*row2 * *(row2 + 1) * *(row2 + 2) == 8)
return 2;
// The sum of the third line is 3/6 Time will tell the difference
if (*row3 * *(row3 + 1) * *(row3 + 2) == 1)
return 1;
if (*row3 * *(row3 + 1) * *(row3 + 2) == 8)
return 2;
// The first column and are 3/6 Time will tell the difference
if (*row1 * *row2 * *row3 == 1)
return 1;
if (*row1 * *row2 * *row3 == 8)
return 2;
// The sum of the second column is 3/6 Time will tell the difference
if (*(row1+1) * *(row2 + 1) * *(row3 + 1) == 1)
return 1;
if (*(row1+1) * *(row2 + 1) * *(row3 + 1) == 8)
return 2;
// The sum of the third column is 3/6 Time will tell the difference
if (*(row1 + 2) * *(row2 + 2) * *(row3 + 2) == 1)
return 1;
if (*(row1 + 2) * *(row2 + 2) * *(row3 + 2) == 8)
return 2;
// The sum of diagonals is 3/6 To decide between the winners and the losers
if (*(row1 + 0) * *(row2 + 1) * *(row3 + 2) == 1)
return 1;
if (*(row1 + 0) * *(row2 + 1) * *(row3 + 3) == 8)
return 2;
// The sum of the inverse diagonals is 3/6 To decide between the winners and the losers
if (*(row1 + 2) * *(row2 + 1) * *(row3 + 0) == 1)
return 1;
if (*(row1 + 2) * *(row2 + 1) * *(row3 + 0) == 8)
return 2;
return 0;
}
// A chess player makes a specified modification to the specified chessboard .
void play(chessBoard* board, piece* onePiece ) {
//board: Designated chessboard
//piece: Once , It contains the positions of players and players (x,y)
const int x = onePiece->x - 1;
const int y = onePiece->y - 1;
const int player = onePiece->player;
*((int*)(board->arr) + 3 * x + y) = player;
(board->num)++;
return;
}
// Print chessboard
void printBoard(chessBoard* board) {
for (int i = 0; i < 3;i++) {
for (int j = 0; j < 3;j++) {
printf("%d ", *((int*)(board->arr) + 3 * i + j));
// If the code here is removed (int *) That would make a mistake . This shows that the meaning of the array name of a two-dimensional array may not be &arr[0][0].
}
printf("\n");
}
return;
}
// Check whether each drop is legal : Don't fall where you've already fallen
int examine(chessBoard* board ,piece* onePiece) {
const int x = onePiece->x-1;
const int y = onePiece->y-1;
const int* addr = (int*)(board->arr);
if (*(addr + 3 * x + y) == 0) {
return 1;// Fall in 0 It's legal
}
return 0;// Otherwise it's illegal
}
// When writing a function , Many local variables are created to accept function parameters again . This wastes memory to some extent ( But it's not long ), But it can simplify the subsequent code and help clarify ideas .#2 Several repaired bug
1. Fixed player 1 after winning , The second player also needs to input questions
2. Fixed the problem that players can play in repeated positions when playing chess
#3 Accumulated experience
1. The meaning of the array name of a two-dimensional array needs to be clear . In terms of time ,int arr[3][3] Of arr Is it right? int* Pointer to type , The value is equal to &arr[0][0], But the meaning is not the first address of a two-dimensional array . If you want to use the first address of a binary array for pointer operation , You need to use cast :arr1=(int* )(arr). The meaning of the converted pointer is equivalent to the first address of a two-dimensional array , have access to *(arr1+3*i+j) Take out [i,j] Subscript elements .
2. The multi file programming used in writing a project is not particularly clear , Although it is used correctly this time .
3. When analyzing a complex project , Still should follow TDD The idea of , Let test drive development . We should carefully consider the basic functions and basic objects of the actual operation of the program , And possible problems . In particular , You need to be in main Set up the framework of program operation in function , Abstract out basic functions and basic objects , Declare the appropriate functions and structures in the header file ( about C In terms of language ), And write notes . Finally, it is implemented in the source file ( function ).
otherwise , There may be some unexpected problems .
Brother Peng is to build the overall framework first , And then realize the specific project ; Besides , He pays special attention to the portability of the code .
边栏推荐
- Codepoint 58880 not found in font, aborting. flutter build apk时报错
- RT-Thread 学习笔记(五)---编辑、下载、调试程序
- 回到顶部的几种方案(js)
- 扫雷pro版2021-08-19
- winpcap 抓包函数pcap_loop(),停止问题
- flutter 背景变灰效果,如何透明度,灰色蒙板遮罩
- 12 don't forget every component when copying an object
- putty的使用教程
- 2021-08-13 learn C language with pengge - array
- 【小程序】onReachBottom 事件为什么不能触发 ?(一秒搞定)
猜你喜欢

GIS方法类期刊和论文的综述(Introduction)怎么写?

第7期:内卷和躺平,你怎么选

2021-08-12 function recursion_ Learn C language with brother Peng

第6期:大学生应该选择哪种主流编程语言

Disable usbjatg in Altium Designer
控制随机抽中几率 [ C# | Random ]
![[machine learning notes] [style transfer] deeplearning ai course4 4th week programming(tensorflow2)](/img/94/ff52b043320b6dea5ca1238e314de8.png)
[machine learning notes] [style transfer] deeplearning ai course4 4th week programming(tensorflow2)

Anaconda is used on vscode (the environment has been configured)

RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)

Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
随机推荐
toolstrip 去边框
创建EOS账户 Action
Dry goods likeshop takeout order system is open source, 100% open source, no encryption
Issue 5: the second essential skill for College Students
使用float实现左中右布局,中间内容自适应
Koin
剑指Offer(二十):包含min函数的栈
px2rem-loader将px转化为rem,适配移动端vant-UI等框架
[dectectron2] follow the official demo
Constructors, method overloads, object arrays, and static
剑指Offer(五十二):正则化表达式
GIS方法类期刊和论文的综述(Introduction)怎么写?
RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
C语言鹏哥20210812C语言函数
Pengge C language sixth class
c 语言中宏参数的字符串化跟宏参数的连接
Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2
C#委托与匿名方法浅析
c结构体中定义的成员指针赋值与结构体指针作为成员函数参数的使用
Phase 4: one of College Students' vocational skills preparation in advance