当前位置:网站首页>C language three chess games
C language three chess games
2022-06-11 07:31:00 【∞ big understand】
Gobang games
Beginners C Language , We can play some games , It can improve our understanding of c The understanding of the , It can also enhance our practical ability . Next, let's finish this little game together : Sanzi .
Sanzi chess must have a 3*3 The chessboard of , There must be pieces on this chessboard , Then the player takes a step , The computer takes one step , We still need to judge whether we win or lose at each step .
The above is the basic logic of Sanzi chess , Now let's do it step by step .
Initialize chessboard
When printing a chessboard , The pieces on the chessboard cannot be random , What we want is a clean chessboard , It's not finished yet . So first initialize the chessboard .
void chushihua(char borad[ROW][ROL], int row, int rol) { for (int i = 0; i < row; i++) { for (int j = 0; j < rol; j++) { borad[i][j] = ' '; } } }In this way, our chess pieces are completely empty .
Print chessboard
The chess pieces on the chessboard have been initialized , We need a function to print out the chessboard , Subsequent matches , Every step , You also need to print out the chessboard .
void daying(char borad[ROW][ROL],int row, int rol) { for (int i = 0; i < row; i++) { for (int j = 0; j < rol; j++) { printf(" %c ", borad[i][j]); if (j < rol - 1) printf("|"); } printf("\n"); if (i < row - 1) { for (int j = 0; j < rol; j++) { printf("---"); if (j < rol - 1) printf("|"); } } printf("\n"); } }Players go
The chessboard has , It's almost chess , So players take one step , Then print out the chessboard . We stipulate that the chess pieces that players take are ’#’.
void playermove(char borad[ROW][ROL], int row, int rol) { printf(" Please enter the coordinates , The coordinates are left blank \n"); int a = 0; int b = 0; again: scanf("%d%d", &a, &b); while (1) { if (a <= 3 && b <= 3) { if (borad[a - 1][b - 1] == ' ') { borad[a - 1][b - 1] = '*'; break; } else { printf(" The coordinates have been occupied , Please re-enter \n"); goto again; } } else { printf(" The coordinates are wrong , Please re-enter \n"); goto again; } } }Computer go
The computer is a random number , No more coordinates , So the code is as follows :
void computermove(char borad[ROW][ROL], int row, int rol) { int x = 0; int y = 0; printf(" Computer go :\n"); while (1) { x = rand() % row; y = rand() % rol; if (borad[x][y] == ' ') { borad[x][y] = '#'; break; } } }Judgement of winning or losing
The logic of judging whether to win or lose is , Three rows or three columns or three diagonals will be the winner .
If the above conditions are not met , And it's full , So it's a draw .
also , Just keep playing chess .
The code is as follows :
char iswin(char borad[ROW][ROL], int row, int rol) { // Judgment line for (int i = 0; i < row; i++) { if (borad[i][0] != ' '&&borad[i][0] == borad[i][1] && borad[i][1] == borad[i][2]) return borad[i][0]; } // Judgment column for (int i = 0; i < row; i++) { if (borad[0][i] != ' '&&borad[0][i] == borad[1][i] && borad[1][i] == borad[2][i]) return borad[0][i]; } // Judging diagonals if (borad[0][0] == borad[1][1] && borad[1][1] == borad[2][2] && borad[0][0] != ' ') return borad[0][0]; if (borad[0][2] == borad[1][1] && borad[1][1] == borad[2][0] && borad[0][2] != ' ') return borad[0][2]; // It ends in a draw if (isfull(borad,ROW,ROL)) return 'Q'; // continue return 'C'; }iswin Function returns ’C‘ Is that the game continues , The return is ’#‘ Computers win , return ’*‘ Game player wins , return ’Q‘ It's a draw . So you also need to have a variable that receives the return value ret.
if (ret == '*') { printf(" Game player wins \n"); } if (ret == '#') { printf(" Computers win \n"); } if (ret == 'Q') printf(" It ends in a draw \n");Judge whether you win or lose , This game has been basically completed .
The following is the code of the game :
The header file :
#pragma once #include<stdio.h> #define ROW 3 #define ROL 3 #include<stdlib.h> #include<time.h> void mune(); void chushihua(char borad[ROW][ROL], int row, int rol); void daying(char borad[ROW][ROL], int row, int rol); void game(); void playermove(char borad[ROW][ROL], int row, int rol); void computermove(char borad[ROW][ROL], int row, int rol); char iswin(char borad[ROW][ROL], int row, int rol);Sanziqi game code :
#include"game.h" void mune() { printf("*******************************************\n"); printf("****************** Sanzi *******************\n"); printf("****************** Please put in a coin :******************\n"); printf("****************** ******************\n"); printf("****************** ******************\n"); } void chushihua(char borad[ROW][ROL], int row, int rol) { for (int i = 0; i < row; i++) { for (int j = 0; j < rol; j++) { borad[i][j] = ' '; } } } void daying(char borad[ROW][ROL],int row, int rol) { for (int i = 0; i < row; i++) { for (int j = 0; j < rol; j++) { printf(" %c ", borad[i][j]); if (j < rol - 1) printf("|"); } printf("\n"); if (i < row - 1) { for (int j = 0; j < rol; j++) { printf("---"); if (j < rol - 1) printf("|"); } } printf("\n"); } } void playermove(char borad[ROW][ROL], int row, int rol) { printf(" Please enter the coordinates , The coordinates are left blank \n"); int a = 0; int b = 0; again: scanf("%d%d", &a, &b); while (1) { if (a <= 3 && b <= 3) { if (borad[a - 1][b - 1] == ' ') { borad[a - 1][b - 1] = '*'; break; } else { printf(" The coordinates have been occupied , Please re-enter \n"); goto again; } } else { printf(" The coordinates are wrong , Please re-enter \n"); goto again; } } } void computermove(char borad[ROW][ROL], int row, int rol) { int x = 0; int y = 0; printf(" Computer go :\n"); while (1) { x = rand() % row; y = rand() % rol; if (borad[x][y] == ' ') { borad[x][y] = '#'; break; } } } int isfull(char borad[ROW][ROL], int row, int rol) { for (int i = 0; i < row; i++) { for (int j = 0; j < rol; j++) { if (borad[i][j] == ' ') return 0; } } return 1; } char iswin(char borad[ROW][ROL], int row, int rol) { // Judgment line for (int i = 0; i < row; i++) { if (borad[i][0] != ' '&&borad[i][0] == borad[i][1] && borad[i][1] == borad[i][2]) return borad[i][0]; } // Judgment column for (int i = 0; i < row; i++) { if (borad[0][i] != ' '&&borad[0][i] == borad[1][i] && borad[1][i] == borad[2][i]) return borad[0][i]; } // Judging diagonals if (borad[0][0] == borad[1][1] && borad[1][1] == borad[2][2] && borad[0][0] != ' ') return borad[0][0]; if (borad[0][2] == borad[1][1] && borad[1][1] == borad[2][0] && borad[0][2] != ' ') return borad[0][2]; // It ends in a draw if (isfull(borad,ROW,ROL)) return 'Q'; // continue return 'C'; } void game() { char ret = 0; char borad[ROW][ROL]; // Initialize chessboard chushihua(borad, ROW, ROL); // Print chessboard daying(borad, ROW, ROL); while (1) { // Players play chess playermove(borad, ROW, ROL); daying(borad, ROW, ROL); // Judgement of winning or losing ret=iswin(borad,ROW,ROL); if (ret != 'C') { break; } // The computer plays chess computermove(borad, ROW, ROL); daying(borad, ROW, ROL); // Judgement of winning or losing ret=iswin(borad,ROW,ROL); if (ret != 'C') { break; } } if (ret == '*') { printf(" Game player wins \n"); } if (ret == '#') { printf(" Computers win \n"); } if (ret == 'Q') printf(" It ends in a draw \n"); }Total program code :
#include"game.h" int main() { srand((unsigned int)time(NULL)); int n = 0; mune(); again: scanf("%d", &n); while (n) { game(); n--; } printf(" Whether the game continues ? Please put in a coin \n"); goto again; return 0; }That's what this issue is about .
边栏推荐
- 黑群晖DSM7.0.1物理机安装教程
- Multi thread review summary parsing volatile keyword
- Pointer to a two-dimensional array
- QT table display data
- 【CF #277.5 (Div. 2)】B. BerSU Ball
- [Oracle database] mammy tutorial day02 use of database management tool sqlplus
- C language to write a calculator calculation logic
- Phi and phi (Mobius inversion + formula)
- Typora set markdown syntax inline mode
- Nosqlzoo question brushing-1
猜你喜欢

2022低压电工考题及在线模拟考试

Decimal to binary

Gobang interface of mobile console (C language)
![[STL source code analysis] summary notes (5): a good helper for understanding iterators --list](/img/a7/c54bfb6a03c04e4ffeafdfcf8cedc2.jpg)
[STL source code analysis] summary notes (5): a good helper for understanding iterators --list

2、 User login and registration

Outer margin collapse

2022 melting welding and thermal cutting exam exercises and answers

JVM Learning record (7) - - class Loading Process and parent delegation Model

1、 Sqlserver2008 installation (with password), database creation, C form project test

Mistakes in Niuke JS exercise
随机推荐
[Oracle database] mammy tutorial day03 Sorting Query
2022年熔化焊接与热切割考试练习题及答案
多线程复习总结之解析Volatile关键字
【AtCoder2376】Black and White Tree(博弈)
[Oracle database] mammy tutorial day02 use of database management tool sqlplus
C language to write a calculator calculation logic
Regular Expression Matching
Classification of MNIST datasets with keras
Typora set markdown syntax inline mode
R language Parallel Computing practice tutorial
QT picture adaptive display control
Djikstra solves the shortest circuit with negative weight
Installation de SQL Server 2008 (avec mot de passe), création d'une base de données, test de projet de formulaire C
MS office level II wrong question record [9]
What is the difference between gaussdb for redis and redis?
The maximum number of divisors of numbers in the int range is 1536
Ffmpe a small demo to understand 80% of common APIs
【AtCoder2387】+/- Rectangle
2022低压电工考题及在线模拟考试
JVM Learning record (7) - - class Loading Process and parent delegation Model