井字游戏设计一个算法_井字游戏

井字游戏设计一个算法

#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<process.h>

char mat[3][3];
void table(void);            //function to print the table
void welcome(void);     //function for welcome screen

void main()
{
 welcome();
     A: clrscr();
 int i,j,m,n,sum=0;
 char ch;

 for(m=0;m<3;++m)
  for(n=0;n<3;++n)
   mat[m][n]=’’;
 table();

 while(sum<10)
 {
 //for player 1
 cout<<“Player 1 is’x’nChoose the position:”;
 cout<<“nRow:”;
 cin>>i;
 cout<<“Coloumn:”;
 cin>>j;

//if position is wrong
 for(;i>3||i<1||j>3||j<1||(‘x’==mat[i-1][j-1]||’o’==mat[i-1][j-1]);)
 {
    cout<<“nSorry!!!! wrong position,Choose the position again”;
    cout<<“nRow:”;
    cin>>i;
    cout<<“Coloumn:”;
    cin>>j;
 }
 mat[i-1][j-1]=’x’;
 sum++;

 //to check if player 1 wins or not
 if(mat[0][0]==’x’&&mat[0][0]==mat[0][1]&&mat[0][0]==mat[0][2])
 {
  table();
  cout<<“nPlayer 1 wins…….!!!” ;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[1][0]==’x’&&mat[1][0]==mat[1][1]&&mat[1][0]==mat[1][2])
 {
  table();
  cout<<“nPlayer 1 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[2][0]==’x’&&mat[2][0]==mat[2][1]&&mat[2][0]==mat[2][2])
 {
  table();
  cout<<“nPlayer 1 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[0][0]==’x’&&mat[0][0]==mat[1][0]&&mat[0][0]==mat[2][0])
 {
  table();
  cout<<“nPlayer 1 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[0][1]==’x’&&mat[0][1]==mat[1][1]&&mat[0][1]==mat[2][1])
 {
  table();
  cout<<“nPlayer 1 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[0][2]==’x’&&mat[0][2]==mat[1][2]&&mat[0][2]==mat[2][2])
 {
  table();
  cout<<“nPlayer 1 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[0][0]==’x’&&mat[0][0]==mat[1][1]&&mat[0][0]==mat[2][2])
 {
  table();
  cout<<“nPlayer 1 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[0][2]==’x’&&mat[0][2]==mat[1][1]&&mat[0][0]==mat[2][0])
 {
  table();
  cout<<“nPlayer 1 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(sum==9) //to check the chances
 {
  table();
  cout<<“ntThe game is over…….no one wins…HaHaHa…..!!!”;
  break;
 }
 //for player 2
 cout<<“nnPlayer 2 is’o’nChoose the position:”;
 cout<<“nRow:”;
 cin>>i;
 cout<<“Coloumn:”;
 cin>>j;

 //if position is wrong
 for(;i>3||i<1||j>3||j<1||(‘x’==mat[i-1][j-1]||’o’==mat[i-1][j-1]);)
 {
    cout<<“nSorry!!!! wrong position,Choose the position again”;
    cout<<“nRow:”;
    cin>>i;
    cout<<“Coloumn:”;
    cin>>j;
 }
 mat[i-1][j-1]=’o’;
 sum++;
 table();

 //to check player 2 wins or not
 if(mat[0][0]==’o’&&mat[0][0]==mat[0][1]&&mat[0][0]==mat[0][2])
 {
  cout<<“nPlayer 2 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[1][0]==’o’&&mat[1][0]==mat[1][1]&&mat[1][0]==mat[1][2])
 {
  cout<<“nPlayer 2 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[2][0]==’o’&&mat[2][0]==mat[2][1]&&mat[2][0]==mat[2][2])
 {
  cout<<“nPlayer 2 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[0][0]==’o’&&mat[0][0]==mat[1][0]&&mat[0][0]==mat[2][0])
 {
  cout<<“nPlayer 2 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[0][1]==’o’&&mat[0][1]==mat[1][1]&&mat[0][1]==mat[2][1])
 {
  cout<<“nPlayer 2 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[0][2]==’o’&&mat[0][2]==mat[1][2]&&mat[0][2]==mat[2][2])
 {
  cout<<“nPlayer 2 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[0][0]==’o’&&mat[0][0]==mat[1][1]&&mat[0][0]==mat[2][2])
 {
  cout<<“nPlayer 2 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }

 if(mat[0][2]==’o’&&mat[0][2]==mat[1][1]&&mat[0][0]==mat[2][0])
 {
  cout<<“nPlayer 2 wins…….!!!”;
  cout<<“nYou have played Great…..!!!”;
  sleep(5);
  break;
 }
 }
 cout<<“nntWould you like to play more….?(y/n):”;
 cin>>ch;
 if(ch==’y’||ch==’Y’)
  goto A;
 else
 {
  cout<<“nntThanks for Playing…….:)”;
  sleep(5);
  exit(0);
 }
}

void table()
{
 clrscr();
 cout<<“nntt  1  2  3n”;
 cout<<“tt1  “<<mat[0][0]<<“|”<<mat[0][1]<<“|”<<mat[0][2];
 cout<<“ntt  –|-|–“;
 cout<<“ntt2  “<<mat[1][0]<<“|”<<mat[1][1]<<“|”<<mat[1][2];
 cout<<“ntt  –|-|–“;
 cout<<“ntt3  “<<mat[2][0]<<“|”<<mat[2][1]<<“|”<<mat[2][2]<<“nn”;
}

void welcome()
{
 textmode(C80);
 textcolor(YELLOW);
 clrscr();

 cout<<“nnnnnttttWelcome To”;
 sleep(2);
 cout<<“nnttttTic-Tac-Toe”;
 sleep(1);
 cout<<“nntttt   Game”;
 sleep(1);
 cout<<“nnnnnnnnntttttPress any key to continue…..!!”;
 getch();
}

翻译自: https://www.thecrazyprogrammer.com/2011/05/tic-tac-toe-game.html

井字游戏设计一个算法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值