C語言實(shí)現(xiàn)簡單三子棋游戲
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)簡單三子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下
游戲介紹:使用C語言中二維數(shù)組和函數(shù)的基本知識實(shí)現(xiàn)一個(gè)三子棋游戲,這個(gè)游戲要實(shí)現(xiàn)的基本功能有初始化棋盤、棋盤的打印、玩家下棋、電腦下棋、判斷輸贏。
代碼框架:
1.頭文件(game.h)
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> #define ROW 3 #define COL 3 //聲明函數(shù) //初始化棋盤 void InitBoard(char board[ROW][COL], int row, int col); //打印棋盤 void DisplayBoard(char board[ROW][COL], int row, int col); //玩家下棋 void PlayerMove(char board[ROW][COL], int row, int col); //電腦下棋 void ComputerMove(char board[ROW][COL], int row,int col); //判斷輸贏 char check_win(char board[ROW][COL], int row, int col); //判斷棋盤是否為滿 int is_full(char board[ROW][COL], int row, int col);
2.函數(shù)實(shí)現(xiàn)(game.c)
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(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++)
{
board[i][j] = ' ';
}
}
}
//void DisplayBoard(char board[ROW][COL], int row, int col)
//{
// int i = 0;
// for (i = 0; i < row; i++)
// {
// //打印數(shù)據(jù)
// printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
// //打印分割行
// if (i<row-1)
// printf("---|---|---\n");
// }
//}
void DisplayBoard(char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
//打印數(shù)據(jù)
int j = 0;
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 PlayerMove(char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf("玩家走:>");
while(1)
{
printf("請輸入坐標(biāo):>");
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] = '*';
break;
}
else
{
printf("該坐標(biāo)被占用,請重新輸入\n");
}
}
else
{
printf("坐標(biāo)非法,清重新輸入\n");
}
}
}
void ComputerMove(char board[ROW][COL], int row, int col)
{
printf("電腦走:>\n");
while (1)
{
int x = rand() % row;
int y = rand() % col;
if (board[x][y] == ' ')
{
board[x][y] = '#';
break;
}
}
}
int is_full(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;
}
}
}
return 1;
}
char check_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][1] == board[i][2] && board[i][0] != ' ')
{
return board[i][0];
}
}
//檢測列是否相同
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
{
return board[0][i];
}
}
//檢測對角線
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];
}
//檢測是否為平局
if (is_full(board,row,col))
{
return 'Q';//平局
}
return ' ';//繼續(xù)玩
}
3.測試文件(test.c)
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("****************************\n");
printf("********* 1.play *********\n");
printf("********* 0.exit *********\n");
printf("****************************\n");
}
void game()
{
int x = 0;
int y = 0;
char ret = 0;
//設(shè)計(jì)三子棋游戲
//數(shù)組應(yīng)該初始化為空格
char board[ROW][COL] = { 0 };//存儲數(shù)據(jù)
InitBoard(board , ROW , COL);//初始化棋盤--空格
//打印棋盤
DisplayBoard(board,ROW,COL);
while (1)
{
PlayerMove(board,ROW,COL);
if ((ret = check_win(board, ROW, COL)) != ' ')
break;
DisplayBoard(board, ROW, COL);
ComputerMove(board,ROW,COL);
if ((ret = check_win(board, ROW, COL)) != ' ')
break;
DisplayBoard(board, ROW, COL);
}
if (ret == '*')
{
printf("玩家贏\n");
}
else if (ret == '#')
{
printf("電腦贏\n");
}
else if (ret == 'Q')
{
printf("平局\n");
}
DisplayBoard(board, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("請選擇:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戲\n");
break;
default:
printf("選擇錯(cuò)誤\n");
break;
}
} while (input);
return 0;
}
4.游戲結(jié)果示例


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言實(shí)現(xiàn)基于最大堆和最小堆的堆排序算法示例
這篇文章主要介紹了C語言實(shí)現(xiàn)基于最大堆和最小堆的堆排序算法示例,分別是基于最大堆的升序排序和基于最小堆的降序排序?qū)嵗?需要的朋友可以參考下2016-06-06
C語言利用數(shù)組和文件實(shí)現(xiàn)登錄注冊功能
這篇文章主要為大家詳細(xì)介紹了C語言利用數(shù)組和文件實(shí)現(xiàn)登錄注冊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
C語言通過二分查找實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了在C語言中如何通過二分查找思想編寫一個(gè)簡單的猜數(shù)字游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02
C++中HTTP?代理服務(wù)器的設(shè)計(jì)與實(shí)現(xiàn)詳解
代理服務(wù)器,即允許一個(gè)網(wǎng)絡(luò)終端(一般為客戶端)通過這個(gè)服務(wù)與另一?個(gè)網(wǎng)絡(luò)終端(一般為服務(wù)器)進(jìn)行非直接的連接,下面我們就來看看如何使用C++設(shè)計(jì)與實(shí)現(xiàn)一個(gè)HTTP?代理服務(wù)器吧2024-01-01

