C語言實現控制台五子棋小遊戲

2022-09-26 18:09:12 字數 1602 閱讀 9871

這是乙個用c語言實現的控制台小遊戲—-五子棋,棋盤大小和獲勝棋子數目是用巨集定義的,可以自定義,所以可以輕鬆改為三子棋、六子棋等等。此遊戲設定為人機對戰(pve),電腦走棋是隨機的。

此程式採用多檔案編譯,共有三個檔案,乙個game.h標頭檔案,乙個game.c檔案和乙個main.c檔案,**分別如下:

game.h

這裡是一些資料和函式的宣告

#ifndef _game_h_

#define _game_h_

#define _board_ 10 //棋盤大小

#define _piece_ 5 //獲勝所需要連起來的棋子個數

typedef struct boardbrd ,*pbrd; //brd:五子棋 pbrd:五子棋指標

void init(pbrd b);

void printbrd(const pbrd b);

void pcrand(pbrd b);

int player(pbrd b);

int judge(pbrd b, int n);

void menu(pbrd b);

#endif // ! _game_h_

game.c

這裡是函式的具體實現

#include

#include

#include

#include "game.h"

void init(pbrd b)

}b->pc = '@';

b->player = '*';

}void printbrd(const pbrd b)

printf("\n");

for (i = 0; i < _board_; i++)

if (i == 0)printf(" player: *");

if (i == 1)printf(" ai: @");

printf("\n\n");

}}void pcrand(pbrd b)}}

}int player(pbrd b)

b->show[i][j] = b->player;

return 1;

}int judge(pbrd b, int n)

}for (j = 0; j < _board_; j++)

}sum = www.cppcns.com0;

for (i = 0, j = 0; i < _board_; 程式設計客棧i++, j++)

sum = 0;

for (i = 0, j = _board_ - 1; i < _board_; i++, j--)

return 0;

}void menu(pbrd b)

else

if (judge(b, n))}}

main.c

程式入口

#include"game.h"

int main()

執行截圖

本文標題: c語言實現控制台五子棋小遊戲

本文位址: /ruanjian/c/411668.html

c 控制台五子棋

用控制台來寫五子棋練習。下面是原始碼 首先我寫了個board類,用來處理跟棋盤有關的東西 其成員有 private int checkerboard new int 20,20 public enum checker private void show public void update int ...

控制台版五子棋實現

using system using system.collections.generic using system.linq using system.text using system.threading.tasks namespace 五子棋 操作記錄 public static listcz...

C語言實現 五子棋遊戲

之前我們實現了關於電腦版的三子棋的遊戲玩法和思路,今天我們來實現五子棋的玩法和思路 和三子棋的很多思路很相似 define crt secure no warnings include include include define row 10 define col 10 char border r...