简易五子棋 V1.1.0
main.cpp
#include "fivechess.cpp" int main()
{
fivechess a;
a.RunGame();
getchar();
return 0;
}
fivechess.h
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <windows.h> using namespace std;
const int MAX_SIZE=15; struct point
{
int cursor_x,cursor_y;
};
void gotoxy(int x,int y)//位置函数
{
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
} class fivechess
{
public:
void RunGame();
private:
void init();
void print_chess();
string getstyle(int ,int );
bool move_cursor();
int who_win(int x,int y); // 返回 0代表没有人胜利,如果为1 则黑子胜利,2则为白棋;
void change_chessboard();
void change_chess();
void show_winnwer();
int chessboard[MAX_SIZE][MAX_SIZE];
int count;
int winner;
point now,pre;
char opt;
};
fivechess.cpp
#include "fivechess.h"
void fivechess::RunGame()
{
init();
system("title 简易五子棋 ——ACM制作");//设置标题
system("mode con cols=63 lines=33");//设置窗口大小
system("color E0");//设置颜色
print_chess();
while(1)
{
if(move_cursor())
{
change_chess();
winner=who_win(now.cursor_x,now.cursor_y);
}
change_chessboard();
pre.cursor_x=now.cursor_x,pre.cursor_y=now.cursor_y;
if(winner)
{
show_winnwer();
break;
}
}
}
/********************************************/
void fivechess::change_chess()
{
int x,y;
x=now.cursor_x*2+1,y=now.cursor_y*2+1;
count++;
chessboard[now.cursor_x][now.cursor_y]=!(count%2)+1;
gotoxy(y*2,x);
if(count%2==1) cout<<"●";
else cout<<"○";
gotoxy(0,30); }
void fivechess::show_winnwer()
{
gotoxy(25,14);
if(winner==1)cout<<"黑棋获胜";
else if(winner==2) cout<<"白棋获胜";
else cout<<" 平 局 ";
gotoxy(0,30);
}
void fivechess::change_chessboard()
{
int x,y;
int xx,yy;
xx=pre.cursor_x*2+1,yy=pre.cursor_y*2+1;
x=now.cursor_x*2+1,y=now.cursor_y*2+1;
gotoxy(yy*2-2,xx-1);
cout<<" ";
gotoxy(yy*2+2,xx-1);
cout<<" ";
gotoxy(yy*2-2,xx+1);
cout<<" ";
gotoxy(yy*2+2,xx+1);
cout<<" "; gotoxy(y*2-2,x-1);
cout<<"┏";
gotoxy(y*2+2,x-1);
cout<<"┓";
gotoxy(y*2-2,x+1);
cout<<"┗";
gotoxy(y*2+2,x+1);
cout<<"┘";
gotoxy(0,30);
}
int fivechess::who_win(int x,int y)
{
int i,z28,z46,z19,z73;
int wanjia=!(count%2)+1;
z28=z46=z19=z73=1; for(i=1; i<5; i++) if(y+1<15&&chessboard[x][y+i]==wanjia) z46++;
else break; // 横着是否有五子连珠
for(i=1; i<5; i++) if(y-1>=0&&chessboard[x][y-i]==wanjia) z46++;
else break; for(i=1; i<5; i++) if(x+1<15&&chessboard[x+i][y]==wanjia) z28++;
else break;
for(i=1; i<5; i++) if(x-1>=0&&chessboard[x-i][y]==wanjia) z28++;
else break; for(i=1; i<5; i++) if(x+1<15&&y+1<15&&chessboard[x+i][y+i]==wanjia) z73++;
else break;
for(i=1; i<5; i++) if(x-1>=0&&y-1>=0&&chessboard[x-i][y-i]==wanjia) z73++;
else break; for(i=1; i<5; i++) if(x+1<15&&y-1>=0&&chessboard[x+i][y-i]==wanjia) z19++;
else break;
for(i=1; i<5; i++) if(y+1<15&&x-1>=0&&chessboard[x-i][y+i]==wanjia) z19++;
else break; if(z28>=5||z46>=5||z19>=5||z73>=5) return wanjia;
else
{
if(count==15*15) return 3;
return 0;
}
} bool fivechess::move_cursor() // 输入操作
{
opt=_getch();
if(opt=='2'&&now.cursor_x!=14) now.cursor_x++;
else if(opt=='8'&&now.cursor_x!=0) now.cursor_x--;
else if(opt=='4'&&now.cursor_y!=0) now.cursor_y--;
else if(opt=='6'&&now.cursor_y!=14) now.cursor_y++;
else if(opt=='~') exit(0);
else if(opt=='5')
{
if(chessboard[now.cursor_x][now.cursor_y]==0) return true;
else return false;
}
return false;
}
void fivechess::init() // 初始化所有信息
{
int i,j;
for(i=0; i<MAX_SIZE; i++)
{
for(j=0; j<MAX_SIZE; j++)
chessboard[i][j]=0;
}
count=0;
winner=0;
now.cursor_x=now.cursor_y=7;
pre.cursor_x=pre.cursor_y=7;
}
string fivechess::getstyle(int i,int j)//获得棋盘中指定坐标交点位置的字符,通过制表符拼成棋盘
{
if(chessboard[j][i]==1)//1为黑子
return "●";
else if(chessboard[j][i]==2)//2为白子
return "○";
else if(i==0&&j==0)//以下为边缘棋盘样式
return "┏";
else if(i==MAX_SIZE-1&&j==0)
return "┓";
else if(i==MAX_SIZE-1&&j==MAX_SIZE-1)
return "┛";
else if(i==0&&j==MAX_SIZE-1)
return "┗";
else if(i==0)
return "┣";
else if(i==MAX_SIZE-1)
return "┫";
else if(j==0)
return "┯";
else if(j==MAX_SIZE-1)
return "┷";
return "╋";//中间的空位
}
void fivechess::print_chess()
{
system("cls");
int MAXSIZE=MAX_SIZE*2+1;
int i,j,x,y;
x=now.cursor_x*2+1,y=now.cursor_y*2+1;
for(i=0; i<MAXSIZE; i++)
{
for(j=0; j<MAXSIZE; j++)
{
if(i%2==1&&j%2==1)
{
cout<<getstyle(j/2,i/2);
}
else if(i%2==0&&j%2==0)
{
if(i+1==x&&j+1==y) cout<<"┏";
else if(i-1==x&&j+1==y) cout<<"└";
else if(i-1==x&&j-1==y) cout<<"┘";
else if(i+1==x&&j-1==y) cout<<"┐";
else cout<<" ";
}
else if(i==0||i==MAX_SIZE*2||j==0||j==MAX_SIZE*2) cout<<" ";
else if(i%2==1&&j%2==0&&i!=0&&i!=MAX_SIZE*2&&j!=0&&j!=MAX_SIZE*2)
{
cout<<"━";
}
else if(i%2==0&&j%2==1&&i!=0&&i!=MAX_SIZE*2&&j!=0&&j!=MAX_SIZE*2)
{
cout<<"│";
}
else cout<<" ";
}
printf("\n");
}
}
简易五子棋 V1.1.0的更多相关文章
- FineUI(专业版)v1.2.0 和 FineUI(开源版)v4.1.1 同时发布!
FineUI(开源版)v4.1.1 (建议所有 v4.x 升级到此版本):http://fineui.com/demo/ +2014-08-15 v4.1.1 -修正Form中表单字段设 ...
- RapidJSON v1.1.0 发布简介
时隔 15.6 个月,终于发布了一个新版本 v1.1.0. 新版本除了包含了这些日子收集到的无数的小改进及 bug fixes,也有一些新功能.本文尝试从使用者的角度,简单介绍一下这些功能和沿由. P ...
- 读阿里巴巴Java开发手册v1.2.0之工程结构有感【架构篇】
首先,把昨天那俩条sql语句的优化原因给大家补充一下,第一条效率极低,第二条优化后的,sql语句截图如下: 经过几个高手的评论和个人的分析: 第一条sql语句查询很慢是因为它首先使用了in关键字查询, ...
- 读阿里巴巴Java开发手册v1.2.0之编程规约有感【架构篇】
不为过去蹉跎,改变当下. 为什么开篇就送这么一句话给大家,我相信很多处于1-3年码龄的哥们儿们,在平时的编码历程中编码的个性可能是多彩的,每个人都有每个人特定的风格,但是我们现在这么随意写,以后这么 ...
- 阿里官方Java代码规范标准《阿里巴巴Java开发手册 终极版 v1.3.0》
终极版 v1.3.0 2017年开春之际,阿里诚意献上重磅大礼:<阿里巴巴Java开发手册>,首次公开阿里官方Java代码规范标准.这套Java统一规范标准将有助于提高行业编码规范化水平, ...
- 浏览器端类EXCEL表格插件 版本更新 - 智表ZCELL产品V1.1.0.1版本发布
智表(ZCELL),浏览器下纯JS表格控件,为您提供EXCEL般的智能体验! 纯国产化.高性价比的可靠解决方案. 更新说明 让大家久等了.因为最近忙其他项目,发布时间稍有延迟. 下次版本更新 ...
- 智表ZCELL产品V1.4.0开发API接口文档 与 产品功能清单
为了方便大家使用ZCELL,应网友要求,整理编写了相关文档,现与产品一起同步发布,供大家下载使用,使用过程中如有疑问,请与我QQ联系. 智表(ZCELL)V1.4.0版本 功能清单文档下载地址: 功 ...
- kubernetes之Kubeadm快速安装v1.12.0版
通过Kubeadm只需几条命令即起一个单机版kubernetes集群系统,而后快速上手k8s.在kubeadm中,需手动安装Docker和kubeket服务,Docker运行容器引擎,kubelet是 ...
- 【开源】SpringBootNetty聊天室V1.2.0升级版本介绍
前言 SpringBoot!微服务微架构的基础,Netty通信框架的元老级别框架,即之前的SpringBoot与Netty的实现聊天室的功能后已经过了不到一周的时间啦,今天我们更新了项目版本从V1.0 ...
随机推荐
- NSArray,NSMutableArray的一些常用方法
不可变数组 ——NSArray 常用的初始化一个数组: NSArray *array1 = [[NSArray alloc] init]; NSArray *array2 = ...
- python多线程(四)
原文:http://www.cnblogs.com/huxi/archive/2010/06/26/1765808.html 本文介绍了Python对于线程的支持,包括“学会”多线程编程需要掌握的基础 ...
- Ubuntu 16.04通过Snap安装应用程序
16.04LTS可以说是一个不寻常的5年支持版本,同时也带来了Snap应用,并通过Snap可以安装众多的软件包.需要注意的是,Snap是一个全新的软件包架构,但是同样也比其它的软件包大很多. 简单的安 ...
- Maven生成项目文档
Maven项目可以通过maven-site-plugin插件生成项目文档,无论什么项目都可以生成. 执行命令: mvn site 生成完成的输出目录在${basedir}/target/site文件夹 ...
- Linux下使用nohup实现在后台运行程序(转)
相比上一篇http://www.cnblogs.com/EasonJim/p/6833417.html使用screen实现后台运行程序,各有各的好处,多一种选择吧. Linux下一般比如想让某个程序在 ...
- IIS Express 的怪毛病 vs2013本机调试
本机调试时,如果同一个项目有多个版本,同时debug,可能会串项目调试,造成不必要的困扰: 通常情况下是 IIS express的映射出现了问题: 解决方案: 1.打开目录:查看文件C:\Users\ ...
- linxu下查看进程的线程方法;如何知道某个进程或者线程运行在哪个CPU上?
1.top -H -p <pid> ; top -H 在top命令后,按H键:或者top -H 2.ps -T -p <pid> “-T”选项可以开启线程查看 3.htop, ...
- 干货--安装eclipse-hadoop-plugin插件及HDFS API编程两个遇到的重要错误的解决
在Windows的eclipse上写hdfs的API程序,都会遇到两个错误,在网上查了很多资料,都没有解决的办法,经过了很多时间的研究,终于把这个问题解决了 错误是 1.java.io.IOExcep ...
- leetcode最长递增子序列问题
题目描写叙述: 给定一个数组,删除最少的元素,保证剩下的元素是递增有序的. 分析: 题目的意思是删除最少的元素.保证剩下的元素是递增有序的,事实上换一种方式想,就是寻找最长的递增有序序列.解法有非常多 ...
- Pascal Hexagrammum Mysticum 的深度探索
PASCAL . Hexagrammum Mysticum . (六角迷魂图) . 的深度探索 . 英中对比.英文蓝色,译文黑色,译者补充说明用紫红色 (已校完,但尚未定稿,想再整理并补充内容 ...