简易五子棋 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 ...
随机推荐
- python结构语句(while,if)
一.基础语法 编码: 默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串 #!/usr/bin/env python # -*- coding:utf- ...
- Redis数据结构之跳跃表
跳跃表是一种有序数据结构,它通过在每个节点中维持多个指向其他节点的指针,从而达到快速访问节点的目的. 一.跳跃表结构定义1. 跳跃表节点结构定义: 2. 跳跃表结构定义: 示例: 二.跳跃表节点中各种 ...
- 王垠:谈 Linux,Windows 和 Mac ( 2013)
这段时间受到很多人的来信.他们看了我很早以前写的推崇 Linux 的文章,想知道如何“抛弃 Windows,学习 Linux”.天知道他们在哪里找到那么老的文章,真是好事不出门…… 我觉得我有责任消除 ...
- 2017 ACM/ICPC Asia Regional Guangxi Online 记录
题目链接 Guangxi 感觉这场比赛完全是读题场啊…… 比赛过程中丢失了一波进度,最后想开题的时候已经来不及了…… Problem A 按题意模拟……按照那个矩阵算就可以了 #include &l ...
- 关于xshell无法连接到centos的问题
1.xshell无法连接到centos:拒绝连接(无线网) 在xshell ping centos出现: 解决方法: 1. 2.重启下网卡: [root@localhost ~]# /etc/init ...
- digits
Digits(digits.cpp/c/pas)Description给一个关于x的多项式,并给定一个x,求该多项式在带入该x时的值最后k位数字.Input第一行两个整数n.k:之后的 行,每行两个数 ...
- 果皇的矩阵[matrix]
#1101. 果皇的矩阵[matrix] 题目描述 输入格式 一行两个数,表示 N,M. 输出格式 一行一个数,表示答案对 10^9+7 取模后的结果 样例 样例输入 3 3 样例输出 38 数据范围 ...
- JavaScript 层
代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--& ...
- GoogLeNet系列解读
GoogLeNet Incepetion V1 这是GoogLeNet的最早版本,出现在2014年的<Going deeper with convolutions>.之所以名为“GoogL ...
- 使用图片作为textview组件的背景
<TextView android:layout_gravity="center" android:layout_width="100dp" androi ...