三子棋,小试牛刀
三子棋的初始版
表面結(jié)構(gòu)
<span style="color:#33ccff"> #include "game.h" void menu() {printf(" ★★★★★★★★★★★★★★★★★★\n");printf(" ★★★ ★★★\n");printf("★★★ 1.開始游戲 0.結(jié)束游戲 ★★★\n");printf(" ★★★ ★★★\n");printf(" ★★★★★★★★★★★★★★★★★★\n");printf("請選擇:"); } void game() {char board[ROW][LINE] = { 0 }; //創(chuàng)建數(shù)組(放棋子)Initboard(board, ROW, LINE); //初始化棋盤Printfboard(board, ROW, LINE); //打印棋盤while (1){Player_Runboard(board, ROW, LINE); //玩家開始下棋computer_Runboard(board, ROW, LINE); //電腦開始下棋Printfboard(board, ROW, LINE);judgeboard(board, ROW, LINE); //判斷輸贏if (judgeboard(board, ROW, LINE) == 'X'){printf("玩家贏!!\n");break;}else if (judgeboard(board, ROW, LINE) == '@'){printf("電腦贏!!\n");break;}} } void test() {int input = 0;srand((unsigned int)time(NULL));do{menu();scanf("%d", &input);switch (input){case 1:game();break;case 0:printf("退出游戲!!\n");break;default:printf("選擇錯誤!!\n");break;}} while (input); } int main() {test();return 0; }</span>隱藏函數(shù)
<span style="color:#cc33cc"> #include "game.h" void Initboard(char board[ROW][LINE], int row, int line) {int i = 0;for (i = 0; i < row; i++){int j = 0;for (j = 0; j < line; j++){board[i][j] = ' ';}} } void Printfboard(char board[ROW][LINE], int row, int line) {int i = 0;int j = 0;for (i = 0; i<row; i++){for (j = 0; j< line; j++){printf(" %c ",board[i][j]);if (j<line-1)printf("|");}printf("\n");if (i < row - 1){for (j = 0; j< line; j++){printf("---");if (j<line - 1)printf("|");}}printf("\n");} } void Player_Runboard(char board[ROW][LINE], int row, int line) {int x = 0;int y = 0;printf("玩家走:\n");while (1){printf("請輸入坐標(biāo):");scanf("%d%d", &x, &y);if ((x >= 1 && x <= (row + 1)) && (y >= 1 && y <= (line + 1))){if (board[x - 1][y - 1] == ' '){board[x - 1][y - 1] = 'X';break;}else{printf("坐標(biāo)已被占用\n");}}else{printf("坐標(biāo)非法\n");}} } void computer_Runboard(char board[ROW][LINE]) {int x = 0;int y = 0;printf("電腦走:\n");while (1){x = rand() % ROW;y = rand() % LINE;if (board[x][y] == ' '){board[x][y] = '@';break;}} } char judgeboard(char board[ROW][LINE], int row, int line) {int i = 0;for (i = 0; i < row; i++) //橫向判斷{if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][1] !=' '){return board[1][1];}}for (i = 0; i < row; i++) //豎向判斷{if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' '){return board[1][1];}}if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ') // 右斜判斷{return board[1][1];}if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') // 左斜判斷{return board[1][1];} }</span>頭文件與函數(shù)的聲明
<span style="color:#cc0000"> #ifndef __GAME_H__ #define __GAME_H__ #include<stdio.h> #include<time.h> #include<stdlib.h> #define ROW 3 #define LINE 3 void Initboard(char board[ROW][LINE], int row, int line); void Printfboard(char board[ROW][LINE], int row, int line); void Player_Runboard(char board[ROW][LINE], int row, int line); void computer_Runboard(char board[ROW][LINE], int row, int line); char judgeboard(char board[ROW][LINE], int row, int line);#endif //__GAME_H__</span>初步運行結(jié)果
總結(jié)
- 上一篇: Source Insight 教程
- 下一篇: python之socket编程