遊戲規則:人機對戰,在九宮格里下棋,O表明電腦所下棋子的地方,X表明玩家下棋的地方,哪一方先連成三個那邊算贏。
程序要求:windows
- 電腦所下位的行列值由電腦隨機生成。
- 玩家經過輸入行列值進行下棋。
- 每走一步都要造成可視化界面,O表明電腦,X表明玩家。
- 要及時分出輸贏或平局,分出勝負後,要 及時結束本局遊戲。
- 遊戲是否結束,由玩家決定。
頭文件:ide
#define _CRT_SECURE_NO_WARNINGS 1 #ifndef _THREE_CHESS_H_ #define _THREE_CHESS_H_ #include <stdio.h> #include <windows.h> #include <string.h> #include <time.h> #define ROW 3 #define COL 3 void ShowUI(); void Game(); void ComputerMove(char board[][COL], int row, int col); void ShowBoard(char board[][COL], int row, int col); char Judge(char board[][COL], int row, int col); void PlayerMove(char board[][COL], int row, int col); #endif
子函數函數
#define _CRT_SECURE_NO_WARNINGS 1 #include "three_chess.h" void ShowUI() { printf("##########################\n"); printf("##1.PLAY 0.EXIT##\n"); printf("##########################\n"); printf("select number: \n"); } void Game() { printf("Game bigin\n"); char board[ROW][COL]; char result = 'N'; srand((unsigned long)time(NULL)); memset(board, ' ', sizeof(board)); //按字節初始化 while (1) { ComputerMove(board, ROW, COL); ShowBoard(board, ROW, COL); result = Judge(board, ROW, COL); if ( result!= 'N') //結果‘X' 'O' 'E' 'N' { break; } PlayerMove(board, ROW, COL); ShowBoard(board, ROW, COL); result = Judge(board, ROW, COL); if (result != 'N') //結果‘X' 'O' 'E' 'N' { break; } } switch (result){ case 'X': printf("congratulation!!!you win..."); break; case 'O': printf("oh no~~~~~you lose"); break; case 'E': printf("平局"); break; default: break; } } void ComputerMove(char board[][COL], int row, int col) { while (1) { int x = rand() % row; int y = rand() % col; if (board[x][y] == ' ') { board[x][y] = 'O'; break; } } } void PlayerMove(char board[][COL], int row, int col) { int x, y; while (1) { printf("Please Enter Your Pose:"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row&&y >= 1 && y <= col) { if (board[x-1][y-1] == ' ') { board[x - 1][y - 1] = 'X'; break; } else printf("Your pose has been filled , try agin"); } else printf("Enter error , try agin"); } } char Judge(char board[ROW][COL], int row, int col) { int i ,j; for (i=0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && //續行符 board[i][0] != ' ') return board[i][0]; } for (j=0; j <col; j++) { if (board[0][j] == board[1][j] && board[0][j] == board[2][j] && //續行符 board[0][j] != ' ') return board[0][j]; } if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && //續行符 board[0][0] != ' ') return board[0][0]; if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && //續行符 board[0][2] != ' ') return board[0][2]; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] == ' ') return 'N'; } } return 'E'; } void ShowBoard(char board[][COL], int row, int col) { int i, j; printf(" 1 2 3 \n"); printf("----------\n"); for (i = 0; i <row; i++) { printf("%d|", i+1); for (j = 0; j < col; j++) { printf("%c|", board[i][j]); } printf("\n"); } }
主程序:code
#include "three_chess.h" int main() { int select; ShowUI(); do{ scanf("%d", &select); switch (select){ case 1: Game(); break; case 0: printf("bye!!\n"); break; default: printf("please printf agin:"); break; } } while(select); system("pause"); return 0; }