博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
井字游戏设计一个算法_井字游戏
阅读量:2507 次
发布时间:2019-05-11

本文共 4379 字,大约阅读时间需要 14 分钟。

井字游戏设计一个算法

#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();
}

翻译自:

井字游戏设计一个算法

转载地址:http://zgwwd.baihongyu.com/

你可能感兴趣的文章
windows环境libevent搭建和demo分析
查看>>
Javascript通过bind()掌控this
查看>>
JavaWeb_JSTL标签数据的存储
查看>>
linux 常见问题
查看>>
Android相机实时自动对焦的完美实现
查看>>
OpenCV入门笔记(七) 文字区域的提取
查看>>
flask中的上下文处理器context_processor
查看>>
项目中的一个分页功能pagination
查看>>
[GraphQL] Use Arguments in a GraphQL Query
查看>>
[React] Pass Data To Event Handlers with Partial Function Application
查看>>
SEO怎么去发布外链?
查看>>
c#基础学习(0703)之string.Format格式化日期
查看>>
成绩分配
查看>>
BZOJ3667: Rabin-Miller算法
查看>>
查找目录下的文件
查看>>
博客文章通知
查看>>
gcc悄无声色将静态函数内联了
查看>>
JS装箱操作和拆箱操作的理解
查看>>
第一次作业
查看>>
Latex常用符号
查看>>