当前位置:网站首页>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 .
边栏推荐
- 访问权限——private,public,protected
- 二叉树的遍历 递归+迭代
- Error[Pe147]: declaration is incompatible with '错误问题
- RT thread learning notes (VII) -- open the elmfat file system based on SPI flash (middle)
- 剑指Offer(四十九):把字符串转换成整数
- toolstrip 去边框
- [leetcode daily question 2021/2/13]448. Find all the missing numbers in the array
- 用两个栈实现队列
- Constructors, method overloads, object arrays, and static
- [leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]
猜你喜欢
![[leetcode daily question 2021/5/8]1723. The shortest time to complete all work](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work

Issue 8: cloud native -- how should college students learn in the workplace

RT-Thread 学习笔记(五)---编辑、下载、调试程序

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

Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2

Dry goods likeshop takeout order system is open source, 100% open source, no encryption
![[leetcode daily question 2021/4/23]368. Maximum divisible subset](/img/0b/32ca862963c842a93f79eaac94fb98.png)
[leetcode daily question 2021/4/23]368. Maximum divisible subset

在神州IV开发板上为STemWin 5.22加入触屏驱动
![[dectectron2] follow the official demo](/img/aa/03e46897234c309415b336ac39b7d9.png)
[dectectron2] follow the official demo
![Error[pe147]: declaration is incompatible with 'error problem](/img/4f/57145d78f4dc1fe84d2f271dd9d82f.png)
Error[pe147]: declaration is incompatible with 'error problem
随机推荐
剑指Offer(五十三):表示数值的字符串
MySQL速学-2021-09-01
Sql Server之查询总结
Flutter TextField怎样去除下划线及有焦点时颜色
Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
点击el-dropdown-item/@click.native
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work
Flutter TextField设置高度并且自动换行,圆角边框去除下划线
Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2
Halcon模板匹配之Shape
对面向抽象编程的理解
剑指Offer(五十二):正则化表达式
RT thread learning notes (V) -- edit, download and debug programs
剑指Offer(七):斐波那契数列
11 handle "self assignment" in operator=
Redis implementation of distributed lock solution
Sql Server 数据库之完整性约束
剑指Offer(九):变态跳台阶
13 managing resources by objects
在altium designer中禁用USBJATG