您好,登錄后才能下訂單哦!
這篇文章主要介紹了C語言如何編寫五子棋游戲,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
一、構建棋盤
首先可以用一個二維數組來構建棋盤,下標表示位置,內容表示黑子白子或者空位。當數組內容為1時,該位置為白字,當數組為0時,該位置為白子,空位輸出+
int w[11][11], flag = 0; int a, b; for (int k = 0; k < 11; k++) printf("第%d列\(zhòng)t", k); printf("\n"); for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (w[i][j] == 0) printf("黑\t"); else if (w[i][j] == 1) printf("白\t"); else printf("+\t"); } printf("第%d行\(zhòng)n\n", i); }
二、判斷位置超范圍與錯誤輸入
當輸入的棋子坐標超出范圍或者該位置已經存在棋子,則再次輸入棋子坐標。其中flag作為標志作為判別黑手白手的標志。
if (flag == 0) { printf("黑色下棋\n"); scanf("%d %d", &a, &b); while (a < 0 || a>10 || b < 0 || b>10) { printf("此位置超出范圍,請重新輸入:"); scanf("%d %d", &a, &b); } while (w[a][b] == 0 || w[a][b] == 1) { printf("此位置已有棋子,請重新輸入:"); scanf("%d %d", &a, &b); } flag = 1; w[a][b] = 0; } else { printf("白色下棋\n"); scanf("%d %d", &a, &b); while (a < 0 || a>10 || b < 0 || b>10) { printf("此位置超出范圍,請重新輸入:"); scanf("%d %d", &a, &b); } while (w[a][b] == 0 || w[a][b] == 1) { printf("此位置已有棋子,請重新輸入:"); scanf("%d %d", &a, &b); }
三、判斷勝負
在五子棋中,出現五個棋子排成一排或者一列或者斜排即可判為勝出。每當落下一個棋子,對該棋子前后上下斜排五個棋子進行判別,超過五個連續(xù)的一樣顏色的棋子則勝出。
int A = 1, B = 1, jieguo = 0, C = 1, D = 1; int x = 1, y = 1, m = 1, n = 1; if (w[a][b] == 1) { for (int i = 1; i < 5; i++) { if (w[a][b + i] == 1) A++; else break; } for (int i = 1; i < 5; i++) { if (w[a][b - i] == 1) A++; else break; } for (int i = 1; i < 5; i++) { if (w[a+i][b] == 1) C++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b] == 1) C++; else break; } if (A > 4 || C > 4) jieguo = 1;//白棋勝利標志 else { for (int i = 1; i < 5; i++) { if (w[a+i][b + i] == 1) x++; else break; } for (int i = 1; i < 5; i++) { if (w[a-i][b - i] == 1) x++; else break; } for (int i = 1; i < 5; i++) { if (w[a + i][b-i] == 1) y++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b+i] == 1) y++; else break; } if (x > 4 || y > 4) jieguo = 1; } } if (w[a][b] == 0) { for (int i = 1; i < 5; i++) { if (w[a][b + i] == 0) B++; else break; } for (int i = 1; i < 5; i++) { if (w[a][b - i] == 0) B++; else break; } for (int i = 1; i < 5; i++) { if (w[a + i][b] == 0) D++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b] == 0) D++; else break; } if (B > 4 || D > 4) jieguo = 2;//黑棋勝利標志 else { for (int i = 1; i < 5; i++) { if (w[a + i][b + i] == 0) m++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b - i] == 0) m++; else break; } for (int i = 1; i < 5; i++) { if (w[a + i][b - i] == 0) n++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b + i] == 0) n++; else break; } if (m > 4 || n > 4) jieguo = 2; } }
程序完成
四、演示結果
五、源代碼
int main() { int w[11][11], flag = 0; int a, b; while (1) { if (flag == 0) { printf("黑色下棋\n"); scanf("%d %d", &a, &b); while (a < 0 || a>10 || b < 0 || b>10) { printf("此位置超出范圍,請重新輸入:"); scanf("%d %d", &a, &b); } while (w[a][b] == 0 || w[a][b] == 1) { printf("此位置已有棋子,請重新輸入:"); scanf("%d %d", &a, &b); } flag = 1; w[a][b] = 0; } else { printf("白色下棋\n"); scanf("%d %d", &a, &b); while (a < 0 || a>10 || b < 0 || b>10) { printf("此位置超出范圍,請重新輸入:"); scanf("%d %d", &a, &b); } while (w[a][b] == 0 || w[a][b] == 1) { printf("此位置已有棋子,請重新輸入:"); scanf("%d %d", &a, &b); } flag = 0; w[a][b] = 1; } int A = 1, B = 1, jieguo = 0, C = 1, D = 1; int x = 1, y = 1, m = 1, n = 1; if (w[a][b] == 1) { for (int i = 1; i < 5; i++) { if (w[a][b + i] == 1) A++; else break; } for (int i = 1; i < 5; i++) { if (w[a][b - i] == 1) A++; else break; } for (int i = 1; i < 5; i++) { if (w[a+i][b] == 1) C++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b] == 1) C++; else break; } if (A > 4 || C > 4) jieguo = 1;//白棋勝利標志 else { for (int i = 1; i < 5; i++) { if (w[a+i][b + i] == 1) x++; else break; } for (int i = 1; i < 5; i++) { if (w[a-i][b - i] == 1) x++; else break; } for (int i = 1; i < 5; i++) { if (w[a + i][b-i] == 1) y++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b+i] == 1) y++; else break; } if (x > 4 || y > 4) jieguo = 1; } } if (w[a][b] == 0) { for (int i = 1; i < 5; i++) { if (w[a][b + i] == 0) B++; else break; } for (int i = 1; i < 5; i++) { if (w[a][b - i] == 0) B++; else break; } for (int i = 1; i < 5; i++) { if (w[a + i][b] == 0) D++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b] == 0) D++; else break; } if (B > 4 || D > 4) jieguo = 2;//黑棋勝利標志 else { for (int i = 1; i < 5; i++) { if (w[a + i][b + i] == 0) m++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b - i] == 0) m++; else break; } for (int i = 1; i < 5; i++) { if (w[a + i][b - i] == 0) n++; else break; } for (int i = 1; i < 5; i++) { if (w[a - i][b + i] == 0) n++; else break; } if (m > 4 || n > 4) jieguo = 2; } } for (int k = 0; k < 11; k++) printf("第%d列\(zhòng)t", k); printf("\n"); for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (w[i][j] == 0) printf("黑\t"); else if (w[i][j] == 1) printf("白\t"); else printf("+\t"); } printf("第%d行\(zhòng)n\n", i); } if (jieguo == 1) { printf("白棋勝利"); break; } if (jieguo == 2) { printf("黑棋勝利"); break; } } return 0; }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“C語言如何編寫五子棋游戲”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。