這篇文章主要講解了“如何利用C語(yǔ)言實(shí)現(xiàn)三子棋游戲”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“如何利用C語(yǔ)言實(shí)現(xiàn)三子棋游戲”吧!
在江川等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專(zhuān)注、極致的服務(wù)理念,為客戶(hù)提供成都做網(wǎng)站、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需制作網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,成都營(yíng)銷(xiāo)網(wǎng)站建設(shè),外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè),江川網(wǎng)站建設(shè)費(fèi)用合理。
1.頭文件部分
#ifndef _THREE_CHESSH
#define _THREE_CHESSH
#include
#include
#include
#include
#pragma warning(disable:4996)
#define ROW 3
#define COL 3
void ShowUI();
void Game();
void ComputerMove(char board[ROW][COL], int row, int col);
void PlayerMove(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);
#endif
2.主函數(shù)部分
#include"Three_Chess.h"
int main()
{
int select = 0;
int quit = 0;
while (!quit)
{
ShowUI(); //打印游戲菜單
scanf("%d", &select); //用戶(hù)進(jìn)行選擇
switch (select)
{
case 1: //進(jìn)入游戲函數(shù)
Game();
break;
case 2: //退出游戲
quit = 1;
printf("ByeBye!\n");
break;
default:
printf("Please Enter Again!\n");
break;
}
}
system("pause");
return 0;
}
3.游戲函數(shù)部分
#define _CRT_SECURE_NO_WARNINGS 1
#include"THREE_CHESS.h"
void ShowUI()
{
printf("\n");
printf(" 1、GAME 2、EXIT \n");
printf("\n");
printf("Please Select:\n");
}
void ComputerMove(char board[][COL], int row, int col)
{
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ') //判斷此位置是否為空,如果為空就賦值'O'表示電腦走在此位置
{
board[x][y] = 'O';
break; //退出循環(huán)
}
}
}
void PlayerMove(char board[][COL], int row, int col)
{
int x, y;
while (1)
{
printf("Please enter position->x y:\n");
scanf("%d %d", &x, &y); //輸入坐標(biāo)
if (x >= 1 && x <= row && y >= 1 && y <= col)
{ //判斷輸入的坐標(biāo)是否滿(mǎn)足條件
if (board[x - 1][y - 1] == ' ') //用戶(hù)輸入的下標(biāo)是從1開(kāi)始的,而數(shù)組下標(biāo)是從0開(kāi)始的,所以這里要對(duì)x和y減1
{
board[x - 1][y - 1] = 'X'; //用戶(hù)將X下入棋盤(pán)
break;
}
else
{
printf("Your place is taken,Try Again:\n");
}
}
else
{
printf("Enter error,Try Again:\n");
}
}
}
char Judge(char board[][COL], int row, int col)//判斷函數(shù)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] != ' ')
{
return board[i][0];
}
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[2][i] != ' ')
{
return board[0][i];
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] != ' ')
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ' ')
{
return board[0][2];
}
for (i = 0; i < row; i++)//判斷是否棋盤(pán)放滿(mǎn)了 { for (j = 0; j < col; j++) { if (board[i][j] == ' ') { return 'N'; //繼續(xù)游戲 } } } //平局 return 'E';
}
void ShowBoard(char board[][COL], int row, int col)
{
system("cls"); //清屏
printf(" 1 2 3\n");
printf("---------------\n");
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
printf("%d |", i + 1);
for (j = 0; j < col; j++)
{
printf(" %c |", board[i][j]);
}
printf("\n---------------\n");
}
printf("\n");
}
void Game()
{
char board[ROW][COL]; //定義棋盤(pán)(用二維數(shù)組實(shí)現(xiàn))
memset(board, ' ', sizeof(board)); //將棋盤(pán)中的元素變成空格
char result = 'N';
srand((unsigned long)time(NULL));
while (1)
{
ComputerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != 'N') //X玩家贏,O電腦贏,E平局,N繼續(xù)
{
break;
}
PlayerMove(board, ROW, COL);
ShowBoard(board, ROW, COL);
result = Judge(board, ROW, COL);
if (result != 'N') //X玩家贏,O電腦贏,E平局,N繼續(xù)
{
break;
}
}
switch (result)
{
case 'X':
printf("Congratulations!You Win!\n");
break;
case 'O':
printf("Damn!You Lose!\n");
break;
case 'E':
printf("Dogfall!\n");
break;
default:
break;
}
}
感謝各位的閱讀,以上就是“如何利用C語(yǔ)言實(shí)現(xiàn)三子棋游戲”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)如何利用C語(yǔ)言實(shí)現(xiàn)三子棋游戲這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!