ox游戏九宫格C语言,九宫格井字游戏(c语言)

#include

#include

#include

int main(int argc,char** argv)

{

int player = 0;

int winner = 0;

int choice = 0;

int row = 0;

int column = 0;

int line = 0;

char board[3][3] = {

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

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

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

};

int i;

for(i = 0;i<9 && winner==0;i++){

printf("\n\n");

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

printf("---+---+---\n");

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

printf("---+---+---\n");

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

player = i%2+1;

//Get valid player square selection

do{

printf("\nPlayer %d, please enter the number of the square "

"where you want to place your %c: ",

player,(player == 1)?'X':'O');

scanf("%d",&choice);

row = --choice/3;

column = choice%3;

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

//insert player symbol

board[row][column] = (player == 1)?'X':'O';

//Check for a winning line--diagonals first

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;

else

//Check rows and columns for a winning line

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;

}

//Game is over so display the final board

printf("\n\n");

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

printf("---+---+---\n");

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

printf("---+---+---\n");

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

//Dispay result message

if(winner == 0)

printf("\nHow boring,it is a draw\n");

else

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

return 0;

}

Player 1, please enter the number of the square where you want to place your X: 1

X | 2 | 3

---+---+---

4 | 5 | 6

---+---+---

7 | 8 | 9

Player 2, please enter the number of the square where you want to place your O: 4

X | 2 | 3

---+---+---

O | 5 | 6

---+---+---

7 | 8 | 9

Player 1, please enter the number of the square where you want to place your X: 2

X | X | 3

---+---+---

O | 5 | 6

---+---+---

7 | 8 | 9

Player 2, please enter the number of the square where you want to place your O: 7

X | X | 3

---+---+---

O | 5 | 6

---+---+---

O | 8 | 9

Player 1, please enter the number of the square where you want to place your X: 3

X | X | X

---+---+---

O | 5 | 6

---+---+---

O | 8 | 9

Congrations,player 1,YOU ARE THE WINNER!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值