-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
173 lines (157 loc) · 5.03 KB
/
test.cpp
File metadata and controls
173 lines (157 loc) · 5.03 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<iostream>
#include<stdlib.h>
#include"cmdChess.h"
using namespace std;
extern const int N ;
extern int A=0, B=0;//int A B为玩家胜利次数
extern char Model='C';
/*
*返回落子结果
*
*/
int ShowResult(GameBoard &obj,int x,int y,char flager){
if (1== obj.CheckBoard(x, y,'x')|| 1== obj.CheckBoard(x, y, 'o'))
{
if (1 == obj.CheckBoard(x, y, 'x'))
{
obj.WinnerCount(1);
}
if (1 == obj.CheckBoard(x, y, 'o'))
{
obj.WinnerCount(2);
}
std::cout << '\n' <<"play again? Y or N " << '\n';
cin >> flager;
if (flager == 'N'||flager=='n')
{
return -1;
}
else
{
obj.initchessBoard();
obj.RenewBoard();
obj.setflag(0);
return 0;//这句没运行????!!
}
}
//判断无人获胜的情况
if (0 == obj.CheckBoard(x, y, ' ')) //判断棋盘是否下满
{
std::cout << "Board is full...No on wins..play againt ? Y OR N " << '\n';
cin >> flager;
if (flager == 'N'||flager=='n')
{
return -1;
}
else
{
system("CLS");
obj.initchessBoard();
obj.RenewBoard();
return 0;
}
}
return 1;
}
int main()
{
GameBoard obj;
while(1)
{
int x=0, y=0;
char flager=' ';
obj.GameStart();
std::cout << "input instruction:";
char command;
cin >> command;
if ('E' == command)
{
system("CLS");
int step=0;
if('H'==Model){
std::cout<<"your are playing with human now ,two players!"<<'\n';
}else if('C'==Model)
{
std::cout<<"your are playing with computer now!"<<'\n';
std::cout<<"please set who is first?Human or Computer? H or C"<<'\n';
char first='H';
std::cin>>first;
if('H'==first){
obj.setflag(0);
std::cout<<"Human first! Computer is player B note with 'x' !"<<'\n';
step=0;
}else{
obj.setflag(1);
std::cout<<"Computer first! Computer is player A note with 'o' !"<<'\n';
step=1;
}
}
obj.initchessBoard();
obj.RenewBoard();
//双人对战模式 每局不交换先手
while ('E' == command && 'H'==Model)
{
std::cout << "input coordinate:";
cin >> x >> y;
obj.Chesspainter(x, y);
if(ShowResult(obj,x,y,flager)==-1){
break;
}else if(ShowResult(obj,x,y,flager)==0){
obj.initchessBoard();
obj.RenewBoard();
obj.setflag(0);
}
//机器对战模式 交换先手了!cmdChess.h 256 Line :return 0没有运行的原因!
while ('E' == command && 'C'==Model){
step=obj.getflag();
//step 0为人类回合,1为机器回合
if(step==0){
std::cout << "input coordinate:";
cin >> x >> y;
obj.Chesspainter(x, y);
//copy双人模式代码,判断获胜和无人获胜结果。
if(ShowResult(obj,x,y,flager)==-1){
break;
}
else if(ShowResult(obj,x,y,flager) == 0)
{
step=0;
}
else if(ShowResult(obj,x,y,flager) == 1){
step=1;
}
//可封装成函数
//成员函数 void GameResult() 删去代码中obj.即可
//非成员函数 void ShowResult(GameBoard &obj)
}else if(step==1){
obj.actionByComputer(x,y);
obj.Chesspainter(x, y);
if(ShowResult(obj,x,y,flager)==-1){
break;
}else if(ShowResult(obj,x,y,flager)==0){
step=0;
}
else if(ShowResult(obj,x,y,flager) == 1){
step=0;
}
//step=0;
}
}
}
else if ('M' == command)
{
system("CLS");
std::cout<< "chose play with Human or Computer... H or C" << '\n';
std::cin>>Model;
}
else if('Q'==command){
break;
}
else
{
system("CLS");
std::cout<< "invalid command,please input again!" << '\n';
obj.GameStart();
}
}
}