三子棋

三子棋
This commit is contained in:
kohler 2023-08-07 11:00:03 +00:00
parent c3a447ccbd
commit 3e943319b4
11 changed files with 362 additions and 0 deletions

32
三子棋/Makefile.win Normal file
View File

@ -0,0 +1,32 @@
# Project: Èý×ÓÆå
# Makefile created by Dev-C++ 6.7.5
CPP = g++.exe -D__DEBUG__
CC = gcc.exe -D__DEBUG__
WINDRES = windres.exe
OBJ = test.o game.o
LINKOBJ = test.o game.o
LIBS = -L"d:/files/software/dev-cpp/mingw32/lib/gcc/mingw32/9.2.0/" -L"d:/files/software/dev-cpp/mingw32/lib/gcc/" -L"d:/files/software/dev-cpp/mingw32/mingw32/lib/" -L"d:/files/software/dev-cpp/mingw32/lib/" -L"D:/files/software/Dev-Cpp/MinGW32/lib" -L"D:/files/software/Dev-Cpp/MinGW32/mingw32/lib" -g3 -static
INCS = -I"d:/files/software/dev-cpp/mingw32/lib/gcc/mingw32/9.2.0/include" -I"d:/files/software/dev-cpp/mingw32/include" -I"d:/files/software/dev-cpp/mingw32/lib/gcc/mingw32/9.2.0/include-fixed"
CXXINCS = -I"d:/files/software/dev-cpp/mingw32/lib/gcc/mingw32/9.2.0/include/c++" -I"d:/files/software/dev-cpp/mingw32/lib/gcc/mingw32/9.2.0/include/c++/mingw32" -I"d:/files/software/dev-cpp/mingw32/lib/gcc/mingw32/9.2.0/include/c++/backward" -I"d:/files/software/dev-cpp/mingw32/lib/gcc/mingw32/9.2.0/include" -I"d:/files/software/dev-cpp/mingw32/include" -I"d:/files/software/dev-cpp/mingw32/lib/gcc/mingw32/9.2.0/include-fixed"
BIN = Èý×ÓÆå.exe
CXXFLAGS = $(CXXINCS) -Wall -Wextra -g3
ENCODINGS = -finput-charset=utf-8 -fexec-charset=gbk
CFLAGS = $(INCS) -Wall -Wextra -g3
RM = del /q /f
.PHONY: all all-before all-after clean clean-custom
all: all-before $(BIN) all-after
clean: clean-custom
${RM} $(OBJ) $(BIN)
$(BIN): $(OBJ)
$(CC) $(LINKOBJ) -o "$(BIN)" $(LIBS)
test.o: test.c game.h
$(CC) -c test.c -o test.o $(CFLAGS)
game.o: game.c game.h
$(CC) -c game.c -o game.o $(CFLAGS)

116
三子棋/game.c Normal file
View File

@ -0,0 +1,116 @@
#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++){
printf(" %c ",board[i][j]);
if(j < col -1)
printf("|");
}
printf("\n");
if(i < row -1){
int j = 0;
for(j = 0;j< col;j++){
printf("---");
if(j < col -1)
printf("|");
}
printf("\n");
}
}
}
//玩家走
void playMove(char board[][COL],int row,int col){
int x = 0;
int y = 0;
printf("玩家走>\n");
while(1){
printf("请输入下棋的坐标:>\n");
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("坐标被占用,请重新输入\n");
}
}else{
printf("坐标非法,请重新输入\n");
}
}
}
//电脑走
void computerMove(char board[][COL],int row,int col){
printf("电脑走\n");
while(1){
int x = rand() % ROW;
int y = rand() % COL;
//判断位置是否被占用
if(board[x][y] == ' '){
board[x][y] = '#';
break;
}
}
}
//判断棋盘是否满了
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;
}
}
}
return 1;//棋盘满了
}
//判断游戏结果
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];
}
//判断平局
//棋盘满返回1不满返回0
int ret = isFull(board,row,col);
if(ret == 1){
return 'Q';
}
//继续
return 'C';
}

29
三子棋/game.h Normal file
View File

@ -0,0 +1,29 @@
//头文件
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//符号的定义
#define ROW 3
#define COL 3
//函数的声明
//初始化棋盘
void initBiard(char board[ROW][COL],int row,int col);
//打印棋盘的函数
void displayBoard(char board[ROW][COL],int row,int col);
//玩家下棋
void playMove(char board[ROW][COL],int row,int col);
//电脑下棋
void computerMove(char board[][COL],int row,int col);
//判断游戏结果
//1. 玩家赢了 -- *
//2. 电脑赢了 -- #
//3. 平局 -- Q
//4. 游戏继续 -- C
char isWin(char board[ROW][COL],int row,int col);

