主要函数模块
一个简易的三子棋,基本的功能实现了,不过画面比较丑,我下来就说一下思路。主要要实现的函数有:
1 2 3 4 5 |
init(char chessBoard[rows][lows]) ; //初始化 display(char chessBoard[rows][lows]); //打印棋盘 Peo_Play(char chessBoard[rows][lows]); //人下棋 Com_Play(char chessBoard[rows][lows]); //电脑下棋 winner(char chessBoard[rows][lows]); //胜利者 |
源代码及注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include"game.h" void menu() //打印菜单 { printf(" *******************************"); printf("\n"); printf(" ************三子棋*************"); printf("\n"); printf(" *****1.play 0.exit*****"); printf("\n"); printf(" *******************************"); printf("\n"); } int main() { int input=0; char chessBoard[rows][lows]; //定义一个二维数组 menu(); srand((unsigned int)time(NULL)); //调用菜单函数 printf("请选择->:"); do{ scanf("%d",&input); switch(input) { case 1: init(chessBoard); //调用init初始化函数 break; case 0: break; //输入有误直接退出 default: printf("输入有误!!"); break; } }while(input); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
#include<stdio.h> #include<stdlib.h> #include<time.h> #define rows 3 #define lows 3 //定义二维数组的行和列 void display(char chessBoard[rows][lows]); //打印棋盘 void Peo_Play(char chessBoard[rows][lows]); //人下棋 void Com_Play(char chessBoard[rows][lows]); //电脑下棋 int winner(char chessBoard[rows][lows]); //胜利者 void init(char chessBoard[rows][lows]) //初始化 { int i=0; int j=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { chessBoard[i][j]=' '; } } display(chessBoard); //调用打印函数 for(i=0;i<3;i++) { Peo_Play(chessBoard); //打印人下的棋 Com_Play(chessBoard); //打印电脑下的题 } winner(chessBoard); //调用胜利者的函数 } void display(char chessBoard[rows][lows]) //打印棋盘 { int i=0; for(i=0;i<3;i++) { printf(" %c | %c | %c \n",chessBoard[i][0],chessBoard[i][1],chessBoard[i][2]); if(i!=2) printf("---|---|---\n"); } } void Peo_Play(char chessBoard[rows][lows]) //人下棋 { int i=0; int j=0; printf("请输入你要下的位置->:"); scanf("%d,%d",&i,&j); system("cls"); while(chessBoard[i][j]!=' ') //棋盘位置不为空 { if((i<0)||(i>2)||(j<0)||(j>2)) //超出二维数组的范围 { printf("ERROR:你输入的位置信息有误!"); } } chessBoard[i][j]='X'; //将人的下棋位置赋值为X display(chessBoard); //在棋盘中打印 } void Com_Play(char chessBoard[rows][lows]) //电脑下棋 { int i=0; int j=0; printf("电脑下棋中...\n"); while(chessBoard[i][j]!=' ') //棋盘位置不为空 { i=rand()%3; j=rand()%3; //产生小于3的随机数 } system("cls"); chessBoard[i][j]='Y'; display(chessBoard); //在棋盘中打印 } int winner(char chessBoard[rows][lows]) //胜利者 { int i=0; if((chessBoard[0][0]==chessBoard[1][1])&&(chessBoard[1][1]==chessBoard[2][2]))//判断正对角线 { if(chessBoard[1][1]=='X') { printf("祝贺你,你赢了!\n"); return 1; } if(chessBoard[1][1]=='Y') { printf("电脑赢了!\n"); return 1; } } if((chessBoard[0][2]==chessBoard[1][1])&&(chessBoard[1][1]==chessBoard[2][0]))//判断反对角线 { if(chessBoard[1][1]=='X') { printf("祝贺你,你赢了!\n"); return 1; } if(chessBoard[1][1]=='Y') { printf("电脑赢了!\n"); return 1; } } for(i=0;i<3;i++) { if((chessBoard[i][0]==chessBoard[i][1])&&(chessBoard[i][1]==chessBoard[i][2]))//判断行 { if(chessBoard[i][1]=='X') { printf("祝贺你,你赢了!\n"); return 1; } if(chessBoard[i][1]=='Y') { printf("电脑赢了!\n"); return 1; } } } for(i=0;i<3;i++) { if((chessBoard[0][i]==chessBoard[1][i])&&(chessBoard[1][1]==chessBoard[2][i]))//判断列 { if(chessBoard[1][i]=='X') { printf("祝贺你,你赢了!\n"); return 1; } if(chessBoard[1][i]=='Y') { printf("电脑赢了!\n"); return 1; } } } printf("你们打成了平局!"); //否则输出平局 return 0; } |