日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

简单的五子棋游戏(C语言)

發布時間:2023/12/2 综合教程 27 生活家
生活随笔 收集整理的這篇文章主要介紹了 简单的五子棋游戏(C语言) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?五子棋游戲

五子棋小程序對于初學C語言的碼農來說,是一個不錯的鍛煉項目。下面我來簡單介紹一下我寫五子棋的簡單程序。

頭文件(game.h)

此部分主要是頭文件還有調用函數的頭文件,不管怎么樣,都是頭文件。

#ifndef __GAME_H__  
#define __GAME_H__  enum OPTION     //枚舉
{EXIT,PLAY
};#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <time.h>  #define ROWS 5  
#define COLS 5  void init_board(char board[ROWS][COLS], int row, int col);  //初始化棋盤
void display_board(char board[ROWS][COLS], int row, int col);  //展示棋盤
void player_move(char board[ROWS][COLS], int row, int col);  //玩家走的函數
void computer_move(char board[ROWS][COLS], int row, int col);  //電腦走的函數
static int is_full(char board[ROWS][COLS], int row, int col);  // 判斷棋盤是否已滿函數
char check_win(char board[ROWS][COLS], int row, int col);  //判斷輸贏函數#endif  

游戲的函數(game.c)

這部分是游戲的幾乎所以需要調用的函數。包括初始化五子棋棋盤的函數,打印五子棋棋盤函數,玩家走的函數,電腦走的函數,判斷棋盤是否已滿函數,判斷輸贏函數。

在初始化棋盤的時候用到memset(),引用百度百科的介紹。

void *memset(void *s, int ch,?size_t?n);

函數解釋:將s中當前位置后面的n個字節 (typedef unsigned int size_t )用 ch 替換并返回 s 。

memset:作用是在一段內存塊中填充某個給定的值,它是對較大的結構體或數組進行清零操作的一種最快方法。

棋盤的大小可以通過更改ROWS和COLS的值來改變行和列。

#include "game.h"  void init_board(char board[ROWS][COLS], int row, int col)
{memset(board, ' ', col*row*sizeof(board[0][0]));  //初始化棋盤,將board中當前位置后面的n個字節 (col*row*sizeof(board[0][0])用space 替換并返回 board
}
void display_board(char board[ROWS][COLS], int row, int col)
{int i = 0;for (i = 0; i<row; i++){printf(" %c | %c | %c | %c | %c \n", board[i][0], board[i][1], board[i][2], board[i][3], board[i][4]); if (i != 4)printf("---|---|---|---|---\n");}
}
void player_move(char board[ROWS][COLS], int row, int col)
{int x = 0;int y = 0;while (1){printf("玩家下棋,輸入坐標(中間加空格):");scanf_s("%d%d", &x, &y);//scanf_s是vs要求,別的編譯器用scanf。x--;y--;if (((x >= 0) && (x<row) && (y >= 0) && (y<col))){if (board[x][y] == ' '){board[x][y] = '#';break;}else{printf("輸入錯誤,請重新輸入!");}}else{printf("輸入錯誤,請重新輸入!");}}
}void computer_move(char board[ROWS][COLS], 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 is_full(char board[ROWS][COLS], int row, int col)
{int i = 0;int j = 0;for (i = 0; i<row; i++){for (j = 1; j<col; j++){if (board[i][j] == ' ')return 0;}}return 1;
}char check_win(char board[ROWS][COLS], 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][2] == board[i][3])&& (board[i][3] == board[i][4])&& (board[i][4] != ' '))return board[i][1];}for (i = 0; i<col; i++){if ((board[0][i] == board[1][i])&& (board[1][i] == board[2][i])&& (board[2][i] == board[3][i])&& (board[3][i] == board[4][i])&& (board[4][i] != ' '))return board[1][i];}if ((board[0][0] == board[1][1])&& (board[1][1] == board[2][2])&& (board[2][2] == board[3][3])&& (board[3][3] == board[4][4])&& (board[4][4] != ' '))return board[1][1];if ((board[0][4] == board[1][3])&& (board[1][3] == board[2][2])&& (board[2][2] == board[3][1])&& (board[3][1] == board[4][0])&& (board[4][0] != ' '))return board[1][1];if (is_full(board, row, col)){return 'q';}return ' ';
}

主函數(test.c)

此部分主要就一個main()主函數。程序開始的入口。

#include "game.h"  void game()
{char board[ROWS][COLS] = { 0 };char ret = 0;init_board(board, ROWS, COLS);display_board(board, ROWS, COLS);srand((unsigned int)time(NULL));while (1){player_move(board, ROWS, COLS);if ((ret = check_win(board, ROWS, COLS)) != ' ')break;display_board(board, ROWS, COLS);computer_move(board, ROWS, COLS);if ((ret = check_win(board, ROWS, COLS)) != ' ')break;display_board(board, ROWS, COLS);}if (ret == '#'){printf("玩家贏\n");}else if (ret == '*'){printf("電腦贏\n");}else if (ret == 'q'){printf("平局\n");}display_board(board, ROWS, COLS);
}void menu()
{printf("***************五子棋游戲**************\n");printf("***************記得選擇哦**************\n");printf("***********(1.play  0.exit)**********\n");printf("**************祝您玩的愉快*************\n");
}int main()
{int input = 0;do{menu();printf("請輸入1或者0:");scanf_s("%d", &input);switch (input){case 1:game();break;case 0:break;default:printf("選擇錯誤\n");break;}} while (input);return 0;
}

這個五子棋游戲最大的缺陷就是電腦下棋是隨機的。因此沒有難度,也沒有意思。還望各位大神多多指教。

總結

以上是生活随笔為你收集整理的简单的五子棋游戏(C语言)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。