Preface: After learning the array knowledge of C language, we can write some small games. Let's learn how to implement the game of backgammon in C language.
1. Game menu
The game menu should include functions such as playing the game and exiting the game. Made into a cycle for easy operation. Not much to say about the code:
void menu()
{
printf("******************************\n");
printf("**********1.玩游戏************\n");
printf("**********0.退出游戏**********\n");
printf("******************************\n");
}
do
{
menu();
printf("请输入:>\n");
scanf("%d",&input);
switch(input)
{
case 1:
game(a,3,3);
break;
case 0:
printf("退出成功\n");
break;
default:
printf("输入错误\n");
break;
}
}while(input);
2. Game implementation
Board initialization:
void Iint(char a[ROW][CLO],int row,int clo)
{
for(int i = 0; i < row; i++){
for(int j = 0; j < clo; j++){
a[i][j] = ' ';
}
}
}
Print the chessboard:
void print(char a[ROW][CLO],int row,int clo)
{
int i,j,k;
for(i = 0; i < row; i++)
{
for(j = 0; j < clo; j++)
{
printf(" %c ",a[i][j]);
if(j<clo-1){
printf("|");
}
}
printf("\n");
if(i<row-1){
for(k = 0; k < clo; k++){
printf("----");
}
printf("\n");
}
}
}
Printing effect:
Computer chess:
We use random numbers to simulate computer chess
void computer(char a[ROW][CLO],int row,int clo)
{
while(1)
{
int x = rand()%3;
int y = rand()%3;
if(a[x][y]==' '){
a[x][y] = '#';
break;
}
}
}
Judge win or lose:
int isfull(char a[ROW][CLO],int row,int clo)
{
for(int i = 0; i < row; i++){
for(int j = 0; j < clo;j++){
if(a[i][j]==' '){
return 0;
}
}
}
return 1;
}//是否棋盘已满
char iswin(char a[ROW][CLO],int row,int clo)
{
int i,j;
for(i = 0; i < row; i++){
if(a[i][0]=='*'&&a[i][1]=='*'&&a[i][2]=='*'){
return '*';
}
}
for(i = 0; i < clo; i++){
if(a[0][i]=='*'&&a[1][i]=='*'&&a[2][i]=='*'){
return '*';
}
}
if((a[0][0]=='#'&&a[1][1]=='#'&&a[2][2]=='#')||(a[0][2]=='#'&&a[1][1]=='#'&&a[2][0]=='#')){
return '#';
}
for(i = 0; i < row; i++){
if(a[i][0]=='#'&&a[i][1]=='#'&&a[i][2]=='#'){
return '#';
}
}
for(i = 0; i < clo; i++){
if(a[0][i]=='#'&&a[1][i]=='#'&&a[2][i]=='#'){
return '#';
}
}
if((a[0][0]=='*'&&a[1][1]=='*'&&a[2][2]=='*')||(a[0][2]=='*'&&a[1][1]=='*'&&a[2][0]=='*')){
return '*';
}
if(isfull(a,3,3)){
return 'q';//平局
}
return 'c';//继续游戏
}
Integration:
void game(char a[ROW][CLO],int row,int clo)
{
int x,y;
Iint(a,3,3);
char ret = 0;
while(1)
{
print(a,3,3);
printf("请输入坐标(空格隔开):>\n");
scanf("%d %d",&x,&y);
while(1){
if(a[x-1][y-1]==' '){
a[x-1][y-1] = '*';
break;
}else{
printf("非法输入请重新输入:>\n");
scanf("%d %d",&x,&y);
}
}
print(a,3,3);
ret = iswin(a,3,3);
if(ret!='c'){
break;
}
printf("电脑输入:\n");
computer(a,3,3);
ret = iswin(a,3,3);
if(ret!='c'){
break;
}
}
if(ret=='*'){
printf("玩家赢\n");
}else if(ret=='#'){
printf("电脑赢\n");
}else{
printf("平局\n");
}
}
source code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define CLO 3
void menu()
{
printf("******************************\n");
printf("**********1.玩游戏************\n");
printf("**********0.退出游戏**********\n");
printf("******************************\n");
}
void Iint(char a[ROW][CLO],int row,int clo)
{
for(int i = 0; i < row; i++){
for(int j = 0; j < clo; j++){
a[i][j] = ' ';
}
}
}
void print(char a[ROW][CLO],int row,int clo)
{
int i,j,k;
for(i = 0; i < row; i++)
{
for(j = 0; j < clo; j++)
{
printf(" %c ",a[i][j]);
if(j<clo-1){
printf("|");
}
}
printf("\n");
if(i<row-1){
for(k = 0; k < clo; k++){
printf("----");
}
printf("\n");
}
}
}
void computer(char a[ROW][CLO],int row,int clo)
{
while(1)
{
int x = rand()%3;
int y = rand()%3;
if(a[x][y]==' '){
a[x][y] = '#';
break;
}
}
}
int isfull(char a[ROW][CLO],int row,int clo)
{
for(int i = 0; i < row; i++){
for(int j = 0; j < clo;j++){
if(a[i][j]==' '){
return 0;
}
}
}
return 1;
}
char iswin(char a[ROW][CLO],int row,int clo)
{
int i,j;
for(i = 0; i < row; i++){
if(a[i][0]=='*'&&a[i][1]=='*'&&a[i][2]=='*'){
return '*';
}
}
for(i = 0; i < clo; i++){
if(a[0][i]=='*'&&a[1][i]=='*'&&a[2][i]=='*'){
return '*';
}
}
if((a[0][0]=='#'&&a[1][1]=='#'&&a[2][2]=='#')||(a[0][2]=='#'&&a[1][1]=='#'&&a[2][0]=='#')){
return '#';
}
for(i = 0; i < row; i++){
if(a[i][0]=='#'&&a[i][1]=='#'&&a[i][2]=='#'){
return '#';
}
}
for(i = 0; i < clo; i++){
if(a[0][i]=='#'&&a[1][i]=='#'&&a[2][i]=='#'){
return '#';
}
}
if((a[0][0]=='*'&&a[1][1]=='*'&&a[2][2]=='*')||(a[0][2]=='*'&&a[1][1]=='*'&&a[2][0]=='*')){
return '*';
}
if(isfull(a,3,3)){
return 'q';
}
return 'c';
}
void game(char a[ROW][CLO],int row,int clo)
{
int x,y;
Iint(a,3,3);
char ret = 0;
while(1)
{
print(a,3,3);
printf("请输入坐标(空格隔开):>\n");
scanf("%d %d",&x,&y);
while(1){
if(a[x-1][y-1]==' '){
a[x-1][y-1] = '*';
break;
}else{
printf("非法输入请重新输入:>\n");
scanf("%d %d",&x,&y);
}
}
print(a,3,3);
ret = iswin(a,3,3);
if(ret!='c'){
break;
}
printf("电脑输入:\n");
computer(a,3,3);
ret = iswin(a,3,3);
if(ret!='c'){
break;
}
}
if(ret=='*'){
printf("玩家赢\n");
}else if(ret=='#'){
printf("电脑赢\n");
}else{
printf("平局\n");
}
}
int main()
{
int input = 0;
char a[3][3];
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入:>\n");
scanf("%d",&input);
switch(input)
{
case 1:
game(a,3,3);
break;
case 0:
printf("退出成功\n");
break;
default:
printf("输入错误\n");
break;
}
}while(input);
return 0;
}
If the family members see this, please pay attention to it ^ _ ^ .