当前位置:网站首页>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
边栏推荐
- MySQL notes 5_ Operation data
- 【2021年新书推荐】Practical Node-RED Programming
- 扫雷小游戏
- MySQL数据库安装与配置详解
- 树莓派:双色LED灯实验
- [recommendation of new books in 2021] practical IOT hacking
- HandlerThread原理和实际应用
- What did you do during the internship
- MySQL5. 7 insert Chinese data and report an error: ` incorrect string value: '\ xb8 \ XDF \ AE \ xf9 \ X80 at row 1`
- How to standardize multidimensional matrix (based on numpy)
猜你喜欢
取消远程依赖,用本地依赖
Android清除应用缓存
【2021年新书推荐】Learn WinUI 3.0
Miscellaneous learning
Cause: dx. jar is missing
C#新大陆物联网云平台的连接(简易理解版)
基于BottomNavigationView实现底部导航栏
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide
[2021 book recommendation] red hat rhcsa 8 cert Guide: ex200
随机推荐
What did you do during the internship
利用官方torch版GCN训练并测试cora数据集
MySQL数据库安装与配置详解
【动态规划】三角形最小路径和
[Andorid] 通过JNI实现kernel与app进行spi通讯
【2021年新书推荐】Effortless App Development with Oracle Visual Builder
Bottom navigation bar based on bottomnavigationview
“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
MySQL5.7插入中文数据,报错:`Incorrect string value: ‘\xB8\xDF\xAE\xF9\x80 at row 1`
[2021 book recommendation] learn winui 3.0
ArcGIS License Server Administrator 无法启动解决方法
Android room database quick start
【动态规划】不同的二叉搜索树
MarkDown基础语法笔记
组件化学习(2)Arouter原理学习
ProcessBuilder工具类
BottomSheetDialogFragment 与 ListView RecyclerView ScrollView 滑动冲突问题
MySQL笔记5_操作数据
Pytorch trains the basic process of a network in five steps
Easyui combobox 判断输入项是否存在于下拉列表中