c语言大作业井字棋程序,C语言写的一个井字棋人机小游戏

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include /* include information about standard library stdio.h */

#include /* include information about standard library string.h */

#include /* include information about standard library Windows.h */

#include // 使用了srand和rand函数

#include // 使用了time函数

int row = 0; /* declare six variables of type of integer namely player,winner,choice,row,column and line */

int column = 0; /* and their value all equal to 0 */

int choice = 0;

int player;

int winner = 0;

int line = 0;

int selection = 0;

char board[3][3] = /* Declaring a two - dimensional array of length 3 namely board */

{

{ '1','2','3' },

{ '4','5','6' },

{ '7','8','9' }

};

void gambo() {

printf(" # # \n"); /*print out the chess board on the screen */

printf(" %c # %c # %c \n", board[0][0], board[0][1], board[0][2]);

printf(" # # \n");

printf("#################\n");

printf(" # # \n");

printf(" %c # %c # %c \n", board[1][0], board[1][1], board[1][2]);

printf(" # # \n");

printf("#################\n");

printf(" # # \n");

printf(" %c # %c # %c \n", board[2][0], board[2][1], board[2][2]);

printf(" # # \n");

}

void end() {

printf("\n\n");

gambo();

if (winner == 0)

printf("\nIt is drawn that no one is winner.\n");

else if (player == 1)

printf("\nCongrations! player ,YOU ARE THE WINNER!\n");

else printf("\ncomputer is the winner\n");

printf("Do you want to play again?press y/n\n");

int rep;

rep = getchar();

if (rep == 'y') {

system("cls");

board[0][0] = '1'; board[0][1] = '2'; board[0][2] = '3';

board[1][0] = '4'; board[1][1] = '5'; board[1][2] = '6';

board[2][0] = '7'; board[2][1] = '8'; board[2][2] = '9';

winner = 0;

main();

}

else

printf("\n\nThank you for playing the tic-tac-toe game, please press a key to close the program\n\n");/*End the program by pressing a key*/

exit(0);

}

void check() {

if ((board[0][0] == board[1][1] && board[0][0] == board[2][2]) ||

(board[0][2] == board[1][1] && board[0][2] == board[2][0]))

{

winner = player;

end();

}

else

for (line = 0; line <= 2; line++)

if ((board[line][0] == board[line][1] && board[line][0] == board[line][2]) ||

(board[0][line] == board[1][line] && board[0][line] == board[2][line]))

{

winner = player; /*Check rows and columns for a winning line */

end();

}

}

void com() {

int dice;

srand((unsigned int)time(NULL));

while (board[row][column]>'9') {

dice = rand() % 9 + 1;

row = --dice / 3; /*give the value to the variables named row and column depending on the choice*/

column = dice % 3;

};

board[row][column] = 'O';

}

void human() {

printf("\n(If you enter the number out of range, "

"you need to reenter the number until the right number.)\n");

do {

printf("\nPlayer, please enter the number of the chess board where you want to place your %c: ",'X');

scanf("%d", &choice);

getchar();

rewind(stdin);

row = --choice / 3;

column = choice % 3;

} while (choice<0 || choice>9 || board[row][column]>'9');

board[row][column] = 'X';

}

int main() /* define a function named main */

{

do {

printf("Do you want to go first or seconf? press 1 or 2 to select.\n");

scanf("%d", &selection);

getchar();

} while (selection < 1 || selection > 2);

rewind(stdin);

int i,j;

if (selection == 1)j = 5;

else j = 4;

for (i = 0; i

{

system("cls");

gambo();

if (selection == 1) {

player = 1;

human();

check();

player = 2;

com();

check();

}

else {

player = 2;

com();

system("cls");

gambo();

check();

player = 1;

human();

check();

}

}

end();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值