BIN
三子棋/game.o Normal file

Binary file not shown.

74
三子棋/test.c Normal file
View File

@ -0,0 +1,74 @@
//test.c 测试游戏的逻辑
//game.h关于游戏相关的函数声明符号声明头文件的包含
//game.c 游戏相关函数的实现
#include "game.h"
void menu(){
printf("*************************\n");
printf("**** 1.play **********\n");
printf("**** 0.exit **********\n");
printf("*************************\n");
}
void game(){
//存储数据---二维数组
char board[ROW][COL];
//初始化棋盘 -- 初始化空格
initBoard(board,ROW,COL);
//打印棋盘 本质是打印数组内容
displayBoard(board,ROW,COL);
char ret = 0;//接收游戏状态
while(1){
//玩家走
playMove(board,ROW,COL);
displayBoard(board,ROW,COL);
//判断玩家是否赢得游戏
ret = isWin(board,ROW,COL);
if(ret != 'C'){
break;
}
//电脑走
computerMove(board,ROW,COL);
displayBoard(board,ROW,COL);
//判断电脑是否赢得游戏
ret = isWin(board,ROW,COL);
if(ret != 'C'){
break;
}
}
if(ret == '*'){
printf("玩家赢了\n");
}else if(ret == '#'){
printf("电脑赢了\n");
}else{
printf("平局\n");
}
displayBoard(board,ROW,COL);
}
int main(){
int input = 0;
srand((unsigned int)time(NULL));
do{
menu();
printf("请选择:>");
scanf("%d",&input);
switch(input){
case 1:
game();
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("选择错误,重新选择!\n");
break;
}
}while(input);
return 0;
}

BIN
三子棋/test.o Normal file

Binary file not shown.

93
三子棋/三子棋.dev Normal file
View File

@ -0,0 +1,93 @@
[Project]
FileName=三子棋.dev
Name=三子棋
UnitCount=3
Type=1
Ver=2
ObjFiles=
Includes=
Libs=
PrivateResource=
ResourceIncludes=
MakeIncludes=
Compiler=
CppCompiler=
Linker=
IsCpp=0
Icon=
ExeOutput=
ObjectOutput=
LogOutput=
LogOutputEnabled=0
OverrideOutput=0
OverrideOutputName=
HostApplication=
UseCustomMakefile=0
CustomMakefile=
UsePrecompiledHeader=0
PrecompiledHeader=
CommandLine=
Folders=头文件,源文件
IncludeVersionInfo=0
SupportXPThemes=0
CompilerSet=1
CompilerSettings=0000000000110000000001000
StaticLink=1
AddCharset=1
UseUTF8=0
[Unit1]
FileName=test.c
Folder=源文件
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=
DetectEncoding=0
Encoding=0
CompileCpp=0
[VersionInfo]
Major=1
Minor=0
Release=0
Build=0
LanguageID=1033
CharsetID=1252
CompanyName=
FileVersion=
FileDescription=Developed using the Dev-C++ IDE
InternalName=
LegalCopyright=
LegalTrademarks=
OriginalFilename=
ProductName=
ProductVersion=
AutoIncBuildNr=0
SyncProduct=1
[Unit2]
FileName=game.c
CompileCpp=0
Folder=源文件
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=
DetectEncoding=0
Encoding=0
[Unit3]
FileName=game.h
CompileCpp=0
Folder=头文件
Compile=1
Link=1
Priority=1000
OverrideBuildCmd=0
BuildCmd=
DetectEncoding=0
Encoding=0

BIN
三子棋/三子棋.exe Normal file

Binary file not shown.

View File

@ -0,0 +1,18 @@
[Editors]
Order=0,2,1
Focused=0
[Editor_0]
CursorCol=2
CursorRow=44
TopLine=1
LeftChar=1
[Editor_1]
CursorCol=15
CursorRow=110
TopLine=62
LeftChar=1
[Editor_2]
CursorCol=14
CursorRow=7
TopLine=2
LeftChar=1

BIN
三子棋/未命名2 Normal file

Binary file not shown.

BIN
三子棋/未命名3.o Normal file

Binary file not shown.