C語言實現(xiàn)雙人五子棋游戲
本文實例為大家分享了C語言實現(xiàn)雙人五子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下
實現(xiàn)功能
生成棋盤玩家1與玩家2對戰(zhàn),哪個玩家率先有連續(xù)5子連線,哪個玩家贏。

如何實現(xiàn)
組成:
二維數(shù)組:board[ROW][COL],定義一個ROW*COL的棋盤。
主要邏輯:
顯示棋盤,提示用戶下子,下子后判斷
1.顯示棋盤很簡單,慢慢湊棋盤就好
2. 用戶下子,注意兩個條件:棋子在棋盤里,下子位置未被占用。
3.判斷是最難的,
方法:從下子位置的8個方向(上,下,左,右,右上,右下,左上,左下)計算相同棋子數(shù)目,然后將對角的棋子數(shù)相加,等于5說明有5子連線
主要函數(shù)中用到三個主要實現(xiàn)函數(shù):
Showboard(board, ROW, COL);//展示棋盤 Playermove(board, ROW, COL, cur);//玩家下注,cur表示哪個玩家下子 Judge(board, ROW, COL);//判斷5子連線 Getcount(board[][COL], row, col, dir)//計算方向dir相同棋子數(shù)
代碼
頭文件
#ifndef __FIVECHREE_H__ #define __FIVECHEE_H__ #include<stdio.h> #include<windows.h> #pragma warning(disable:4996) #define ROW 10//棋盤行數(shù) #define COL 10//棋盤列數(shù) #define INIT '*'//棋盤初始化 #define PLAYER1 1 #define PLAYER2 2 #define NEXT 3//繼續(xù)往下下 #define DRAW 4//棋盤下滿 平局 //8個方向 #define UP 10 #define RIGHT_UP 11 #define RIGHT 12 #define RIGHT_DOWN 13 #define DOWN 14 #define LEFT_DOWN 15 #define LEFT 16 #define LEFT_UP 17 extern void Menu(); extern void Game(); #endif
main函數(shù)源文件
#include"fivechree.h"
int main(){
int quit = 0;
while (!quit){
Menu();
int select = 0;
scanf("%d", &select);
switch (select){
case 1:
Game();
break;
case 2:
quit = 1;
break;
default:
printf("Enter Error!\n");
break;
}
}
printf("Byebye\n");
system("pause");
return 0;
}
函數(shù)定義源文件
#include"fivechree.h"
static int x = 0;
static int y = 0;
void Menu(){
printf("+---------------------+\n");
printf("+- 1.Play 2.Exit -+\n");
printf("+---------------------+\n");
printf("Please Enter Your Select#");
}
static void Showboard(int board[][COL], int row, int col){//展示棋盤 o玩家1棋子,x玩家2棋子
system("cls");
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
switch (board[i][j]){
case PLAYER1:
board[i][j] = 'o';
break;
case PLAYER2:
board[i][j] = 'x';
break;
case 0:
board[i][j] = INIT;
break;
default:
break;
}
}
}
printf(" ");
for (int i =1; i <= row; i++){
printf("%2d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++){
printf("%-2d", i);
for (int j = 1; j <= col; j++){
printf(" %c ", board[i - 1][j - 1]);
}
printf("\n");
}
}
static void Playermove(int board[][COL], int row, int col, int who){//玩家下子,who 為哪個玩家下子
while (1){
printf("Please Enter PLAYER%d Postion<x,y>#", who);
scanf("%d %d", &x, &y);
if (x<1 || x>row || y<1 || y>col){ //超過棋盤范圍
printf("Postion is error!\n");
continue;
}
if (board[x - 1][y - 1] == INIT){//判斷位置是否已被下子
board[x - 1][y - 1] = who;
break;
}
printf("Postion is not empty\n");
}
}
static int Getcount(int board[][COL], int row, int col, int dir){//判斷8個方向相同棋子的數(shù)目
int _x = x;//_x,_y變化,后面與x,y棋子相比較
int _y = y;
int count = 0;
while (1){
switch (dir){
case UP:
_x--;
break;
case DOWN:
_x++;
break;
case LEFT:
_y--;
break;
case RIGHT:
_y++;
break;
case RIGHT_UP:
_x--, _y++;
break;
case RIGHT_DOWN:
_x++, _y++;
break;
case LEFT_DOWN:
_x++, _y--;
break;
case LEFT_UP:
_x--, _y--;
break;
default:
break;
}
if (_x>=1 || _x<=row || _y>=1 || _y<=col){//棋子不能越界
if (board[x-1][y-1] == board[_x-1][_y-1]){
//printf("yes\n");
count++;
}
else{
//printf("no\n");
break;
}
}
else{
return count;
}
}
return count;
}
//如何判斷:從下子位置的8個方向(上,下,左,右,右上,右下,左上,左下)
//計算相同棋子數(shù)目,然后將對角的棋子數(shù)相加,等于5說明有5子連線
static int Judge(int board[][COL], int row, int col){
int count1 = Getcount(board, row, col, UP)\
+ Getcount(board, row, col, DOWN);
//printf("%d\n", count1);
if (count1 >= 4){
return board[x-1][y-1];
}
count1 = Getcount(board, row, col, RIGHT_UP)\
+ Getcount(board, row, col, LEFT_DOWN);
//printf("%d\n", count1);
if (count1 >= 4){
return board[x-1][y-1];
}
count1 = Getcount(board, row, col, RIGHT)\
+ Getcount(board, row, col, LEFT);
//printf("%d\n", count1);
if (count1 >= 4){
return board[x-1][y-1];
}
count1 = Getcount(board, row, col, RIGHT_DOWN)\
+ Getcount(board, row, col, LEFT_UP);
if (count1 >= 4){
return board[x-1][y-1];
}
for (int i = 0; i < row; i++){//判斷棋盤是否下滿
for (int j = 0; j < col; j++){
if (board[i][j] == INIT){
return NEXT;
}
}
}
return DRAW;
}
void Game(){
int board[ROW][COL] = { 0 };
//memset(board, INIT, ROW*COL);
int result = 0;
int cur = PLAYER1;
Showboard(board, ROW, COL);//先展示棋盤
while (1){
//Showboard(board, ROW, COL);
Playermove(board, ROW, COL, cur);
Showboard(board, ROW, COL);//棋盤將board數(shù)組變化,所以要在判斷前將數(shù)組變化
result = Judge(board, ROW, COL);
if (result != NEXT){
break;
}
cur = (cur == PLAYER1 ? PLAYER2 : PLAYER1);//三目表達式,注意不是 PLAYER1 ? PLAYER2 : PLAYER1
}
Showboard(board, ROW, COL);
switch (result){
case 'o':
printf("Player1 Win!\n");
break;
case 'x':
printf("Player2 Win!\n");
break;
case DRAW:
printf("Tie Game!\n");
break;
default:
//printf("%c\n", result);
printf("BUG\n");
break;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實現(xiàn)LeetCode(152.求最大子數(shù)組乘積)
這篇文章主要介紹了C++實現(xiàn)LeetCode(152.求最大子數(shù)組乘積),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07
Visual?Studio2022的完全卸載及安裝到D盤的操作方法
這篇文章主要介紹了Visual?Studio2022的完全卸載以及完全安裝到D盤,因為VS如果隨便寫在會有很多很多的亂七八糟的東西掉出來,所以我們選擇制式一點的卸載方式,需要的朋友可以參考下2022-09-09

