-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessBoard.cpp
128 lines (126 loc) · 3.19 KB
/
ChessBoard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "ChessBoard.h"
#include "Tools.h"
#include "stdio.h"
#include "conio.h"
#include <map>
#include<stack>
map<int,char> m;
ChessBoard::ChessBoard()
{
memset(myChessBoard,'.',sizeof(myChessBoard));
m[BLACK] = 'x';m[WHITE] = 'o';
}
ChessBoard::~ChessBoard()
{
}
ChessBoard* ChessBoard::instance = nullptr;
ChessBoard* ChessBoard::getInstance()
{
if(!instance)
instance = new ChessBoard();
return instance;
}
bool ChessBoard::PlayChess(pair<short,short> pos)
{
if(myChessBoard[pos.first][pos.second]=='.')
if(turn==BLACK){
myChessBoard[pos.first][pos.second] = 'x';
if(CheckWinner(pos))
{
SetCursorPos(pair<short,short>(0,16));
cout<<"win->Press any key to stop"<<endl;
print(pos);
getchar();
return false;
}
st.push(pos);
turn = WHITE;
return true;
}
else if(turn==WHITE){
myChessBoard[pos.first][pos.second] = 'o';
if(CheckWinner(pos))
{
SetCursorPos(pair<short,short>(0,16));
cout<<"win->Press any key to stop"<<endl;
print(pos);
getchar();
return false;
}
st.push(pos);
turn = BLACK;
return true;
}
return false;
}
void ChessBoard::print(pair<short,short> twinkle)
{
for(int i = 0;i < 15;i++)
{
for(int j = 0;j < 15;j++)
{
SetCursorPos(pair<short,short>(i,j));
cout<<myChessBoard[i][j];
}
}
while(true)
{
Sleep(80);
SetCursorPos(twinkle);
cout<<" ";
Sleep(80);
SetCursorPos(twinkle);
cout<<myChessBoard[twinkle.first][twinkle.second];
if(kbhit())
break;
}
}
bool ChessBoard::CheckWinner(pair<short,short> pos)
{
if(CheckLine(pos,pair<short,short>(1,0)))return true;
if(CheckLine(pos,pair<short,short>(0,1)))return true;
if(CheckLine(pos,pair<short,short>(1,1)))return true;
if(CheckLine(pos,pair<short,short>(1,-1)))return true;
return false;
}
bool ChessBoard::CheckLine(pair<short,short> pos,pair<short,short>offset)
{
int linkNum = 1;
int Count = 0;
//ÓÒ±ß
for(int i = offset.first,j = offset.second;(pos.first+i>=0&&pos.first+i<15)&&
(pos.second+j>=0&&pos.second+j<15);i+=offset.first,j+=offset.second)
{
if(myChessBoard[pos.first+i][pos.second+j]==m[turn])
{
linkNum++;
}else
{
break;
}
}
//×ó±ß
for(int i = -offset.first,j = -offset.second;(pos.first+i>=0&&pos.first+i<15)&&
(pos.second+j>=0&&pos.second+j<15);i-=offset.first,j-=offset.second)
{
if(myChessBoard[pos.first+i][pos.second+j]==m[turn])
{
linkNum++;
}else
{
break;
}
}
if (linkNum > 4)
return true;
return false;
}
void ChessBoard::Retract()
{
if(st.size()>=2){
myChessBoard[st.top().first][st.top().second] = '.';
st.pop();
myChessBoard[st.top().first][st.top().second] = '.';
st.pop();
}
}