您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何利用C語言實現三子棋游戲”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何利用C語言實現三子棋游戲”吧!
1.頭文件部分
#ifndef _THREE_CHESSH
#define _THREE_CHESSH
#include<stdio.h>
#include<windows.h>
#include<string.h>
#include<time.h>
#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.主函數部分
#include"Three_Chess.h"
int main()
{
int select = 0;
int quit = 0;
while (!quit)
{
ShowUI(); //打印游戲菜單
scanf("%d", &select); //用戶進行選擇
switch (select)
{
case 1: //進入游戲函數
Game();
break;
case 2: //退出游戲
quit = 1;
printf("ByeBye!\n");
break;
default:
printf("Please Enter Again!\n");
break;
}
}
system("pause");
return 0;
}
3.游戲函數部分
#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; //退出循環
}
}
}
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); //輸入坐標
if (x >= 1 && x <= row && y >= 1 && y <= col)
{ //判斷輸入的坐標是否滿足條件
if (board[x - 1][y - 1] == ' ') //用戶輸入的下標是從1開始的,而數組下標是從0開始的,所以這里要對x和y減1
{
board[x - 1][y - 1] = 'X'; //用戶將X下入棋盤
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)//判斷函數
{
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++)//判斷是否棋盤放滿了 { for (j = 0; j < col; j++) { if (board[i][j] == ' ') { return 'N'; //繼續游戲 } } } //平局 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]; //定義棋盤(用二維數組實現)
memset(board, ' ', sizeof(board)); //將棋盤中的元素變成空格
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繼續
{
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("Congratulations!You Win!\n");
break;
case 'O':
printf("Damn!You Lose!\n");
break;
case 'E':
printf("Dogfall!\n");
break;
default:
break;
}
}
感謝各位的閱讀,以上就是“如何利用C語言實現三子棋游戲”的內容了,經過本文的學習后,相信大家對如何利用C語言實現三子棋游戲這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。