赞
踩
#include<iostream> #include<vector> #include<stdlib.h> #include<iomanip> using namespace std; int Five_row, Five_col; //显示表格15*15 void showv2(vector<vector<unsigned char>>v2) { int n1 = v2.size(); int n2 = v2[0].size(); cout << endl; int row = 1, col = 1; //显示多少列 cout << setw(5) << setfill(' ')<<1; for (int col = 2; col <= n2; col++) cout << setw(4)<<setfill(' ')<<col; cout << endl; cout << setw(3) << setfill(' ')<<'-'; for (int col = 2; col <= n2; col++) cout << setw(4) << setfill('-')<<'-'; cout << "----"<<endl; //打印棋盘 for (int i = 0; i < n1; i++) { //显示多少行 cout << setw(2) << setfill(' ')<<row; //设置固定宽度 row++; cout << '|'; for (int j = 0; j < n2; j++) { cout <<" "<<v2[i][j]<<' ' << '|'; //cout << ' '; } cout << endl; cout << setw(3) << setfill(' ') << '-'; for (int col = 2; col <= n2; col++) cout << setw(4) << setfill('-') << '-'; cout << "----" << endl; } cout << endl; } //初始化表格 vector<vector<unsigned char>> showgrid(vector<vector<unsigned char>>& v2, vector<unsigned char>& v1) { for (int i = 0; i < Five_row; i++) { for (int j = 0; j < Five_col; j++) { v1.push_back(' '); } v2.push_back(v1); } showv2(v2); return v2; } //检查输入 int checkinput(int &p1_1, int &p1_2, vector<vector<unsigned char>> &v2) { while(1) { if (p1_1 >= 1 && p1_1 <= Five_row && p1_2 >= 1 && p1_2 <= Five_col) { //检查输入是否已经有棋子 if (v2[p1_1 - 1][p1_2 - 1] != ' ') { cin.clear(); cin.ignore(100, '\n'); cout << "输入位置已有棋子,请重新输入!" << endl; cin >> p1_1 >> p1_2; continue; } else { cout << "您输入的坐标为:[" << p1_1 << ", " << p1_2 << "]" << endl;; break; } } else { //cout << p1_1 << " " << p1_2 << endl; //清除之前的输入,防止非法输入占用 cin.clear(); cin.ignore(100, '\n'); cout << "输入有误,请重新输入!" << endl; cin >> p1_1 >> p1_2; } } return p1_1, p1_2; } //判断是不是赢了 bool winner(vector<vector<unsigned char>>v2,unsigned char &p) { for (int i = 0; i < Five_row; i++) { for (int j = 0; j < Five_col; j++) { //横向判断是否连成五子 if (j + 4 < Five_col && v2[i][j] == p && v2[i][j + 1] == p && v2[i][j + 2] == p && v2[i][j + 3] == p && v2[i][j + 4] == p) return true; //左斜判断是否连成五子 else if (i + 4 < Five_row && j - 4 >= 0 && v2[i][j] == p && v2[i + 1][j - 1] == p && v2[i + 2][j - 2] == p && v2[i + 3][j - 3] == p && v2[i + 4][j - 4] == p) return true; //纵向判断是否连成五子 else if (i + 4 < Five_row && v2[i][j] == p && v2[i + 1][j] ==p && v2[i + 2][j] == p && v2[i + 3][j] == p && v2[i + 4][j] == p) return true; //右斜判断是否连成五子 else if (i + 4 < Five_row && j + 4 < Five_col && v2[i][j] == p && v2[i + 1][j + 1] == p && v2[i + 2][j + 2] == p && v2[i + 3][j + 3] == p && v2[i + 4][j + 4] == p) return true; } } return false; } //自定义棋盘大小 void Init_board(int &Five_row, int &Five_col) { int n1, n2; cout << "请定义棋盘大小,n1>=5,n2>=5" << endl; cin >> n1 >> n2; //判断输入是否合规 while (1) { if (n1 >= 5 && n2 >= 5) { Five_row = n1; Five_col = n2; cout << "您输入棋盘大小为" << Five_row << "×" << Five_col << "。" << endl; break; } else { cout << "您输入有误,请重新定义棋盘大小!" << endl; cin.clear(); cin.ignore(100, '\n'); cin >> n1 >> n2; } } } int main() { //定义控制台背景为亮白色 system("color f0"); //自定义棋盘大小 Init_board(Five_row, Five_col); while (1) { vector<unsigned char>v1; vector<vector<unsigned char>>v2; //初始化表格 showgrid(v2, v1); while (1) { cout << "请一号选手('○')出棋:请输入两个数字:第一个数字范围1~"<<Five_row<<",第二个数字范围1~"<<Five_col<<"。" << endl; int p1_1, p1_2; cin >> p1_1 >> p1_2; //检查输入 p1_1, p1_2 = checkinput(p1_1, p1_2, v2); //更新棋盘 unsigned char ch = 'O'; v2[p1_1 - 1][p1_2 - 1] = ch; system("cls"); showv2(v2); //判断是否赢了 bool w2 = winner(v2, ch); if (w2) { cout << "恭喜一号,你胜利了!!" << endl; break; } cout << "请二号选手('●')出棋:请输入两个数字:第一个数字范围1~" << Five_row << ",第二个数字范围1~" << Five_col << "。" << endl; int p2_1, p2_2; cin >> p2_1 >> p2_2; //检查输入 p2_1, p2_2 = checkinput(p2_1, p2_2, v2); //更新棋盘 ch = '*'; v2[p2_1 - 1][p2_2 - 1] = ch; system("cls"); showv2(v2); //判断是否赢了 bool w1 = winner(v2, ch); if (w1) { cout << "恭喜二号,你胜利了!!" << endl; break; } } cout << "是否开始新的一局? Y/N" << endl; char ans; cin >> ans; if (ans == 'Y' || ans == 'y') system("cls"); else break; } system("pause"); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。