五子棋
1 #include <iostream> 2 #include <string> 3 #include <conio.h> 4 /*conio.h是Console Input/Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输入输出的函数, 5 主要是一些用户通过键盘按键产生的对应操作,比如getchar()函数等等*/ 6 using namespace std; 7 char board[16][16], o; 8 void init() { 9 for (int i = 1; i <= 15; i++)//31 10 for (int j = 1; j <= 15; j++)//31 11 board[i][j] = '+'; 12 for (int i = 1, ii = 0; ii <= 30; i++, ii += 2) board[0][i] = i + 64;//A、B、C……O的ASCII码 13 for (int j = 1, jj = 0; jj <= 30; j++, jj += 2) board[j][0] = j + 64; 14 board[0][0] = '*'; 15 } 16 void display() { 17 cout << "|================================================|" << endl; 18 for (int i = 0; i <= 15; i++) { 19 cout << "|"; 20 for (int j = 0; j <= 15; j++) { 21 cout << board[i][j]; 22 if (j != 15 && i != 0) cout << "--"; 23 else cout << " "; 24 } 25 cout << "|" << endl << "| | | | | | | | | | | | | | | | |" << endl; 26 } 27 cout << "|================================================|" << endl; 28 } 29 void place(bool color) { 30 string colour; 31 char x, y; 32 x = y = ' '; 33 system("cls");/*清屏操作,调用系统命令cls完成清屏操作,system函数是C语言 34 提供的与操作系统衔接的函数*/ 35 display(); 36 if (!color) colour = "黑"; 37 if (color) colour = "白"; 38 cout << colour << "方放置棋子,请输入棋子的行和列(大写字母):"; 39 cin >> x >> y; 40 if (x - 64 > 16 || y - 64 > 16 || x - 64 < 1 || y - 64 < 1) { 41 cout << "该位置超出棋盘范围,请重新放置!" << x << y << endl; 42 system("pause"); 43 place(color); 44 } 45 if (board[x - 64][y - 64] == 'b' || board[x - 64][y - 64] == 'w') { 46 cout << "该位置已有棋子" << board[x - 64][y - 64] << ",请重新放置!" << endl; 47 system("pause"); 48 place(color); 49 } 50 if (!color) board[x - 64][y - 64] = 'b'; 51 if (color) board[x - 64][y - 64] = 'w'; 52 } 53 int compute() { 54 int num=0; 55 for (int i = 1; i <= 15; i++) 56 for (int j = 1; j <= 15; j++) 57 if (board[i][j] == 'b' || board[i][j] == 'w') num++; 58 if (num == 225) return 2;//棋盘已满,平局 59 for (int i = 1; i <= 15; i++) 60 for (int j = 1; j <= 15; j++) { 61 if (board[i][j] == 'b'&&board[i][j] == board[i + 1][j] && board[i][j] == board[i + 2][j] && board[i][j] == board[i + 3][j] && board[i][j] == board[i + 4][j]) return 0; 62 if (board[i][j] == 'w'&&board[i][j] == board[i + 1][j] && board[i][j] == board[i + 2][j] && board[i][j] == board[i + 3][j] && board[i][j] == board[i + 4][j]) return 1; 63 if (board[i][j] == 'b'&&board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2] && board[i][j] == board[i][j + 3] && board[i][j] == board[i][j + 4]) return 0; 64 if (board[i][j] == 'w'&&board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2] && board[i][j] == board[i][j + 3] && board[i][j] == board[i][j + 4]) return 1; 65 if (board[i][j] == 'b'&&board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] && board[i][j] == board[i + 3][j + 3] && board[i][j] == board[i + 4][j + 4]) return 0; 66 if (board[i][j] == 'w'&&board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] && board[i][j] == board[i + 3][j + 3] && board[i][j] == board[i + 4][j + 4]) return 1; 67 if (board[i][j] == 'b'&&board[i][j] == board[i - 1][j + 1] && board[i][j] == board[i - 2][j + 2] && board[i][j] == board[i - 3][j + 3] && board[i][j] == board[i - 4][j + 4]) return 0; 68 if (board[i][j] == 'w'&&board[i][j] == board[i - 1][j + 1] && board[i][j] == board[i - 2][j + 2] && board[i][j] == board[i - 3][j + 3] && board[i][j] == board[i - 4][j + 4]) return 1; 69 } 70 } 71 int main() { 72 game_start: 73 init(); 74 system("cls"); 75 system("title 双人五子棋"); 76 cout << "Copyright (C) XiyuWang 2018 All rights reserved." << endl; 77 cout << "双人五子棋小游戏" << endl; 78 cout << "小提示:棋盘中b代表黑方(black),w代表白方(white)" << endl; 79 cout << "请按任意键开始游戏......"; 80 _getch(); 81 while (true) { 82 place(0);//黑棋走,place(1)白棋走 83 if (compute() == 0) { 84 system("cls"); 85 display(); 86 cout << "黑方胜!" << endl; 87 break; 88 } 89 if (compute() == 2) { 90 system("cls"); 91 display(); 92 cout << "平局!" << endl; 93 break; 94 } 95 place(1); 96 if (compute() == 1) { 97 system("cls"); 98 display(); 99 cout << "白方胜!" << endl; 100 break; 101 } 102 if (compute() == 2) { 103 system("cls"); 104 display(); 105 cout << "平局!" << endl; 106 break; 107 } 108 } 109 cout << "再来一局?Y/N "; 110 o = _getch(); 111 if (o == 'Y' || o == 'y') goto game_start; 112 return 0; 113 }
运行结果:


浙公网安备 33010602011771号