C++实现五子棋小程序

2025-05-27 0 13

这是一个用C++写的五子棋的小程序,关于A若是占据了已经下了的位置处理的不好。改动 hight,与width ,与q[][] 可以将棋盘扩大。

?

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

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352
#include<iostream>

#include<vector>

using namespace std;

class qipan

{

public:

qipan() {}

~qipan() {};

//向上下左右,斜的方向

char left(int x, int y)

{//检查是否合适

if (x >= 1 && x <= hight&& y - 1 >= 1 && y - 1 <= width)

{

return q[x][y - 1];

}

else {

return 'F';

}

}

char right(int x, int y)

{

if (x >= 1 && x <= hight&&y + 1 >= 1 && y + 1 <= width)

{

return q[x][y + 1];

}

else {

return 'F';

}

}

char up(int x, int y)

{

if (x - 1 >= 1 && x - 1 <= hight && y >= 1 && y <= width)

{

return q[x - 1][y];

}

else {

return 'F';

}

}

char down(int x, int y)

{

if (x + 1 >= 1 && x + 1 <= hight && y >= 1 && y <= width)

{

return q[x + 1][y];

}

else {

return 'F';

}

}

char left_up(int x, int y)

{

if (x - 1 >= 1 && x - 1 <= hight && y - 1 >= 1 && y - 1 <= width)

{

return q[x - 1][y - 1];

}

else {

return 'F';

}

}

char left_down(int x, int y)

{

if (x + 1 >= 1 && x + 1 <= hight && y - 1 >= 1 && y - 1 <= width)

{

return q[x + 1][y - 1];

}

else {

return 'F';

}

}

char right_up(int x, int y)

{

if (x - 1 >= 1 && x - 1 <= hight && y + 1 >= 1 && y + 1 <= width)

{

return q[x - 1][y + 1];

}

else {

return 'F';

}

}

char right_down(int x, int y)

{

if (x + 1 >= 1 && x + 1 <= hight && y + 1 >= 1 && y + 1 <= width)

{

return q[x + 1][y + 1];

}

else {

return 'F';

}

}

void init_cur() {

h_cur = hang;

l_cur = lie;

}

bool win()

{

bool WIN = false;

char temp = q[hang][lie];

//以上为例,每次先看上面的5个,假如一样,iter++;否则 break;再加上下的方向,同样iter++;最后iter+1==5,WIN=true;并且退出

//各个方向重复,上下左右,斜着的。

//赢了直接退出。

//显示是谁赢了

//左右

int count_lr = 1;

init_cur();

for (int i = 0; i < 4; i++)

{

if (left(h_cur, l_cur) == temp)

count_lr++;

else

break;

l_cur--;

}

init_cur();

for (int i = 0; i < 4; i++)

{

if (right(h_cur, l_cur) == temp)

count_lr++;

else

break;

l_cur++;

}

if (count_lr == 5)

WIN = true;

//上下

int count_ud = 1;

init_cur();

for (int i = 0; i < 4; i++)

{

if (up(h_cur, l_cur) == temp)

count_ud++;

else

break;

h_cur--;

}

init_cur();

for (int i = 0; i < 4; i++)

{

if (down(h_cur, l_cur) == temp)

count_ud++;

else

break;

h_cur++;

}

if (count_ud == 5)

WIN = true;

//左斜

int count_lt = 1;

init_cur();

for (int i = 0; i < 4; i++)

{

if (left_up(h_cur, l_cur) == temp)

count_lt++;

else

break;

h_cur--;

l_cur--;

}

init_cur();

for (int i = 0; i < 4; i++)

{

if (left_down(h_cur, l_cur) == temp)

count_lt++;

else

break;

h_cur++;

l_cur--;

}

if (count_lt == 5)

WIN = true;

//右边斜

int count_rt = 1;

init_cur();

for (int i = 0; i < 4; i++)

{

if (right_up(h_cur, l_cur) == temp)

count_rt++;

else

break;

h_cur--;

l_cur++;

}

init_cur();

for (int i = 0; i < 4; i++)

{

if (right_down(h_cur, l_cur) == temp)

count_rt++;

else

break;

h_cur++;

l_cur++;

}

if (count_rt == 5)

WIN = true;

return WIN;

}

void qipan_array()

{

for (int i = 0; i < hight; i++) {

for (int j = 0; j < width; j++)

q[i][j] = '+';

}

}

void prin_qipan()

{

//打印二维数组;每一行要打印上行号,以及列号

for (int i = 0; i <hight; i++)

{

for (int j = 0; j < width; j++)

{

cout << q[i][j] << " ";

}

cout << i;

cout << endl;

}

for (int j = 0; j <width; j++)

{

cout << j << " ";

}

cout << endl << "________________________________" << endl;

}

int xiaqi(int player)

{

if (player == 1) {

q[hang][lie] = '*';

}

else if (player == 2)

{

q[hang][lie] = '@';

}

else

cout << "input player is wrong" << endl;

return 0;

}

//初始化行列

int gethang(int h)

{

hang = h;

return 0;

}

int getlie(int l)

{

lie = l;

return 0;

}

int h_cur;

int l_cur;

const int hight = 9;

const int width = 9;

int hang; int lie;

char q[9][9];

};

int main()

{

int hang, lie;

qipan wzq;

wzq.qipan_array();

cout << "A 与B 玩五子棋" << endl;

cout << "A use * and B use @" << endl;

cout << "________________________________" << endl;

for (int i = 0; i < 15; i++) {

cout << "A 输入行: ";

cin >> hang;

cout << "A 输入列: ";

cin >> lie;

if (wzq.q[hang][lie] != '+')

cout << "输入的行列数字已经有人占据了" << endl;

else {

wzq.gethang(hang);

wzq.getlie(lie);

wzq.xiaqi(1);

wzq.prin_qipan();

if (wzq.win())

{

cout << "A is winner" << endl;

exit(0);

}

}

//b 开始了

cout << "B 输入行: ";

cin >> hang;

cout << "B 输入列: ";

cin >> lie;

if (wzq.q[hang][lie] != '+')

cout << "输入的行列数字已经有人占据了" << endl;

else {

wzq.gethang(hang);

wzq.getlie(lie);

wzq.xiaqi(2);

wzq.prin_qipan();

if (wzq.win())

{

cout << "B is winner" << endl;

exit(0);

}

}

}

return 0;

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 C++实现五子棋小程序 https://www.kuaiidc.com/73101.html

相关文章

发表评论
暂无评论