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");
}
}

最新文章

  1. zabbix3.0.4 部署之七 (zabbix3.0.4 邮件报警) &amp; 微信报警
  2. 【kAri OJ604】圣哲的树
  3. HTTP基础03--HTTP报文
  4. switch,break和default语句练习
  5. Python的交互式界面 编写 .
  6. java 读取文件的字节数组
  7. 数据结构学习笔记05图(最小生成树 Prim Kruskal)
  8. Java中使用验证码和二维码
  9. jboss服务器配置多实例
  10. 那天有个小孩跟我说LINQ(三)转载
  11. 日期函数(SqlServer)
  12. WPF使用Log4net.dll库的demo(转载加个人观点)
  13. Android的事件处理-android学习之旅(四十四)
  14. DB2读取CLOB字段-was报错:操作无效:已关闭 Lob。 ERRORCODE=-4470, SQLSTATE=null
  15. 匿名函数function前面的! ~等符号作用小解
  16. F#周报2019年第5期
  17. linux下chmod使用
  18. 1.3.4、CDH 搭建Hadoop在安装之前(端口---Impala使用的端口)
  19. VMware vCenter Orchestrator
  20. 服务器购买+建站流程教程——适合新手没有经验的人Chinar总结

热门文章

  1. 标准C程序设计七---23
  2. php——两种无限级分类
  3. TOT 傅立叶变换 FFT 入门
  4. java内部类理解使用
  5. ios下读取jason中的nsstring时间并本地化成中文gmt时间显示上午下午
  6. DeepFM
  7. 源码编译安装php
  8. ceph工作原理和安装
  9. SolidEdge 如何绘制断裂剖视图 局部剖视图
  10. RESTful Web Service