当前位置:网站首页>C language -- game of Sanzi
C language -- game of Sanzi
2020-11-09 12:58:00 【osc_rmqoxylv】
Three chess game
Problem description :
3*3 On the chessboard of , As long as three identical pieces appear on a line, you will win ( Players or computers ); If the chessboard is full and there are no three pieces, a line is drawn .
Specific details :
- Initialize chessboard ( Initialize with spaces )
// Initialize chessboard
void initChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
chessbox[row][col] = ' ';
}
}
}
- Print chessboard
// Print chessboard
void printChess(char chessbox[ROW][COL]){
system("cls");
printf("+---+---+---+\n");
for (int row = 0; row < ROW; row++){
printf("| %c | %c | %c |\n",
chessbox[row][0], chessbox[row][1],
chessbox[row][2]);
printf("+---+---+---+\n");
}
}
- Computer move later ( use o It means that the computer is down )
// Computer move later ( use o Express )
void computerMove(char chessbox[ROW][COL]){
srand(time(0));
while (1){
int row = rand() % 3;
int col = rand() % 3;
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'o';
break;
}
}
}
- The player is down
// The player is down ( use x Express )
void playerMove(char chessbox[ROW][COL]){
int row, col;
while (1){
printf(" Please enter your location :");
scanf("%d %d", &row, &col);
if (row >= 3 || col >= 3){
printf(" The position you entered is wrong , Please re-enter :");
continue;
}
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'x';
break;
}
printf(" There are already pieces in this position , Please re-enter :");
}
}
- Three pieces, one line
- In a row or column to achieve three pieces of a line
// That's ok
for (int row = 0; row < ROW; row++){
if (chessbox[row][0] != ' '
&&chessbox[row][0] == chessbox[row][1]
&& chessbox[row][0] == chessbox[row][2]){
return chessbox[row][0];
}
}
// Column
for (int col = 0; col < COL; col++){
if (chessbox[0][col] != ' '
&&chessbox[0][col] == chessbox[1][col]
&& chessbox[0][col] == chessbox[2][col]){
return chessbox[0][col];
}
}
- Diagonal line to achieve three pieces of a line
if (chessbox[0][0] != ' '
&&chessbox[0][0] == chessbox[1][1]
&& chessbox[0][0] == chessbox[2][2]){
return chessbox[0][0];
}
if (chessbox[2][0] != ' '
&&chessbox[2][0] == chessbox[1][1]
&& chessbox[2][0] == chessbox[0][2]){
return chessbox[2][0];
}
- draw
- The chessboard is full and has not won , It's a draw , It's a draw .
Insert a code chip here
// draw
if(isFull(checkbox)){
return 'a';
}
- Win lose agreement :
- return x Win on behalf of the player
if (isWinner(chessbox) == 'x'){
printf(" Congratulations on winning !\n");
break;
}
- return o For computers to win
if (isWinner(chessbox) == 'o'){
printf(" unfortunately , You lost !\n");
break;
}
- return a For a draw ( A draw )
if (isWinner(chessbox) == 'a'){
printf(" You are on the same level as the computer !\n");
break;
}
- Judge whether the chessboard is full :
- return 1 The chessboard is full
- return 0 The chessboard is not full
// Judge whether the chessboard is full
//1 It means full ;0 Express dissatisfaction .
int isFullChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
// Find the space , It's not full
if (chessbox[row][col] == ' '){
return 0;
}
}
}
return 1;
}
Source code :
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define ROW 3
#define COL 3
// The player is down ( use x Express )
void playerMove(char chessbox[ROW][COL]){
int row, col;
while (1){
printf(" Please enter your location :");
scanf("%d %d", &row, &col);
if (row >= 3 || col >= 3){
printf(" The position you entered is wrong , Please re-enter :");
continue;
}
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'x';
break;
}
printf(" There are already pieces in this position , Please re-enter :");
}
}
// Computer move later ( use o Express )
void computerMove(char chessbox[ROW][COL]){
srand(time(0));
while (1){
int row = rand() % 3;
int col = rand() % 3;
if (chessbox[row][col] == ' '){
chessbox[row][col] = 'o';
break;
}
}
}
// Initialize chessboard
void initChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
chessbox[row][col] = ' ';
}
}
}
// Print chessboard
void printChess(char chessbox[ROW][COL]){
system("cls");
printf("+---+---+---+\n");
for (int row = 0; row < ROW; row++){
printf("| %c | %c | %c |\n",
chessbox[row][0], chessbox[row][1],
chessbox[row][2]);
printf("+---+---+---+\n");
}
}
// Judge whether the chessboard is full , Full back 1; Return before full 0
int isFullChess(char chessbox[ROW][COL]){
for (int row = 0; row < ROW; row++){
for (int col = 0; col < COL; col++){
// Find the space , It's not full
if (chessbox[row][col] == ' '){
return 0;
}
}
}
return 1;
}
// Appointment : return x On behalf of the player wins ; return o On behalf of the computer wins ; return a For a draw
char isWinner(char chessbox[ROW][COL]){
// That's ok
for (int row = 0; row < ROW; row++){
if (chessbox[row][0] != ' '
&&chessbox[row][0] == chessbox[row][1]
&& chessbox[row][0] == chessbox[row][2]){
return chessbox[row][0];
}
}
// Column
for (int col = 0; col < COL; col++){
if (chessbox[0][col] != ' '
&&chessbox[0][col] == chessbox[1][col]
&& chessbox[0][col] == chessbox[2][col]){
return chessbox[0][col];
}
}
// Diagonals
if (chessbox[0][0] != ' '
&&chessbox[0][0] == chessbox[1][1]
&& chessbox[0][0] == chessbox[2][2]){
return chessbox[0][0];
}
if (chessbox[2][0] != ' '
&&chessbox[2][0] == chessbox[1][1]
&& chessbox[2][0] == chessbox[0][2]){
return chessbox[2][0];
}
// draw
if (isFullChess(chessbox)){
return 'a';
}
return 0;
}
// Start a game
void game(){
char chessbox[ROW][COL] = {
0 };
initChess(chessbox);
printf(" The game begins !\n");
printChess(chessbox);
while (1){
// The player is down
playerMove(chessbox);
// Print chess and cards
printChess(chessbox);
// Judge the outcome
if (isWinner(chessbox) == 'x'){
printf(" Congratulations on winning !\n");
break;
}
if (isWinner(chessbox) == 'a'){
printf(" You are on the same level as the computer !\n");
break;
}
// Computer move later
computerMove(chessbox);
// Print chess and cards
printChess(chessbox);
// Judge the outcome
if (isWinner(chessbox) == 'o'){
printf(" unfortunately , You lost !\n");
break;
}
if (isWinner(chessbox) == 'a'){
printf(" You are on the same level as the computer !\n");
break;
}
}
}
int menu(){
printf("===============\n");
printf("1. Start the game \n");
printf("0. End the game \n");
printf("===============\n");
int choice;
printf(" Please enter your choice :");
scanf("%d", &choice);
return choice;
}
int main()
{
ile (1){
int choice=menu();
if (choice == 1){
game();
continue;
}
else if (choice == 0){
break;
}else{
printf(" Incorrect input , Please re-enter !\n");
continue;
}
}
system("pause");
return 0;
}
Running results :
版权声明
本文为[osc_rmqoxylv]所创,转载请带上原文链接,感谢
边栏推荐
- SEO见风使舵,是对还是错?
- JVM学习(四)-垃圾回收器和内存分配
- 零基础IM开发入门(四):什么是IM系统的消息时序一致性?
- Adobe Experience Design /Xd 2020软件安装包(附安装教程)
- Navigation component of Android architecture (2)
- Well, the four ways to query the maximum value of sliding window are good
- Efficient Estimation of Word Representations in Vector Space 论文笔记
- JVM learning (5) - execution subsystem
- inet_ Pton () and INET_ Detailed explanation of ntop() function
- JVM learning (6) - memory model and thread
猜你喜欢
What really drags you down is sunk costs
外贸自建网站域名的选择— Namesilo 域名购买
PAT_甲级_1074 Reversing Linked List
接口测试如何在post请求中传递文件
CSP-J/S 2020考前注意事项
走进京东 | 中国空间技术研究院青年创新联盟成员莅临参观京东总部
Adobe Experience Design /Xd 2020软件安装包(附安装教程)
深圳C1考证历程
Download Netease cloud music 10W + music library with Python
Interface tests how to pass files in post requests
随机推荐
Understanding runloop in OC
Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive
JS判断对象类型方法_typeof怎么用_instanceof怎么用_constructor怎么用_Object.prototype.toString()怎么用
The history of C1 research in Shenzhen
What are the implementations of distributed locks?
inet_pton()和inet_ntop()函数详解
The choice of domain name of foreign trade self built website
10款必装软件,让Windows使用效率飞起!
iPhone“连到系统上的设备没有发挥作用”原因分析及解决方法 20200105
跟我一起学.NetCore之EF Core 实战入门,一看就会
导师制Unity网课 双十一优惠报名进行中
从汇编的角度看pdb文件
服务应用 ClockService安卓实现闹钟
Clock service Android implementation of alarm clock
03.优先链接模型
【分布式】分布式锁都有哪些实现方案?
通配符SSL证书应该去哪里注册申请
Reduce of Flink
FGC online service troubleshooting, this is enough!
EFF 认为 RIAA 正在“滥用 DMCA”来关闭 YouTube-DL