当前位置:网站首页>Game three piece chess
Game three piece chess
2022-07-24 07:29:00 【Beta flying a plane】
Three chess game
1. Write the test function in the main function .
2. In test function , Edit the game menu , Whether to play games , type 1, Enter the three chess game , type 0 Quit the game , Type other , Re enter the correct instruction .
3. Sanziqi game :1. initialization 2. Print chessboard 3. Players play chess , And decide whether to win or lose ; The computer plays chess , Judgement of winning or losing .
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include "game.h"
void game()
{
char ret;
char board[ROW][COL] = {
0 };
// Initialize array
InitBoard(board, ROW, COL);
// Print chessboard
DisplayBoard(board, ROW, COL);
while (1)
{
// Players play chess
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
// Winning or losing
ret=IsWin(board, ROW, COL);
if (ret != 'c')
{
break;
}
// The computer plays chess
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret=IsWin(board, ROW, COL);
if (ret != 'c')
{
break;
}
}
if (ret == '#')
{
printf(" Game player wins \n");
}
else if (ret == '*')
{
printf(" Computers win \n");
}
else
{
printf(" It ends in a draw \n");
}
}
void menu()
{
printf("********************\n");
printf("***1.play 0.exit***\n");
printf("********************\n");
}
void test()
{
int input=0;
srand((unsigned int)time(NULL));
do
{
menu(); // menu
printf(" Input ->");
scanf("%d", &input);
switch (input)
{
case 1:
game();//“ Three chess game ”
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Please input the correct command \n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
game.h
#define ROW 3
#define COL 3
#include <stdlib.h>
#include <time.h>
void InitBoard(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
char IsWin(char board[ROW][COL], int row, int col);
// Tell us four game states
// Game player wins ->'#'
// Computers win ->'*'
// It ends in a draw ->'Q'
// continue ->'c'
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include "game.h"
int IsFull(char board[ROW][COL], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;
}
}
}
return 1;
}
void InitBoard(char board[ROW][COL], int row, int col)
{
int i, j;
for(i=0;i<row;i++)
for (j = 0; j < col; j++)
{
board[i][j] = ' ';
}
}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i,j;
for (i = 0; i < row; i++)
{
//1. Print a line of data
for (j = 0; j <col; j++)
{
printf(" % c ", board[i][j]);
if (j < col- 1)
printf("|");
}
printf("\n");
//2. Print line breaks
if (i < row - 1)
{
for (j = 0; j <col; j++)
{
printf("---");
if (j < col - 1)
printf("|");
}
}
printf("\n");
}
}
void PlayerMove(char board[ROW][COL], int row, int col)
{
int x, y;
printf(" The player \n");
while (1)
{
printf(" Please enter the coordinates you want to enter :->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '#';
break;
}
else
{
printf(" The coordinates are occupied \n");
}
}
else
{
printf(" Out of bounds , Please re-enter \n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x, y;
printf(" The computer ->\n");
while (1)
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '*';
break;
}
}
}
char IsWin(char board[ROW][COL], int row, int col)
{
int i, j;
// Three horizontal lines
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
{
return board[i][0];
}
}
// Vertical three column
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
// Diagonally
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
return board[0][0];
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ')
return board[0][2];
// It ends in a draw
/*for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { if (board[i][j] == ' ') { return 'c'; } } } return 'Q'; }*/
if (1 == IsFull(board, ROW, COL))
{
return 'Q';
}
return 'c';
}
result 

边栏推荐
- C语言文件操作
- [steering wheel] the super favorite idea efficiency artifact save actions is uninstalled
- [leetcode simple] 20. Valid brackets stack
- Stm32h750vbt6 drives programmable gain amplifier module pga113 -- Hal Library Based on cubemx
- oracle中有A,B连个表,这两个表需要第三个表C关联,那怎么将A表中的字段MJ1更新为B表中MJ2的值
- csdn,是时候说再见!
- OpenCascade笔记:gp包
- numpy.cumsum
- 【C语言入门】ZZULIOJ 1011-1015
- Part I - Fundamentals of C language_ 11. Comprehensive project - greedy snake
猜你喜欢

php 转义字符串

Riotboard development board series notes (IX) -- buildreoot porting matchbox

Bookkeeping app: xiaoha bookkeeping 2 - production of registration page

Simple installation of sqli Labs

Feature Selective Anchor-Free Module for Single-Shot Object Detection

MITRE ATT&CK超详细学习笔记-02(大量案例)

Using depth and normal textures in unity

Part II - C language improvement_ 4. Secondary pointer

Jackson parsing JSON detailed tutorial

Injectfix principle learning (to realize the heat of repair addition)
随机推荐
win10声音图标有个没有声音
What kind of mode can make platform users self-help fission- Chain 2+1
Blockbuster live broadcast | orb-slam3 series code explanation map points (topic 2)
Feature Selective Anchor-Free Module for Single-Shot Object Detection
numpy.inf
二维平面多段线Y轴最短距离
mysql查询当前节点的所有父级
OpenCascade笔记:gp包
Gimp custom screenshot
服务漏洞&FTP&RDP&SSH&rsync
Filter filter
C语言文件操作
MITRE ATT&CK超详细学习笔记-01(背景,术语,案例)
oracle中有A,B连个表,这两个表需要第三个表C关联,那怎么将A表中的字段MJ1更新为B表中MJ2的值
深度学习二三事-回顾那些经典卷积神经网络
django.db.utils. OperationalError: (2002, “Can‘t connect to local MySQL server through socket ‘/var/r
InjectFix原理学习(实现修复加法的热更)
MySQL语句
China trichlorosilane Market Forecast and Strategic Research Report (2022 Edition)
Basic syntax of MySQL DDL and DML and DQL