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

c語言實現 **三子棋小遊戲**

|

備註都寫在程式碼中 標頭檔案 game.h

#ifndef __GAME_H_
#define __GAME_H_
#define ROW 3
#define COL 3
#define _CRT_SECURE_NO_WARNINGS 
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void InitBoard(char board[ROW][COL], int row, int col);
void DisplayBoard(char board[ROW][COL], int row, int col);
void ComputerMove(char board[ROW][COL], int row, int col);
void PlayerMove(char board[ROW][COL], int row, int col);
int IsFull(char board[ROW][COL], int row, int col);
char IsWin(char board[ROW][COL], int row, int col);


//X 電腦贏
//* 玩家贏
//q 平局,棋盤滿
//  遊戲繼續

#endif __GAME_H_

原始檔game.c

#include "game.h"

//初始化棋盤
void InitBoard(char board[ROW][COL], int row, int col){
	int i = 0;
	int j = 0;
	for (i = 0; i < row;i++){
		for (j = 0; j < col; j++){
			board[i][j] = ' ';
		}
	}
}

//列印棋盤
void DisplayBoard(char board[ROW][COL], int row, int col){
	int i = 0;
	for (i = 0; i < row; i++){
		printf(" %c | %c | %c ", board[i][0], board[i][1], board[i][2]);
		printf("\n");
		if (i < row - 1){
			printf("___|___|___");
			printf("\n");
		}
		if (i==2){
			printf("   |   |   ");
		}
	}
	printf("\n");
}


//電腦走
void ComputerMove(char board[ROW][COL], int row, int col){
	int x = 0;
	int y = 0;
	while (1){
		x = rand() % ROW;
		y = rand() % COL;
		if (board[x][y] == ' '){
			board[x][y] = 'X';
			break;
		}
	}
}


//玩家走
void PlayerMove(char board[ROW][COL], int row, int col){
	int x = 0;
	int y = 0;
	while (1){
		printf("請輸入座標x,y\n");
		scanf("%d,%d", &x, &y);
		if ((x >= 1) && (x <= row) && (y >= 1) && (y <= col)){
			if (board[x-1][y-1] == ' '){
				board[x-1][y-1] = '*';
				break;
			}
		}
		else
			printf("座標輸入有誤,請重新輸入\n");
	}
}

//判斷棋盤是否已滿
int IsFull(char board[ROW][COL], int row, int col){
	int i = 0;
	int j = 0;
	int r = 0;
	for (i = 0; i < row; i++){
		for (j = 0; j < col; j++){
			if (board[i][j] ==' '){
				return 0;
			}
		}
	}
	return 1;
}


//判斷輸贏
char IsWin(char board[ROW][COL], int row, int col){
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++){
		if ((board[i][0] == board[i][1] )&&( board[i][1] == board[i][2])) //橫排一樣
			return board[i][0];
	}
	for (j = 0; j < col; j++){
		if ((board[0][j] == board[1][j] )&& (board[1][j] == board[2][j])) //豎排一樣
			return board[0][j];
	}
	if ((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])){ //對角線一樣
		return board[1][1];
	}
	else if ((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])){ //對角線一樣
		return board[1][1];
	}
	else if (IsFull(board,ROW,COL)==1){  //平局 
		return 'q';
	}
	else//棋盤未滿遊戲繼續
		return ' ';

}

最後是測試程式碼test.c

#include "game.h"

void menu(){
	printf("********************************\n");
	printf("*********三子棋小遊戲***********\n");
	printf("*********1. 開始遊戲 ***********\n");
	printf("*********2. 退出遊戲 ***********\n");
	printf("********************************\n");
}

void game(){
	char board[ROW][COL] = { 0 };
	InitBoard(board,ROW,COL);
	DisplayBoard(board, ROW, COL);
	while(1){
		printf("電腦走->\n");
		ComputerMove(board, ROW, COL); //電腦每走一步判斷一次是否要繼續遊戲
		if (IsWin(board, ROW, COL) != ' '){
			break;
		}
		DisplayBoard(board, ROW, COL);
		
		printf("玩家走->\n");
		PlayerMove(board, ROW, COL);//玩家每走一步判斷一次是否要繼續遊戲
		if (IsWin(board, ROW, COL) != ' '){
			break;
		}
		DisplayBoard(board, ROW, COL);


	}
	if (IsWin(board, ROW, COL) == 'X')
		printf("電腦贏\n");
	else if (IsWin(board, ROW, COL) == '*')
		printf("玩家贏\n");
	else
		printf("平局\n");
}





int main(){
	int input = 0;
	srand((unsigned int)time(NULL));
	do{
		menu();
		printf("請選擇->\n");
		scanf("%d", &input);
		switch (input){
		case 1:
			game();
			break;
		case 0:
			printf("退出遊戲\n");
			break;
		default:
			printf("請輸入1或0\n");
		}
	} while (input);
		return 0;

}