目錄
創(chuàng)新互聯(lián)專注于雨湖網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供雨湖營銷型網(wǎng)站建設(shè),雨湖網(wǎng)站制作、雨湖網(wǎng)頁設(shè)計、雨湖網(wǎng)站官網(wǎng)定制、小程序設(shè)計服務(wù),打造雨湖網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供雨湖網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。一、主要涉及的知識點(diǎn)有:
二、玩家玩三子棋流程
三、判斷一次游戲是否結(jié)束?
四、代碼
1、主函數(shù)部分
2、自定義頭文件部分
3、自定義函數(shù)定義部分
邏輯表達(dá)式、for循環(huán)、switch選擇、do-while循環(huán)、while循環(huán)、if語句、自定義函數(shù)、二維數(shù)組、時間戳、rand函數(shù)生成隨機(jī)數(shù)字等。
二、玩家玩三子棋流程三、判斷一次游戲是否結(jié)束?四、代碼 1、主函數(shù)部分#define _CRT_SECURE_NO_WARNINGS 1
#include#include"game.h"
void game()
{
char board[ROW][COL] = { 0 };
Prim_board(board, ROW, COL);//初始化棋盤
Print_board(board, ROW, COL);//打印棋盤
do //判斷誰先走
{
int k = 0;
int i = 0;
printf("****** 1.Player First ******\n");
printf("****** 0.Computer First ******\n");
printf("請選擇:>");
scanf("%d", &i);
switch (i)
{
case 1:
printf("玩家先走!\n");//玩家走
goto player;
break;
case 0:
printf("電腦先走!\n");//電腦走
goto computer;
break;
default:
printf("請重新選擇!\n");
break;
}
} while (1);
char ret = 0;
while (1)
{
player://玩家走
Player_board(board, ROW, COL);
Print_board(board, ROW, COL);//打印棋盤
ret = Who_win(board, ROW, COL);//判斷是否獲勝
if (ret != 'C')
{
break;
}
computer:
printf("\n\n電腦走!\n");
Computer_board(board, ROW, COL);
Print_board(board, ROW, COL);//打印棋盤
ret = Who_win(board, ROW, COL);//判斷是否獲勝
if (ret != 'C')
{
break;
}
}
if (ret == 'Q')
{
printf("平局!\n");
}
else if (ret == '*')
{
printf("恭喜您獲勝!");
}
else if (ret == '#')
{
printf("電腦獲勝!");
}
else
{
printf("平局");
}
}
void FirtPage()
{
printf("****** 1.Play ******\n");
printf("****** 0.Exit ******\n");
int i = 0;
do
{
printf("\n請選擇是否開始:>");
scanf("%d", &i);
switch (i)
{
case 0:
break;
case 1:
game();
break;
default:
printf("請重新輸入:>");
break;
}
} while (i);
}
int main()
{
srand((unsigned int)time(NULL));
FirtPage();
return 0;
}
2、自定義頭文件部分#define _CRT_SECURE_NO_WARNINGS 1
#include#include#include#define ROW 3
#define COL 3
Prim_board(char board[ROW][COL], int row, int col);
void Print_board(char board[ROW][COL], int row, int col);
void Player_board(char board[ROW][COL], int row, int col);
void Computer_board(char board[ROW][COL], int row, int col);
char Who_win(char board[ROW][COL], int row, int col);
3、自定義函數(shù)定義部分#include"game.h"
Prim_board(char board[ROW][COL], int row, int col)
{
int i = 0, j = 0;
for (i = 0; i< row; i++)
{
for (j = 0; j< col; j++)
{
board[i][j] = ' ';
}
}
}
void Print_board(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i< row; i++)//打印行
{
for (j = 0; j< col; j++)//具體的每一大行-空格行
{
printf(" %c ", board[i][j]);
if (j< col - 1)
printf("|");
}
printf("\n");
if (i< row - 1)
{
for (j = 0; j< col; j++)//具體的每一大行-分隔行
{
printf("---");
if (j< col - 1)
printf("|");
}
printf("\n");
}
}
}
void Player_board(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("\n\n玩家,請輸入坐標(biāo)x, y!\n");
do
{
scanf("%d,%d", &x, &y);
if (x >0 && x<= row && y >0 && y<= col)
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
{
printf("坐標(biāo)不為空,請重新輸入!\n");
}
}
else
{
printf("坐標(biāo)非法,請重新輸入!\n");
}
} while (1);
}
void Computer_board(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
do
{
x = rand() % row;
y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
} while (1);
}
int IsFull(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i< row; i++)
{
for (j = 0; j< col; j++)
{
if (board[i][j] == ' ')
return 0;
}
}
}
char Who_win(char board[ROW][COL], int row, int col)//勝,敗,平
{
int i = 0;
//三行
for (i = 0; i< row; i++)
{
if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != ' ')
return board[i][0];
}
//三列
for (i = 0; i< col; i++)
{
if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ')
return board[0][i];
}
//對角線
if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ')
return board[0][0];
if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != ' ')
return board[0][2];
//平局
int a = 1;
a = IsFull(board, ROW, COL);
if (a == 1)
return 'Q';
return 'C';
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