三子棋

BLANK-BLACK發表於2020-12-31

三子棋實現模組``

#define _CRT_SECURE_NO_WARNINGS 1

#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++)
	{	
		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 PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家走\n");
	while (1)
	{
		printf("請輸入座標:>");
		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] = 'O';
				break;
			}
			else
			{
				printf("該位置已被佔用,請重新輸入\n");
			}
		}
		else
		{
			printf("輸入非法,請重新輸入\n");
		}
	}
}
void ComputeMove(char board[ROW][COL], int row, int col)//電腦移動
{
	int x = 0;
	int y = 0;
	printf("電腦走\n");
	while(1)
	
	{
		x = rand() % row;
		y = rand() % col;

		if (board[x][y ] == ' ')
		{
			board[x][y] = 'X';
			break;
		}
	}
}
//返回1表示滿了
//返回0表示沒滿
int IsFull(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][1] != ' ')
		{
			return board[i][1];
		}
	}
	for (i = 0; i < col; i++)//豎三列
	{
		if (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[1][i];
		}
	}
		if(board[0][0]==board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
		{
			return board[1][1];
		}
		if (board[1][1] == board[0][2] && board[1][1] == board[2][0] && board[1][1] != ' ')
		{
			return board[1][1];
		}
		if (1 == IsFull(board, ROW, COL))
		{
			return 'Q';
		}
	return 'C';
}

三子棋主程式模組

 #define _CRT_SECURE_NO_WARNINGS 1
//三子棋遊戲測試
#include "game.h"
void game()
{	
	char ret = 0;
	char board[ROW][COL] = { 0 };//全部空格
	//初始化棋盤
	InitBoard(board , ROW, COL);
	//列印棋盤
	DisplayBoard(board, ROW, COL);
	while (1)
	{
		PlayerMove (board, ROW, COL);//玩家移動
		DisplayBoard (board, ROW, COL);
		ret=IsWin(board, ROW, COL);//判斷玩家贏
		if (ret != 'C')
		{
			break;
		}
		ComputeMove(board, ROW, COL);
		DisplayBoard (board, ROW, COL);
		ret=IsWin(board, ROW, COL);//判斷電腦贏
		if (ret != 'C')
		{
			break;
		}

	}
	if (ret == 'O')
	{
		printf("玩家贏\n");
	}
	else if (ret == 'X')
	{
		printf("電腦贏\n");
	}
	else
	{
		printf("平局\n");
	}
}
void menu()
{
	printf("*****************************\n");
	printf("*****1.PLAY     0.EXIT  *****\n");
	printf("*****************************\n");
}
void test()
{	
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("請選擇:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("三子棋\n");
			game();
			break;
		case 0:
			printf("退出遊戲\n");
			break;
		default:
			printf("輸入錯誤,請重新選擇\n");
			break;
		}
	} while (input);
}
int main()
{	
	
	test();
	return 0;
}

三子棋定義模組

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

//宣告
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 ComputeMove(char board[ROW][COL], int row, int col);
char IsWin(char board[ROW][COL], int row, int col);

相關文章