1. 程式人生 > >C語言實現簡單的三子棋小程式

C語言實現簡單的三子棋小程式

1 game.h中用於函式定義

#ifndef __GAME_H__
#define __GAME_H__

#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>

#define ROW 6
#define COL 6

void InitBoard(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_H__

2.game.c實現遊戲的函式

#define _CRT_SECURE_NO_WARNINGS
#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 \n",board[i][0],board[i][1],board[i][2]);
	if(i<(row-1))
	{
	printf("---|---|---\n");
	}
	}*/
	int i = 0;
	for (i = 0; i<row; i++)
	{
		int j = 0;
		for (j = 0; j<col; j++)
		{
			printf(" %c |", board[i][j]);
		}
		printf("\n");
		for (j = 0; j<col; j++)
		{
			if (i<(row - 1))
			{
				printf("---|");
			}
		}
		printf("\n");
	}
}
void PlayerMove(char board[ROW][COL], int row, int col)   //玩家走的函式
{
	int x = 0;
	int y = 0;
	while (1)
	{
		printf("玩家走,請輸入橫縱座標》\n");
		scanf("%d%d", &x, &y);
		if ((x >= 0 && x <= row) && (y >= 0 && y <= col))
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = 'x';
				break;
			}
			else
			{
				printf("此處已走過,請重新走\n");
			}
		}
		else
		{
			printf("輸入有誤,請重新輸入\n");
		}
	}
}
void Computermove(char board[ROW][COL], int row, int col)   //電腦走的函式
{
	while (1)
	{
		int x = rand() % 3;
		int y = rand() % 3;
		if (board[x][y] == ' ')
		{
			board[x][y] = '0';
			break;
		}
	}
}
static int  is_full(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++)
		{
			if (board[i][j] == ' ')  //棋盤還有空位置
				return 0;
		}
	}
	return 1;     //棋盤已滿 
}
char IsWin(char board[ROW][COL], 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][0] != ' ')
	  {
	      return board[i][0];
	  }
	}
	for(i=0;i<col;i++)   //縱向三子贏了
	{
	  if(board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
	  {
	      return board[0][i];
	  }
	}
	//斜向三子贏了
	if(board[0][0] == board[1][1] && board[1][1] == 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];
	}

	//玩家贏   電腦贏
	//int i = 0;
	int j = 0;
	for (i = 0; i<row; i++)  //橫向三子贏了
	{
		for (j = 0; j<col; j++)
		{
			if ((board[i][j] == board[i][j + 1] &&
				board[i][j + 1] == board[i][j + 2] && board[i][i] != ' ') ||
				(board[i][j] == board[i][j - 1] &&
				board[i][j - 1] == board[i][j + 1] && board[i][i] != ' ') ||
				(board[i][j] == board[i][j - 1] &&
				board[i][j - 1] == board[i][j - 2] && board[i][i] != ' '))
			{
				return board[i][j];
			}
			if ((board[i][j] == board[i + 1][j] &&
				board[i + 1][j] == board[i + 2][j] && board[i][j] != ' ') ||
				(board[i][j] == board[i + 1][j] &&
				board[i + 1][j] == board[i - 1][j] && board[i][j] != ' ') ||
				(board[i][j] == board[i - 1][j] &&
				board[i - 1][j] == board[i - 2][j] && board[i][j] != ' '))
			{
				return board[i][j];
			}
			//斜向三子贏了
			if ((board[i][j] == board[i - 1][j + 1] &&
				board[i - 1][j + 1] == board[i + 1][j - 1] && board[i][j] != ' ') ||
				(board[i][j] == board[i - 1][j + 1] &&
				board[i - 1][j + 1] == board[i + 1][j - 1] && board[i][j] != ' ') ||
				(board[i][j] == board[i + 1][j - 1] &&
				board[i + 1][j - 1] == board[i + 2][j - 2] && board[i][j] != ' '))
			{
				return board[i][j];
			}
			if ((board[i][j] == board[i + 1][j + 1] &&
				board[i + 1][j + 1] == board[i + 2][j + 2] && board[i][j] != ' ') ||
				(board[i][j] == board[i - 1][j - 1] &&
				board[i - 1][j - 1] == board[i + 1][j + 1] && board[i][j] != ' ') ||
				(board[i][j] == board[i - 1][j - 1] &&
				board[i - 1][j - 1] == board[i - 2][j - 2] && board[i][j] != ' '))
			{
				return board[i][j];
			}
		}
	}
	if (is_full(board, row, col))
	{
		return 'Q';            //平局
	}
	return ' ';  //繼續遊戲
}



3.test.c用來存放主函式


#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()  //列印選單 
{
	printf("*****************************\n");
	printf("***  歡迎進入三子棋介面  ****\n");
	printf("*****************************\n");
	printf("****  1 play     0 exit  ****\n");
	printf("*****************************\n");
}
void game()
{
	char win = 0;
	int c;
	char board[ROW][COL] = { 0 };
	InitBoard(board, ROW, COL);
	DisplayBoard(board, ROW, COL);
	printf("1 玩家先走   2 電腦先走   0 退出遊戲\n");
	scanf("%d", &c);
	switch (c)
	{
	case 1:
		printf("玩家先走:\n");
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		win = IsWin(board, ROW, COL);
		if (win != ' ')
		{
			break;
		}
	case 2:
		printf("電腦先走:\n");
		Computermove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		win = IsWin(board, ROW, COL);
		if (win != ' ')
		{
			break;
		}
	}
	while (1)
	{
		printf("玩家走》\n");
		PlayerMove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		win = IsWin(board, ROW, COL);
		if (win != ' ')
		{
			break;
		}
		printf("電腦走》\n");
		Computermove(board, ROW, COL);
		DisplayBoard(board, ROW, COL);
		win = IsWin(board, ROW, COL);
		if (win != ' ')
		{
			break;
		}
	}
	if (win == 'x')
	{
		printf("玩家贏了\n");
	}
	else if (win == '0')
	{
		printf("電腦贏了\n");
	}
	else
	{
		printf("平局\n");
	}
}
void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do{
		menu();
		printf("請選擇:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出遊戲\n");
			break;
		default:
			printf("您的輸入有誤,請重新輸入!\n");
		}
	} while (input);
}
int main()
{
	test();
	system("pause");
	return 0;
}