c/c++實現五子棋
阿新 • • 發佈:2018-11-22
本人新手,最近嘗試著編寫一些程式碼,感覺五子棋是個不錯的選擇,就動手嘗試了一下,因為好多都是現學的,所以在程式碼編寫的過程中,顯得十分地繁瑣,所以僅供新手之間相互交流。
程式流程
生成棋盤:輸入座標落子:判斷遊戲狀態:輸出結果
功能:人人對戰,手動輸入落子座標。
// wuziqi.cpp : 定義控制檯應用程式的入口點。 // #include "stdafx.h" #include<iostream> using namespace std; void printjiemian(char juzhen[15][15]) { system("cls"); for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { cout << "|---"; } cout <<"|"<< endl; for (int k = 0; k < 15; k++) { cout << "| " << juzhen[i][k]<<" "; } cout <<"|"<< endl; } for (int l = 0; l < 15; l++) { cout << "|---"; } cout << "|" << endl; } int luozi(char juzhen[15][15],int hang,int lie) { int i, j; int row, col,row1,col1; row = hang; col = lie; row1 = hang; col1 = lie; for (j = 0; j < 11; j++) { if (juzhen[row][j] == juzhen[row][j + 1] && juzhen[row][j + 1] == juzhen[row][j + 2] && juzhen[row][j + 2] == juzhen[row][j + 3] && juzhen[row][j + 3] == juzhen[row][j + 4] && juzhen[row][j + 4] == 'o') goto here; else if (juzhen[row][j] == juzhen[row][j + 1] && juzhen[row][j + 1] == juzhen[row][j + 2] && juzhen[row][j + 2] == juzhen[row][j + 3] && juzhen[row][j + 3] == juzhen[row][j + 4] && juzhen[row][j + 4] == 'X') goto here; } for (int i = 0; i < 11; i++) { if (juzhen[i][col] == juzhen[i+1][col] && juzhen[i+1][col] == juzhen[i+2][col] && juzhen[i+2][col] == juzhen[i+3][col] && juzhen[i+3][col] == juzhen[i+4][col] && juzhen[i+4][col] == 'o') goto here; else if (juzhen[i][col] == juzhen[i + 1][col] && juzhen[i + 1][col] == juzhen[i + 2][col] && juzhen[i + 2][col] == juzhen[i + 3][col] && juzhen[i + 3][col] == juzhen[i + 4][col] && juzhen[i + 4][col] == 'X') goto here; } for (i = 0; i < 11; i++) { int k; k=abs(row - col); for (j = 0; j < 11; j++) { if (abs(i - j) == k) { if (juzhen[i][j] == juzhen[i + 1][j + 1] && juzhen[i + 1][j + 1] == juzhen[i + 2][j + 2] && juzhen[i + 2][j + 2] == juzhen[i + 3][j + 3] && juzhen[i + 3][j + 3] == juzhen[i + 4][j + 4] && juzhen[i + 4][j + 4] == 'o') goto here; else if (juzhen[i][j] == juzhen[i + 1][j + 1] && juzhen[i + 1][j + 1] == juzhen[i + 2][j + 2] && juzhen[i + 2][j + 2] == juzhen[i + 3][j + 3] && juzhen[i + 3][j + 3] == juzhen[i + 4][j + 4] && juzhen[i + 4][j + 4] == 'X') goto here; } } } for (i = 0;; i++) { if (row1 == 0 || col1 == 14) break; else { row1++; col1--; } } if (juzhen[row1][col1] == juzhen[row1 + 1][col1 - 1] && juzhen[row1 + 1][col1 - 1] == juzhen[row1 + 2][col1 - 2] && juzhen[row1 + 2][col1 - 2] == juzhen[row1 + 3][col1 - 3] && juzhen[row1 + 3][col1 - 3] == juzhen[row1 + 4][col1 - 4] && juzhen[row1][col1] == 'o') goto here; else if (juzhen[row1][col1] == juzhen[row1 + 1][col1 - 1] && juzhen[row1 + 1][col1 - 1] == juzhen[row1 + 2][col1 - 2] && juzhen[row1 + 2][col1 - 2] == juzhen[row1 + 3][col1 - 3] && juzhen[row1 + 3][col1 - 3] == juzhen[row1 + 4][col1 - 4] && juzhen[row1][col1] == 'X') goto here; return 0; here: return 1; } bool panduan(char juzhen[15][15]) { int i, j; for (i = 0; i < 15; i++) { for (j = 0; j < 15; j++) { if (juzhen[i][j] == ' ') { goto here; } } } here: if (i < 15 || j < 15) return 1; else return 0; } char qizi(int diyige,int hang,int lie) { int m, n; m = hang; n = lie; char heizi = 'X'; char baizi = 'o'; char juzhen[15][15]; if (diyige%2!=0) juzhen[m][n] = 'o'; else juzhen[m][n] = 'X'; return juzhen[m][n]; } int main() { int const m = 15; int state = -1; int zhuangtai = 0; char juzhen[m][m]; int diyige = 0; int hang=0, lie=0; for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { juzhen[i][j] = ' '; } } printjiemian(juzhen); cout << "輸入落子座標:" << "\n" << "X="; cin >> hang; cout << "Y="; cin >> lie; juzhen[hang][lie] = qizi(diyige,hang,lie); printjiemian(juzhen); zhuangtai = panduan(juzhen); while (zhuangtai) { diyige = diyige+1; state = luozi(juzhen,hang,lie); if (state == 0) { printjiemian(juzhen); cout << "輸入落子座標:" << "\n" << "X="; cin >> hang; cout << "Y="; cin >> lie; juzhen[hang][lie] = qizi(diyige, hang, lie); printjiemian(juzhen); } else if (state == 1) { printjiemian(juzhen); cout << "you win!!!" << endl; break; } if (diyige == 15*15) { printjiemian(juzhen); cout << "keep going!!!" << endl; break; } } return 0; }