1. 程式人生 > >c語言實現簡易三子棋遊戲

c語言實現簡易三子棋遊戲

遊戲整體思路:

  1. 建立並輸出棋盤;
  2. 玩家落子並判定輸贏;
  3. 電腦落子並判定輸贏;
  4. 若無人勝則繼續2 ,3步驟直至棋盤滿。

下面是實現程式碼:

//game.h
#ifndef __GAME_H__
#define __GAME_H__

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#define ROW 3
#define COL 3

void setboard(char board[ROW][COL], int row, int col);//初始化棋盤
void displayboard(char board[ROW][COL], int row, int col);//列印棋盤
void playermove(char board[ROW][COL], int row, int col);//玩家落子
void computermove(char board[ROW][COL], int row, int col);//電腦落子
char IsWin(char board[ROW][COL], int row, int col);//輸贏判定

#endif 
//game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include   "game.h"

void setboard(char board[ROW][COL], int row, int col)
{
	memset(board, ' ', ROW*COL*sizeof(board[0][0]));
}
void displayboard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j < col - 1)
				printf("|");
		}
		printf("\n");
		if (i < row - 1)
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
					printf("|");
			}
			printf("\n");
		}	
	}
}
void computermove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("電腦走 >:\n");
	while (1)
	{
		x= rand()%3;//隨機產生座標
		y= rand()%3;
		if (board[x][y] == ' ')  //若座標未被佔用則落子。
		{
			board[x][y] = '0';
			break ;           //落子則跳出迴圈,反之繼續產生有效座標。
		}
	}
}
void playermove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;	
	while (1){
		printf("玩家走>:\n");
		printf("請重新輸入座標:\n");
		scanf("%d %d", &x, &y);
		if (board[x-1][y-1] == ' ')
		{
			board[x-1][y-1] = 'X';
			break;
		}
		printf("輸入錯誤,請重新輸入");
	}
}
char IsWin(char board[ROW][COL], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < 3; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] != ' ')
		{
			return board[i][0];
		}
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[2][i] != ' ')
		{
			return board[0][i];
		}
	}
	if (board[1][1] == board[2][2] && board[2][2] == board[0][0] && board[0][0] != ' ')
	{
		return board[0][0];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ' ')
	{
		return board[2][0];
	}
	//程式到此證明你與電腦未分勝負,接下來判定平局。
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')//棋盤還有空
			{
				return ' ';      //返回遊戲繼續標記
			}
		}
	}
	return 'p';//棋盤沒空了,返回平局標記
}
//test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include   "game.h"

void game()
 {
	char i ;
	char board[ROW][COL];
	setboard(board, ROW, COL);
	displayboard(board, ROW, COL);
	while(1)
	{			
		playermove(board, ROW, COL);
		i=IsWin(board,ROW,COL);
		if (i != ' ')                  //i不為空格則遊戲結束
		{
			break;
		}
		displayboard(board, ROW, COL);	
		computermove(board, ROW, COL);
		displayboard(board, ROW, COL);
		i=IsWin(board, ROW, COL);		
		if (i != ' ')                 //i不為空格則遊戲結束
		{
			break;
		}
	}
	if (i == 'X')
	{
		printf("玩家贏了!!\n");
		displayboard(board, ROW, COL);
	}
	else if(i == '0')
	{
		printf("電腦贏了!!\n");
		displayboard(board, ROW, COL);
	}
	else if (i == 'p')
	{
		printf("平局!!\n");
		displayboard(board, ROW, COL);
	}
}
void  menu()
{
	printf("***********************\n");
	printf("*******1. play*********\n");
	printf("*******0. exit*********\n");
	printf("***********************\n");	
}
int main()
{
	int input = 0;
	srand((unsigned)time(NULL));
	do
	{
		menu();
		printf("請選擇>>:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
		case 0:
			break;
		default:
			printf("輸入錯誤,請重新輸入\n");
		}
	} while (input);
	

	system("pause");
	return 0;
}

遊戲實現效果圖: 程式執行效果圖