1. 程式人生 > >五子棋 (用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 檔案

#include "game.h"


void init_board(char board[ROWS][COLS], int row, int col)
{
	memset(board, ' ', col*row*sizeof(board[0][0]));
}
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("%d%d", &x, &y);
	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)
{
	while(1)
	{
		int x = rand()%row;
		int y = rand()%col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '*';
			break;
		} 
	}
	printf ("應該到電腦走了!\n");
}

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檔案

#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;
		/*if ((check_win(board, ROWS, COLS)) != ' ')
        {
            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;
		/*if ((check_win(board, ROWS, COLS)) != ' ')
        {
            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 ("請選擇^_^:");
		scanf("%d",&input);
		switch(input)
		{
			case 1:
				game ();
				break;
			case 0:
				break;
			default:
				printf("選擇錯誤\n");
				break;
		}
	}
	while(input);
	return 0;
}

來讓我們一起玩一下游戲吧!

玩家贏了哦^_^

很遺憾,電腦贏了

哎呀,平局了哦