欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

三子棋的实现

程序员文章站 2024-03-18 20:25:16
...

C语言实现三子棋

 

C语言实现三子棋关键是运用到二维数组的知识,使用多文件编程来实现这个程序,我们需要建立一个头文件,两个源文件来实现:

main.c
chess.c
chess.h

如下图所示,玩家的棋为字符 ‘x’,电脑的棋为字符 ‘0’ 

 

                                 三子棋的实现

接下来,我们看一下具体的代码实现:

首先,要完成对头文件的声明:

#ifndef _CHESS_H_
#define _CHESS_H_

#include <stdio.h>
#include<windows.h>
#include <time.h>

#define ROW 3
#define COL 3

#define PLAYER_COLOR 'X'
#define COMPUTER_COLOR 'O'

#pragma warning(disable:4996)

void Game();
void InitBoard(char board[][COL], int row, int col);
void ShowBoard(char board[][COL], int row, int col);
void PlayerMove(char board[][COL], int row, int col);
void Judge(char board[][COL], int row, int col);
void ComputerMove(char board[][COL], int row, int col);



#endif

 

 两个源文件:

#include "chess.h"
void InitBoard(char board[][COL], int row, int col)
{
	int i = 0;
	for (; i < row; i++)
	{
		int j = 0;
		for (; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}
void ShowBoard(char board[][COL], int row, int col)
{
	int i;
	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");
	}
}
void PlayerMove(char board[][COL], int row, int col)
{
	while (1)
	{
		int x = 0;
		int y = 0;
		printf("please enter pos<x,y>:  ");
		scanf("%d %d", &x, &y);
		if (x >= 1  &&  x <= 3  &&  y >= 1  &&  y <= 3 )
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = PLAYER_COLOR;
				break;
			}
			else{
				printf("Enter error,Try Again!\n");
			}
		}
	}
}

int FullBoard(char Board[][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 Judge(char Board[][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] && Board[i][0] != ' '){
			return Board[i][0];
		}
	}
	for (i = 0; i < col; i++){     
		if (Board[0][j] == Board[1][j] && Board[1][j] == Board[2][j] && Board[0][j] != ' '){
			return Board[0][j];
		}
	}
	if (Board[0][0] == Board[1][1] && Board[1][1] == Board[2][2] && Board[0][0] != ' '){    
		return Board[0][0];
	}
	else if (Board[0][2] == Board[1][1] && Board[1][1] == Board[2][0] && Board[2][0] != ' '){
		return Board[0][2];
	}
	if (FullBoard){  
		return 'N';
	}
	else{
		return 'E';   
	}
}

int GetRandom(int start, int end)
{
	return rand() % end+1;
}
void ComputerMove(char board[][COL], int row, int col)
{
	while (1)
	{
		int x = GetRandom(1,3);
		int y = GetRandom(1,3);
		
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = COMPUTER_COLOR;
				break;
			}
	}
}

void Game()
{
	char result = '\0';
	char board[ROW][COL];
	InitBoard(board, ROW, COL);
	ShowBoard(board, ROW, COL);
	srand((unsigned int)time(NULL));
	while (1){
		PlayerMove(board, ROW, COL);
		ShowBoard(board, ROW, COL);
		result=Judge(board, ROW, COL);
		if (result != 'N'){
			break;
		}
		ComputerMove(board, ROW, COL);
		ShowBoard(board, ROW, COL);
		result=Judge(board, ROW, COL);
		if (result != 'N'){
			break;
		}
	}
	switch (result){
	case 'X': //you win
		break;
	case 'O': //computer win
		break;
	case 'E': //Equal
		break;
	default:
		printf("bug?!\n");
		break;
	}



	//memset(board, ' ', sizeof(board));
	printf("再来一局?\n");
}
#include "chess.h"

void ShowMenu()
{
	printf("####################################\n");
	printf("#####  1.Play         2.Exit  ######\n");
	printf("####################################\n");
	printf("Please Select:  ");
}

int main()
{
	int select = 0;
	int quit = 0;
	while (!quit)
	{
		ShowMenu();
		scanf("%d", &select);
		switch (select)
		{
		case 1:
			Game();
			break;
		case 2:
			printf("byebye!\n");
			quit = 1;
			break;
		default:
			printf("您输入有误,请重新输入!\n");
			break;
		}
	}
	system("pause");
	return 0;
}

 实际上与我们自己的思考一样,计算机采用我们的思维完成了这个游戏:

三子棋的实现