井字游戏(C++版)终级版

作者在 2009-06-18 12:43:41 发布以下内容

原帖及讨论:http://bbs.bccn.net/thread-113488-1-1.html

前两个版本都多多少少有些缺陷,经过本人仔细研究,终于克服重重困难,编写出了井字游戏终结版,经过大量实验,应该没什么问题了,大家试试吧!!!!!不知道这个游戏能不能评为精品呀???同时还推出了C语言版
#include <iostream>
#include <string>
using namespace std;
typedef char chess[10];                    //字符数组
typedef int temparr[10];                //整型数组
chess arr;                                //定义字符数组变量
temparr brr;                            //定义整型数组变量
int number,suc,n3,c3,n2,c2,n1,c1;        
void inarrdata(chess a)                    //初始化棋盘编号
{
    a[1]='1';a[2]='2';a[3]='3';
    a[4]='4';a[5]='5';a[6]='6';
    a[7]='7';a[8]='8';a[9]='9';
}
void display(chess a)                    //输出棋盘状态
{
    cout<<endl;cout<<endl;
    cout<<"  "<<a[1]<<" "<<'|'<<" "<<a[2]<<" "<<'|'<<" "<<a[3]<<endl;
    cout<<" -----------"<<endl;
    cout<<"  "<<a[4]<<" "<<'|'<<" "<<a[5]<<" "<<'|'<<" "<<a[6]<<endl;
    cout<<" -----------"<<endl;
    cout<<"  "<<a[7]<<" "<<'|'<<" "<<a[8]<<" "<<'|'<<" "<<a[9]<<endl;
    cout<<endl;cout<<endl;
}
int arrfull()                //判断还有没有下棋的位置
{
    int i;                
    int arrf=0;
    for(i=1;i<=9;i++)
        if(i==arr[i]-48)        //如果字符arr[i]-48等于i
            arrf=1;                //那么arrf=1,也就是可以走棋
    return arrf;
}
void cn(int line)                //判断状态
{
    switch(line)
        {
            case 0:c3=c3+1;break;
            case 1:n2=n2+1;break;
            case 2:c2=c2+1;break;
            case 3:n1=n1+1;break;
            case 4:c1=c1+1;break;
            case 5:n3=n3+1;break;
        }
}
int linenum(char a,char b,char c)    //判断状态
{
    int ln=6;
    if((a=='X')&&(b=='X')&&(c=='X'))
        ln=0;
    if(((a=='O')&&(b=='O')&&(c!='O'))||((a=='O')&&(b!='O')&&(c=='O'))||((a!='O')&&(b=='O')&&(c=='O')))
        ln=1;
    if(((a=='X')&&(b=='X')&&(c!='X'))||((a=='X')&&(b!='X')&&(c=='X'))||((a!='X')&&(b=='X')&&(c=='X')))
        ln=2;
    if(((a=='O')&&(b!='O')&&(c!='O'))||((a!='O')&&(b=='O')&&(c!='O'))||((a!='O')&&(b!='O')&&(c=='O')))
        ln=3;
    if(((a=='X')&&(b!='X')&&(c!='x'))||((a!='X')&&(b=='X')&&(c!='X'))||((a!='X')&&(b!='X')&&(c=='X')))
        ln=4;
if((a=='O')&&(b=='O')&&(c=='O'))
        ln=5;
    return ln;
}
int maxbrr(int *br)        //判断最大权值
{
    int temp,i,mb;
    temp=-888;
    for(i=1;i<=9;i++)
        {
            if(temp<=br[i])
                {
                    temp=br[i];
                    mb=i;
                }
        }
    return mb;
}
void manstep() //人走棋处理模块
{
    int j;
    display(arr);
    if(arrfull())  //如果棋盘上还有下棋的位置,人走一步棋
        {
            cout<<"您要走哪一步?请输入数字(1--9):";
            cin>>j;
            while((j<1)||(j>9)||(j!=arr[j]-48))
                {
                    cout<<"对不起,您输入的数字不对,请重新输入(1--9):";
                    cin>>j;
                }
            arr[j]='O';
            n3=0;c3=0;n2=0;c2=0;n1=0;c1=0;
            number=linenum(arr[1],arr[2],arr[3]);cn(number);
            number=linenum(arr[4],arr[5],arr[6]);cn(number);
            number=linenum(arr[7],arr[8],arr[9]);cn(number);
            number=linenum(arr[1],arr[4],arr[7]);cn(number);
            number=linenum(arr[2],arr[5],arr[8]);cn(number);
            number=linenum(arr[3],arr[6],arr[9]);cn(number);
            number=linenum(arr[1],arr[5],arr[9]);cn(number);
            number=linenum(arr[3],arr[5],arr[7]);cn(number);
            if(n3!=0)                //您赢了
                {
                    display(arr);
                    cout<<endl;
                    cout<<"恭喜您赢了!!!"<<endl;
                    suc=0;
                }
        }
}
void computerstep()  //计算机走棋处理模块
{
    int i;
    if(arrfull())  //如果棋盘上还有可下棋的位置,则计算机走棋
        {
            for(i=1;i<=9;i++)  //对每一步可走的棋进行计算
                {
                    if(i==arr[i]-48)
                        {
                            c3=0;n2=0;c2=0;n1=0;c1=0;
                            arr[i]='X';
                            number=linenum(arr[1],arr[2],arr[3]);cn(number);
                            number=linenum(arr[4],arr[5],arr[6]);cn(number);
                            number=linenum(arr[7],arr[8],arr[9]);cn(number);
                            number=linenum(arr[1],arr[4],arr[7]);cn(number);
                            number=linenum(arr[2],arr[5],arr[8]);cn(number);
                            number=linenum(arr[3],arr[6],arr[9]);cn(number);
                            number=linenum(arr[1],arr[5],arr[9]);cn(number);
                            number=linenum(arr[3],arr[5],arr[7]);cn(number);
                            brr[i]=(128*c3-63*n2+31*c2-15*n1+7*c1);   //计算此步权值
                            arr[i]=i+48;
                        }
                    else
                        brr[i]=-999;
                }
            arr[maxbrr(brr)]='X';    //确定计算机走哪一步,权值最大的一步
            c3=0;n2=0;c2=0;n1=0;c1=0;
             number=linenum(arr[1],arr[2],arr[3]);cn(number);
             number=linenum(arr[4],arr[5],arr[6]);cn(number);
             number=linenum(arr[7],arr[8],arr[9]);cn(number);
             number=linenum(arr[1],arr[4],arr[7]);cn(number);
             number=linenum(arr[2],arr[5],arr[8]);cn(number);
             number=linenum(arr[3],arr[6],arr[9]);cn(number);
             number=linenum(arr[1],arr[5],arr[9]);cn(number);
             number=linenum(arr[3],arr[5],arr[7]);cn(number);
             if(c3!=0)                //计算机已赢
                {
                    display(arr);
                    cout<<endl;
                    cout<<"计算机赢了!!!"<<endl;
                    suc=0;
                }
            }
    else
        suc=0;
}
int main()
{
    cout<<"游戏规则:"<<endl<<"棋盘格式如图,人和计算机在棋盘上交替走棋"<<endl;
    cout<<"约定计算机使用符号X,人使用符号O"<<endl;
    cout<<"谁先使一横行或一竖行或对角线上有三个自己的符号,就胜利了!"<<endl;
    string s="y";
    string ch;
    while(s=="y"||s=="Y")
    {
    inarrdata(arr);          //棋盘坐标编号
    display(arr);            //显示初始棋盘
    suc=1;
    cout<<"请选择您是否先走?(y/n)";
    cin>>ch;
    while(ch!="y"&&ch!="Y"&&ch!="n"&&ch!="N")
        {
            cout<<"错误!请输入y或n:";
            cin>>ch;
        }
    if((ch=="y")||(ch=="Y"))    //输入Y,表示人先走棋
        {
            while(suc)
                {
                    manstep();
                    computerstep();
                }
         }
    else                         //计算机先走棋
        {
            while(suc)
                {
                    computerstep();
                    if(suc)
                        manstep();
                }
        }
   if(n3==0&&c3==0)
       cout<<endl<<"和棋!"<<endl<<endl;
    cout<<"再来一盘(y/n)?";
    cin>>s;
    while(s!="y"&&s!="Y"&&s!="n"&&s!="N")
        {
            cout<<"错误!请输入y或n:";
            cin>>s;
        }
    }
    return 0;
}

 

默认分类 | 阅读 2718 次
文章评论,共2条
lovemm6
2009-06-18 14:12
1
还是有漏洞啊 !在走的时候我输入字母或其他不是数字的字符!就会出现死循环!这个怎么解决啊?
zbh120307
2023-10-01 16:02
2
当我胜利时,计算机也会走棋,并形成如下情况:

  O | O | O
 -----------
  4 | X | X
 -----------
  X | 8 | O



恭喜您赢了!!!


  O | O | O
 -----------
  X | X | X
 -----------
  X | 8 | O



计算机赢了!!!
再来一盘(y/n)?
游客请输入验证码
文章分类