C語言實(shí)現(xiàn)三子棋游戲
更新時(shí)間:2021年07月28日 09:53:52 作者:fchwpo?
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)三子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)三子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下
game.h
設(shè)置頭文件
#define ROW 3 #define COL 3 #include<stdio.h> #include<stdlib.h> #include<time.h> //聲明函數(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 CheckWin(char board[ROW][COL], int row, int col);
game.c
實(shí)現(xiàn)三子棋游戲功能模塊
#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]);
int j = 0;
for (j = 0; j < col; j++) {
printf(" %c ", board[i][j]);
if (j < col - 1) {
printf("|");
}
}
printf("\n");//打印完數(shù)據(jù)換行
//打印分隔行
//if(i<row-1)
// 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("玩家走:\n");
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;
}
}
}
static 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;//有空
}
}
}
return 1;//已滿
}
char CheckWin(char board[ROW][COL], int row, int col) {
int i = 0;
int j = 0;
int count = 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 < row; i++) {
count = 0;
for (j = 0; j < col - 1; j++) {
if (board[i][j] == board[i][j + 1] && board[i][j] != ' ') {
count++;
}
else
{
break;
}
//if (count == col - 1) {
// return board[i][0];
//}
}
if (count == col - 1) {
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];
// }
//}
for (i = 0; i < col; i++) {
count = 0;
for (j = 0; j < row - 1; j++) {
if (board[j][i] == board[j+1][i] && board[j][i] != ' ') {
count++;
}
else
{
break;
}
//if (count == row - 1) {
// return board[0][i];
//}
}
if (count == row - 1) {
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[2][0] == board[1][1] && board[1][1] == board[0][2] && board[1][1] != ' ') {
// return board[1][1];
//}
//分別判斷兩條對角線
int count1 = 0;
for (i = 0; i < row-1; i++) {
//count = 0;
if ((board[i][i] == board[i + 1][i + 1])&&(board[0][0]!=' ')) {//循環(huán)判斷第一條對角線 如果相同計(jì)數(shù)器累加 不相同就跳出循環(huán)
count1++;
}
else
{
break;
}
//if (count1 == row - 1) {//到此時(shí) 第一條對角線上所有元素相同 游戲結(jié)束 返回勝利條件 count達(dá)不到row-1?。?
// return board[0][0];
//}
}
if (count1 == row - 1) {//到此時(shí) 需要在外層判斷?。?第一條對角線上所有元素相同 游戲結(jié)束 返回勝利條件
return board[0][0];
}
int count2 = 0;
for (i = 0; i < row - 1; i++) {
//count = 0;
if (board[i][row-1-i] == board[i + 1][row-2-i] && board[row-1-i][0] != ' ') {
count2++;
}
else
{
break;
}
//if (count2 == row - 1) {
// return board[row-1][0];
//}
}
if (count2 == row - 1) {
return board[row - 1][0];
}
//判斷平局 棋盤是否滿了 是否有空格
if (IsFull(board,ROW,COL)==1) {
return 'Q';
}
//不是平局
return 'C';
}
test.c
主函數(shù)
#include"game.h"
void game() {
char ret = 0;
//printf("game start");
char board[ROW][COL] = { 0 };//數(shù)組應(yīng)該初始化為空格
InitBoard(board, ROW, COL);//初始化棋盤
//打印棋盤
DisplayBoard(board, ROW, COL);
//分析:
//1、玩家贏 * 2、電腦贏 # 3、平局 Q 4、繼續(xù) C
//要判斷輸贏
while (1)
{
//玩家下棋
PlayerMove(board,ROW,COL);
DisplayBoard(board, ROW, COL);
//判斷輸贏
ret = CheckWin(board, ROW, COL);
if (ret != 'C') {
break;
}
//電腦下棋
ComputerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
//判斷輸贏
ret = CheckWin(board, ROW, COL);
if (ret != 'C') {
break;
}
}
if (ret == '*') {
printf("玩家贏\n");
}
else if(ret == '#')
{
printf("電腦贏\n");
}
else
{
printf("平局\n");
}
}
void menu() {
printf("##########################\n");
printf("######### 1、play ########\n");
printf("######### 0、exit ########\n");
printf("##########################\n");
}
int main() {
int input = 0;
srand((unsigned int)time(NULL));//生成隨機(jī)數(shù)
do {
menu();
printf("請選擇:");
scanf("%d", &input);
switch (input)
{
case 1:
game();//三子棋游戲
break;
case 0:
printf("退出游戲\n");
break;
default:
printf("輸入錯誤,請重新輸入\n");
break;
}
} while (input);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++中的數(shù)字轉(zhuǎn)字符串to_string
這篇文章主要介紹了C++中的數(shù)字轉(zhuǎn)字符串to_string,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11
C++ 實(shí)現(xiàn)優(yōu)先隊(duì)列的簡單實(shí)例
這篇文章主要介紹了C++ 實(shí)現(xiàn)優(yōu)先隊(duì)列的簡單實(shí)例的相關(guān)資料,希望通過本文能幫助大家實(shí)現(xiàn)優(yōu)先隊(duì)列,需要的朋友可以參考下2017-08-08
C語言實(shí)現(xiàn)繪制貝塞爾曲線的函數(shù)
貝塞爾曲線,又稱貝茲曲線或貝濟(jì)埃曲線,是應(yīng)用于二維圖形應(yīng)用程序的數(shù)學(xué)曲線。本文將利用C語言實(shí)現(xiàn)繪制貝塞爾曲線的函數(shù),需要的可以參考一下2022-12-12
opencv實(shí)現(xiàn)圖像顏色空間轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了opencv實(shí)現(xiàn)圖像顏色空間轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08

