当前位置:网站首页>Gobang games
Gobang games
2022-04-23 07:19:00 【Amyniez】
First, in the VC2013 Create project on , Create separate :test.c、game.c、game.h Three sub files .
game.h:
#define ROW 3
#define COL 3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Statement
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);
// Tell us the four states of the game : Game player wins 、 Computers win 、 It ends in a draw 、 Keep playing
char IsWin(char board[ROW][COL], int row, int col);
int IsFull(char board[ROW][COL], int row, int col);
test.c:
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
printf("#######################################\n");
printf("##########1.play 0.exit##########\n");
printf("#######################################\n");
}
void game()
{
char board[ROW][COL] = {
0 };// The array stores the information of the chessboard
char ret = 0;
// Initialize chessboard
InitBoard(board, ROW, COL);
// Print chessboard and output
DisplayBoard(board, ROW, COL);
// Start playing chess
while (1)
{
// Players play chess ( First of all )
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
// Judge whether the player wins
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
// The computer plays chess ( Later stage )
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C')
{
break;
}
}
if (ret == '*')
{
printf(" Players win \n");
}
else if (ret == '#')
{
printf(" The computer wins \n");
}
else
{
printf(" It ends in a draw \n");
}
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf(" Please select :");
scanf("%d ", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Game over \n");
break;
default:
printf(" Wrong choice , Please re-enter !\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
game.c:
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void InitBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
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 = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
//1. Print one line of data
printf(" %c ", board[i][j]);
if (j < col - 1)
{
printf(" | ");
}
}
printf("\n");
//2. Print split lines
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 = 0;
int y = 0;
printf(" Players go :>\n");
while (1)
{
printf(" Please enter the coordinates you want to enter :>\n");
scanf("%d %d", &x, &y);
// Judge x,y The validity of coordinates
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(" Illegal coordinates , Please re-enter !\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf(" Computer go :\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 = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
return board[i][1];
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
return board[1][i];
}
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];
}
if (1 == IsFull(board, ROW, COL))
{
return 'Q';
}
return 'C';
}
// return 1 The chessboard is full
// return 0 The chessboard is not full
int IsFull(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ')
{
return 0;// Not full
}
}
}
return 1;
}
版权声明
本文为[Amyniez]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610096497.html
边栏推荐
- Component learning (2) arouter principle learning
- 免费使用OriginPro学习版
- Data class of kotlin journey
- MySQL数据库安装与配置详解
- 最简单完整的libwebsockets的例子
- [2021 book recommendation] kubernetes in production best practices
- How to standardize multidimensional matrix (based on numpy)
- [recommendation of new books in 2021] enterprise application development with C 9 and NET 5
- winform滚动条美化
- MySQL5. 7 insert Chinese data and report an error: ` incorrect string value: '\ xb8 \ XDF \ AE \ xf9 \ X80 at row 1`
猜你喜欢
随机推荐
MySQL notes 4_ Primary key auto_increment
[2021 book recommendation] effortless app development with Oracle visual builder
Thanos.sh灭霸脚本,轻松随机删除系统一半的文件
MySQL数据库安装与配置详解
ffmpeg常用命令
三子棋小游戏
Some common data type conversion methods in pytorch are similar to list and NP Conversion method of ndarray
WebRTC ICE candidate里面的raddr和rport表示什么?
【2021年新书推荐】Enterprise Application Development with C# 9 and .NET 5
杂七杂八的学习
Cause: dx. jar is missing
The Cora dataset was trained and tested using the official torch GCN
三种实现ImageView以自身中心为原点旋转的方法
1.2 初试PyTorch神经网络
红外传感器控制开关
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
face_recognition人脸检测
C connection of new world Internet of things cloud platform (simple understanding version)
Component based learning (1) idea and Implementation
[dynamic programming] triangle minimum path sum