当前位置:网站首页>C language games - three chess
C language games - three chess
2022-07-06 20:32:00 【farewell12345】
The game passes 3 Source file implementation
1. The header file game.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3 // The number of rows on the chessboard
#define COL 3 // The number of rows on the chessboard
// Initialize chessboard
void Initboard(char board[ROW][COL], int row, int col);
// Print chessboard
void Displayboard(char board[ROW][COL], int row, int col);
// Players play chess
void PlayerMove(char board[ROW][COL], int row, int col);
// The computer plays chess
void ComputerMove(char board[ROW][COL], int row, int col);
// Judgement of winning or losing
char IsWin(char board[ROW][COL], int row, int col);
2. The game logic test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("**************************\n");
printf("******** 1. Sanzi *****\n");
printf("******** 0. sign out *****\n");
printf("**************************\n");
}
void game()
{
char ret = 0;// Accept the state of winning or losing the game
char board[ROW][COL];
// Initialize chessboard
Initboard(board, ROW, COL);
// Print chessboard
Displayboard(board, ROW, COL);
while (1)
{
// Players play chess
PlayerMove(board, ROW, COL);
Displayboard(board, ROW, COL);
// Judgement of winning or losing
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
// The computer plays chess
ComputerMove(board, ROW, COL);
Displayboard(board, ROW, COL);
// Judgement of winning or losing
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf(" Players win \n");
}
else if (ret == '#')
{
printf(" Computer victory \n");
}
else
{
printf(" It ends in a draw \n");
}
Displayboard(board, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
// Run the three piece chess game
game();
break;
case 0:
printf(" Exit procedure \n");
break;
default:
printf(" Input error , Please re-enter \n");
break;
}
} while (input);
return 0;
}
3. The subject of the game game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
// Initialize chessboard
void Initboard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i; i < row; i++)
{
int j = 0;
for (j; j < col; j++)
{
board[i][j] = ' ';
}
}
}
// Print chessboard
void Displayboard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i; i < row; i++)
{
int j = 0;
for (j; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
if (i < row - 1)
{
j = 0;
for (j; j < col; j++)
{
printf("---");
if (j < col - 1)
{
printf("|");
}
}
printf("\n");
}
}
}
// Players play chess
void PlayerMove(char board[ROW][COL], int row, int col)
{
printf(" Players go :\n");
printf(" Please enter the coordinates of playing chess :");
int i = 0;
int j = 0;
// Judge the validity of input coordinates
while(1)
{
scanf("%d %d", &i, &j);
if (board[i - 1][j - 1] != ' ')
{
printf(" Illegal input coordinates , Please re-enter \n");
}
else
{
break;
}
}
// Modify the value of the corresponding position in the array
board[i - 1][j - 1] = '*';
}
// The computer plays chess
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf(" Computer go :\n");
while (1)
{
int i = rand() % row;
int j = rand() % col;
if (board[i][j] == ' ')
{
board[i][j] = '#';
break;
}
}
}
// Judgement of winning or losing
char IsWin(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i; i < row; i++)
{
// There is... In the line 3 Same
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
{
return board[i][1];
}
}
int j = 0;
for (j; j < col; j++)
{
// There are... In one column 3 Same
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
{
return board[1][j];
}
}
// Diagonal yes 3 Same
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
{
return board[1][1];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
{
return board[1][1];
}
// The chessboard is full of draws
i = 0;
for (i; i < row; i++)
{
j = 0;
for (j; j < col; j++)
{
if (board[i][j] == ' ')
{
return 'C';
}
}
}
return 'Q';
}
边栏推荐
- Rhcsa Road
- Leetcode question 448 Find all missing numbers in the array
- Function optimization and arrow function of ES6
- [weekly pit] calculate the sum of primes within 100 + [answer] output triangle
- 枚举根据参数获取值
- Appx code signing Guide
- Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
- Recyclerview GridLayout bisects the middle blank area
- Detailed introduction of distributed pressure measurement system VIII: basic introduction of akka actor model
- 持续测试(CT)实战经验分享
猜你喜欢
5. 无线体内纳米网:十大“可行吗?”问题
Maximum likelihood estimation and cross entropy loss
Tencent T4 architect, Android interview Foundation
22-07-05 upload of qiniu cloud storage pictures and user avatars
Ideas and methods of system and application monitoring
Tencent architects first, 2022 Android interview written examination summary
Error analysis ~csdn rebound shell error
Rhcsa Road
Value of APS application in food industry
Boder radius has four values, and boder radius exceeds four values
随机推荐
OLED屏幕的使用
【GET-4】
Wechat applet common collection
2022 nurse (primary) examination questions and new nurse (primary) examination questions
2022 refrigeration and air conditioning equipment installation and repair examination contents and new version of refrigeration and air conditioning equipment installation and repair examination quest
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
APS taps home appliance industry into new growth points
使用ssh连接被拒
Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle
Discussion on beegfs high availability mode
5. Wireless in vivo nano network: top ten "feasible?" problem
Enumeration gets values based on parameters
电子游戏的核心原理
SSH connection denied
[cloud lesson] EI lesson 47 Mrs offline data analysis - processing OBS data through Flink
[weekly pit] information encryption + [answer] positive integer factorization prime factor
01 基础入门-概念名词
BeagleBoneBlack 上手记
【云原生与5G】微服务加持5G核心网
In line elements are transformed into block level elements, and display transformation and implicit transformation