您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關教你使用C語言寫一個五子棋游戲,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
采用二維數組制作五子棋,用坐標下棋。
請看代碼。
include<stdio.h> #include<string.h> #include<stdlib.h> #include<windows.h> #include<conio.h> #include<time.h> void menu()//目錄 { char xuanze = 0,xuanze2; printf("\n"); printf("\n"); printf(" #############################################\n"); printf(" *五子棋小游戲*\n"); printf(" *1.雙人游戲*\n"); printf(" *2.人機對戰(暫未開放)\n"); printf(" *3.游戲須知\n"); printf(" #############################################\n"); printf(" 請選擇選項,目前僅有 1 選項\n"); printf("\n"); printf(" "); xuanze = _getch(); switch (xuanze) { case 1: break; case 3: printf("本游戲是鍵盤輸入坐標來落子的,白棋先落,第二次落子為黑色,依次落子。\n"); printf("若落子坐標非法則需重新落子。\n"); printf("鍵盤輸入1直接進入游戲\n"); } } int t = 2; int pan[20][20] = { {1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6}, {7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9} }; void qp()//棋盤 { int i, j, k = 0; printf(" "); for (i = 0; i < 10; i++) printf(" %d", i + 1); for (i = 10; i < 20; i++) printf("%d", i + 1); printf("\n"); printf("1 "); //printf("┌ ┬ ┐├ ┼ ┤└ ┴ ┘│ ");©○●┼ for (i = 0; i < 20; i++) for (j = 0; j < 20; j++) { switch (pan[i][j]) { case 1: printf("┌ "); k++; if (k % 20 == 0) { printf("\n"); } break; case 2: printf("┬ "); k++; if (k % 20 == 0) printf("\n"); break; case 3: printf("┐ "); k++; if (k % 20 == 0) { printf("%d", i+1); printf("\n"); printf("%d ", i + 1); } break; case 4: printf("├ "); k++; if (k % 20 == 0) printf("\n"); break; case 5: printf("┼ "); k++; if (k % 20 == 0) printf("\n"); break; case 6: printf("┤ "); k++; if (k % 20 == 0) { printf("%d", i+1); printf("\n"); if(i<8) printf("%d ", i + 2); if(i>7) printf("%d", i + 2); } break; case 7: printf("└ "); k++; if (k % 20 == 0) printf("\n"); break; case 8: printf("┴ "); k++; if (k % 20 == 0) printf("\n"); break; case 9: printf("┘ "); k++; if (k % 20 == 0) { printf("%d", i + 1); printf("\n"); } break; case 10: printf("○"); k++; if (k % 20 == 0) { printf("%d", i+1); printf("\n"); } break; case 11: printf("●"); k++; if (k % 20 == 0) { printf("%d", i+1); printf("\n"); } break; } } printf(" "); for (i = 0; i < 10; i++) printf(" %d", i + 1); for (i = 10; i < 20; i++) printf("%d", i + 1); printf("\n"); printf("\n"); } void play() { int i, j; while (1) { printf("*請選擇落子坐標(x,y): "); scanf_s("%d %d", &j, &i); printf("\n"); if(i>0&&i<21&&j>0&&j<21) { if (pan[i - 1][j - 1] != 10 && pan[i - 1][j - 1] != 11) { if (t % 2 == 1) { t++; pan[i - 1][j - 1] = 11; break; } else if (t % 2 == 0) { t++; pan[i - 1][j - 1] = 10; break; } } else if (pan[i-1][j-1] == 10 || pan[i-1][j-1] == 11) { printf("此處已經有棋子,請重新輸入\n"); continue; } } else if (i < 1 || i > 21 || j < 1 || j > 21) { printf("坐標不合法,請重新輸入\n"); continue; } } } void test() { int i, j; for(i=0;i<20;i++) for (j = 0; j < 20; j++) { if (pan[i][j] != 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9) { if (pan[i][j] == pan[i][j + 1]&&pan[i][j] == pan[i][j + 2]&&pan[i][j] == pan[i][j + 3]&&pan[i][j] == pan[i][j + 4]) { if (pan[i][j] == 11) { printf("白棋獲勝\n"); } if (pan[i][j] == 10) { printf("黑棋獲勝\n"); } } if (pan[i][j] == pan[i + 1][j]&&pan[i][j] == pan[i + 2][j]&&pan[i][j] == pan[i + 3][j]&&pan[i][j] == pan[i + 4][j]) { if (pan[i][j] == 11) { printf("白棋獲勝\n"); } if (pan[i][j] == 10) { printf("黑棋獲勝\n"); } } if (pan[i][j] == pan[i + 1][j + 1]&&pan[i][j] == pan[i + 2][j + 2]&&pan[i][j] == pan[i + 3][j + 3] &&pan[i][j]== pan[i + 4][j + 4]) { if (pan[i][j] == 11) { printf("白棋獲勝\n"); } if (pan[i][j] == 10) { printf("黑棋獲勝\n"); } } if (pan[i][j] == pan[i - 1][j + 1]&&pan[i][j] == pan[i - 2][j + 2] &&pan[i][j]== pan[i - 3][j + 3]&&pan[i][j] == pan[i - 4][j + 4]) { if (pan[i][j] == 11) { printf("白棋獲勝\n"); } if (pan[i][j] == 10) { printf("黑棋獲勝\n"); } } } } } int main() { system("mode con cols=98 lines=48"); SetConsoleTitleA("五子棋小游戲"); //設置窗口 menu(); system("cls"); while (1) { qp(); test(); play(); system("cls"); } system("pause"); return 0; }
上述就是小編為大家分享的教你使用C語言寫一個五子棋游戲了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。