1. 程式人生 > >三字棋遊戲的簡單實現

三字棋遊戲的簡單實現

三字棋遊戲:玩家與電腦對抗,棋盤是3*3的小方格,當任一行或任一列,或者正對角線,或者副對角線棋子個數為三或棋子型別完全一致,則該把棋局就贏了,但是如果棋盤滿了有沒有贏,則為平局。下面我們就開始我們的程式設計:

chess.h 標頭檔案(用於函式的宣告)

chess.c 原始檔(用於函式的定義)

main.c  原始檔(用於主函式的實現)

程式碼如下:

chess.h 標頭檔案(用於函式的宣告)

#ifndef __CHESS_H_
#define __CHESS_H_

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#pragma warning(disable:4996)

#define ROWS 3  //三字棋   //5 五子棋
#define COLS 3  //三字棋   //5 五子棋

void show(char showboard[][COLS], int rows, int cols);
char play(char showboard[][COLS], int rows, int cols);
void game();




#endif //__CHESS_H_

 

chess.c   原始檔(用於函式的定義【實現】)

#include "chess.h"

void show(char showboard[][COLS], int rows, int cols)
{
	int i = 0;
	for (; i < ROWS; i++)
	{
		int j = 0;
		for (; j < COLS; j++)
		{
			if (j == COLS - 1){
				printf(" %c ", showboard[i][j]);
			}
			else{
				printf(" %c |", showboard[i][j]);
			}
		}
		printf("\n");
		for (j = 0; j < COLS; j++){
			if (i < ROWS - 1){
				if (j < COLS - 1){
					printf("---|");
				}
				else{
					printf("---");
				}
			}
		}
		printf("\n");
	}
}

static int IsWin(char showboard[][COLS], int rows, int cols)
{
	int i, j;
	for (i = 0; i < rows; i++)
	{
		if ((showboard[i][0] != ' ') && (showboard[i][0] == showboard[i][1]) && (showboard[i][1] == showboard[i][2]))
		{
			return 1;
		}
	}
	for (j = 0; j < cols; j++)
	{
		if ((showboard[0][j] != ' ') && (showboard[0][j] == showboard[1][j]) && (showboard[1][j] == showboard[2][j]))
		{
			return 1;
		}
	}
	//正對角線
	int count = 0;
	for (i = 0; i < rows; i++){
		if ((showboard[i][i] == '*')){
			count++;
		}
	}
	if (count == rows){
		return 1;
	}

	count = 0;
	for (i = 0; i < rows; i++){
		if ((showboard[i][i]) == '#'){
			count++;
		}
	}
	if (count == rows){
		return 1;
	}
	//反對角線
	count = 0;
	j = 0;
	for (i = rows - 1; i >= j; i--){  //只用檢查到兩個座標相等即可
		if ((showboard[i][j] == showboard[j][i]) && (showboard[i][j] == '*')){
			count++;
		}
		j += 1;
	}
	if (count == j){
		return 1;
	}

	count = 0;
	j = 0;
	for (i = rows - 1; i >= j; i--){
		if ((showboard[i][j] == showboard[j][i]) && (showboard[i][j] == '#')){
			count++;
		}
		j++;
	}
	if (count == j){
		return 1;
	}

	return 0;
}

static int IsFull(char showboard[][COLS], int rows, int cols)
{
	int i, j;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			if (showboard[i][j] == ' ')
			{
				return 0;
			}
		}
	}
	return 1;
}

char play(char showboard[][COLS], int rows, int cols)
{
	//電腦先走
	srand((unsigned int)time(NULL));
	show(showboard, ROWS, COLS);
	while (1){
		printf("computer play> ");
		Sleep(1000);
		while (1){
			int x = rand() % (rows + 1);//隨機產生0-rows的數字
			int y = rand() % (cols + 1);//隨機產生0-cols的數字
			if ((x > 0) && (y > 0) && (showboard[x - 1][y - 1] == ' ')){
				printf("%d %d\n\n", x, y);
				showboard[x - 1][y - 1] = '*';
				Sleep(1000);
				system("CLS");
				break;
			}
		}
		int ret = IsWin(showboard, rows, cols);
		int tmp = IsFull(showboard, rows, cols);
		if (ret == 1)
		{
			return '*';//電腦勝了
		}
		else if (tmp == 1){
			return 'p';
		}
		//玩家走
		else if (ret == 0){
			show(showboard, ROWS, COLS);
			while (1){
				printf("Dear user play> ");
				int x, y;
				scanf("%d%d", &x, &y);
				Sleep(1000);
				system("CLS");
				if ((x > 0 && x <= rows) && (y > 0 && y <= cols) && (showboard[x - 1][y - 1] == ' ')){
					showboard[x - 1][y - 1] = '#';
					show(showboard, ROWS, COLS);
					break;
				}
				else{
					printf("can't push, try again> \n");
					show(showboard, ROWS, COLS);
				}
			}
			int ret = IsWin(showboard, rows, cols);
			int tmp = IsFull(showboard, rows, cols);
			if (ret == 1)
			{
				return '#';//玩家勝了
			}
			else if (tmp == 1)
			{
				return 'p';
			}
		}
	}
	return 0;
}

void game()
{
	//定義棋盤陣列
	char showboard[ROWS][COLS] = { 0 };
	//初始化棋盤
	memset(showboard, ' ', sizeof(showboard));
	//列印棋盤
	//show(showboard, ROWS, COLS);
	//開始下棋
	system("CLS");
	char ret = play(showboard, ROWS, COLS);
	if ('*' == ret)//返回*則電腦贏了
	{
		//列印棋盤
		show(showboard, ROWS, COLS);
		printf("computer win !!!\n");
	}
	else if ('p' == ret)//返回b平局
	{
		show(showboard, ROWS, COLS);
		printf("balance !!!\n");
	}
	else
	{
		//show(showboard, ROWS, COLS);
		printf("you win !!!\n");
	}
}

 

main.c  原始檔(用於主函式的實現)

#include "chess.h"

void menu()
{
	printf("************************\n");
	printf("*** 1. play  2.exit  ***\n");
	printf("************************\n");
	printf("please select number<1,2> ");
}

int main()
{
	int i = 0;
	menu();
	scanf("%d", &i);
	switch (i)
	{
	case 1:
		game();
		break;
	case 2:
		exit(0);
		break;
	default:
		break;
	}

	system("pause");
	return 0;
}

此程式設計還是很冗餘,效能不夠優越,以後慢慢不斷更新此版本,

注意:此程式既可實現三字棋,也可實現五子棋,只需要改變標頭檔案中的巨集,ROWS 和 COLS  的值就可以了。

 

原始碼見GitHub連線:https://github.com/xiaobaiyuan-bit/chess_3/commit/5c39d152c2aba539d4e639d57a39113be1330c07

請大家多提建議,多多指教,謝謝!