三子棋代码

三子棋:

game.c:

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void InitBoard(char arr[ROW][COL], int row,int col)//初始化

{

        inti = 0;

        for(i = 0; i < row; i++)

        {

                 intj = 0;

                 for(j = 0; j < col; j++)

                 {

                         arr[i][j]= ' ';

                 }

        }

}

void DisplayBoard(char arr[ROW][COL], introw, int col)//打印

{

        inti = 0;

        intj = 0;

        for(i = 0; i < row; i++)

        {

                 for(j = 0; j < col; j++)

                 {

                         printf("%c ", arr[i][j]);

                         if(j < col - 1)

                         printf("|");

                 }

                 printf("\n");

                 if(i < row - 1)

                 {

                         for(j = 0; j < col; j++)

                         {

                                  if(i < row - 1)

                                          printf("---");

                                  if(j < col - 1)

 

                                          printf("|");

                         }

                         printf("\n");

                 }

        }

}

       

 

void PlayerMove(char arr[ROW][COL], introw, int col)//玩家玩

{

        intx = 0;

        inty = 0;

        while(1)

        {

                 printf("请输入坐标:>");

                 scanf("%d%d", &x , &y);

                 if(((x > 0) && (x <= row)) && ((y > 0) && (y<= col)))// 判断坐标是否合理

                 {

                         if(arr[x - 1][y - 1] == ' ')

                         {

                                  arr[x- 1][y - 1] = 'x';

                                  break;

                         }

                         else

                         {

                                  printf("坐标已被占用");

                                 

                         }

                 }

        }

}

void ComputerMove(char arr[ROW][COL], introw, int col)

{

        intx = 0;

        inty = 0;

        while(1)

        {

                 x= rand() % row;

                 y= rand() % col;

                 if(arr[x][y] == ' ')

                 {

                         arr[x][y]= '0';

                         break;

                 }

        }

 

}

int Isfull(char arr[ROW][COL], int row, intcol)

{

        inti = 0;

        intj = 0;

        for(i = 0; i < row; i++)

        {

                 for(j = 0; j < col; j++)

                 {

                         if(arr[i][j] == ' ')

 

                                  return0;

 

        }

        return  1;

}

char IsWine(char arr[ROW][COL], int row,int col)

{

        inti = 0;

        intj = 0;

        for(i = 0; i < row; i++)

        {

                 if((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) &&(arr[i][1] != ' '))

                 {

                         returnarr[i][1];

                 }

        }

        for(j = 0; i < col; j++)

        {

                 if((arr[0][j] == arr[1][j]) && (arr[1][j] == arr[2][j]) &&(arr[1][j] != ' '))

                 {

                         returnarr[1][j];

                 }

        }

        if((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) &&(arr[1][1] != ' '))

        {

                 returnarr[1][1];

        }

        if((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0]) &&(arr[1][1] != ' '))

        {

                 returnarr[1][1];

        }

        if(Isfull(arr, row, col))

        {

                 return'Q';

        }

        return' ';

}

 

Game.h:

#ifndef _GAME_H_

#define _GAME_H_

 

#include

#include

#include

#include

 

#define ROW 3

#define COL 3

 

void InitBoard(char arr[ROW][COL], int row,int col);

void DisplayBoad(char arr[ROW][COL], introw, int col);

void PlayMove(char arr[ROW][COL], int row,int col);

void ComputerMove(char arr[ROW][COL], introw, int col);

char IsWine(char arr[ROW][COL], int row,int col);

 

#endif

Test.c:

 

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

void menu()

{

        printf("*************************\n");

        printf("*********1.game**********\n");

        printf("*********0.exit**********\n");

        printf("*************************\n");

 

}

 

void game()

{

        charret = 0;

        charboard[ROW][COL] = { 0 };

        InitBoard(board,ROW, COL);

        DisplayBoard(board,ROW, COL);

        while(1)

        {

                 PlayerMove(board,ROW, COL);

                 DisplayBoard(board,ROW, COL);

                 IsWine(board,ROW, COL);

                 ret= IsWine(board, ROW, COL);

                 if(ret != ' ')

                 {

                         break;

                 }

                 printf("轮到电脑下棋\n");

                 ComputerMove(board,ROW, COL);

                 DisplayBoard(board,ROW, COL);

                 ret= IsWine(board, ROW, COL);

                 if(ret != ' ')

                 {

                         break;

                 }

        }

        if(ret == 'x')

                 printf("玩家赢\n");

 

        elseif (ret == '0')

                 printf("电脑赢\n");

 

        else

                 printf("平局\n");

 

 

}

void test()

{

        intinput = 0;

        srand((unsignedint)time(NULL));

        do

        {

 

                 menu();

                 printf("请选择:>");

                 scanf("%d",&input);

                 switch(input)

                 {

                 case1: game();

                         break;

                 case0:

                         printf("游戏结束\n");

                         break;

                 default:

                         break;

                 }

        }while (input);

 

}

int main()

{

        test();

        system("pause");

        return0;

}

 

 

 

你可能感兴趣的:(三子棋代码)