简单的五子棋游戏(C语言)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?五子棋游戲
五子棋小程序?qū)τ诔鯇W(xué)C語言的碼農(nóng)來說,是一個(gè)不錯(cuò)的鍛煉項(xiàng)目。下面我來簡(jiǎn)單介紹一下我寫五子棋的簡(jiǎn)單程序。
頭文件(game.h)
此部分主要是頭文件還有調(diào)用函數(shù)的頭文件,不管怎么樣,都是頭文件。
#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); //玩家走的函數(shù)
void computer_move(char board[ROWS][COLS], int row, int col); //電腦走的函數(shù)
static int is_full(char board[ROWS][COLS], int row, int col); // 判斷棋盤是否已滿函數(shù)
char check_win(char board[ROWS][COLS], int row, int col); //判斷輸贏函數(shù)#endif
游戲的函數(shù)(game.c)
這部分是游戲的幾乎所以需要調(diào)用的函數(shù)。包括初始化五子棋棋盤的函數(shù),打印五子棋棋盤函數(shù),玩家走的函數(shù),電腦走的函數(shù),判斷棋盤是否已滿函數(shù),判斷輸贏函數(shù)。
在初始化棋盤的時(shí)候用到memset(),引用百度百科的介紹。
void *memset(void *s, int ch,?size_t?n);
函數(shù)解釋:將s中當(dāng)前位置后面的n個(gè)字節(jié) (typedef unsigned int size_t )用 ch 替換并返回 s 。
memset:作用是在一段內(nèi)存塊中填充某個(gè)給定的值,它是對(duì)較大的結(jié)構(gòu)體或數(shù)組進(jìn)行清零操作的一種最快方法。
棋盤的大小可以通過更改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中當(dāng)前位置后面的n個(gè)字節(jié) (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("玩家下棋,輸入坐標(biāo)(中間加空格):");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("輸入錯(cuò)誤,請(qǐng)重新輸入!");}}else{printf("輸入錯(cuò)誤,請(qǐng)重新輸入!");}}
}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 ' ';
}
主函數(shù)(test.c)
此部分主要就一個(gè)main()主函數(shù)。程序開始的入口。
#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("請(qǐng)輸入1或者0:");scanf_s("%d", &input);switch (input){case 1:game();break;case 0:break;default:printf("選擇錯(cuò)誤\n");break;}} while (input);return 0;
}
這個(gè)五子棋游戲最大的缺陷就是電腦下棋是隨機(jī)的。因此沒有難度,也沒有意思。還望各位大神多多指教。
總結(jié)
以上是生活随笔為你收集整理的简单的五子棋游戏(C语言)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【渝粤教育】国家开放大学2018年春季
- 下一篇: 【渝粤教育】国家开放大学2018年春季