1. 程式人生 > >三子棋,小試牛刀

三子棋,小試牛刀

三子棋的初始版

表面結構

#include "game.h"
void menu()
{
	printf("  ★★★★★★★★★★★★★★★★★★\n");
	printf(" ★★★                          ★★★\n");
	printf("★★★  1.開始遊戲     0.結束遊戲 ★★★\n");
	printf(" ★★★                          ★★★\n");
	printf("  ★★★★★★★★★★★★★★★★★★\n");
	printf("請選擇:");
}
void game()
{
	char board[ROW][LINE] = { 0 };            //建立陣列(放棋子)
	Initboard(board, ROW, LINE);              //初始化棋盤
	Printfboard(board, ROW, LINE);            //列印棋盤
	while (1)
	{
		Player_Runboard(board, ROW, LINE);                   //玩家開始下棋
		computer_Runboard(board, ROW, LINE);                 //電腦開始下棋
		Printfboard(board, ROW, LINE);
		judgeboard(board, ROW, LINE);                       //判斷輸贏
		if (judgeboard(board, ROW, LINE) == 'X')
		{
			printf("玩家贏!!\n");
			break;
		}
		else if (judgeboard(board, ROW, LINE) == '@')
		{
			printf("電腦贏!!\n");
			break;
		}
	}
}
void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出遊戲!!\n");
			break;
		default:
			printf("選擇錯誤!!\n");
			break;
		}
	} while (input);
}
int main()
{
	test();
	return 0;
}

隱藏函式

#include "game.h"
void Initboard(char board[ROW][LINE], int row, int line)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < line; j++)
		{
			board[i][j] = ' ';
		}
	}
}
void Printfboard(char board[ROW][LINE], int row, int line)
{
	int i = 0;
	int j = 0;
	for (i = 0; i<row; i++)
	{
		for (j = 0; j< line; j++)
		{
			printf(" %c ",board[i][j]);
			if (j<line-1)
				printf("|");
		}
		printf("\n");
		if (i < row - 1)
		{
			for (j = 0; j< line; j++)
			{
				printf("---");
				if (j<line - 1)
					printf("|");
			}
		}
		printf("\n");
	}
}
void Player_Runboard(char board[ROW][LINE], int row, int line)
{
	int x = 0;
	int y = 0;
	printf("玩家走:\n");
	while (1)
	{
		printf("請輸入座標:");
		scanf("%d%d", &x, &y);
		if ((x >= 1 && x <= (row + 1)) && (y >= 1 && y <= (line + 1)))
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = 'X';
				break;
			}
			else
			{
				printf("座標已被佔用\n");
			}
		}
		else
		{
			printf("座標非法\n");
		}
	}
}
void computer_Runboard(char board[ROW][LINE])
{
	int x = 0;
	int y = 0;
	printf("電腦走:\n");
	while (1)
	{
		x = rand() % ROW;
		y = rand() % LINE;
		if (board[x][y] == ' ')
		{
			board[x][y] = '@';
			break;
		}
	}
}
char judgeboard(char board[ROW][LINE], int row, int line)
{
	int i = 0;
	for (i = 0; i < row; i++)                 //橫向判斷
	{
		if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][1] !=' ')
		{
			return board[1][1];
		}
	}
	for (i = 0; i < row; i++)                 //豎向判斷
	{
		if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] != ' ')
		{
			return board[1][1];
		}
	}
	if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != ' ')  // 右斜判斷
	{
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')  // 左斜判斷
	{
		return board[1][1];
	}
}

標頭檔案與函式的宣告

#ifndef __GAME_H__
#define __GAME_H__
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW  3
#define LINE 3
void Initboard(char board[ROW][LINE], int row, int line);
void Printfboard(char board[ROW][LINE], int row, int line);
void Player_Runboard(char board[ROW][LINE], int row, int line);
void computer_Runboard(char board[ROW][LINE], int row, int line);
char judgeboard(char board[ROW][LINE], int row, int line);

#endif  //__GAME_H__

初步執行結果